-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
113 lines (96 loc) · 2.75 KB
/
helper.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
package gohealthchecker
import (
"fmt"
"github.com/ervitis/goprocinfo/linux"
"net"
"os"
"runtime"
"sync"
"time"
)
const (
Running string = "R"
Sleeping = "S"
Waiting = "D"
Zombie = "Z"
Stopped = "T"
Traced = "t"
Dead = "X"
)
type (
ipAddressInfo struct {
ipAddress string
}
mappedProcessesStatus map[string]bool
memoryData struct {
total uint64
free uint64
available uint64
}
SystemInformation struct {
mappedProcessStatus mappedProcessesStatus
processStatus string
processActive bool
pid uint64
startTime time.Time
memory *memoryData
ipAddress *ipAddressInfo
runtimeVersion string
canAcceptWork bool
mtx sync.Mutex
}
)
// GetIpAddress returns the ip address of the machine
// it returns an IpAddressInfo object which stores the IpAddress
// we are using the net.Dial function to get the origin of the connection to the host we want to connect
func (info *SystemInformation) getIpAddress() *ipAddressInfo {
conn, _ := net.Dial("udp", "1.1.1.1:80")
defer conn.Close()
return &ipAddressInfo{ipAddress: conn.LocalAddr().(*net.UDPAddr).IP.String()}
}
// GetRuntimeVersion returns the Golang version which was compiled
func (info *SystemInformation) getRuntimeVersion() string {
return runtime.Version()
}
func (info *SystemInformation) initMappedStatus() {
if info.mappedProcessStatus != nil {
return
}
info.mtx.Lock()
defer info.mtx.Unlock()
info.mappedProcessStatus = make(mappedProcessesStatus)
info.mappedProcessStatus[Running] = true
info.mappedProcessStatus[Sleeping] = true
info.mappedProcessStatus[Traced] = false
info.mappedProcessStatus[Waiting] = false
info.mappedProcessStatus[Zombie] = false
info.mappedProcessStatus[Stopped] = false
info.mappedProcessStatus[Dead] = false
}
// GetSystemInfo initialize the data of the current process running
// it returns an error if it cannot locate the *nix files to get the data
func (info *SystemInformation) GetSystemInfo() error {
info.initMappedStatus()
info.mtx.Lock()
defer info.mtx.Unlock()
processStatus, err := linux.ReadProcessStat(fmt.Sprintf("/proc/%d/stat", os.Getpid()))
if err != nil {
return err
}
memInfo, err := linux.ReadMemInfo("/proc/meminfo")
if err != nil {
return err
}
info.pid = processStatus.Pid
info.processActive = info.mappedProcessStatus[processStatus.State]
info.processStatus = processStatus.State
info.memory = &memoryData{
total: memInfo.MemTotal,
free: memInfo.MemFree,
available: memInfo.MemAvailable,
}
info.runtimeVersion = info.getRuntimeVersion()
info.ipAddress = info.getIpAddress()
info.canAcceptWork = info.processActive && (info.memory.available > 0)
return nil
}