#!/usr/bin/python
import sys
import socket
from raid import raid
import re
from subprocess import PIPE, Popen
hostname = socket.gethostname()

f = open("/opt/zabbix/tmp/raid.tmp", 'w')

f.write("%s %s %s\n" % (hostname, 'raid["AbortedCommands"]', raid.get_total_aborted_commands()))
f.write("%s %s %s\n" % (hostname, 'raid["Battery"]', raid.is_battery_good()))
f.write("%s %s %s\n" % (hostname, 'raid["FailedStripes"]',  raid.has_failed_stripes()))
f.write("%s %s %s\n" % (hostname, 'raid["FirmwareState"]',  raid.is_optimal()))
f.write("%s %s %s\n" % (hostname, 'raid["MediaErrors"]', raid.get_total_media_errors()))
f.write("%s %s %s\n" % (hostname, 'raid["OfflineSectors"]', raid.get_total_smart("offilne_uncorrectable")))
f.write("%s %s %s\n" % (hostname, 'raid["ReallocatedSectors"]', raid.get_total_smart("reallocated_sector_ct")))
f.write("%s %s %s\n" % (hostname, 'raid["SmartErrors"]', raid.get_total_smart("smart_errors")))
f.write("%s %s %s\n" % (hostname, 'raid["UDMACRC"]', raid.get_total_smart("udma_crc_errors")))
f.write("%s %s %s\n" % (hostname, 'raid["UncorrectedReads"]', raid.get_total_smart("uncorrected_read")))
f.write("%s %s %s\n" % (hostname, 'raid["UncorrectedWrites"]', raid.get_total_smart("uncorrected_write")))
f.close()

zabbix_regex = re.compile('ServerActive\s*=\s*([a-zA-Z0-9\.-]+)')

f = open("/opt/zabbix/etc/zabbix_agentd.conf","r")
for line in f.readlines():
    line_match = re.match(zabbix_regex, line)
    if line_match:
        zabbix_server =  line_match.group(1)


Popen(["/opt/zabbix/bin/zabbix_sender", "-z", zabbix_server, "-i", "/opt/zabbix/tmp/raid.tmp" ]).communicate()
