-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredisMetric.go
96 lines (78 loc) · 2.28 KB
/
redisMetric.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
package main
import (
"bufio"
"fmt"
"log"
"regexp"
"sort"
"strconv"
"strings"
)
type redisMetric struct {
name string
value float64
}
func generateUniqueMetrics(redisInfoOutput string) []redisMetric {
var metrics []redisMetric
// ensure that the map is always iterated in the same order
metricNames := make([]string, 0, len(uniqueMetricMap))
for k := range uniqueMetricMap {
metricNames = append(metricNames, k)
}
sort.Strings(metricNames)
for _, metricName := range metricNames {
submNam := uniqueMetricMap[metricName]
val, err := fetchMetricValue(redisInfoOutput, metricName)
if err == nil {
metrics = append(metrics, redisMetric{
name: submNam,
value: val,
})
} else {
log.Println(err)
}
}
return metrics
}
func fetchMetricValue(redisInfoOutput string, metricName string) (float64, error) {
scanner := bufio.NewScanner(strings.NewReader(redisInfoOutput))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, metricName) {
value, err := strconv.ParseFloat(strings.Split(line, ":")[1], 64)
errCheckFatal(err)
return value, nil
}
}
return 0, fmt.Errorf("metric %s not found", metricName)
}
func generateRecordsMetrics(redisInfoOutput string) []redisMetric {
var metrics []redisMetric
// group 1 is the db ID; group 2 is the number of keys (records)
re := regexp.MustCompile(`^db(\d+):keys=(\d+)`)
scanner := bufio.NewScanner(strings.NewReader(redisInfoOutput))
for scanner.Scan() {
line := scanner.Text()
match := re.FindStringSubmatch(line)
if len(match) > 0 {
id := match[1]
value, _ := strconv.ParseFloat(match[2], 64)
metrics = append(metrics, redisMetric{
name: fmt.Sprintf("records-%s", id),
value: value,
})
}
}
return metrics
}
func parsePutvalString(instanceName string, metric redisMetric) string {
var submitValue string
if strings.Contains(metric.name, "ps_cputime") {
//redis returns decimal digits for CPU usage but collectd expects none
submitValue = strconv.FormatFloat(metric.value, 'f', 0, 64)
} else {
// only return the required decimal digitas and trim trailing 0s
submitValue = strconv.FormatFloat(metric.value, 'f', -1, 64)
}
return fmt.Sprintf("PUTVAL \"%s/redis-%s/%s\" interval=%f N:%s\n", hostname, instanceName, metric.name, collectdInterval, submitValue)
}