|
|
- #! /usr/bin/python3
- # coding=utf-8
-
- import subprocess
- import json
- import sys
- from pyzabbix import ZabbixMetric, ZabbixSender
-
- command = 'cat irst.txt'
- hostname = 'testhost'
- server = '192.168.1.56'
-
- def exec(exe):
- result = subprocess.Popen(exe, stdout=subprocess.PIPE, shell=True)
- return result.communicate()[0].decode('utf-8')
-
-
- def discovery(txt):
- array = 0
- volume = 0
- data = {'data': []}
- controller = False
- device = 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:
- data['data'].append({'#CONTROLLER': "controller-1"})
- if array and not volume and 'Name' in line:
- data['data'].append({'#ARRAY': 'ARRAY-' + str(array - 1)})
- if volume and 'Name' in line:
- data['data'].append({'#VOLUME': ('volume-' + str(array - 1) + '.' + str(volume - 1))})
- if device and 'ID:' in line:
- data['data'].append({'#DISK': 'disk-' + str(device - 1)})
- device += 1
- return data
-
-
- def sender(txt):
- 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:
- device += 1
- 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:
- 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()))
-
- for val in packet:
- print(val)
- zbx = ZabbixSender(server)
- zbx.send(packet)
-
- def args():
- if len(sys.argv) > 2:
- print('too many args')
- return False
- elif len(sys.argv) == 1:
- return 'discovery'
- elif len(sys.argv) == 2 and sys.argv[1] == 'send':
- return 'sender'
- else:
- print('wrong args')
- return False
-
-
- def main():
- if not args():
- exit(0)
- elif args() == "discovery":
- print(json.dumps(discovery(exec(command)), indent=4, sort_keys=False))
- else:
- sender(exec(command))
-
-
- if __name__ == '__main__':
- main()
|