/
redis-multi.go
148 lines (129 loc) · 3.68 KB
/
redis-multi.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"errors"
"fmt"
"os/exec"
"strconv"
"strings"
"time"
sdkArgs "github.com/newrelic/infra-integrations-sdk/v4/args"
"github.com/newrelic/infra-integrations-sdk/v4/data/event"
"github.com/newrelic/infra-integrations-sdk/v4/integration"
)
type argumentList struct {
sdkArgs.DefaultArgumentList
}
const (
integrationName = "com.myorganization.redis-multi"
integrationVersion = "0.1.0"
instanceOnePort = 16379
instanceTwoPort = 26379
)
var (
args argumentList
)
func queryGaugeRedisInfo(query string, port int) (float64, error) {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("redis-cli -p %d info | grep %s", port, query))
output, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
splittedLine := strings.Split(string(output), ":")
if len(splittedLine) != 2 {
return 0, errors.New("cannot split the output line")
}
return strconv.ParseFloat(strings.TrimSpace(splittedLine[1]), 64)
}
func queryAttrRedisInfo(query string, port int) (string, string) {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("redis-cli -p %d info | grep %s", port, query))
output, err := cmd.CombinedOutput()
if err != nil {
return "", ""
}
splittedLine := strings.Split(string(output), ":")
if len(splittedLine) != 2 {
return "", ""
}
return strings.TrimSpace(splittedLine[0]), strings.TrimSpace(splittedLine[1])
}
func main() {
// Create Integration
i, err := integration.New(integrationName, integrationVersion, integration.Args(&args))
panicOnErr(err)
// Create Entity, entities name must be unique
e1, err := i.NewEntity("instance-1", "instance-1", "redis")
panicOnErr(err)
// Add event when redis starts
if args.All() || args.Events {
uptime, err := queryGaugeRedisInfo("uptime_in_seconds:", instanceOnePort)
panicOnErr(err)
if uptime < 60 {
ev, _ := event.NewNotification("Redis Server recently started")
e1.AddEvent(ev)
}
panicOnErr(err)
if uptime < 60 {
ev, _ := event.New(time.Now(), "summary", "category")
e1.AddEvent(ev)
}
panicOnErr(err)
}
// Add Inventory item
if args.All() || args.Inventory {
key, value := queryAttrRedisInfo("redis_version", instanceOnePort)
if key != "" {
err = e1.AddInventoryItem(key, "value", value)
}
panicOnErr(err)
}
// Add Metric
if args.All() || args.Metrics {
metricValue, err := queryGaugeRedisInfo("instantaneous_ops_per_sec:", instanceOnePort)
panicOnErr(err)
g, _ := integration.Gauge(time.Now(), "query.instantaneousOpsPerSecond", metricValue)
e1.AddMetric(g)
}
// Add the first entity to the integration
i.AddEntity(e1)
// Create another Entity
e2, err := i.NewEntity("instance-2", "my-instance", "redis")
panicOnErr(err)
// Add event when redis starts
if args.All() || args.Events {
uptime, err := queryGaugeRedisInfo("uptime_in_seconds:", instanceOnePort)
panicOnErr(err)
if uptime < 60 {
ev, _ := event.NewNotification("Redis Server recently started")
e2.AddEvent(ev)
}
panicOnErr(err)
if uptime < 60 {
ev, _ := event.New(time.Now(), "summary", "category")
e2.AddEvent(ev)
}
panicOnErr(err)
}
// Add Inventory item
if args.All() || args.Inventory {
key, value := queryAttrRedisInfo("redis_version", instanceTwoPort)
if key != "" {
err = e2.AddInventoryItem(key, "value", value)
}
panicOnErr(err)
}
if args.All() || args.Metrics {
metricValue, err := queryGaugeRedisInfo("instantaneous_ops_per_sec:", instanceTwoPort)
panicOnErr(err)
g, _ := integration.Gauge(time.Now(), "query.instantaneousOpsPerSecond", metricValue)
e2.AddMetric(g)
}
// Add the second entity to the integration
i.AddEntity(e2)
// Print the JSON document to stdout
panicOnErr(i.Publish())
}
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}