📜 ⬆️ ⬇️

Monitoring SMART HDD - windows + python + zabbix

Download and install the following programs.

Python 3.6.4
SMART montools

Do not forget to change% username% in the username in the system. The full path can be obtained when installing Python 3.6.4
')
Do not forget that you need to run the installation on behalf of the system administrator.

The code takes two parameters from the command line — the name of the disk and the parameter to be output. In the case of launching without parameters, the disks are detected and the output is a ready-made format that the zabbix-server accepts.

We spread the code in any directory accessible to you. The main point is the same path in the UserParameter

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)) 

On the observed host in zabbix-agentd.conf or, if you have the Include section open then in the file defined in it, add the UserParameters listed below

 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 

Adding a template to the server "Zabbix"

I posted it on git - I don’t think that all the turnips are very positive - I am learning :)

Download the template - import it to the server and add it to the monitored host.
You can add your own data items. The logic is as follows - the data element sends parameters to the script, which in turn parsits the output of smartctl, if you want to add something of your own, there are no problems - there are comments in the code that will help you to do this.

PS Script for the lazy. With Linux, the same monitoring of the same disks has been working for a year now, Windows just got around to it.

Source: https://habr.com/ru/post/345502/


All Articles