/
redis.go
100 lines (83 loc) · 2.59 KB
/
redis.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 main
import (
"fmt"
"os/exec"
"strconv"
"strings"
sdkArgs "github.com/newrelic/infra-integrations-sdk/args"
"github.com/newrelic/infra-integrations-sdk/data/event"
"github.com/newrelic/infra-integrations-sdk/data/metric"
"github.com/newrelic/infra-integrations-sdk/integration"
)
type argumentList struct {
sdkArgs.DefaultArgumentList
Hostname string `default:"localhost" help:"Hostname or IP where Redis server is running."`
Port int `default:"6379" help:"Port on which Redis server is listening."`
}
const (
integrationName = "com.myorg.redis"
integrationVersion = "0.1.0"
)
var args argumentList
func queryRedisInfo(query string) (float64, error) {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("redis-cli -h %s -p %d info | grep %s", args.Hostname, args.Port, query))
output, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
splittedLine := strings.Split(string(output), ":")
if len(splittedLine) != 2 {
return 0, fmt.Errorf("cannot split the output line")
}
return strconv.ParseFloat(strings.TrimSpace(splittedLine[1]), 64)
}
func queryRedisConfig(query string) (string, string) {
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("redis-cli -h %s -p %d CONFIG GET %s", args.Hostname, args.Port, query))
output, err := cmd.CombinedOutput()
panicOnErr(err)
splittedLine := strings.Split(string(output), "\n")
return splittedLine[0], splittedLine[1]
}
func main() {
// Create Integration
i, err := integration.New(integrationName, integrationVersion, integration.Args(&args))
panicOnErr(err)
entity := i.LocalEntity()
panicOnErr(err)
// Add Event
if args.All() || args.Events {
uptime, err := queryRedisInfo("uptime_in_seconds:")
panicOnErr(err)
if uptime < 60 {
err = entity.AddEvent(event.NewNotification("Redis Server recently started"))
}
panicOnErr(err)
if uptime < 60 {
err = entity.AddEvent(event.New("Redis Server recently started", "redis-server"))
}
panicOnErr(err)
}
// Add Inventory item
if args.All() || args.Inventory {
key, value := queryRedisConfig("dbfilename")
err = entity.SetInventoryItem(key, "value", value)
panicOnErr(err)
key, value = queryRedisConfig("bind")
err = entity.SetInventoryItem(key, "value", value)
panicOnErr(err)
}
// Add Metric
if args.All() || args.Metrics {
ms := entity.NewMetricSet("MyorgRedisSample")
metricValue, err := queryRedisInfo("instantaneous_ops_per_sec:")
panicOnErr(err)
err = ms.SetMetric("query.instantaneousOpsPerSecond", metricValue, metric.GAUGE)
panicOnErr(err)
}
panicOnErr(i.Publish())
}
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}