/
statsd.go
90 lines (58 loc) · 1.19 KB
/
statsd.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
package main
import (
"fmt"
"github.com/cactus/go-statsd-client/statsd"
"github.com/mitchellh/go-ps"
"github.com/shirou/gopsutil/process"
_ "io/ioutil"
_ "math/rand"
"os"
_ "os/exec"
_ "strconv"
"time"
)
func hostname() string {
host, _ := os.Hostname()
return host
}
func apachePID() (pid []int) {
k, _ := ps.Processes()
for _, exe := range k {
if exe.Executable() == "apache2" {
pid = append(pid, exe.Pid())
}
}
return pid
}
func memoryUtil() (value int64, rate float32) {
pids := apachePID()
total := make([]int64, len(pids))
var sum int64
for i, pid := range pids {
k, _ := process.NewProcess(int32(pid))
rss, _ := k.MemoryInfo()
val := rss.RSS / 1024
total[i] = int64(val)
}
for k := 0; k < len(total); k++ {
sum = sum + total[k]
}
rat := (sum / 1024) * 100
fmt.Println(sum, rat)
return sum / 1024, float32(rat)
}
func main() {
for {
client, err := statsd.NewClient("104.130.20.4:8125", hostname())
if err != nil {
fmt.Println(err.Error())
}
memoryUtil()
defer client.Close()
value, _ := memoryUtil()
fmt.Println(value)
client.Gauge("Apache2", value, 0.4)
fmt.Println(hostname())
time.Sleep(1 * time.Second)
}
}