#!/usr/bin/python
import glob
import pprint
import smart
from subprocess import Popen, PIPE
import re
import os.path
import socket


proxy_line_regex = ('^\s*Server\s*=\s*(.*),?')

dest_box = "zabbix3.hou1.endurance.com"
f = open("/etc/zabbix/zabbix_agentd.conf","r")

for l in f.readlines():
    match=re.search(proxy_line_regex, l)
    if match:
        dest_box=match.group(1)

def get_smart_info():
    sg_devices = glob.glob('/dev/sg*')

    disks = []
    for i in range(len(sg_devices)):
        disks.append({'smart_info': {}, 'device': sg_devices[i]})

    for i in disks:
        smart.get_smart(i, i['device'], 0)
        if 'hours' not in i['smart_info']:
            smart.get_smart(i, i['device'], 1)
    return disks

def get_raid_output():
    arc_process = Popen(["/usr/local/eig_zabbix/sbin/arcconf", "getconfig", "1", "al"], stdout=PIPE)
    output,error = arc_process.communicate()
    if not error:
        return output

def output_has_failed_stripes(output):
    if "Failed stripes                           : Yes" in output:
        return 1
    else:
        return 0
def output_has_degraded_or_failed(output):
    if "Status of logical device                 : Degraded" in output or "Status of logical device                 : Failed" in output:
        return 0
    else:
        return 1

def send_value(key, value):
    sender_process = Popen(["/usr/bin/zabbix_sender", "-s", socket.gethostname(), "-z", dest_box, "-k", key, "-o", str(value)], stdout=PIPE)
    output,error = sender_process.communicate()
    if not error:
        return output

total_smart = 0
total_unc_read = 0
total_unc_write = 0
total_offline = 0
total_reallocated = 0
total_crc = 0
for i in get_smart_info():
    if i['smart_info']:
        if 'smart_errors' in i['smart_info']:
            total_smart = total_smart + int(i['smart_info']['smart_errors'])
        if 'uncorrected_read' in i['smart_info']:
            total_unc_read = total_unc_read + int(i['smart_info']['uncorrected_read'])
        if 'uncorrected_write' in i['smart_info']:
            total_unc_write = total_unc_write + int(i['smart_info']['uncorrected_write'])
        if 'offline_uncorrectable' in i['smart_info']:
            total_offline = total_offline + int(i['smart_info']['offline_uncorrectable'])
        if 'reallocated_sector_ct' in i['smart_info']:
            total_reallocated = total_reallocated + int(i['smart_info']['reallocated_sector_ct'])
        if 'udma_crc_errors' in i['smart_info']:
            total_crc = total_crc + int(i['smart_info']['udma_crc_errors'])
send_value('adaptec["FirmwareState"]', str(output_has_degraded_or_failed(get_raid_output())))
send_value('adaptec["FailedStripes"]', str(output_has_failed_stripes(get_raid_output())))
send_value('adaptec["SmartErrors"]', total_smart)
send_value('adaptec["UncorrectedReads"]', total_unc_read)
send_value('adaptec["UncorrectedWrites"]', total_unc_write)
send_value('adaptec["OfflineSectors"]', total_offline)
send_value('adaptec["ReallocatedSectors"]', total_reallocated)
send_value('adaptec["UDMACRC"]', total_crc)
