-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathhealth.go
123 lines (102 loc) · 2.73 KB
/
health.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
package main
/*
Health
Hold the general structure to be used for gathering health metrics.
Health should know how to transform itself into a map, which is
useful for those who don't want to deal with strongly-typed value
with their reporters
*/
import (
"fmt"
"github.com/jondot/gosigar"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
)
type DiskInfo struct {
DeviceName string `json:"deviceName"`
Used uint64 `json:"used"`
UsedPcent float64 `json:"usedPercent"`
}
type Health struct {
LoadAvg1 float64 `json:"loadAvg1"`
LoadAvg5 float64 `json:"loadAvg5"`
LoadAvg15 float64 `json:"loadAvg15"`
MemActualFree uint64 `json:"freeMemory"`
MemActualUsed uint64 `json:"usedMemory"`
Disks []DiskInfo `json:"disks"`
CPUTemp float64 `json:"cpuTemp"`
}
func GetHealth(tempPath string) (h *Health, err error) {
avg := sigar.LoadAverage{}
err = avg.Get()
if err != nil {
return
}
mem := sigar.Mem{}
err = mem.Get()
if err != nil {
return
}
health := &Health{
LoadAvg1: avg.One,
LoadAvg5: avg.Five,
LoadAvg15: avg.Fifteen,
MemActualFree: mem.ActualFree,
MemActualUsed: mem.ActualUsed,
}
fslist := sigar.FileSystemList{}
err = fslist.Get()
if err != nil {
return
}
for _, fs := range fslist.List {
usage := sigar.FileSystemUsage{}
usage.Get(fs.DirName)
health.Disks = append(health.Disks, DiskInfo{DeviceName: fs.DevName, Used: usage.Used, UsedPcent: usage.UsePercent()})
}
health.CPUTemp = ABS_ZERO
if tempPath != "" {
health.CPUTemp = getCpuTemp(tempPath)
}
h = health
return
}
const ABS_ZERO = -273.15 // doh. http://en.wikipedia.org/wiki/Absolute_zero
func getCpuTemp(path string) float64 {
text, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot read sensor data at %s: %s\n", path, err)
return ABS_ZERO
}
v, err := strconv.Atoi(strings.TrimRight(string(text), "\n"))
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid temp: %s\n", err)
return ABS_ZERO
}
return float64(v) / 1000.0
}
func (self *Health) Map() map[string]interface{} {
m := map[string]interface{}{
"load.avg1": self.LoadAvg1,
"load.avg5": self.LoadAvg5,
"load.avg15": self.LoadAvg15,
"mem.actualfree": self.MemActualFree,
"mem.actualused": self.MemActualUsed,
"sensors.cputemp": self.CPUTemp,
}
for _, disk := range self.Disks {
m[deviceToKey(fmt.Sprintf("disks.%s.used", disk.DeviceName))] = disk.Used
m[deviceToKey(fmt.Sprintf("disks.%s.used_pcent", disk.DeviceName))] = disk.UsedPcent
}
return m
}
func deviceToKey(dev string) string {
re := regexp.MustCompile("[-_/:\\s]")
re2 := regexp.MustCompile("\\.+")
s := re.ReplaceAllString(dev, ".")
s = re2.ReplaceAllString(s, ".")
return s
}