-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
125 lines (110 loc) · 3.06 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
"math"
"net"
"strings"
)
const (
Normal = "Normal"
Halt = "Halt"
Drain = "Drain"
Down = "Down"
)
var (
initialRun = true
)
const (
returnIdle = true
)
func handleClient(conn net.Conn) {
defer conn.Close()
conn.Write(GetResponseForMode())
conn.Close()
}
func GetResponseForMode() (response []byte) {
ramThresholdValue := GlobalConfig.Ram.ThresholdValue.ToFloat()
cpuThresholdValue := GlobalConfig.Cpu.ThresholdValue.ToFloat()
cpuImportance := GlobalConfig.Cpu.ImportanceFactor.ToFloat()
ramImportance := GlobalConfig.Ram.ImportanceFactor.ToFloat()
switch GlobalConfig.AgentStatus.Value {
case Normal:
cpuLoad, err := cpu.Percent(0, false)
if err != nil {
return []byte("0%\n")
}
v, err := mem.VirtualMemory()
if err != nil {
return []byte("0%\n")
}
averageCpuLoad := cpuLoad[0]
usedRam := v.UsedPercent
// If any resource is important and utilized 100% then everything else is not important
if averageCpuLoad > cpuThresholdValue && cpuThresholdValue > 0 || (usedRam > ramThresholdValue && ramThresholdValue > 0) {
response = []byte("0%\n")
}
utilization := 0.0
divider := 0.0
utilization = utilization + averageCpuLoad*cpuImportance
if cpuImportance > 0 {
divider++
}
utilization = utilization + usedRam*ramImportance
if ramImportance > 0 {
divider++
}
for _, tcpService := range GlobalConfig.TCPService {
sessionOccupied := GetSessionUtilized(tcpService.IPAddress.Value, tcpService.Port.Value, tcpService.MaxConnections.ToInt())
utilization = utilization + sessionOccupied*tcpService.ImportanceFactor.ToFloat()
if tcpService.ImportanceFactor.ToFloat() > 0 {
divider++
}
if sessionOccupied > 99 && tcpService.ImportanceFactor.ToFloat() == 1 {
response = []byte("0%\n")
break
}
}
utilization = utilization / divider
if utilization < 0 {
utilization = 0
}
if utilization > 100 {
utilization = 100
}
if returnIdle {
response = []byte(fmt.Sprintf("%v%%\n", math.Ceil(100-utilization)))
} else {
response = []byte(fmt.Sprintf("%v%%\n", math.Ceil(utilization)))
}
if initialRun {
response = append([]byte("up ready "), response...)
}
case Drain:
response = []byte("drain\n")
case Halt:
response = []byte("down\n")
default:
response = []byte("error\n")
}
return
}
func GetSessionUtilized(IPAddress, servicePort string, maxNumberOfSessionsPerService int) (result float64) {
numberOfEstablishedConnections := getNumberOfLocalEstablishedConnections(IPAddress, servicePort)
if numberOfEstablishedConnections > 0 && maxNumberOfSessionsPerService > 0 {
result = float64(maxNumberOfSessionsPerService) / float64(numberOfEstablishedConnections)
}
return
}
func getNumberOfLocalEstablishedConnections(ipAddress string, port string) int {
if ipAddress == "*" {
ipAddress = ""
}
result := runcmd("netstat -nt | findstr " + ipAddress + ":" + port + " | findstr ESTABLISHED ")
count := len(strings.Split(result, "\n"))
if count == 0 {
return count
}
return count - 1
}