-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
207 lines (171 loc) · 4.35 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/parnurzeal/gorequest"
g "github.com/soniah/gosnmp"
"log"
"os"
)
var (
cmdTarget string
cmdConfigFile string
)
func init() {
flag.StringVar(
&cmdTarget, "target",
"http://192.168.99.100:8000/receive",
"Where to send data to")
flag.StringVar(
&cmdConfigFile, "conf",
"example.config.json", "Location of config file")
flag.Parse()
}
type Oids struct {
Name string `json:"name"`
Oid string `json:"oid"`
}
type SNMPConfig struct {
Community string `json:"community"`
Hostname string `json:"hostname"`
IP string `json:"ip"`
Oids struct {
CPU []Oids `json:"cpu"`
Disk []struct {
Name string `json:"name"`
Oids []Oids `json:"oids"`
} `json:"disk"`
Interfaces []struct {
Name string `json:"name"`
Oids []Oids `json:"oids"`
} `json:"interfaces"`
LoadAverage []Oids `json:"load_average"`
Memory []Oids `json:"memory"`
} `json:"oids"`
ServerSubType string `json:"server_sub_type"`
ServerType string `json:"server_type"`
Timeout int `json:"timeout"`
Verbose bool `json:"verbose"`
Database string `json:"database"`
ReceiverUrl string `json:"receiver_url"`
ReceiverToken string `json:"receiver_token"`
}
type Point struct {
Measurement string `json:"measurement"`
Fields Field `json:"fields"`
}
type Field struct {
Value interface{} `json:"value"`
}
type MessagePayload struct {
Database string `json:"db"`
Data []Item `json:"data"`
}
type Item struct {
Tags struct {
Hostname string `json:"hostname"`
Ip string `json:"ip"`
ServerType string `json:"server_type"`
ServerSubType string `json:"server_sub_type"`
Sample string `json:"sample"`
NIC string `json:"nic,omitempty"`
Disk string `json:"disk,omitempty"`
} `json:"tags"`
Points []Point `json:"points"`
}
/**
* Get SNMP metric
*/
func getSimpleMetric(measurement string, oids []Oids, conf SNMPConfig) []Item {
g.Default.Target = conf.IP
g.Default.Community = conf.Community
err := g.Default.Connect()
if err != nil {
log.Fatalf("Connect() err: %s", err)
}
defer g.Default.Conn.Close()
items := []Item{}
lookupOids := []string{}
for _, oid := range oids {
lookupOids = append(lookupOids, oid.Oid)
}
result, err2 := g.Default.Get(lookupOids)
if err2 != nil {
log.Fatalf("Get() err: %v", err2)
}
for _, variable := range result.Variables {
point := Point{}
point.Measurement = measurement
item := Item{}
item.Tags.Hostname = conf.Hostname
item.Tags.Ip = conf.IP
item.Tags.ServerType = conf.ServerType
item.Tags.ServerSubType = conf.ServerSubType
// Which oid is this?
for _, o := range oids {
if o.Oid == variable.Name {
item.Tags.Sample = o.Name
}
}
m := Field{}
switch variable.Type {
case g.OctetString:
m.Value = string(variable.Value.([]byte))
default:
m.Value = g.ToBigInt(variable.Value)
}
point.Fields = m
item.Points = append(item.Points, point)
items = append(items, item)
}
return items
}
/**
* main()
*/
func main() {
file, err := os.Open(cmdConfigFile)
if err != nil {
fmt.Println(err)
return
}
decoder := json.NewDecoder(file)
conf := SNMPConfig{}
err = decoder.Decode(&conf)
if err != nil {
fmt.Println("error:", err)
return
}
measurement := Point{}
measurement.Measurement = conf.IP
// Get all the values from the config file
payload := MessagePayload{}
payload.Database = conf.Database
loadAverages := getSimpleMetric("load_average", conf.Oids.LoadAverage, conf)
payload.Data = loadAverages
cpu := getSimpleMetric("cpu", conf.Oids.CPU, conf)
payload.Data = append(payload.Data, cpu...)
memory := getSimpleMetric("memory", conf.Oids.Memory, conf)
payload.Data = append(payload.Data, memory...)
for _, iface := range conf.Oids.Interfaces {
item := getSimpleMetric("interfaces", iface.Oids, conf)
for o, _ := range item {
item[o].Tags.NIC = iface.Name
}
payload.Data = append(payload.Data, item...)
}
for _, disk := range conf.Oids.Disk {
item := getSimpleMetric("disk", disk.Oids, conf)
for o, _ := range item {
item[o].Tags.Disk = disk.Name
}
payload.Data = append(payload.Data, item...)
}
req := gorequest.New()
req.Post(conf.ReceiverUrl).
Set("Authorization", fmt.Sprintf("Bearer %s", conf.ReceiverToken)).
Set("Content-Type", "application/json").
Send(payload).
End()
}