-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
360 lines (308 loc) · 9.94 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright (c) 2019 Bernhard Fluehmann. All rights reserved.
// Use of this source code is governed by ISC-style license
// that can be found in the LICENSE file.
// Nagios check for poe disconnect events
// Mainly used to protect public facing AP ports
// Optionally provision (block) affected ports
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"strconv"
"strings"
"time"
checker "github.com/BFLB/monitoringplugin"
p "github.com/BFLB/monitoringplugin/performancedata"
r "github.com/BFLB/monitoringplugin/range"
"github.com/BFLB/monitoringplugin/status"
activeWriter "github.com/BFLB/monitoringplugin/writers/activeWriter"
"github.com/BFLB/unifi"
)
// Goreleaser
var (
version = "dev"
commit = "none"
date = "unknown"
)
// Flags
var (
host = flag.String("host", "", "Controller hostname")
port = flag.String("port", "8443", "Controller port")
site = flag.String("site", "default", "Site ID or name, UniFi v3 and later")
user = flag.String("user", "", "Controller username")
pass = flag.String("pass", "", "Controller password")
path = flag.String("path", "", "Path of output file")
contVersion = flag.Int("version", 5, "Controller base version")
warning = flag.String("warning", "7", "Execution Time(s) warning threshold")
critical = flag.String("critical", "10", "Execution Time(s) critical threshold")
profileBlock = flag.String("profileBlock", "", "If set, affected ports will be set to the given port profile, e.g. disabled")
profileCurr = flag.String("profileCurr", "", "Check only applies to ports of given port profile. Only non default profiles allowed")
portNameBlock = flag.String("portNameBlock", "", "Set port name after blocking")
portNameCurr = flag.String("portNameCurr", "", "Set port name after reset (set back to curr)")
perfdata = flag.Bool("perfdata", false, "Add perfdata")
eventFilterLimit = flag.Int("eventFilterLimit", 3000, "Maximum number of alert events to be fetched")
eventFilterStart = flag.Int("eventFilterStart", 0, "At witch alarm event to start fetching")
eventFilterWithin = flag.Int("eventFilterWithin", 24, "How many hours back to be fetched")
v = flag.Bool("V", false, "Version")
)
// Counter struct used for extit message
type counters struct {
events int
provisionedBlock int
provisionedUnblock int
blocked int
unblocked int
failed int
}
func main() {
// Timestamp to calculate execution time
timestampStart := time.Now()
// Counters
counters := counters{}
// Create check
check := checker.New()
// Check-message
message := ""
// Create writer
writer := activeWriter.New()
// Parse command-line args
flag.Parse()
// Version information
if *v {
message = fmt.Sprintf("%v, commit %v, built at %v, (monitoring-library:%s)", version, commit, date, checker.VERSION)
check.Status.Unknown()
check.Message(message)
writer.Write(check)
}
// Check mandatory args
if *host == "" {
flag.Usage()
}
if *user == "" {
flag.Usage()
}
if *pass == "" {
flag.Usage()
}
if *profileCurr == "" {
flag.Usage()
}
// Catch not allowed profile settings
switch strings.ToLower(*profileCurr) {
case "", "all", "disabled":
flag.Usage()
}
// Set ranges (monitoring) TODO: Move down
var rangeExecWarn *r.Range
var rangeExecCrit *r.Range
if *warning != "" {
rangeExecWarn = r.New()
rangeExecWarn.Parse(*warning)
}
if *critical != "" {
rangeExecCrit = r.New()
rangeExecCrit.Parse(*critical)
}
// Login to UniFi controller
u, err := unifi.Login(*user, *pass, *host, *port, *site, *contVersion)
if err != nil {
message = fmt.Sprintf("Login error:%s", err.Error())
check.Status.Unknown()
check.Message(message)
writer.Write(check)
}
defer u.Logout()
// Get site
site, err := u.Site(*site)
if err != nil {
log.Fatal(err)
}
// Get current port-profile
currProfile, err := u.PortProfile(site, *profileCurr)
if err != nil {
message = fmt.Sprintf("Port-profile not found:%s", err.Error())
check.Status.Unknown()
check.Message(message)
writer.Write(check)
}
// Get block port-profile (optional)
var blockProfile *unifi.PortProfile
if *profileBlock == "" {
blockProfile = nil
} else {
blockProfile, err = u.PortProfile(site, *profileBlock)
if err != nil {
message = fmt.Sprintf("Port-profile not found:%s", err.Error())
check.Status.Unknown()
check.Message(message)
writer.Write(check)
}
}
// Set event filters
var eventFilter unifi.EventFilter
// TODO: Add flags
eventFilter.Limit = *eventFilterLimit
eventFilter.Start = *eventFilterStart
eventFilter.Within = *eventFilterWithin
// Get a slice of raw events
rawAlarms, err := u.RawAlarms(site, eventFilter)
if err != nil {
log.Fatalln(err)
return
}
// Get a slice of poe events
events, err := poeEvents(rawAlarms)
for _, event := range events {
do(u, site, event, currProfile, blockProfile, *portNameCurr, *portNameBlock, &counters)
if err != nil {
counters.failed += 1
}
}
tExec := time.Now().Sub(timestampStart).Seconds()
// HACK: Better way to do it?
// Round to 3 digits
tExecRounded := fmt.Sprintf("%.3f", tExec)
tExec, _ = strconv.ParseFloat(tExecRounded, 64)
// Add message
message = fmt.Sprintf("%d active matching alerts, %d ports blocked (%d provisioned-block, %d provisioned-unblock, %d failed, EcecTime %f)", counters.events, counters.blocked, counters.provisionedBlock, counters.provisionedUnblock, counters.failed, tExec)
check.Message(message)
// Set ranges for Executiontime warning and critical
rangeWarn := r.New()
rangeWarn.Parse(*warning)
rangeCrit := r.New()
rangeCrit.Parse(*critical)
// Add performance data
if *perfdata {
dataObj, _ := p.New("ActMatchAlerts", float64(counters.events), "", nil, r.New(), nil, nil)
check.Perfdata(dataObj)
dataObj, _ = p.New("PortsBlocked", float64(counters.blocked), "", r.New(), nil, nil, nil)
check.Perfdata(dataObj)
dataObj, _ = p.New("ProvBlocked", float64(counters.provisionedBlock), "", nil, nil, nil, nil)
check.Perfdata(dataObj)
dataObj, _ = p.New("ProvUnblocked", float64(counters.provisionedBlock), "", nil, nil, nil, nil)
check.Perfdata(dataObj)
dataObj, _ = p.New("Failed", float64(counters.failed), "", nil, r.New(), nil, nil)
check.Perfdata(dataObj)
dataObj, _ = p.New("ExecTime", tExec, "s", rangeWarn, rangeCrit, nil, nil)
check.Perfdata(dataObj)
}
// Everything done. Setup return values and quit
// Set Status
// New Status (OK)
status := status.New()
// Status Events (Critical if > 0)
status.Threshold(float64(counters.events), nil, r.New(), false)
// Status Failed (Critical if > 0)
status.Threshold(float64(counters.failed), nil, r.New(), false)
// Status blocked (Warning if > 0)
status.Threshold(float64(counters.blocked), r.New(), nil, false)
// Status Executiontime (Warning if > warning, Critical if > critical)
status.Threshold(tExec, rangeWarn, rangeCrit, false)
// Assign Status to check
check.Status = status
// Write results
writer.Write(check)
}
// Gets the switch and port and checks if the configured profile belongs to the check.
// Adds coniguration changes to the port depending of command arguments
func do(u *unifi.Unifi, site *unifi.Site, event unifi.EVT_SW_PoeDisconnect, currProfile *unifi.PortProfile, blockProfile *unifi.PortProfile, currPortName string, blockPortName string, c *counters) error {
// Get the switch
usw, err := u.USW(site, event.SwName)
if err != nil {
return err
}
// Get port-overrides (non default settings)
overrides := usw.PortOverrides
// Find the port
for i := range usw.PortOverrides {
if overrides[i].PortIdx == event.Port {
// Active (non archived) event
if archived(event) == false {
// Check if port belongs current profile matches
if overrides[i].PortconfID == currProfile.ID {
c.events += 1
// Check if must be blocked
if blockProfile != nil {
// Change settings (block)
overrides[i].PortconfID = blockProfile.ID
if blockPortName != "" {
overrides[i].Name = blockPortName
}
u.SetPortoverrides(site, usw.DeviceID, overrides)
c.provisionedBlock += 1
}
} else { // Check if port already blocked
if overrides[i].PortconfID == blockProfile.ID {
c.events += 1
c.blocked += 1
}
}
} else { // Archived event
// Check if port belongs to blocked profile
if blockProfile != nil {
if overrides[i].PortconfID == blockProfile.ID {
c.blocked += 1
// Change settings (unblock)
overrides[i].PortconfID = currProfile.ID
if currPortName != "" {
overrides[i].Name = currPortName
}
u.SetPortoverrides(site, usw.DeviceID, overrides)
c.provisionedUnblock += 1
}
}
}
break
}
}
return nil
}
// Returns a slice with poeEvents. One per switch/port combination. Non-Archived wins.
func poeEvents(rawEvents []unifi.RawAlarm) ([]unifi.EVT_SW_PoeDisconnect, error) {
var poeEvents []unifi.EVT_SW_PoeDisconnect
for _, rawEvent := range rawEvents {
switch rawEvent.Key {
case "EVT_SW_PoeDisconnect":
// Unmarshal rawEvent to poeEvent
var e unifi.EVT_SW_PoeDisconnect
err := json.Unmarshal(rawEvent.Data, &e)
if err != nil {
return poeEvents, err
}
// Avoid duplicates
if len(poeEvents) == 0 {
poeEvents = append(poeEvents, e)
} else {
var found bool
found = false
for _, poeEvent := range poeEvents {
if poeEvent.SwName == e.SwName && poeEvent.Port == e.Port {
found = true
// Override archived
if e.Archived == nil {
poeEvent.Archived = nil
break
} else if *e.Archived == false {
*poeEvent.Archived = false
break
}
}
}
// New event
if found == false {
poeEvents = append(poeEvents, e)
}
}
}
}
return poeEvents, nil
}
// Check if event is archived
func archived(event unifi.EVT_SW_PoeDisconnect) bool {
if event.Archived == nil {
return false
}
return *event.Archived
}