def notify(args): msg_type = "markdown" if args.markdown else "text" notifier = Notifier(args.content, args.subject, msg_type=msg_type) for type_ in NOTIFY_TYPES: values = vars(args)[type_] if values: addrs = [i for v in values for i in re.split(r"[,\s]+", v)] try: getattr(notifier, type_)(addrs, from_addr=args.from_addr) except Exception as e: logger.exception("Notifier.%s(%s) failed: %s", type_, addrs, e) report()
def notify(args): notifier = Notifier(from_addr=ICINGA_EMAIL) env = ( dict( TARGET_TYPE='service', NAGIOS_LONGDATETIME='2016-05-11 16:30:50 +8000', NAGIOS_NOTIFICATIONTYPE='PROBLEM', NAGIOS_HOSTALIAS='sa', NAGIOS_SERVICEDESC='fakeservice', NAGIOS_SERVICEOUTPUT="整个中文试试", NAGIOS_SERVICESTATE='CRITICAL', NOTIFICATIONAUTHORNAME='sysadmin', NOTIFICATIONCOMMENT='没病走两步~', NOTIFICATION_IS_ARCHIVE=False, NAGIOS_CONTACTNAME='shuaisa', SERVICE_DURATION_SEC='5.001102', NAGIOS_CUSTOM_WIKI='' ) if args.test else os.environ) unicode_env = {} for name, value in env.items(): if isinstance(value, six.string_types): unicode_env[six.ensure_text(name)] = six.ensure_text(value) else: unicode_env[six.ensure_text(name)] = value env = AttrDict(unicode_env, _default_value=six.u('')) short_env = dict( type=env.NAGIOS_NOTIFICATIONTYPE[:3].upper(), host=env.NAGIOS_HOSTALIAS, hoststate=env.HOSTSTATE, service=env.NAGIOS_SERVICEDESC, time=' '.join(env.NAGIOS_LONGDATETIME.split()[:2]), extra=(env.NAGIOS_HOSTOUTPUT if env.TARGET_TYPE == 'host' else env.NAGIOS_SERVICEOUTPUT), link='', custom_wiki_url=env.NAGIOS_CUSTOM_WIKI, wiki_base_url=ALERT_WIKI_BASE_URL.rstrip(' /') ) duration = env.SERVICE_DURATION_SEC if env.TARGET_TYPE == 'service' \ else env.HOST_DURATION_SEC short_env = AttrDict(short_env, _default_value='') ack_link = icinga_cluster_config.get_ack_link(env) reboot_host_link = icinga_cluster_config.get_reboot_host_link(env) icinga_link = icinga_cluster_config.get_icinga_link(env) for type_ in NOTIFY_TYPES: values = vars(args)[type_] if values: addrs = [i for v in values for i in re.split(r'[,\s]+', v)] addrs = [a for a in addrs if a] if not addrs: logger.warning('ignore empty %s addrs' % type_) continue title, content = render_notification( env=env, short_env=short_env, notify_type=type_, ack_link=ack_link, reboot_host_link=reboot_host_link, icinga_link=icinga_link) try: ok = add_notification( env.NAGIOS_NOTIFICATIONTYPE, short_env.host, short_env.hoststate, short_env.service, content, type_, ', '.join(addrs), duration) logger.info('notification gateway permit: %s', ok) except Exception: # we catch the exception and send it to sentry, but let the program continue to run report() logger.exception('add notification to gateway failed: ') ok = True if ok: try: getattr(notifier, type_)(addrs, title=title, content=content) except Exception as e: report() logger.error('Notifier.%s(%s) failed: %s', type_, addrs, e)
def notify(args): notifier = Notifier(from_addr=ICINGA_EMAIL, msg_type="markdown") env = ( dict( TARGET_TYPE="service", NAGIOS_LONGDATETIME="2016-05-11 16:30:50 +8000", NAGIOS_NOTIFICATIONTYPE="PROBLEM", NAGIOS_HOSTALIAS="sa", NAGIOS_SERVICEDESC="fakeservice", NAGIOS_SERVICEOUTPUT="整个中文试试", NAGIOS_SERVICESTATE="CRITICAL", NOTIFICATIONAUTHORNAME="sysadmin", NOTIFICATIONCOMMENT="没病走两步~", NOTIFICATION_IS_ARCHIVE=False, NAGIOS_CONTACTNAME="shuaisa", SERVICE_DURATION_SEC="5.001102", NAGIOS_CUSTOM_WIKI="https://wiki.example.com/service/fakeservice", ) if args.test else os.environ ) unicode_env = {} for name, value in env.items(): if isinstance(value, six.string_types): unicode_env[six.ensure_text(name)] = six.ensure_text(value) else: unicode_env[six.ensure_text(name)] = value env = AttrDict(unicode_env, _default_value=six.u("")) short_env = dict( type=env.NAGIOS_NOTIFICATIONTYPE[:3].upper(), host=env.NAGIOS_HOSTALIAS, hoststate=env.HOSTSTATE, service=env.NAGIOS_SERVICEDESC, time=" ".join(env.NAGIOS_LONGDATETIME.split()[:2]), extra=(env.NAGIOS_HOSTOUTPUT if env.TARGET_TYPE == "host" else env.NAGIOS_SERVICEOUTPUT), link="", custom_wiki_url=env.NAGIOS_CUSTOM_WIKI, wiki_base_url=ALERT_WIKI_BASE_URL.rstrip(" /"), ) duration = env.SERVICE_DURATION_SEC if env.TARGET_TYPE == "service" else env.HOST_DURATION_SEC short_env = AttrDict(short_env, _default_value="") ack_link = icinga_cluster_config.get_ack_link(env) reboot_host_link = icinga_cluster_config.get_reboot_host_link(env) icinga_link = icinga_cluster_config.get_icinga_link(env) for type_ in NOTIFY_TYPES: values = vars(args)[type_] if values: addrs = [i for v in values for i in re.split(r"[,\s]+", v)] addrs = [a for a in addrs if a] if not addrs: logger.warning("ignore empty %s addrs" % type_) continue title, content = render_notification( env=env, short_env=short_env, notify_type=type_, ack_link=ack_link, reboot_host_link=reboot_host_link, icinga_link=icinga_link, ) try: ok = add_notification( env.NAGIOS_NOTIFICATIONTYPE, short_env.host, short_env.hoststate, short_env.service, content, type_, ", ".join(addrs), duration, ) logger.info("notification gateway permit: %s", ok) except Exception: # we catch the exception and send it to sentry, but let the program continue to run report() logger.exception("add notification to gateway failed: ") ok = True if ok: try: getattr(notifier, type_)(addrs, title=title, content=content, company=args.company) except Exception as e: report() logger.error("Notifier.%s(%s) failed: %s", type_, addrs, e)