-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.go
More file actions
104 lines (85 loc) · 2.19 KB
/
graph.go
File metadata and controls
104 lines (85 loc) · 2.19 KB
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
package muninnode
import (
"fmt"
"log"
"strings"
"unicode"
)
type Graph struct {
name string
title string
configs map[string]interface{}
values []*Value
prefetchf func()
}
func NewGraph(name string, title string, config map[string]interface{}, pf func()) *Graph {
return &Graph{
name,
title,
config,
[]*Value{},
pf,
}
}
func (m *Graph) AddValue(name string, getf func() interface{}, configs map[string]interface{}) {
m.values = append(m.values, &Value{muninKey(name), getf, configs})
}
func muninKey(s string) string {
var key string
// Munin does not like keys that start with numbers
if !unicode.IsLetter(rune(s[0])) {
key = "_" + s
} else {
key = s
}
// No dots in key names either
return strings.Replace(key, ".", "_", -1)
}
func (m *Graph) fetch() string {
if m.prefetchf != nil {
m.prefetchf()
}
lines := make([]string, 0, 10)
for _, v := range m.values {
lines = append(lines, v.fetch())
}
return strings.Join(lines, "\n") + "\n."
}
func (m *Graph) config() string {
lines := make([]string, 0, 10)
if m.title != "" {
lines = append(lines, fmt.Sprintf("graph_title %s", m.title))
} else {
lines = append(lines, fmt.Sprintf("graph_title %s", m.name))
}
for k, v := range m.configs {
lines = append(lines, fmt.Sprintf("%s %v", k, v))
}
for _, v := range m.values {
lines = append(lines, v.config()...)
}
return strings.Join(lines, "\n") + "\n."
}
type Value struct {
name string
getValue func() interface{} //Must return builtin numeric type: int, uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32, float64
configs map[string]interface{}
}
func (v *Value) fetch() string {
value := v.getValue()
switch value.(type) {
case int, uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32, float64, string:
return fmt.Sprintf("%s.value %v", v.name, value)
default:
log.Println("value must be a pointer to builtin numeric type or string")
return "\n."
}
}
func (v *Value) config() []string {
lines := make([]string, 0, 5)
lines = append(lines, fmt.Sprintf("%s.label %v", v.name, v.name))
for k, vv := range v.configs {
lines = append(lines, fmt.Sprintf("%s.%s %v", v.name, k, vv))
}
return lines
}