forked from hobbsh/check-idrac-sensor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_idrac_sensor.py
executable file
·209 lines (166 loc) · 5.86 KB
/
check_idrac_sensor.py
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
#!/usr/bin/env python
import sys
import argparse
import re
import json
import paramiko
def build_parser():
parser = argparse.ArgumentParser(description='Check iDRAC Sensors')
parser.add_argument('-H', '--host', required=True, type=str, dest='host')
parser.add_argument(
'-u', '--username', required=True, type=str, dest='username')
parser.add_argument(
'-p', '--password', required=True, type=str, dest='password')
parser.add_argument(
'-s', '--sensortype', required=False, type=str, dest='sensor', default='all')
parser.add_argument(
'-d', '--debug', required=False, type=bool, dest='debug', default=False)
return parser
def main():
parser = build_parser()
args = parser.parse_args()
debug = args.debug
valid_sensors = [
'battery',
'current',
'intrusion',
'memory',
'power',
'temperature',
'fan',
'performance',
'processor',
'redundancy',
'system_performance',
'voltage',
'all'
]
if args.sensor in valid_sensors:
host = args.host
user = args.username
password = args.password.strip("'").replace('\\', '')
sensor = args.sensor
sensor_data = ssh_connect(host, user, password, "racadm getsensorinfo")
if sensor_data:
formatted_out = lines_to_dict(sensor_data)
sensors_text = nagios_output(formatted_out, sensor)
if debug:
print(json.dumps(formatted_out, sort_keys=True, indent=4))
if sensors_text:
return sensors_text
else:
print("No response from iDRAC!")
sys.exit(3)
else:
print("ERROR: Invalid command or sensortype. Please check that command or sensortype is valid. Exiting...")
sys.exit(3)
def ssh_connect(host, user, password, command):
try:
drac_con = paramiko.SSHClient()
drac_con.set_missing_host_key_policy(paramiko.AutoAddPolicy())
drac_con.connect(host, username=user, password=password)
stdin, stdout, stderr = drac_con.exec_command(command)
stdin.close()
return stdout.readlines()
except Exception as e:
print("UNKNOWN: Unable to run ssh racadm by SSH: {}".format(e))
sys.exit(3)
# Extract output lines and format a huge dict with all sensors informations
def lines_to_dict(lines):
sensors = {}
match = False
g = ""
for line in lines:
m = re.match("Sensor\\sType\\s:\\s([A-Z\\s]+)", line)
if m:
match = True
g = m.groups()[0].strip().replace(' ', '_')
sensors[g] = {}
elif re.findall('^\\<|^\\[', line):
# Jump if line starts with junk as < or [
continue
else:
sections = [x.strip() for x in line.split(' ') if x.strip()]
if sections:
s = sections.pop(0)
sensors[g][s] = sections
return sensors
# Ugly sensor output
def dump_sensors(sensors):
for item_name, sensor_arr in sensors.items():
print("ITEM = {}".format(item_name))
for sensor_name, sensor_values in sensor_arr.items():
print("\t * {}".format(sensor_name))
for value in sensor_values:
print("\t\t - {}".format(value))
# Each sensors should have a matching function to format the results
def redundancy(status):
redundancy_out = ''
for n, a in sorted(status.items()):
if "Full Redundant" in a:
redundancy_out += '- %s : is Ok ' % n
return redundancy_out
def power(status):
power_out = ''
for n, a in sorted(status.items()):
if "Present" == a[0]:
power_out += "- %s : is Ok " % n
return power_out
def memory(status):
memory_out = ''
for n, a in sorted(status.items()):
if "Presence_Detected" == a[1]:
memory_out += "- %s is %s " % (n, a[0])
return memory_out
def intrusion(status):
intrusion_out = ''
for n, a in sorted(status.items()):
if "Closed" == a[0]:
intrusion_out += "- %s is Ok " % (n)
return intrusion_out
# Generic status retriever no further formating is required
def sensor_generic(status):
sensor_generic = ''
for n, a in sorted(status.items()):
sensor_generic += "- %s=%s is %s " % (n, a[1], a[0])
return sensor_generic
# Generate text output icinga/nagios formatted
def nagios_output(sensor_data, sensor):
'''Provide Nagios output for check results'''
# Function pointer look-alike map
funct_map = {
'redundancy': redundancy,
'temperature': sensor_generic,
'power': power,
'battery': sensor_generic,
'system_performance': sensor_generic,
'current': sensor_generic,
'fan': sensor_generic,
'voltage': sensor_generic,
'memory': memory,
'performance': sensor_generic,
'intrusion': intrusion,
'processor': sensor_generic
}
output = ''
if sensor == 'all':
for item_name, sensors in sensor_data.items():
output += "[%s] " % item_name
output += funct_map[item_name.lower()](sensors)
else:
output += "[%s] " % sensor.upper()
output = funct_map[sensor](sensor_data[sensor.upper()])
return output
if __name__ == "__main__":
sensors_text = main()
# Let's take care of Nagios / Icinga statutes
# Just match Warning/Critical in output messages
if re.findall('Critical', sensors_text):
print("CRITICAL: {}".format(sensors_text))
sys.exit(2)
elif re.findall('Warning', sensors_text):
print("WARNING: {}".format(sensors_text))
sys.exit(1)
else:
print("OK: {}".format(sensors_text))
sys.exit(0)