#!/usr/bin/python ## -- EPICS PV caget -- ## Execute caget command to check EPICS PV Status which will be reported into Check_Mk web interface ## Developed by Maurizio Montis - INFN Legnaro - Italy ## Mail: maurizio.montis@lnl.infn.it import sys, datetime, os, string, time, subprocess # Variables: record_list = [] # Create Record list from file: # - conf_file_name = file to open for reading the record list def create_dictionary (conf_file_name): conf_file = open(conf_file_name, 'r') dictionary = conf_file.readlines() for elem in dictionary: ind_elem = dictionary.index(elem) nchar=1 while ( ( elem[0] != '#' ) and ( elem[nchar-1] != '#' ) and ( elem[nchar-1] != ' ' ) and ( elem[nchar-1] != '\r' ) and ( elem[nchar-1] != '\n' ) and ( elem[nchar-1] != '\t' ) and ( nchar < len(elem) ) ): dictionary[ind_elem] = elem[:nchar] nchar+=1 counter_ind = 0 while ( counter_ind < len(dictionary) ): if (( dictionary[counter_ind][0] == '\r' ) or ( dictionary[counter_ind][0] == '\n' ) or ( dictionary[counter_ind][0] == '#' )): del dictionary[counter_ind] else: counter_ind +=1 conf_file.close() return dictionary # Create Check_Mk output line : # - severity = alarm severity associated to EPICS PV # - elem = element with status indicated # - message = message description def status_output ( severity, elem, message): output_line = '' if ( severity == '' ): status = 0 status_advise = 'OK' output_line = '%i %s - %s: %s' % (status, elem, status_advise, message) elif ( severity == 'MINOR' ): status = 1 status_advise = 'WARNING' output_line = '%i %s - %s: %s' % (status, elem, status_advise, message) elif ( severity == 'MAJOR' ): status = 2 status_advise = 'CRITICAL' output_line = '%i %s - %s: %s' % (status, elem, status_advise, message) elif ( severity == 'DISCONNECTED' ): status = 3 status_advise = 'DISCONNECTED' output_line = '%i %s - %s: %s' % (status, elem, status_advise, message) else: status = 3 status_advise = 'UNKNOW' output_line = '%i %s - %s: %s' % (status, elem, status_advise, message) return output_line # Check PV disconnection: # - epics_pv = EPICS Record to verify connection def check_disconnection (epics_pv): #if ( os.popen("caget -a " + epics_pv).read() == '' ): if (subprocess.Popen(['caget', '-a', elem], stdout=subprocess.PIPE, stderr=subprocess.PIPE ).communicate()[0] == '' ): return 1 else: return 0 ### Main ### # create record list looking for any txt file in current directory: for elem in os.listdir("."): if elem.endswith('.txt'): record_list = record_list + create_dictionary(elem) for elem in record_list: # record disconnected: if (check_disconnection(elem)): print status_output( 'DISCONNECTED', 'EPICS_caget_'+elem, 'Record ' + elem + ' not connected!') # record connected: else: shell_command = subprocess.Popen(['caget', '-a', elem], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) #os.popen("caget -a " + elem).read() record_status, record_status_err = shell_command.communicate() if ( 'MAJOR' in record_status ): print status_output( 'MAJOR', 'EPICS_caget_'+elem, 'Record ' + elem + ' CRITICAL Alarm Severity!') elif ( 'MINOR' in record_status ): print status_output( 'MINOR', 'EPICS_caget_'+elem, 'Record ' + elem + ' WARNING Alarm Severity!') elif ( 'INVALID' in record_status ): print status_output( 'INVALID', 'EPICS_caget_'+elem, 'Record ' + elem + ' INVALID Alarm Severity!') else: print status_output( '', 'EPICS_caget_'+elem, 'Record ' + elem + ' NO Alarm Severity')