#! /usr/bin/python3 # coding=utf-8 import subprocess import json import sys from pyzabbix import ZabbixMetric, ZabbixSender import os command = str(os.path.join(os.path.dirname(sys.argv[0]), 'rstcli64.exe --information')) hostname = 'srv-sbis01' server = '192.168.1.56' def exec(exe): result = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) try: ret = result.communicate()[0].decode('utf-8') except Exception: print(os.getcwd()) return False else: return ret def discovery(txt, tosend): if not txt: print(command) print('ERROR') exit(1) array = 0 volume = 0 data = {'data': []} controller = False device = 0 controller_index = 0 for line in txt.split("\n"): if '--CONTROLLER INFORMATION--' in line: controller = True if controller and 'Dynamic Storage Accelerator:' in line: controller = False elif '--ARRAY INFORMATION--' in line: array += 1 volume = 0 elif '--VOLUME INFORMATION--' in line: volume += 1 elif '--END DEVICE INFORMATION--' in line: device += 1 else: if controller and 'Name:' in line and tosend == 'controller': data['data'].append({"{#CONTROLLER}": "controller-{}".format(controller_index)}) if array and not volume and 'Name' in line and tosend == 'array': data['data'].append({'{#ARRAY}': 'ARRAY-' + str(array - 1)}) if volume and 'Name' in line and tosend == 'volume': data['data'].append({'{#VOLUME}': ('volume-' + str(array - 1) + '.' + str(volume - 1))}) if device and 'ID:' in line and tosend == 'device': data['data'].append({'{#DISK}': 'disk-' + str(device - 1)}) device += 1 return data def sender(txt): if not txt: print(1) exit(1) array = 0 arraysec = False volume = 0 volsec = False packet = [] controller = False device = 0 devicesec = False for line in txt.split("\n"): if '--CONTROLLER INFORMATION--' in line: controller = True if controller and 'Dynamic Storage Accelerator:' in line: controller = False elif '--ARRAY INFORMATION--' in line: array += 1 volume = 0 arraysec = True volsec = False elif '--VOLUME INFORMATION--' in line: volume += 1 arraysec = False volsec = True elif '--END DEVICE INFORMATION--' in line: devicesec = True arraysec = False volsec = False else: if controller: if 'Name:' in line: packet.append(ZabbixMetric(hostname, 'name[controller-1]', line.split(":")[1].strip())) elif 'Type:' in line: packet.append(ZabbixMetric(hostname, 'type[controller-1]', line.split(":")[1].strip())) if arraysec: if 'Name:' in line: packet.append(ZabbixMetric(hostname, 'name[{}]'.format('ARRAY-' + str(array - 1)), line.split(":")[1].strip())) elif 'Size:' in line: packet.append(ZabbixMetric(hostname, 'size[{}]'.format('ARRAY-' + str(array - 1)), line.split(":")[1].strip().split()[0])) elif 'Free:' in line: packet.append(ZabbixMetric(hostname, 'free[{}]'.format('ARRAY-' + str(array - 1)), line.split(":")[1].strip().split()[0])) elif 'Num Disks:' in line: packet.append(ZabbixMetric(hostname, 'numdisks[{}]'.format('ARRAY-' + str(array - 1)), line.split(":")[1].strip())) elif 'Num Vols:' in line: packet.append(ZabbixMetric(hostname, 'numvols[{}]'.format('ARRAY-' + str(array - 1)), line.split(":")[1].strip())) if volsec: if 'Name:' in line: packet.append(ZabbixMetric(hostname, 'name[{}]'.format('volume-' + str(array - 1) + '.' + str(volume - 1)), line.split(":")[1].strip())) elif 'Raid Level:' in line: packet.append(ZabbixMetric(hostname, 'raidlvl[{}]'.format('volume-' + str(array - 1) + '.' + str(volume - 1)), line.split(":")[1].strip() ) ) elif 'Size:' in line and not "StripeSize" in line: packet.append(ZabbixMetric(hostname, 'size[{}]'.format('volume-' + str(array - 1) + '.' + str(volume - 1)), line.split(":")[1].strip().split()[0] ) ) elif 'State:' in line: packet.append(ZabbixMetric(hostname, 'state[{}]'.format('volume-' + str(array - 1) + '.' + str(volume - 1)), line.split(":")[1].strip() ) ) if devicesec: if 'ID:' in line: device += 1 packet.append(ZabbixMetric(hostname, 'id[{}]'.format('disk-' + str(device - 1)), line.split(":")[1].strip())) elif 'Disk Type:' in line: packet.append(ZabbixMetric(hostname, 'disktype[{}]'.format('disk-' + str(device - 1)), line.split(":")[1].strip())) elif 'State:' in line: packet.append(ZabbixMetric(hostname, 'state[{}]'.format('disk-' + str(device - 1)), line.split(":")[1].strip())) elif 'Size:' in line: packet.append(ZabbixMetric(hostname, 'size[{}]'.format('disk-' + str(device - 1)), line.split(":")[1].strip().split()[0])) elif 'Serial Number:' in line: packet.append(ZabbixMetric(hostname, 'serial[{}]'.format('disk-' + str(device - 1)), line.split(":")[1].strip())) elif 'Model:' in line: packet.append(ZabbixMetric(hostname, 'model[{}]'.format('disk-' + str(device - 1)), line.split(":")[1].strip())) zbx = ZabbixSender(server) try: zbx.send(packet) except Exception: print(1) else: print(0) def args(): if len(sys.argv) > 2: print('too many args') return False elif len(sys.argv) == 1: print('Too few args') elif len(sys.argv) == 2: if sys.argv[1] == 'send': return 'sender' elif sys.argv[1] == 'volume': return 'volume' elif sys.argv[1] == 'controller': return 'controller' elif sys.argv[1] == 'array': return 'array' elif sys.argv[1] == 'device': return 'device' else: print('wrong args') else: print('wrong args') return False def main(): if not args(): exit(0) elif args() != "sender": print(json.dumps(discovery(exec(command), args()), indent=4, sort_keys=False)) else: sender(exec(command)) if __name__ == '__main__': main()