def __init__(self, name, receiver, sender, options=[], defaults={}): self.name = name self.receiver = receiver self.sender = sender self.defaults = defaults or self.defaults self.tangodb = ft.get_database() self.alarms = panic.api()
def get_panic_devices(api=None, devfilter='*', exported=False): api = api or panic.api() devs = [d.lower() for d in api.devices if clmatch(devfilter, d)] if exported: alldevs = fd.get_all_devices(exported=True) devs = [d for d in devs if d in alldevs] return devs
def generate_html_report(args): print('panic.extra.generate_html_report(%s)' % args) assert web, 'fandango.web not available' OUTPUT = args[-1] if len(args) else '/srv/www/htdocs/reports/alarms.html' FILTER = args[1] if len(args) > 1 else '*' HOST = os.getenv('TANGO_HOST', os.getenv('HOST')) print 'OUTPUT = %s, FILTER = %s, HOST= %s' % (OUTPUT, FILTER, HOST) api = panic.api(FILTER) lines = [] severities = ['ALARM', 'WARNING', 'ERROR', 'INFO', 'DEBUG'] txt = '<html>\n' + web.title('Active alarms in %s' % HOST, 2) summary = dict((k, 0) for k in severities) values = {} for d in sorted(api.devices): try: dev = panic.AlarmDS(d, api).get() active = dev.read_attribute('ActiveAlarms').value values[HOST + '/' + d] = active if active: lines.append(web.title(d.upper(), 3)) lines.append(web.list_to_ulist([a for a in active])) for a in api.get(device=d): if any(s.startswith(a.tag + ':') for s in active): summary[a.severity or 'WARNING'] += 1 except Exception, e: lines.append(web.title(d.upper(), 3)) se = traceback.format_exc() if 'desc =' in se: se = '\n'.join([l for l in se.split('\n') if 'desc =' in l][:1]) lines.append('<pre>%s</pre>' % se)
def get_devices_eval_times(api=None, devfilter='*', timeout=10000.): api = api or panic.api() devs = get_panic_devices(api, devfilter, exported=True) times = {} print('%d devices' % len(devs)) for d in devs: try: times[d] = get_device_eval_times(d, timeout, True) except: times[d] = END_OF_TIME return sorted([v, k] for k, v in times.items())
def get_devices_eval_ratio(api=None,devfilter='*',timeout=10000.): api = api or panic.api() devs = get_panic_devices(api,devfilter,exported=True) times = {} print('%d devices'%len(devs)) for d in devs: try: times[d] = get_device_eval_times(d,timeout,True) except: times[d] = END_OF_TIME return sorted([v/float(api.devices[k].config['PollingPeriod']),v, k,len(api.devices[k].alarms)] for k,v in times.items())
def get_alarms_variables(api=None, alarmfilter='*'): api = api or panic.api() attrs = {} for d, v in api.devices.items(): for a, vv in v.alarms: if clmatch(alarmfilter, a): try: attrs[a] = len(get_alarm_variables(a, api)) except: attrs[a] = -1 ss = sum([attrs[a] for a in v.alarms if attrs[a] > 0]) print('%s : %s attributes in %s alarms' % (d, ss, len(v.alarms))) return attrs
def get_alarms_eval_times(api=None, alarmfilter='*', timeout=10000.): api = api or panic.api() devs = get_panic_devices(api, exported=True) times = {} for d in devs: try: et = get_device_eval_times(d, timeout, False) except: et = dict((a, END_OF_TIME) for a in api.devices[d].alarms) for a, t in et.items(): times[d + '/' + a] = t return sorted([v, k] for k, v in times.items())
def check_alarms_vs_archiving(alarms=[]): import PyTangoArchiving api = panic.api() alarms = alarms or [a for a in api if fn.check_device(api[a].device)] rd = PyTangoArchiving.Reader() archs = rd.get_attributes() models = dict((a,api[a].get_attribute(full=1).lower()) for a in alarms) alarms = [a for a in alarms if names[a] in archs] olds = {} for a in sorted(alarms): af = names[a] olds[a] = rd.get_attribute_values(af,-7200) for a in sorted(alarms): af = names[a] v = fn.tango.read_attribute(af) if 1 or olds[a] and olds[a][0][1]!=v: print(a,af,olds[a][0][1],v) return
def get_all_timeouts(api=None, devfilter='*', timeout=3000.): api = api or panic.api() devs = [d.lower() for d in api.devices if clmatch(devfilter, d)] print('testing %d devices on %s' % (len(devs), api.tango_host)) times, failed, down = {}, [], [] for d in devs: t = get_device_timeout(d, timeout, tries=3) print(d, t) times[d] = t if t < 0: down.append(d) if t == END_OF_TIME: failed.append(d) print('') print('%d devices were not exported: %s' % (len(down), ','.join(down))) print('%d devices failed: %s' % (len(failed), ','.join(failed))) return sorted([v, k, len(api.devices[k].alarms)] for k, v in times.items())
def get_panic_status(*args): if args and isinstance(args[0], panic.AlarmAPI): api = args[0] else: api = panic.api(*args) txt = ['Panic(%s) Status:' % str(api.filters)] txt.append('\t%d alarms in db' % len(api)) txt.append('\t%d devices in db' % len(api.devices)) txt.append('') states = defaultdict(list) [states[check_device(d)].append(d) for d in api.devices] for s, v in sorted(states.items()): txt.append('%d devices in %s state' % (len(v), s)) ds = ['%s (%d)' % (d, len(api.devices[d].alarms)) for d in sorted(v)] txt.append('\t%s' % ', '.join(ds)) return '\n'.join(txt)
def extract(target): """ Extract devices used in alarms """ import panic api = panic.api() target = fn.toList(target) alarms = [] devs = [] for t in target: if t in api: alarms.append(t) if t in api.devices: alarms.extend(api.devices[t].alarms.keys()) for a in alarms: devs.append(api[a].device) attrs = api.parse_attributes(a) devs.extend(d.rsplit('/', 1)[0] for d in attrs) return sorted(set(map(str.lower, devs)))
def main(): global tangodb, alarms, receiver, sender, smtp_host args = sys.argv[1:] if not args or 'help' in str(args): print('Usage: test_mail.py receiver sender smtp_host:port') sys.exit(0) receiver, sender, smtp_host = args[:3] if ':' not in smtp_host: smtp_host += ':25' tangodb = ft.get_database() alarms = panic.api() cleanup() configure() try: run() except: traceback.print_exc() finally: cleanup(delete = False) fn.log.info('MAIL_TEST: DONE')
def get_panic_report(api=None, timeout=3000., tries=3, devfilter='*', attr='ActiveAlarms', trace=False): """ The key of the results: key.count('/') == 0 : alarm == 1: server == 2: device == 4: summary """ if api is None: api = panic.api() elif isString(api): api = panic.api(api) if not len(api.servers): if devfilter == '*': api.servers.load_by_name('PyAlarm/*') if isString(devfilter): api.servers.load_by_name(devfilter) else: [api.servers.load_by_name(d) for d in devfilter] alldevs = fd.tango.get_all_devices(exported=True) result = fd.dicts.defaultdict(dict) result['//alarms//'] = api.alarms.keys() result['//devices//'] = api.devices.keys() result['//servers//'] = api.servers.keys() off = [] hung = [] slow = [] aslow = [] errors = {} for s, ss in api.servers.items(): admin = fd.parse_tango_model(ss.get_admin_name())['device'] if admin.lower() not in alldevs: off.append(admin) result[s]['devices'] = ss.get_classes()['PyAlarm'] for d in result[s]['devices']: if isSequence(devfilter): if d not in devfilter: continue elif not clsearch(devfilter, d): continue if admin in off: t = END_OF_TIME off.append(d) else: proxy = fd.get_device(d) t = get_device_timeout(d, timeout, tries, alldevs, proxy, trace=False, attr=attr) if t == END_OF_TIME: hung.append(d) if t * 1e3 > timeout: slow.append(d) result[d]['timeout'] = t result[d]['attrs'] = 0 result[d]['alarms'] = len(api.devices[d].alarms) polling = float(api.devices[d].config['PollingPeriod']) result[d]['polling'] = polling try: evals = get_device_eval_times(proxy, timeout) teval = sum(evals.values()) ratio = teval / float(polling) result[d]['eval'], result[d]['ratio'] = teval, ratio except: evals = {} result[d]['eval'], result[d]['ratio'] = -1, -1 for a, aa in api.devices[d].alarms.items(): attrs = api.parse_attributes(aa['formula']) result[d]['attrs'] += len(attrs) result[a]['device'] = d result[a]['attrs'] = attrs result[a]['timeout'] = evals.get(a, -1) if result[a]['timeout'] > polling / result[d]['alarms']: aslow.append(a) result['//off//'] = off result['//bloat//'] = [ k for k in result if k.count('/') == 2 and result[k].get('ratio', -1) >= 1. ] result['//hung//'] = hung result['//slow_devices//'] = slow result['//slow_alarms//'] = aslow #print('off devices: %s\n'%(off)) #print('hung devices: %s\n'%(hung)) #print('slow devices: %s\n'%(slow)) #print('bloat devices: %s\n'%([k for k in result #if '/' in k and result[k].get('ratio',-1)>=1.])) #print('slow alarms: %s\n'%aslow) return result
def load(self, **kwargs): self.filters = kwargs.get('filters') self.api = panic.api(self.filters)
def main_test(): print msg try: msg=""" #Create the test device #Launch it; take snapshot of memory usage NOTE: This testing is not capable of testing email/SMS sending. This will have to be human-tested defining a test receiver: Basic steps: * Create a simulator with attributes suitable for testing. * Start the simulators * Ensure that all alarms conditions are not enabled reseting attribute values. <pre><code class="python">""" if check_step(0): tango.add_new_device('PySignalSimulator/test-alarms','PySignalSimulator','test/test/alarms-test') tango.put_device_property('test/test/alarms-test',{ 'DynamicAttributes':map(str.strip, """#ALARM TESTING A=READ and VAR('VALUE1') or WRITE and VAR('VALUE1',VALUE) B=DevDouble(READ and VAR('B') or WRITE and VAR('B',VALUE)) S=DevDouble(READ and VAR('B')*sin(t%3.14) or WRITE and VAR('B',VALUE)) D=DevLong(READ and PROPERTY('DATA',True) or WRITE and WPROPERTY('DATA',VALUE)) C = DevLong(READ and VAR('C') or WRITE and VAR('C',VALUE)) T=t""".split('\n')), 'DynamicStates': 'STATE=C #INT to STATE conversion' }) fandango.Astor().start_servers('PySignalSimulator/test-alarms') time.sleep(10.) simulator = fandango.get_device('test/test/alarms-test') [simulator.write_attribute(a,0) for a in 'ABSDC'] msg="""</pre> * Create 2 PyAlarm instances, to check attribute-based and alarm/group based alarms * Setup the time variables that will manage the alarm cycle <pre><code class="python">""" alarms = panic.api() if check_step(1): tango.add_new_device('PyAlarm/test-alarms','PyAlarm','test/alarms/alarms-test') tango.add_new_device('PyAlarm/test-group','PyAlarm','test/alarms/alarms-group') threshold = 3 polling = 5 autoreset = 60 alarmdevs = ['test/alarms/alarms-test','test/alarms/alarms-group'] props = { 'Enabled':'15', 'AlarmThreshold':threshold, 'AlertOnRecovery':'email', 'PollingPeriod':polling, 'Reminder':0, 'AutoReset':autoreset, 'RethrowState':True, 'RethrowAttribute':False, 'IgnoreExceptions':True, 'UseSnap':True, 'CreateNewContexts':True, 'MaxMessagesPerAlarm':20, 'FromAddress':'*****@*****.**', 'LogLevel':'DEBUG', 'SMSConfig':':', 'StartupDelay':0, 'EvalTimeout':500, 'UseProcess':False, 'UseTaurus':False, } [tango.put_device_property(d,props) for d in alarmdevs] N,msg=2,gb+"* Start the PyAlarm devices"+ge if check_step(N): receiver = "[email protected],SMS:+3400000000" fandango.Astor().start_servers('PyAlarm/test-alarms') fandango.Astor().start_servers('PyAlarm/test-group') time.sleep(15.) N,msg=3,gb+"* create simple and group Alarms to inspect."+ge if check_step(N): alarms.add(tag='TEST_A',formula='test/test/alarms-test/A',device='test/alarms/alarms-test', receivers=receiver,overwrite=True) alarms.add(tag='TEST_DELTA',device='test/alarms/alarms-test',receivers=receiver,overwrite=True, formula = 'not -5<test/sim/test-00/S.delta<5 and ( test/sim/test-00/S, test/sim/test-00/S.delta )') alarms.add(tag='TEST_STATE',formula='test/test/alarms-test/State not in (OFF,UNKNOWN,FAULT,ALARM)', device='test/alarms/alarms-test',receivers=receiver,overwrite=True) alarms.add(tag='TEST_GROUP1',formula='any([d>0 for d in FIND(test/alarms/*/TEST_[ABC].delta)]) and FIND(test/alarms/*/TEST_[ABC])', device='test/alarms/alarms-group',receivers=receiver,overwrite=True) alarms.add(tag='TEST_GROUP2',formula='GROUP(TEST_[ABC])', device='test/alarms/alarms-group',receivers=receiver,overwrite=True) N,msg=4,gb+""" Test steps: * Enable an alarm condition in the simulated A attribute. * Alarm should be enabled after 1+AlarmThreshold*PollingPeriod time (and not before) * Group alarm should be enabled after 1+AlarmThreshold*PollingPeriod """+ge if check_step(N): pass N,msg=5,gb+""" * Disable alarm condition * Alarm should enter in recovery state after AlarmThreshold*PollingPeriod. * Alarm should AutoReset after AutoReset period. * Group should reset after AutoReset period. ###############################################################################"""+ge if check_step(N): pass N,msg=6,gb+""" * Testing properties ** RethrowAttribute ... None/NaN ** RethrowState ... True/False ** Enabled ... time or formula or both ###############################################################################"""+ge if check_step(N): pass msg=ge except: print traceback.format_exc() N,msg = -1,"""Stopping all servers ...""" check_step(N) fandango.Astor().stop_servers('PySignalSimulator/test-alarms') fandango.Astor().stop_servers('PyAlarm/test-alarms') fandango.Astor().stop_servers('PyAlarm/test-group')
def test_AlarmAPI(): global api api = panic.api() assert api
def get_alarm_variables(alarm, api=None): api = api or panic.api() return api.parse_attributes(api[alarm].formula)
def get_alarm_data(alarm): api = panic.api() return api[alarm].to_dict()
def get_alarm_eval_time(alarm): api = panic.api() et = get_device_eval_times(api[alarm].device) return et[alarm]
#!/usr/bin/python import sys,os,urllib,traceback try: import panic,fandango from fandango import web except: sys.path.append('/homelocal/sicilia/lib/python/site-packages') import panic,fandango from fandango import web print sys.argv OUTPUT = sys.argv[-1] if len(sys.argv)>1 else '/srv/www/htdocs/reports/alarms.html' FILTER = sys.argv[1] if len(sys.argv)>2 else '*' HOST = os.getenv('TANGO_HOST',os.getenv('HOST')) print 'OUTPUT = %s, FILTER = %s, HOST= %s'% (OUTPUT,FILTER,HOST) api = panic.api(FILTER) lines = [] severities = ['ALARM','WARNING','ERROR','INFO','DEBUG'] txt = '<html>\n'+web.title('Active alarms in %s'%HOST,2) summary = dict((k,0) for k in severities) values = {} for d in sorted(api.devices): try: dev = panic.AlarmDS(d,api).get() active = dev.read_attribute('ActiveAlarms').value values[HOST+'/'+d]=active if active: lines.append(web.title(d.upper(),3)) lines.append(web.list_to_ulist([a for a in active])) for a in api.get(device=d): if any(s.startswith(a.tag+':') for s in active):
def get_device_alarms(device, timeout=10000.): api = panic.api() return sorted( (k, a['formula']) for k, a in api.devices[device].alarms.items())
#!/usr/bin/env python # -*- coding: utf-8 -*- try: import pandas as pd import traceback import logging import panic except Exception as e: logging.error('Missing dependencies', traceback.format_exc()) alarms = panic.api() try: df = pd.read_excel('PANIC-Solaris-138.xlsx', sheetname='PANIC-S2I') tag = df['tag'].apply( lambda x: str(x).replace(' ', '_').replace('-', '_').replace( '\\', '_').replace('/', '_').replace('__', '_').upper().strip()) except Exception as e: logging.error(traceback.format_exc()) for i in df.index: try: _update = df['update'][i].encode('utf-8').strip().lower() _tag = tag[i] _formula = 'sys/tg_test/1/ampli>' + str(i) _device_tmp = 'alarm/com/alarm02' _device = df['device'][i] _description = df['description'][i].encode('utf-8').strip() _sms = str(df['sms'][i]).replace(' ', '').strip().replace('+', '')