-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_information.go
100 lines (85 loc) · 2.64 KB
/
cluster_information.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
package qdbinterface
import (
"time"
"github.com/bureau14/qdb-api-rest/models"
"github.com/bureau14/qdb-api-go/v3"
)
// global structures, I know not pretty
// TODO(vianney) find a way to keep information
// from one call to another
// ClusterInformation : all cluster information
var ClusterInformation models.Cluster
// NodesInformation : all nodes information
var NodesInformation = make(map[string]models.Node)
// Do not overload the server with call
// really dumb implementation but will work for now
var lastUpdate = time.Unix(0, 0)
var updateInterval = time.Duration(5 * time.Second)
var lastError = error(nil)
func shouldUpdate() bool {
now := time.Now()
if lastUpdate.Add(updateInterval).After(now) {
return false
}
lastUpdate = now
return true
}
func resetInformation() {
ClusterInformation.DiskTotal = new(int64)
ClusterInformation.DiskUsed = new(int64)
ClusterInformation.MemoryTotal = new(int64)
ClusterInformation.MemoryUsed = new(int64)
ClusterInformation.Nodes = []string{}
ClusterInformation.Status = new(string)
}
// RetrieveInformation : retrieve all informations
func RetrieveInformation(handle qdb.HandleType) error {
if !shouldUpdate() && lastError == nil {
return nil
}
if ClusterInformation.Status == nil {
ClusterInformation.Status = new(string)
}
stats, err := handle.Statistics()
if err == nil {
*ClusterInformation.Status = "stable"
} else {
*ClusterInformation.Status = "unstable"
}
diskTotal := int64(0)
diskUsed := int64(0)
memoryTotal := int64(0)
memoryUsed := int64(0)
ClusterInformation.Nodes = []string{}
NodesInformation = make(map[string]models.Node)
for _, stat := range stats {
diskUsedByNode := stat.Disk.BytesTotal - stat.Disk.BytesFree
diskTotal += stat.Disk.BytesTotal
diskUsed += diskUsedByNode
memoryTotal += stat.Memory.Physmem.Total
memoryUsed += stat.Memory.Physmem.Used
ClusterInformation.Nodes = append(ClusterInformation.Nodes, stat.NodeID)
id := stat.NodeID
node := models.Node{}
cpuUser := int64(stat.CPU.User)
cpuSystem := int64(stat.CPU.System)
cpuIdle := stat.CPU.Idle
cpuUsed := cpuUser + cpuSystem
cpuTotal := cpuUsed + cpuIdle
node.CPUTotal = &cpuTotal
node.CPUUsed = &cpuUsed
node.DiskTotal = &stat.Disk.BytesTotal
node.DiskUsed = &diskUsedByNode
node.ID = &id
node.MemoryTotal = &stat.Memory.Physmem.Total
node.MemoryUsed = &stat.Memory.Physmem.Used
node.Os = &stat.OperatingSystem
node.QuasardbVersion = &stat.EngineVersion
NodesInformation[id] = node
}
ClusterInformation.DiskTotal = &diskTotal
ClusterInformation.DiskUsed = &diskUsed
ClusterInformation.MemoryTotal = &memoryTotal
ClusterInformation.MemoryUsed = &memoryUsed
return err
}