-
Notifications
You must be signed in to change notification settings - Fork 85
/
snmp_environmental_1.py
239 lines (207 loc) · 9.57 KB
/
snmp_environmental_1.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
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
#!/usr/bin/env python
"""snmp-environmental - Exports JSON file with SNMP data
Output data will look like this:
{"temperature": "75", "humidity": "50", "ups_load": "4.7", "runtime": "1:17"}
"""
from pysnmp.entity.rfc3413.oneliner import cmdgen
import time
import json
import logging.config
from credentials import SNMP_COMMUNITY
__author__ = '[email protected] (Scott Vintinner)'
# =================================SETTINGS======================================
SAMPLE_INTERVAL = 60
# ===============================================================================
class MonitorJSON:
"""This is a simple class passed to Monitor threads so we can access the current JSON data in that thread"""
def __init__(self):
self.json = json.dumps(
{
"ups_load": "--",
"runtime": "--",
"temperature": "--",
"hot_aisle": "--",
"humidity": "--"
}, indent=4
)
def generate_json(snmp_monitor):
"""This function will take the device config and raw data (if any) from the snmp_monitor and output JSON data
formatted for the StatusBar iPad App"""
logger = logging.getLogger("snmp_environmental_1")
# Create the pysnmp SNMP CommandGenerator, used to get SNMP data
cmd_gen = cmdgen.CommandGenerator()
try:
# ===============CLT NetBotz1 data
logger.debug("Getting: CLT NetBotz1 data")
error_indication, error_status, error_index, var_binds = cmd_gen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget(('10.5.50.235', 161)),
"1.3.6.1.4.1.5528.100.4.1.1.1.9.636159851", # Room Temp (Rack 4 Top)
"1.3.6.1.4.1.5528.100.4.1.1.1.9.3031356659", # Hot Aisle (HAC 2 Temp)
"1.3.6.1.4.1.5528.100.4.1.2.1.8.1744856019" # Humidity
)
if error_indication or error_status:
logger.warning("CLT NetBotz1: " + str(error_indication))
clt_temperature = "XX"
hot_aisle = "XX"
clt_humidity = "XX"
else:
clt_temperature = int(var_binds[0][1])
hot_aisle = int(var_binds[1][1])
clt_humidity = int(var_binds[2][1])
# ===============CLT NetBotz2 data
logger.debug("Getting: CLT NetBotz2 data")
error_indication, error_status, error_index, var_binds = cmd_gen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget(('10.5.50.236', 161)),
"1.3.6.1.4.1.5528.100.4.1.1.1.9.2628357572", # Cold Aisle (Rack 3 bottom Temp)
)
if error_indication or error_status:
logger.warning("CLT NetBotz2: " + str(error_indication))
cold_aisle = "XX"
else:
cold_aisle = int(var_binds[0][1])
# ============ CLT Symmetra data
logger.debug("Getting: CLT Symmetra data")
error_indication, error_status, error_index, var_binds = cmd_gen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY),
cmdgen.UdpTransportTarget(("10.5.50.230", 161)),
"1.3.6.1.4.1.318.1.1.1.2.2.3.0", # UPS Runtime
"1.3.6.1.4.1.318.1.1.1.9.3.3.1.7.1.1.1", # UPS Load Phase 1
"1.3.6.1.4.1.318.1.1.1.9.3.3.1.7.1.1.2", # UPS Load Phase 2
"1.3.6.1.4.1.318.1.1.1.9.3.3.1.7.1.1.3" # UPS Load Phase 3
)
if error_indication or error_status:
logger.warning("CLT Symmetra: " + str(error_indication))
clt_runtime = "XX"
clt_load = "XX"
else:
clt_runtime = int(var_binds[0][1]) / 100 / 60 # Convert TimeTicks to Seconds to minutes
clt_runtime = int(clt_runtime)
load_p1 = int(var_binds[1][1])
load_p2 = int(var_binds[2][1])
load_p3 = int(var_binds[3][1])
clt_load = (load_p1 + load_p2 + load_p3) / 1000 # Convert to kVA
clt_load = round(clt_load, 1)
# ===============RH APC SMARTUPS (Must be SNMPv1)
logger.debug("Getting: RH APC SMARTUPS data")
error_indication, error_status, error_index, var_binds = cmd_gen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY, mpModel=0), # mpModel=0 for SNMPv1
cmdgen.UdpTransportTarget(("apc-rh-0.rbh.local", 161)),
"1.3.6.1.2.1.33.1.2.3.0", # UPS Runtime
"1.3.6.1.2.1.33.1.4.4.1.5.1", # UPS Output Load Percent
"1.3.6.1.4.1.318.1.1.10.2.3.2.1.6.1", # Humidity
"1.3.6.1.4.1.318.1.1.10.2.3.2.1.4.1" # Temperature
)
if error_indication or error_status:
logger.warning("TRI APC: " + str(error_indication))
rh_temperature = "XX"
rh_humidity = "XX"
rh_load = "XX"
rh_runtime = "XX"
else:
rh_runtime = int(var_binds[0][1])
rh_load = int(var_binds[1][1])
rh_humidity = int(var_binds[2][1])
rh_temperature = int(var_binds[3][1])
rh_temperature = int(1.8 * rh_temperature) + 32
# ===============TRI APC SMARTUPS (Must be SNMPv1)
logger.debug("Getting: TRI APC SMARTUPS data")
error_indication, error_status, error_index, var_binds = cmd_gen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY, mpModel=0), # mpModel=0 for SNMPv1
cmdgen.UdpTransportTarget(("apc-tri-0.rbh.local", 161)),
"1.3.6.1.2.1.33.1.2.3.0", # UPS Runtime
"1.3.6.1.2.1.33.1.4.4.1.5.1", # UPS Output Load Percent
"1.3.6.1.4.1.318.1.1.10.2.3.2.1.6.1", # Humidity
"1.3.6.1.4.1.318.1.1.10.2.3.2.1.4.1" # Temperature
)
if error_indication or error_status:
logger.warning("TRI APC: " + str(error_indication))
tri_temperature = "XX"
tri_humidity = "XX"
tri_load = "XX"
tri_runtime = "XX"
else:
tri_runtime = int(var_binds[0][1])
tri_load = int(var_binds[1][1])
tri_humidity = int(var_binds[2][1])
tri_temperature = int(var_binds[3][1])
tri_temperature = int(1.8 * tri_temperature) + 32
# ===============RAL APC SMARTUPS (Must be SNMPv1)
logger.debug("Getting: RAL APC SMARTUPS data")
error_indication, error_status, error_index, var_binds = cmd_gen.getCmd(
cmdgen.CommunityData(SNMP_COMMUNITY, mpModel=0), # mpModel=0 for SNMPv1
cmdgen.UdpTransportTarget(("apc-ral-0.rbh.local", 161)),
"1.3.6.1.2.1.33.1.2.3.0", # UPS Runtime
"1.3.6.1.2.1.33.1.4.4.1.5.1", # UPS Output Load Percent
"1.3.6.1.4.1.318.1.1.10.2.3.2.1.6.1", # Humidity
"1.3.6.1.4.1.318.1.1.10.2.3.2.1.4.1" # Temperature
)
if error_indication or error_status:
logger.warning("RAL APC: " + str(error_indication))
ral_temperature = "XX"
ral_humidity = "XX"
ral_load = "XX"
ral_runtime = "XX"
else:
ral_runtime = int(var_binds[0][1])
ral_load = int(var_binds[1][1])
ral_humidity = int(var_binds[2][1])
ral_temperature = int(var_binds[3][1])
ral_temperature = int(1.8 * ral_temperature) + 32
# =========== Create Dictionary for JSON output
snmp_data = {
"cold_aisle": cold_aisle,
"hot_aisle": hot_aisle,
"clt_temp": clt_temperature,
"clt_humidity": clt_humidity,
"clt_load": clt_load,
"clt_runtime": clt_runtime,
"tri_temp": tri_temperature,
"tri_humidity": tri_humidity,
"tri_load": tri_load,
"tri_runtime": tri_runtime,
"ral_temp": ral_temperature,
"ral_humidity": ral_humidity,
"ral_load": ral_load,
"ral_runtime": ral_runtime,
"rh_temp": rh_temperature,
"rh_humidity": rh_humidity,
"rh_load": rh_load,
"rh_runtime": rh_runtime
}
except Exception as error:
snmp_data = {"error": "There was a problem accessing SNMP data"}
logger.error("Error getting SNMP:" + str(error))
snmp_monitor.json = json.dumps(snmp_data)
logger.debug(snmp_monitor.json)
return
#
# ======================================================
# __main__
#
# If you run this module by itself, it will instantiate
# the MonitorJSON class and start an infinite loop
# printing data.
# ======================================================
#
if __name__ == '__main__':
# When run by itself, we need to create the logger object (which is normally created in webserver.py)
try:
f = open("log_settings.json", 'rt')
log_config = json.load(f)
f.close()
logging.config.dictConfig(log_config)
except FileNotFoundError as e:
print("Log configuration file not found: " + str(e))
logging.basicConfig(level=logging.DEBUG) # fallback to basic settings
except json.decoder.JSONDecodeError as e:
print("Error parsing logger config file: " + str(e))
raise
monitor = MonitorJSON()
while True:
main_logger = logging.getLogger(__name__)
generate_json(monitor)
# Wait X seconds for the next iteration
main_logger.debug("Waiting for " + str(SAMPLE_INTERVAL) + " seconds")
time.sleep(SAMPLE_INTERVAL)