-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
148 lines (129 loc) · 4.32 KB
/
utils.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
// Copyright (c) 2018 the check_snmp_authors. All rights reserved.
// Use of this source code is governed by ISC-style license
// that can be found in the LICENSE file.
package check_snmp_synology
import (
"fmt"
"os"
"time"
)
var (
//OID declarations
OID_syno = "1.3.6.1.4.1.6574"
OID_model = "1.3.6.1.4.1.6574.1.5.1.0"
OID_serialNumber = "1.3.6.1.4.1.6574.1.5.2.0"
OID_DSMVersion = "1.3.6.1.4.1.6574.1.5.3.0"
OID_DSMUpgradeAvailable = "1.3.6.1.4.1.6574.1.5.4.0"
OID_systemStatus = "1.3.6.1.4.1.6574.1.1.0"
OID_temperature = "1.3.6.1.4.1.6574.1.2.0"
OID_powerStatus = "1.3.6.1.4.1.6574.1.3.0"
OID_systemFanStatus = "1.3.6.1.4.1.6574.1.4.1.0"
OID_CPUFanStatus = "1.3.6.1.4.1.6574.1.4.2.0"
OID_disk = "1.3.6.1.4.1.6574.2"
OID_diskID = "1.3.6.1.4.1.6574.2.1.1.2"
OID_diskModel = "1.3.6.1.4.1.6574.2.1.1.3"
OID_diskType = "1.3.6.1.4.1.6574.2.1.1.4"
OID_diskStatus = "1.3.6.1.4.1.6574.2.1.1.5"
OID_diskTemperature = "1.3.6.1.4.1.6574.2.1.1.6"
OID_raid = "1.3.6.1.4.1.6574.3"
OID_raidIndex = "1.3.6.1.4.1.6574.3.1.1.1"
OID_raidName = "1.3.6.1.4.1.6574.3.1.1.2"
OID_raidStatus = "1.3.6.1.4.1.6574.3.1.1.3"
OID_raidFreeSize = "1.3.6.1.4.1.6574.3.1.1.4"
OID_raidTotalSize = "1.3.6.1.4.1.6574.3.1.1.5"
//OID_Storage = "1.3.6.1.2.1.25.2.3.1"
//OID_StorageDesc = "1.3.6.1.2.1.25.2.3.1.3"
//OID_StorageAllocationUnits = "1.3.6.1.2.1.25.2.3.1.4"
//OID_StorageSize = "1.3.6.1.2.1.25.2.3.1.5"
//OID_StorageSizeUsed = "1.3.6.1.2.1.25.2.3.1.6"
//OID_UpsModel = "1.3.6.1.4.1.6574.4.1.1.0"
//OID_UpsSN = "1.3.6.1.4.1.6574.4.1.3.0"
//OID_UpsStatus = "1.3.6.1.4.1.6574.4.2.1.0"
//OID_UpsLoad = "1.3.6.1.4.1.6574.4.2.12.1.0"
//OID_UpsBatteryCharge = "1.3.6.1.4.1.6574.4.3.1.1.0"
//OID_UpsBatteryChargeWarning = "1.3.6.1.4.1.6574.4.3.1.4.0"
OID_ebox = "1.3.6.1.4.1.6574.105"
OID_eboxIndex = "1.3.6.1.4.1.6574.105.1.1.1"
OID_eboxModel = "1.3.6.1.4.1.6574.105.1.1.2"
OID_eboxPower = "1.3.6.1.4.1.6574.105.1.1.3"
OID_eboxRedundantPower = "1.3.6.1.4.1.6574.105.1.1.4"
)
type Args struct {
Hostname string
Version int
Username string
Password string
Community string
Port int
Timeout int
Commandfile string
TempWarn int
TempCrit int
StorageWarn int
StorageCrit int
UpgradeStatus bool
DiskChecks bool
HA string
Ebox int
RaidWarn int
RaidCrit int
}
type Metrics struct {
Checks int
TimeStart time.Time
TimeToConnect time.Duration
TimeToFetch time.Duration
TimeToProcess time.Duration
TimeToPrint time.Duration
TimeTotal time.Duration
}
type Utilities struct {
Args Args
Metrics Metrics
}
// Nagios exit codes
const OK = 0
const WARNING = 1
const CRITICAL = 2
const UNKNOWN = 3
func Write(hostname string, service string, exitcode int, message string, perfdata string, utils *Utilities) {
start := time.Now()
// Parameters
command := "PROCESS_SERVICE_CHECK_RESULT"
timestamp := time.Now().Unix()
var result string
// Create the messaga
if perfdata != "" {
//result = fmt.Sprintf("[%d] %s;%s;%s;%d;%s: %s | %s", timestamp, command, hostname, service, exitcode, NagiState(exitcode), message, perfdata)
result = fmt.Sprintf("[%d] %s;%s;%s;%d;%s | %s", timestamp, command, hostname, service, exitcode, message, perfdata)
} else {
//result = fmt.Sprintf("[%d] %s;%s;%s;%d;%s: %s", timestamp, command, hostname, service, exitcode, NagiState(exitcode), message)
result = fmt.Sprintf("[%d] %s;%s;%s;%d;%s", timestamp, command, hostname, service, exitcode, message)
}
// TODO Errorhandling?
if utils.Args.Commandfile == "stdout" {
fmt.Println(result)
} else {
f, err := os.OpenFile(utils.Args.Commandfile, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
fmt.Fprintf(f, "%s\n", result)
f.Close()
}
// Write successful, finally update metrics
utils.Metrics.Checks += 1
utils.Metrics.TimeToPrint += time.Now().Sub(start)
}
func NagiState(exitcode int) (state string) {
switch exitcode {
case OK:
return "OK"
case WARNING:
return "WARNING"
case CRITICAL:
return "CRITICAL"
default:
return "UNKNOWN"
}
}