#!/usr/bin/python
import sys
import socket
from raid import raid_datas
import re
import os
from subprocess import PIPE, Popen
hostname = socket.gethostname()
tmp_path = "/var/lib/zabbix/tmp"
tmp_filename = "raid.tmp"
key_prime = "raid"

if not os.path.exists(tmp_path):
    os.makedirs(tmp_path)

f = open(tmp_path + "/" + tmp_filename, 'w')
if raid_datas:
    for raid_data in sorted(raid_datas):
        f.write("%s %s[\"%s\"] %s\n" % (hostname, key_prime, raid_data, raid_datas[raid_data]))
f.close()

Popen(["/var/lib/zabbix/sbin/raidcheck/raid/ssdlife"]).communicate()

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

if os.path.exists('/etc/zabbix/zabbix_agentd.conf'):
    f = open("/etc/zabbix/zabbix_agentd.conf","r")
elif os.path.exists('/opt/zabbix/etc/zabbix_agentd.conf'):
    f = open("/opt/zabbix/etc/zabbix_agentd.conf","r")

zabbix_server="zabbix3.hou1.endurance.com"
for line in f.readlines():
    line_match = re.match(zabbix_regex, line)
    if line_match:
        zabbix_server = line_match.group(1)

if os.path.exists('/usr/bin/zabbix_sender'):
    zabbix_sender = '/usr/bin/zabbix_sender'
elif os.path.exists('/opt/zabbix/bin/zabbix_sender'):
    zabbix_sender = '/opt/zabbix/bin/zabbix_sender'

Popen([zabbix_sender, "-z", zabbix_server, "-i", tmp_path + "/" + tmp_filename ]).communicate()
