#!/usr/bin/python

# Using CompCU.jar

import re
import sys
import subprocess
import commands
import time
from optparse import OptionParser

Host='c-host-1'
#DRHost='dr-host-1'
SCUser='nags'
#password='' uncomment and add pass in if running script manually


def report_error(message,data="",fatal=True):
    print message
    print data
    if fatal:
        sys.exit(1)

def run_CompCU (command):
    output=commands.getoutput('java -jar CompCU.jar -host %s -user %s -password %s -c "%s"' % (Host,SCUser,password,command))
    
    # output is very strict to start with:
    #Compellent Command Utility (CompCU) 7.1.1.1
    #
    #=================================================================================================
    #User Name:              Admin
    #Host/IP Address:        ch-sc9000-1
    #Single Command:         replay checkviews
    #=================================================================================================
    #Connecting to Storage Center: ch-sc9000-1 with user: Admin
    #Running Command: replay checkviews

    lines=output.split("\n")
    (CUHeader,blank,CUBreak1,CUUser,CUHost,CUIntent,CUBreak2,CUConnect,CURunning)=lines[:9]
    CUOutput=lines[9:]
    if not "Compellent Command Utility" in CUHeader:
        report_error("Command utility run failure",CUHeader,fatal=False)
        return False
    if not "Connecting to Storage Center" in CUConnect:
        report_error("Failed to connect",CUConnect,fatal=False)
        return False
    if not "Running Command" in CURunning:
        report_error("Command Utility failure",output,fatal=False)
        return False
    if not "Successfully finished running Compellent Command Utility (CompCU) application." in CUOutput:
        report_error("Command error",CUOutput,fatal=False)
        return False

    return CUOutput[:-1]

# "alert show " |grep -q Critical\\\|Degr

CRITICAL=2
WARN=1
OKAY=0

parser=OptionParser()
parser.add_option("-J",dest="host",help="Dell StorageCenter",metavar="HOST")
parser.add_option("-P",dest="password",help="Dell password",metavar="PASSWD")
parser.add_option("-t",dest="type",help="Check type",metavar="TYPE")
parser.add_option("-w",dest="warn",help="Warning level",metavar="WARN")
parser.add_option("-c",dest="crit",help="Critical level",metavar="CRIT")

(options,args)=parser.parse_args()

nagios_return=OKAY
nagios_message=""

#if not options.host:
   # print "Required - hostname"
    #exit(3)
#if not options.password:
   # print "Password needed"
    #exit(3)

alerts = run_CompCU("alert show")
header = alerts[1]
format = alerts[2]

fields=format.split()
field_size=[]
for field in fields:
    field_size+=[len(field)]

for alert in alerts[3:]:
    fields=[]
    pos=0
    for i in field_size:
        field=alert[pos:(pos+i)]
        fields+=[field]
        pos+=i+1
    (controller,index,type,message,status,date_created,object,category,count,acked,date_cleared,ref)=fields

    if "Alert" in type:
        if "Critical" in status and 'false' in acked:
            nagios_message+=message+"\n"
            nagios_return=CRITICAL
        if "Degraded" in status and 'false' in acked:
            nagios_message+=message+"\n"
            nagios_return=CRITICAL


print nagios_message
sys.exit(nagios_return)
