-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyno_monitoring_azure.py
476 lines (355 loc) · 17.8 KB
/
syno_monitoring_azure.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/bin/python
#########################################################################################################
# Author: Kernelkaribou
#
# Script captures SNMP data from a synology NAS and sends it to Azure Log Analytics to be reviewed.
# The submission logic was taken from the HTTP API for Log Analytics, Python Sample:
# https://docs.microsoft.com/en-us/azure/azure-monitor/platform/data-collector-api#sample-requests
# Script is free to use however you want
#########################################################################################################
from subprocess import check_output
import requests
import json
import datetime
import hashlib
import hmac
import base64
import re
import math
import os
import time
##################
######Config######
##################
host_address = "localhost" #NAS IP, suggest to run directly on NAS but can be remote
hostname = "" #Leave blank if you want it to pull from the NAS itself, otherwise it can be defined here.
#Capture Interval in seconds. Highly recommend nothing below 20 seconds, set to 60 if you want it to capture once per minute.
capture_interval = 20
#Stats to Capture, set to false for metrics not desired, true to be captured
capture_system_temperature = "true"
capture_cpu = "true"
capture_memory = "true"
capture_network = "true"
capture_volume = "true"
capture_disk = "true"
capture_ups = "false" #Default is false, enable if you have an UPS connected to your Syno and want the metrics
ups_name = "" #Leave blank if you want to have it be the hostname of the NAS, otherwise you customize it here
# This is your Log Analytics workspace ID
workspace_id = "xxxxxxxx-xxx-xxx-xxx-xxxxxxxxxxxx"
# For the shared key, use either the primary or the secondary Connected Sources client authentication key
shared_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# The log type is the name of the event that is being submitted. _CL is appended by Azure to whatever value is set here. You will query the below value + '_CL'
log_type = 'SynoMon'
#####################
######Functions######
#####################
#Function to pull the SNMP Stats based upon OID and flags
def get_snmp_data(host_address, oid, snmpwalk_flags):
snmp_data = check_output(["snmpwalk", "-v", "2c", "-c", "public", host_address, oid, "-O", snmpwalk_flags]).decode("utf-8").replace("\"","").replace('\r', "").split('\n')
snmp_data = list(filter(None, snmp_data)) #Remove Empty entries from split
return snmp_data
#Function to build List of metrics for a data point
def build_counter_list(hostname, object_name, counter_name, instance_name, counter_value, counter_type):
counter_list = ({"Computer" : hostname, "ObjectName" : object_name, "CounterName" : counter_name, "InstanceName" : instance_name , "CounterValue" : counter_value, "Type" : counter_type})
return counter_list
#Function to get instance name and ID for SNMP stats of multiple data points e.g. volumes or Disks
def get_snmp_instances(snmp_data):
instance_name = snmp_data.rsplit('STRING: ', 1)[-1]
instance_id = snmp_data.rsplit('.', 1)[-1].split(" = STRING")[0]
snmp_instances = {"name" : instance_name, "id" : instance_id}
return snmp_instances
def get_instance_value(oid, oid_dump, data_type):
#Iterate through SNMP MIB data and find matching OID for a specific instance
idx = [i for i, item in enumerate(oid_dump) if re.search(oid, item)][0]
instance_value = oid_dump[idx].rsplit(': ', 1)[-1].split(" ")[0]
if data_type == "int":
instance_value = int(instance_value)
elif data_type == "str":
instance_value = str(instance_value)
elif data_type == "percent":
instance_value = int(float(instance_value))
return instance_value
def write_file(net_octets):
print("Writing file here")
#Getting System Temperature
def get_system_temperature():
object_name = "System"
instance_name = "System Temperature"
counter_type = "Status"
oid_system_temp = "1.3.6.1.4.1.6574.1.2"
snmpwalk_flags = "qv"
counter_value = get_snmp_data(host_address, oid_system_temp, snmpwalk_flags)
system_temp = int(counter_value[0])
counter_name = "Temperature"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance_name, system_temp, counter_type))
#Getting CPU Usage
def get_cpu_counters():
object_name = "Processor"
instance_name = "_Total"
counter_type = "Perf"
oid_processor = "1.3.6.1.4.1.2021.11.11.0"
snmpwalk_flags = "qv"
counter_value = get_snmp_data(host_address, oid_processor, snmpwalk_flags)
processor_load = 100 - int(counter_value[0])
counter_name = "% Processor Time"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance_name, processor_load, counter_type))
#Getting Memory Information (this requires gathering multiple metrics for calculation)
def get_memory_counters():
object_name = "Memory"
instance_name = "Memory"
counter_type = "Perf"
memory_oid = "1.3.6.1.4.1.2021.4"
snmpwalk_flags = "n"
memory_data = get_snmp_data(host_address, memory_oid, snmpwalk_flags)
oid_memory_total = "1.3.6.1.4.1.2021.4.5.0"
memory_total = get_instance_value(oid_memory_total, memory_data, "int")
oid_memory_avail = "1.3.6.1.4.1.2021.4.6.0"
memory_avail = get_instance_value(oid_memory_avail, memory_data, "int")
oid_memory_buffer = "1.3.6.1.4.1.2021.4.14.0"
memory_buffer = get_instance_value(oid_memory_buffer, memory_data, "int")
oid_memory_cached = "1.3.6.1.4.1.2021.4.15.0"
memory_cached = get_instance_value(oid_memory_cached, memory_data, "int")
memory_used = int(round(((memory_total - memory_avail - memory_buffer - memory_cached) / float(memory_total)) * 100))
counter_name = "% Used Memory"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance_name, memory_used, counter_type))
#Getting Network OID Information
def get_network_counters():
object_name = "Network"
counter_type = "Perf"
network_oid = "1.3.6.1.2.1.2.2.1"
snmpwalk_flags = "n"
network_data = get_snmp_data(host_address, network_oid, snmpwalk_flags)
#First confirm which instance matches the networks to be reviewed, gathering ID and Name for each
network_instances = []
for network in network_data:
if re.search("eth+[0-9]$", network) or re.search("bond+[0-9]$", network):
network_instances.append(get_snmp_instances(network))
#iterate through each network instance and get details of interestered OID
for instance in network_instances:
#Getting Network Rx stat
oid_netrx = "1.3.6.1.2.1.2.2.1.10." + instance["id"]
current_rx = get_instance_value(oid_netrx, network_data, "int")
counter_name = "Total Octets Received"
oid_nettx = "1.3.6.1.2.1.2.2.1.16." + instance["id"]
current_tx = get_instance_value(oid_nettx, network_data, "int")
counter_name = "Total Octets Transmitted"
oid_netspeed = "1.3.6.1.2.1.2.2.1.5." + instance["id"]
net_speed = get_instance_value(oid_netspeed, network_data, "int")
current_timestamp = int((datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(0)).total_seconds())
#Check if we have a previous capture or if the instance/NIC name is new to us
if not snmp_previous_data or not instance["name"] in snmp_previous_data:
rx_rate = 0
tx_rate = 0
else:
#Get time difference for caculation of the data rate
time_difference = int(current_timestamp) - int(snmp_previous_data["timecaptured"])
#Calculate the speed
rx_rate = abs(((current_rx - snmp_previous_data[instance["name"]]["rx"]) / time_difference ))
tx_rate = abs(((current_tx - snmp_previous_data[instance["name"]]["tx"]) / time_difference ))
#Add Data to counter capture list
counter_name = "Bytes Received/sec"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], rx_rate, counter_type))
counter_name = "Bytes Transmitted/sec"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], tx_rate, counter_type))
#Add raw data to counter capture list
counter_name = "Bytes Received"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], current_rx, counter_type))
counter_name = "Bytes Transmitted"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], current_tx, counter_type))
#add the current counters to the list
snmp_current_data[instance["name"]] = {
"rx" : current_rx,
"tx" : current_tx
}
#snmp_current_data["timecaptured"] = current_timestamp
#Getting Volume Information
def get_volume_counters():
object_name = "Logical Volume"
counter_type = "Status"
volume_oid = "1.3.6.1.2.1.25.2.3.1"
snmpwalk_flags = "n"
volume_data = get_snmp_data(host_address, volume_oid, snmpwalk_flags)
volume_instances = []
for volume in volume_data:
if re.search("/volume+[0-9]$", str(volume)):
volume_instances.append(get_snmp_instances(volume))
for instance in volume_instances:
oid_volume_blocksize = "1.3.6.1.2.1.25.2.3.1.4." + instance["id"]
volume_blocksize = get_instance_value(oid_volume_blocksize, volume_data, "int")
oid_volume_size = "1.3.6.1.2.1.25.2.3.1.5." + instance["id"]
volume_size = get_instance_value(oid_volume_size, volume_data, "int") * volume_blocksize
counter_name = "Volume Size Bytes"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], volume_size, counter_type))
oid_volume_used = "1.3.6.1.2.1.25.2.3.1.6." + instance["id"]
volume_used = get_instance_value(oid_volume_used, volume_data, "int") * volume_blocksize
volume_used = int(math.ceil(round((volume_used / float(volume_size) ) * 100, 1)))
counter_name = "% Used Space"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], volume_used, counter_type))
#Getting Disk Temperature Information
def get_disk_temperatures():
object_name = "Physical Disk"
counter_type = "Status"
disk_oid = "1.3.6.1.4.1.6574.2.1.1"
snmpwalk_flags = "n"
disk_data = get_snmp_data(host_address, disk_oid, snmpwalk_flags)
disk_instances = []
for disk in disk_data:
if re.search("Drive", disk) or re.search("Cache", disk) or re.search("Disk", disk):
disk_instances.append(get_snmp_instances(disk))
for instance in disk_instances:
oid_disk_temp = "1.3.6.1.4.1.6574.2.1.1.6." + instance["id"]
disk_temp = get_instance_value(oid_disk_temp, disk_data, "int")
counter_name = "Disk Temperature"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], disk_temp, counter_type))
#Getting Logical Disk Information, still physical but MIB's separate the actual names and references so its focusing on more logical stats
def get_disk_counters():
object_name = "Logical Disk"
counter_type = "Perf"
disk_oid = "1.3.6.1.4.1.6574.101.1.1"
snmpwalk_flags = "n"
disk_data = get_snmp_data(host_address, disk_oid, snmpwalk_flags)
disk_instances = []
for disk in disk_data:
if re.search("sd", disk) or re.search("nvm", disk):
disk_instances.append(get_snmp_instances(disk))
for instance in disk_instances:
oid_disk_load = "1.3.6.1.4.1.6574.101.1.1.8." + instance["id"]
disk_load = get_instance_value(oid_disk_load, disk_data, "int")
counter_name = "% Disk Load"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], disk_load, counter_type))
oid_disk_reads = "1.3.6.1.4.1.6574.101.1.1.12." + instance["id"]
current_reads = get_instance_value(oid_disk_reads, disk_data, "int")
oid_disk_writes = "1.3.6.1.4.1.6574.101.1.1.13." + instance["id"]
current_writes = get_instance_value(oid_disk_writes, disk_data, "int")
current_timestamp = int((datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(0)).total_seconds())
#Check if we have a previous capture or if the instance/NIC name is new to us
if not snmp_previous_data or not instance["name"] in snmp_previous_data:
disk_reads = 0
disk_writes = 0
else:
#Get time difference for caculation of the data rate
time_difference = int(current_timestamp) - int(snmp_previous_data["timecaptured"])
#Calculate the speed
disk_reads = abs(((current_reads - snmp_previous_data[instance["name"]]["reads"]) / time_difference ))
disk_writes = abs(((current_writes - snmp_previous_data[instance["name"]]["writes"]) / time_difference ))
counter_name = "Bytes Read/sec"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], disk_reads, counter_type))
counter_name = "Bytes Written/sec"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], disk_writes, counter_type))
#add raw data for calculation as needed.
counter_name = "Bytes Read"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], current_reads, counter_type))
counter_name = "Bytes Written"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, instance["name"], current_writes, counter_type))
#add the current counters to the list
snmp_current_data[instance["name"]] = {
"reads" : current_reads,
"writes" : current_writes
}
snmp_current_data["timecaptured"] = current_timestamp
#Getting UPS Information
def get_ups_counters():
global ups_name
#Naming the instance of the UPS
if ups_name == "":
ups_name = hostname + "_UPS"
object_name = "UPS"
counter_type = "Status"
ups_oid = "1.3.6.1.4.1.6574.4.3"
snmpwalk_flags = "n"
ups_data = get_snmp_data(host_address, ups_oid, snmpwalk_flags)
oid_ups_runtime = "1.3.6.1.4.1.6574.4.3.6.1.0"
ups_runtime = get_instance_value(oid_ups_runtime, ups_data, "int")
counter_name = "Battery Runtime Seconds"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, ups_name, ups_runtime, counter_type))
oid_ups_charge = "1.3.6.1.4.1.6574.4.3.1.1.0"
ups_charge = get_instance_value(oid_ups_charge, ups_data, "percent")
counter_name = "% Battery Charge"
snmp_data.append(build_counter_list(hostname, object_name, counter_name, ups_name, ups_charge, counter_type))
# Build the API signature
def build_signature(workspace_id, shared_key, date, content_length, method, content_type, resource):
x_headers = 'x-ms-date:' + date
string_to_hash = method + "\n" + str(content_length) + "\n" + content_type + "\n" + x_headers + "\n" + resource
bytes_to_hash = bytes(string_to_hash).encode('utf-8')
decoded_key = base64.b64decode(shared_key)
encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest())
authorization = "SharedKey {}:{}".format(workspace_id,encoded_hash)
return authorization
# Build and send a request to the POST API
def post_data(workspace_id, shared_key, body, log_type):
method = 'POST'
content_type = 'application/json'
resource = '/api/logs'
rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
content_length = len(body)
signature = build_signature(workspace_id, shared_key, rfc1123date, content_length, method, content_type, resource)
uri = 'https://' + workspace_id + '.ods.opinsights.azure.com' + resource + '?api-version=2016-04-01'
headers = {
'content-type': content_type,
'Authorization': signature,
'Log-Type': log_type,
'x-ms-date': rfc1123date
}
response = requests.post(uri,data=body, headers=headers)
if (response.status_code >= 200 and response.status_code <= 299):
print 'Accepted'
else:
print "Response code: {}".format(response.status_code)
#############################
######Gathering Metrics######
#############################
def __main__():
#Empty list of all the metrics to be captured
global snmp_data
snmp_data = []
#Getting Hostname (used for all metric host association)
global hostname
if hostname == "":
oid_hostname = "1.3.6.1.2.1.1.5"
snmpwalk_flags = "qvt"
counter_value = get_snmp_data(host_address, oid_hostname, snmpwalk_flags)
hostname = counter_value[0]
#Check if capturing anything that stores previous capture data and open
if capture_network == "true" or capture_disk == "true":
global snmp_previous_data
global snmp_current_data
snmp_current_data = {}
#check if net_octets file exists
data_file = "/tmp/syno_snmp.txt"
try:
with open(data_file) as json_file:
snmp_previous_data = json.load(json_file)
except:
print("File doesn't exist, starting fresh")
snmp_previous_data = None
if capture_system_temperature == "true":
get_system_temperature()
if capture_cpu == "true":
get_cpu_counters()
if capture_memory == "true":
get_memory_counters()
if capture_network == "true":
get_network_counters()
if capture_volume == "true":
get_volume_counters()
if capture_disk == "true":
get_disk_temperatures()
get_disk_counters()
if capture_ups == "true":
get_ups_counters()
#Write the data to the file if the disk or network capture was done.
if capture_network == "true" or capture_disk == "true":
with open(data_file, 'w') as data_file_handle:
json.dump(snmp_current_data, data_file_handle)
#Convert list to JSON
body = json.dumps(snmp_data)
#Post data to Log Analytics Workspace
post_data(workspace_id, shared_key, body, log_type)
print(body)
#Calculate sleep time based upon capture_interval. 60 / capture_interval
execute_count = int(round(60 / capture_interval))
print("Executing " + str(execute_count) + " time(s)")
for count in range(0, execute_count):
__main__()
print("Completed execution " + str(count + 1) + ". Sleeping for " + str(capture_interval) + " seconds")
time.sleep(capture_interval - 1)