-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadavg_put.go
67 lines (60 loc) · 1.57 KB
/
loadavg_put.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
package main
import (
"flag"
"github.com/go-redis/redis"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"os"
"time"
)
func main() {
redisPtr := flag.String("redis", "", "Redis server")
flag.Parse()
if *redisPtr == "" {
flag.PrintDefaults()
os.Exit(1)
}
var loadAverage int
var secondi time.Duration = 300
rdb := redis.NewClient(&redis.Options{
Addr: *redisPtr + ":" + "6379", // use default Addr
Password: "", // no password set
DB: 1, // use default DB
})
err := rdb.Ping().Err()
if err != nil {
panic(err)
}
// We use this second connection to dynamically populate list of servers
rdb2 := redis.NewClient(&redis.Options{
Addr: *redisPtr + ":" + "6379", // use default Addr
Password: "", // no password set
DB: 2, // use default DB
})
err = rdb2.Ping().Err()
if err != nil {
panic(err)
}
for true {
location, _ := time.LoadLocation("Europe/Rome")
t := time.Now().In(location)
d := time.Duration(5 * time.Minute)
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
hostname_time := t.Round(d).Format("15:04:05.999999999") + "_" + hostname
load, _ := load.Avg()
loadAverage = int(load.Load5)
cpuNumber, err := cpu.Counts(true)
loadAverage = loadAverage / cpuNumber
err = rdb.Set(hostname_time, loadAverage, 86100*time.Second).Err()
err = rdb2.Set(hostname, "1", 600*time.Second).Err()
if err != nil {
panic(err)
}
time.Sleep(secondi * time.Second)
}
rdb.Close()
rdb2.Close()
}