-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadavg_get.go
120 lines (104 loc) · 3.27 KB
/
loadavg_get.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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/go-redis/redis"
)
func main() {
var i int = 288
var future = time.Date(2000, 2, 1, 23, 55, 0, 0, time.UTC)
var s []time.Time
var vettore []string
redisPtr := flag.String("redis", "", "Redis server")
flag.Parse()
if *redisPtr == "" {
flag.PrintDefaults()
os.Exit(1)
}
f, err := os.Create("loadavg.html")
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
for k := 0; k < i; k++ {
future = future.Add(5 * time.Minute)
s = append(s, future)
}
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)
}
defer rdb.Close()
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)
}
defer rdb2.Close()
vettore, err = rdb2.Keys("*").Result()
// Creazione prima riga tabella con la lista dei server
_, _ = f.WriteString(`
<head><meta http-equiv="refresh" content="300"></head>
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{background:#eee;font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-c3ow{border-color:inherit;text-align:center;vertical-align:top}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
.tableFixHead { overflow-y: auto; height: 100%; }
.tableFixHead thead th { position: sticky; top: 0; }
/* Just common table stuff. Really. */
table { border-collapse: collapse; width: 100%;}
th, td { padding: 8px 16px; }
</style>
<div class="tableFixHead">
<table align=center border=1 class="tg"><thead><tr><th></th>
`)
for _, v := range vettore {
v2 := strings.Split(v, ".")
linea := fmt.Sprintln("<th class=tg-0pky>", v2[0], "</th>")
_, _ = f.WriteString(linea)
}
//fmt.Println("</tr><tr>")
_, _ = f.WriteString("</thead><tbody></tr><tr>")
for _, v := range s {
linea := fmt.Sprintln("<td class=tg-0pky>", v.Format("15:04:00"), "</td>")
_, _ = f.WriteString(linea)
for _, a := range vettore {
chiave := v.Format("15:04:00") + "_" + a
val, err := rdb.Get(chiave).Result()
if err != nil {
//panic(err)
val = "-1"
}
//Cambiamo il colore dello sfondo in base al valore.
i, err := strconv.Atoi(val)
if i == 0 {
linea = fmt.Sprintln("<td class=tg-0pky align=center>", i, "</td>")
} else if i >= 1 {
linea = fmt.Sprintln("<td class=tg-0pky align=center bgcolor=red>", i, "</td>")
} else {
linea = fmt.Sprintln("<td class=tg-0pky align=center bgcolor=green>", i, "</td>")
}
_, _ = f.WriteString(linea)
}
// fmt.Println("</tr>")
_, _ = f.WriteString("</tr>")
}
//fmt.Println("</tr></table>
_, _ = f.WriteString("</tr></tbody></table></div>")
}