from subprocess import Popen, PIPE, check_output import re import json import sys import hashlib path = '\"C:\\Program Files\\smartmontools\\bin\\smartctl\"' # for linux use 'sudo smartctl' smart_params = ['Model_Family', 'Device_Model', 'Serial_Number', 'test_result', 'Firmware_Version'] # if u need more \ # add ur data to this list codec = 'windows-1252' # for linux use utf8 def params(disk_name, raw_data=""): # Pars output from smartctl if raw_data not in smart_params and raw_data != "": # Pars smartctl data from sensors (smartctl -A /dev/sd*) out_data = re.findall(r'{}.*- *(\d+)'.format(raw_data), Popen('{} -x /dev/{}'.format(path, disk_name), shell=True, stdout=PIPE, ).communicate()[0].decode(codec)) return out_data[0] elif raw_data != "" and raw_data in smart_params: # Pars smartctl information about disks (smartctl -i /dev/sd*) out_data = re.findall(r'{}. *(.*)'.format(raw_data.replace('_', ' ')), Popen('{} -x /dev/{}'.format(path, disk_name), shell=True, stdout=PIPE, ).communicate()[0].decode(codec)) return out_data[0] elif raw_data == "": # check sum of smartctl --scan hash_object = hashlib.sha224(check_output(path + " --scan")) return hash_object.hexdigest() try: # if no argumens from cli works as discovery try: if sys.argv[1] and sys.argv[2]: print(params(sys.argv[1], sys.argv[2])) except IndexError: print(params(sys.argv[1])) except IndexError: # Discovery for disks data = check_output(path + " --scan").decode(codec) disks = set(re.findall(r'/dev/(.{3})', data)) output = [] for disk in disks: smart = check_output(path + " -i /dev/{}".format(disk)).decode(codec) if smart: output.append({"{#DISKNAME}": disk, "{#SMART_ENABLED}": "1"}) else: output.append({"{#DISKNAME}": disk, "{#SMART_ENABLED}": "0"}) output = {"data": output} print(json.dumps(output))
UserParameter=uHDD.discovery,C:\\Users\\%username%\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:\\zabbix\\lld\\hdd_discovery.py UserParameter=uHDD[*],C:\\Users\\%username%\\AppData\\Local\\Programs\\Python\\Python36-32\\python.exe C:\\zabbix\\lld\\hdd_discovery.py $1 $2
Source: https://habr.com/ru/post/345502/
All Articles