def ack(self, **kwargs): k, i = pa.parse_function_params(kwargs, 'ki', 'SS') lvar = pa.get_item(f'lvar:alarmer/{i}') if not lvar: raise pa.ResourceNotFound if not pa.key_check(k, lvar): raise pa.AccessDenied lv = pa.api_call("state", i=f'lvar:alarmer/{i}', full=True) pa.api_call("clear", i=f'lvar:alarmer/{i}') db = get_db() u = pa.get_aci('u') if not u: u = '' utp = pa.get_aci('utp') key_id = pa.get_aci('key_id') if not utp: utp = '' try: db.execute(sql( 'insert into alarmer_log' '(u, utp, key_id, alarm_id, description, action, t, level)' 'values (:u, :utp, :key_id, :alarm_id, :d, :action, :t, :level)' ), u=u, utp=utp, key_id=key_id, alarm_id=i, d=lv['description'], action='A', t=time.time(), level=0) except: logger.error(f'Unable to insert log record for alarm: {i}') pa.log_traceback() return True
def destroy_alarm(i): lvar_id = f'alarmer/{i}' success = True result = pa.api_call('management_api_call', i=flags.lm, f='destroy_lvar', p={'i': lvar_id}) if result['code'] != apiclient.result_ok: success = False else: pa.api_call('reload_controller', i=flags.lm) rule_id = i.rsplit('/', 1)[-1] for rtp in ['w', 'a']: result = pa.api_call('management_api_call', i=flags.lm, f='destroy_rule', p={'i': f'{rule_id}_{rtp}'}) if result['code'] != apiclient.result_ok: success = False try: get_db().execute(sql('delete from alarmer_sub where alarm_id=:i'), i=i) except: pa.log_traceback() success = False return success
def set_rule_props(self, **kwargs): i, rw, ra, save = pa.parse_api_params(kwargs, 'iwaS', 'S..b') rule_id = i.rsplit('/')[-1] if rw: result = pa.api_call('management_api_call', i=flags.lm, f='set_rule_prop', p={ 'i': f'{rule_id}_w', 'v': rw, 'save': save }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to set warning rule props {rule_id}_w at ' f'{flags.lm} ({result["code"]})') if ra: result = pa.api_call('management_api_call', i=flags.lm, f='set_rule_prop', p={ 'i': f'{rule_id}_a', 'v': ra, 'save': save }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to set alarm rule props {rule_id}_a at ' f'{flags.lm} ({result["code"]})') return True
def set_description(self, **kwargs): i, d, save = pa.parse_api_params(kwargs, 'idS', 'Ssb') lvar_id = f'lvar:alarmer/{i}' rule_id = i.rsplit('/')[-1] result = pa.api_call('management_api_call', i=flags.lm, f='set_prop', p={ 'i': lvar_id, 'p': 'description', 'v': d, 'save': save }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to set lvar description {lvar_id} at ' f'{flags.lm} ({result["code"]})') for rtp in ['w', 'a']: result = pa.api_call('management_api_call', i=flags.lm, f='set_rule_prop', p={ 'i': f'{rule_id}_{rtp}', 'p': 'description', 'v': d, 'save': save }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to set rule description {rule_id}_{rtp} at ' f'{flags.lm} ({result["code"]})') pa.api_call('reload_controller', i=flags.lm) return True
def list_rule_props(self, **kwargs): i = pa.parse_api_params(kwargs, 'i', 'S') rules = {} rule_id = i.rsplit('/')[-1] for rtp in ['w', 'a']: result = pa.api_call('management_api_call', i=flags.lm, f='list_rule_props', p={ 'i': f'{rule_id}_{rtp}', }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to list rule props {rule_id}_{rtp} at ' f'{flags.lm} ({result["code"]})') d = result['data'] for x in [ 'enabled', 'macro', 'macro_args', 'macro_kwargs', 'priority' ]: try: del d[x] except KeyError: pass rules['r' + rtp] = d return rules
def my_test(self, **kwargs): # let's return the result of test_phi API function return pa.api_call('test_phi', i='ct1', c='self')
def notify(alarm_id, level): level = int(level) lv = pa.api_call('state', i=f'lvar:alarmer/{alarm_id}', full=True) db = get_db() try: db.execute(sql( 'insert into alarmer_log' '(u, utp, key_id, alarm_id, description, action, t, level)' 'values (:u, :utp, :key_id, :alarm_id, :d, :action, :t, :level)'), u='', utp='', key_id='', alarm_id=alarm_id, d=lv['description'], action='T', t=time.time(), level=level) except: logger.error(f'Unable to insert log record for alarm: {alarm_id}') pa.log_traceback() try: if lv['status'] == 1: cur_value = lv['value'] if cur_value: cur_value = int(cur_value) else: cur_value = 0 if cur_value >= level: logger.info('Skipping alarm notifications, ' f'already triggered: {alarm_id}') else: pa.api_call('set', i=f'lvar:alarmer/{alarm_id}', v=level) logger.warning('Alarm triggered: ' f'{alarm_id}, level: {get_level_name(level)}') r = db.execute(sql('select u, utp from alarmer_sub ' 'where alarm_id=:i and level<=:level'), i=alarm_id, level=level) recip = [] subject = f'{get_level_name(level)}: {lv["description"]}' text = (f'{get_level_name(level)}: {lv["description"]} ' f'({alarm_id})\n' f'System: {eva.core.config.system_name}') sendmail = partial(eva.mailer.send, subject=subject, text=text) while True: ui = r.fetchone() if ui: r2 = db.execute(sql('select value from userinfo where ' 'name=:name and u=:u and utp=:utp'), name=flags.userinfo_email_field, u=ui.u, utp=ui.utp) while True: d = r2.fetchone() if d: logger.debug( f'sending alarm email to {d.value}') sendmail(rcp=recip) else: break else: break else: logger.debug(f'Inactive alarm triggered: {alarm_id}') except: logger.error(f'Unable to send notifications for alarm: {alarm_id}') pa.log_traceback() raise
def create(self, **kwargs): u, d, g, rw, ra, save = pa.parse_api_params(kwargs, 'udgwaS', 'sssRRb') import uuid alarm_id = u if u else str(uuid.uuid4()) alarm_full_id = f'{g if g else ""}{"/" if g else ""}{alarm_id}' lvar_id = f'alarmer{"/" if g else ""}{g if g else ""}/{alarm_id}' try: result = pa.api_call('management_api_call', i=flags.lm, f='create_lvar', p={ 'i': lvar_id, 'save': save and not d }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed(f'unable to create lvar {lvar_id} at' f' {flags.lm} ({result["code"]})') if d: result = pa.api_call('management_api_call', i=flags.lm, f='set_prop', p={ 'i': lvar_id, 'p': 'description', 'v': d, 'save': save }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to set lvar description {lvar_id} at ' f'{flags.lm} ({result["code"]})') result = pa.api_call('management_api_call', i=flags.lm, f='create_rule', p={ 'u': f'{alarm_id}_w', 'v': rw }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to create warning rule {alarm_id}w at ' f'{flags.lm} ({result["code"]})') result = pa.api_call('management_api_call', i=flags.lm, f='set_rule_prop', p={ 'i': f'{alarm_id}_w', 'v': { 'description': d, 'macro': '@x_alarmer_notify', 'macro_args': [alarm_full_id, 1], 'priority': 1, 'enabled': True }, 'save': save }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to set warning rule {alarm_id}_w props ' f'at {flags.lm} ({result["code"]})') result = pa.api_call('management_api_call', i=flags.lm, f='create_rule', p={ 'u': f'{alarm_id}_a', 'v': ra }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to create alarm rule {alarm_id}w at ' f'{flags.lm} ({result["code"]})') result = pa.api_call('management_api_call', i=flags.lm, f='set_rule_prop', p={ 'i': f'{alarm_id}_a', 'v': { 'description': d, 'macro': '@x_alarmer_notify', 'macro_args': [alarm_full_id, 2], 'priority': 1, 'enabled': True }, 'save': save }) if result['code'] != apiclient.result_ok: raise pa.FunctionFailed( f'unable to set alarm rule {alarm_id}_a props ' f'at {flags.lm} ({result["code"]})') pa.api_call('reload_controller', i=flags.lm) except: pa.log_traceback() destroy_alarm(f'{g if g else ""}{"/" if g else ""}{alarm_id}') raise return {'id': alarm_full_id, 'lvar_id': lvar_id}