Example #1
0
 def add_notification_callback(self, callback, **kwargs):
     """
     Register a notification callback with collectd. kwargs will be passed
     to collectd.register_notification. The callback will be called by
     collectd with a collectd.Notification object as the only argument.
     """
     collectd.register_notification(callback, **kwargs)
 def wallarm_snmp_notify_init(self):
     if self.config['configured']:
         collectd.register_notification(self.wallarm_snmp_notify)
     else:
         self.log(
             'warning',
             'A configuration error occured. Abort initializing'
         )
Example #3
0
    def add_notification_callback(self, callback, **kwargs):
        """Register a notification callback with collectd.

        kwargs will be passed to collectd.register_notification.
        The callback will be called by collectd with a collectd.
        Notification object as the only argument.
        """
        collectd.register_notification(callback, **kwargs)
def plugin_config(conf):
    """
    :param conf:
      https://collectd.org/documentation/manpages/collectd-python.5.shtml#config

    Parse the config object for config parameters:
      ProcessInfo: true or false, whether or not to collect process
        information. Default is true.
      Notifications: true or false, whether or not to emit notifications
      if Notifications is true:
        URL: where to POST the notifications to
        Token: what auth to send along
        Timeout: timeout for the POST
        NotifyLevel: what is the lowest level of notification to emit.
          Default is to only emit notifications generated by this plugin
    """

    DOGSTATSD_INSTANCE.config.configure_callback(conf)

    for kv in conf.children:
        if kv.key == 'Notifications':
            if kv.values[0]:
                log("sending collectd notifications")
                collectd.register_notification(receive_notifications)
        elif kv.key == 'ProcessInfo':
            global PROCESS_INFO
            PROCESS_INFO = kv.values[0]
        elif kv.key == 'DPM':
            global DPM
            DPM = kv.values[0]
            if DPM:
                collectd.register_write(receive_datapoint)
        elif kv.key == 'URL':
            global POST_URL
            POST_URL = kv.values[0]
        elif kv.key == 'Token':
            global API_TOKEN
            API_TOKEN = kv.values[0]
        elif kv.key == 'Timeout':
            global TIMEOUT
            TIMEOUT = int(kv.values[0])
        elif kv.key == 'Interval':
            global INTERVAL
            INTERVAL = int(kv.values[0])
        elif kv.key == 'NotifyLevel':
            global NOTIFY_LEVEL
            if string.lower(kv.values[0]) == "okay":
                NOTIFY_LEVEL = 4
            elif string.lower(kv.values[0]) == "warning":
                NOTIFY_LEVEL = 2
            elif string.lower(kv.values[0]) == "failure":
                NOTIFY_LEVEL = 1
def register_plugin(collectd):
    """Bind plugin hooks to collectd and viceversa."""
    config = Config.instance()

    # Setup loggging
    log_handler = CollectdLogHandler(collectd=collectd, config=config)
    ROOT_LOGGER.addHandler(log_handler)
    ROOT_LOGGER.setLevel(logging.DEBUG)

    # Creates collectd plugin instance
    instance = Plugin(collectd=collectd, config=config)

    # Register plugin callbacks
    collectd.register_config(instance.config)
    collectd.register_shutdown(instance.shutdown)
    collectd.register_notification(instance.notify)
Example #6
0
 def config_cb(self, config, data=None):
     self.config = util.map_collectd_config(config)
     if "Module.config" in self.config:
         self._log("config_cb: {!r}".format(self.config))
     if "Module.init" in self.config:
         collectd.register_init(util.init_closure(self), name=self.__module__)
     if "Module.read" in self.config:
         collectd.register_read(util.read_closure(self), name=self.__module__)
     if "Module.write" in self.config:
         collectd.register_write(util.write_closure(self), name=self.__module__)
     if "Module.notification" in self.config:
         collectd.register_notification(util.notification_closure(self), name=self.__module__)
     if "Module.flush" in self.config:
         collectd.register_flush(util.flush_closure(self), name=self.__module__)
     if "Module.log" in self.config:
         collectd.register_log(util.log_closure(self), name=self.__module__)
     if "Module.shutdown" in self.config:
         collectd.register_shutdown(util.shutdown_closure(self), name=self.__module__)
Example #7
0
def notification(notif):
    """
    This method has been regstered as the notification callback. Let's count the notifications
    we receive and emit that as a metric.

    :param notif: a Notification object.
    :return: None
    """

    global NOTIFICATION_COUNT
    NOTIFICATION_COUNT += 1


if __name__ != "__main__":
    # when running inside plugin register each callback
    collectd.register_config(config)
    collectd.register_read(read)
    collectd.register_init(init)
    collectd.register_shutdown(shutdown)
    collectd.register_write(write)
    collectd.register_flush(flush)
    collectd.register_log(log_cb)
    collectd.register_notification(notification)
else:
    # outside plugin just collect the info
    read()
    if len(sys.argv) < 2:
        while True:
            time.sleep(10)
            read()
Example #8
0
        try:
            macros['ds']              = ds_name
            macros['current_value']   = ds_value
            macros['threshold_value'] = ts_value
        except:
            pass
        try:
            params
            for _k,_v in params.iteritems():
                exec("macros['%s'] = '%s'" % (_k,str(_v),))
        except:
            pass

        from karesansui.lib.collectd.utils import evaluate_macro
        watch_mail_body = evaluate_macro(watch_mail_body,macros)

        send_mail(recipient=recipient,sender=sender,server=smtp_server,port=smtp_port,message=watch_mail_body,extra_message=script_result_message,watch_name=name,logfile=logfile)
        pass

    """ comment
    collectd.Values(type='cpu',type_instance='steal',
                    plugin='cpu',plugin_instance='0',
                    host='host.example.com',
                    message='Oops, the cpu steal cpu is currently 0!',
                    time=%lu,interval=%i)
    """

collectd.register_config(config)
collectd.register_init(init)
collectd.register_notification(notification,data=optional_data)
    details.
    """
    try:
        platform.system()
    except:
        log("executing SIGCHLD workaround")
        signal.signal(signal.SIGCHLD, signal.SIG_DFL)
    if __name__ != "__main__":
        DOGSTATSD_INSTANCE.init_callback()


# Note: Importing collectd_dogstatsd registers its own endpoints

if __name__ != "__main__":
    # when running inside plugin
    collectd.register_notification(steal_host_from_notifications)
    collectd.register_init(restore_sigchld)
    collectd.register_config(plugin_config)
    collectd.register_read(send)
    collectd.register_shutdown(DOGSTATSD_INSTANCE.register_shutdown)

else:
    # outside plugin just collect the info
    restore_sigchld()
    send()
    log(json.dumps(get_host_info(), sort_keys=True,
                   indent=4, separators=(',', ': ')))
    if len(sys.argv) < 2:
        while True:
            time.sleep(INTERVAL)
            send()
def plugin_config(conf):
    """
    :param conf:
      https://collectd.org/documentation/manpages/collectd-python.5.shtml
      #config

    Parse the config object for config parameters
    """

    DOGSTATSD_INSTANCE.config.configure_callback(conf)

    global POST_URLS
    for kv in conf.children:
        if kv.key == 'Notifications':
            if kv.values[0]:
                global NOTIFICATIONS
                NOTIFICATIONS = kv.values[0]
        elif kv.key == 'ProcessInfo':
            global PROCESS_INFO
            PROCESS_INFO = kv.values[0]
        elif kv.key == 'Datapoints':
            global DATAPOINTS
            DATAPOINTS = kv.values[0]
        elif kv.key == 'Utilization':
            global UTILIZATION
            UTILIZATION = kv.values[0]
        elif kv.key == 'DPM':
            global DPM
            DPM = kv.values[0]
        elif kv.key == 'Verbose':
            global DEBUG
            DEBUG = kv.values[0]
            log('setting verbose to %s' % DEBUG)
        elif kv.key == 'URL':
            POST_URLS.extend(kv.values)
        elif kv.key == 'Token':
            global API_TOKEN
            API_TOKENS.extend(kv.values)
        elif kv.key == 'Timeout':
            global TIMEOUT
            TIMEOUT = int(kv.values[0])
        elif kv.key == 'Interval':
            global INTERVAL
            INTERVAL = int(kv.values[0])
        elif kv.key == 'NotifyLevel':
            global NOTIFY_LEVEL
            if string.lower(kv.values[0]) == "okay":
                NOTIFY_LEVEL = 4
            elif string.lower(kv.values[0]) == "warning":
                NOTIFY_LEVEL = 2
            elif string.lower(kv.values[0]) == "failure":
                NOTIFY_LEVEL = 1

    if not POST_URLS:
        POST_URLS = [DEFAULT_POST_URL]

    if API_TOKENS and len(POST_URLS) != len(API_TOKENS):
        log("You have specified a different number of Tokens than URLs, "
            "please fix this")
        sys.exit(0)

    if NOTIFICATIONS:
        log("sending collectd notifications")
        collectd.register_notification(receive_notifications)
    else:
        collectd.register_notification(steal_host_from_notifications)

    collectd.register_write(write)

    if UTILIZATION:
        collectd.register_read(UTILIZATION_INSTANCE.read,
                               1,
                               name="utilization_reads")

    collectd.register_read(send, INTERVAL)
    set_aws_url(get_aws_info())
                GWM_HOST = str(node.values[0])
            else:
                warning("Unknown config parameter: %s" % node.key)

        if not GWM_HOST:
            warning("Improperly configured: `Host` is necessary")


def notify_gwm(notification):
    N = notification
    if GWM_HOST:
        data = urlencode(
            dict(host=N.host,
                 plugin=N.plugin,
                 plugin_instance=N.plugin_instance,
                 type=N.type,
                 type_instance=N.type_instance,
                 time=N.time,
                 message=N.message,
                 severity=N.severity))

        try:
            urllib2.urlopen("%s/metrics/alert" % GWM_HOST, data, 10)
        except urllib2.URLError as e:
            warning("Couldn't post notification: %s" % str(e))


collectd.register_config(config_notify_gwm)
collectd.register_init(init_notify_gwm)
collectd.register_notification(notify_gwm)
Example #12
0
    def notify(self, n):
        """Collectd notification callback"""
        # type='gauge',type_instance='link_status',plugin='ovs_events',plugin_instance='br0',
        # host='silv-vmytnyk-nos.ir.intel.com',time=1476441572.7450583,severity=4,
        # message='link state of "br0" interface has been changed to "UP"')
        collectd_event_severity_map = {
            collectd.NOTIF_FAILURE : 'CRITICAL',
            collectd.NOTIF_WARNING : 'WARNING',
            collectd.NOTIF_OKAY : 'NORMAL'
        }
        fault = Fault(self.get_event_id())
        fault.event_severity = collectd_event_severity_map[n.severity]
        fault.specific_problem = '{}-{}'.format(n.plugin_instance, n.type_instance)
        fault.alarm_condition = n.message
        self.event_send(fault)

    def shutdown(self):
        """Collectd shutdown callback"""
        # stop the timer
        self.stop_timer()

# The collectd plugin instance
plugin_instance = VESPlugin()

# Register plugin callbacks
collectd.register_config(plugin_instance.config)
collectd.register_init(plugin_instance.init)
collectd.register_write(plugin_instance.write)
collectd.register_notification(plugin_instance.notify)
collectd.register_shutdown(plugin_instance.shutdown)
def config_notify_gwm(data=None):
    global GWM_HOST
    if data:
        for node in data.children:
            if node.key == "Host":
                GWM_HOST = str(node.values[0])
            else:
                warning("Unknown config parameter: %s" % node.key)

        if not GWM_HOST:
            warning("Improperly configured: `Host` is necessary")


def notify_gwm(notification):
    N = notification
    if GWM_HOST:
        data = urlencode(dict(host=N.host, plugin=N.plugin,
            plugin_instance=N.plugin_instance, type=N.type,
            type_instance=N.type_instance, time=N.time, message=N.message,
            severity=N.severity))

        try:
            urllib2.urlopen("%s/metrics/alert" % GWM_HOST, data, 10)
        except urllib2.URLError as e:
            warning("Couldn't post notification: %s" % str(e))


collectd.register_config(config_notify_gwm)
collectd.register_init(init_notify_gwm)
collectd.register_notification(notify_gwm)
import http_check


NAME = "check_local_endpoint"


class CheckLocalEndpoint(http_check.HTTPCheckPlugin):
    def __init__(self, *args, **kwargs):
        super(CheckLocalEndpoint, self).__init__(*args, **kwargs)
        self.plugin = NAME


plugin = CheckLocalEndpoint(collectd)


def config_callback(conf):
    plugin.config_callback(conf)


def notification_callback(notification):
    plugin.notification_callback(notification)


def read_callback():
    plugin.conditional_read_callback()


collectd.register_config(config_callback)
collectd.register_notification(notification_callback)
collectd.register_read(read_callback, base.INTERVAL)
Example #15
0
                             alarm_state=_alarm_state,
                             entity_type_id=fm_constants.FM_ENTITY_TYPE_HOST,
                             entity_instance_id=obj.entity_id,
                             severity=_severity_num,
                             reason_text=reason,
                             alarm_type=base_obj.alarm_type,
                             probable_cause=base_obj.cause,
                             proposed_repair_action=base_obj.repair,
                             service_affecting=base_obj.service_affecting,
                             suppression=base_obj.suppression)

        alarm_uuid = api.set_fault(fault)
        if is_uuid_like(alarm_uuid) is False:
            collectd.error("%s %s:%s set_fault failed:%s" %
                           (PLUGIN, base_obj.id, obj.entity_id, alarm_uuid))
            return 0

    # update the lists now that
    base_obj._manage_alarm(obj.entity_id, severity_str)

    collectd.info("%s %s alarm %s:%s %s:%s thld:%2.2f value:%2.2f" %
                  (PLUGIN, _alarm_state, base_obj.id, severity_str,
                   obj.instance, obj.entity_id, obj.threshold, obj.value))

    # Debug only: comment out for production code.
    # obj._state_audit("change")


collectd.register_init(init_func)
collectd.register_notification(notifier_func)
status_conv = {
    collectd.NOTIF_OKAY: 0,
    collectd.NOTIF_WARNING: 1,
    collectd.NOTIF_FAILURE: 2
}

url = 'http://nagios.c.developer-222401.internal:6315/submit_result'


def handle_notification(notif):
    requests.post(url,
                  json={
                      'host':
                      notif.host,
                      'service':
                      '{}-{}/{}-{}'.format(
                          notif.plugin,
                          notif.plugin_instance,
                          notif.type,
                          notif.type_instance,
                      ),
                      'status':
                      status_conv[notif.severity],
                      'output':
                      notif.message
                  }).raise_for_status()


collectd.register_notification(handle_notification)
Example #17
0
            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'X-Auth-Token': self.sess.get_token()
            }
            requests.put(self.inspector_url, data=data, headers=headers)

    def handle_notify(self, notification, data=None):
        if (notification.seerity == collectd.NOTIF_FAILURE
                or notification.severity == collectd.NOTIF_WARNING):
            if (self.start_notifications == 1
                    and self.inspector_notified == 0):
                write_debug(
                    "Received down notification:"
                    "doctor monitor detected at %s\n" % time.time(), "a",
                    self.compute_user)
                self.notify_inspector()

        elif notification.severity == collectd.NOTIF_OKAY:
            collectd.info("Interface status: UP again %s\n" % time.time())
        else:
            collectd.info("Unknown notification severity %s\n" %
                          notification.severity)


monitor = DoctorMonitorCollectd()

collectd.register_config(monitor.config_func)
collectd.register_init(monitor.init_collectd)
collectd.register_notification(monitor.handle_notify)
Example #18
0

def flush_cb(timeout, identifier, data=None):
    collectd.info("flush_cb: [{0}] {1}".format(timeout, identifier))

def log_cb(severity, message, data=None):
    # print here, if logging is used it will cause infinite recursion.
    print("log_cb: severity({}) - {}".format(severity, message))


## Register the call-back functions

collectd.register_config(config_cb)
collectd.register_init(init_cb)
collectd.register_shutdown(shutdown_cb)

collectd.register_read(read_cb)
collectd.register_write(write_cb)
collectd.register_notification(notification_cb)

collectd.register_flush(flush_cb)
collectd.register_log(log_cb)


collectd.info("trace plugin module loaded")


## Local Variables:
## mode: python
## End:
Example #19
0
                    _k,
                    str(_v),
                ))
        except:
            pass

        from karesansui.lib.collectd.utils import evaluate_macro
        watch_mail_body = evaluate_macro(watch_mail_body, macros)

        send_mail(recipient=recipient,
                  sender=sender,
                  server=smtp_server,
                  port=smtp_port,
                  message=watch_mail_body,
                  extra_message=script_result_message,
                  watch_name=name,
                  logfile=logfile)
        pass
    """ comment
    collectd.Values(type='cpu',type_instance='steal',
                    plugin='cpu',plugin_instance='0',
                    host='host.example.com',
                    message='Oops, the cpu steal cpu is currently 0!',
                    time=%lu,interval=%i)
    """


collectd.register_config(config)
collectd.register_init(init)
collectd.register_notification(notification, data=optional_data)
Example #20
0
    return


def flush_cb(timeout, identifier, data=None):
    return

def log_cb(severity, message, data=None):
    return


## Register the call-back functions

data = "stub-string"         # placeholder
name = init_cb.__module__    # the default
interval = 10                # the default

collectd.register_config(config_cb, data, name)
collectd.register_init(init_cb, data, name)
collectd.register_shutdown(shutdown_cb, data, name)

collectd.register_read(read_cb, interval, data, name)
collectd.register_write(write_cb, data, name)
collectd.register_notification(notification_cb, data, name)

collectd.register_flush(flush_cb, data, name)
collectd.register_log(log_cb, data, name)

## Local Variables:
## mode: python
## End:
            elif node.key == "Password":
                password = node.values[0]
            elif node.key == "TenantName":
                tenant_name = node.values[0]
            elif node.key == "AuthURL":
                auth_url = node.values[0]
        self.novaclient = client.Client(version='1.1',
                                        username=username,
                                        password=password,
                                        project_id=tenant_name,
                                        auth_url=auth_url,
                                        endpoint_type='internalURL',
                                        connection_pool=True)
        # print >> sys.stderr, (username, password, tenant_name, auth_url)

    def notify(self, vl, data=None):
        if vl.severity < 4:
            hypervisors = self.novaclient.hypervisors.search(self.hostname,
                                                             servers=True)
            for hyper in hypervisors:
                if hasattr(hyper, 'servers'):
                    for server in hyper.servers:
                        self.novaclient.servers.live_migrate(
                            server['uuid'], None, True, False)


cli = OSCliNova()

collectd.register_config(cli.configure)
collectd.register_notification(cli.notify)
                        else:
                            rval = itemlist[0].getElementsByTagName(
                                self.result_type[name]
                            )[0].childNodes[0].toxml()
                        self.logger.debug(
                            "Got val for {}: '{}'".format(name, rval))
                        yield {'type_instance': name, 'values': rval}
                    except Exception as e:
                        msg = ("Got exception while parsing "
                               "response for '{}': {}").format(name, e)
                        raise base.CheckException(msg)


plugin = ContrailApiPlugin(collectd)


def config_callback(conf):
    plugin.config_callback(conf)


def notification_callback(notification):
    plugin.notification_callback(notification)


def read_callback():
    plugin.conditional_read_callback()

collectd.register_config(config_callback)
collectd.register_notification(notification_callback)
collectd.register_read(read_callback, INTERVAL)
Example #23
0
#!/usr/bin/python

import collectd
import datetime
try:
  from pymongo import MongoClient as Mongo
except ImportError:
  from pymongo import Connection as Mongo

def read_notif(notif):
  collection = notif.plugin
  parts = notif.message.split("^")
  data = {}
  for p in parts:
    if ": " not in p:
      continue
    k,v = p.split(": ")
    data[k] = v
  data["last_updated"] = datetime.datetime.now()
  customer = data["customer"]
  conn = Mongo()
  dbname = "xervmon_collectd_%s" % (customer, )
  db = conn[dbname]
  info = db[collection]
  info.insert(data)
  conn.close()

collectd.register_notification(read_notif)
 def wallarm_snmp_notify_init(self):
     if self.config['configured']:
         collectd.register_notification(self.wallarm_snmp_notify)
     else:
         self.log('warning',
                  'A configuration error occured. Abort initializing')
def plugin_config(conf):
    """
    :param conf:
      https://collectd.org/documentation/manpages/collectd-python.5.shtml
      #config

    Parse the config object for config parameters
    """

    DOGSTATSD_INSTANCE.config.configure_callback(conf)

    global POST_URLS
    for kv in conf.children:
        if kv.key == 'Notifications':
            if kv.values[0]:
                global NOTIFICATIONS
                NOTIFICATIONS = kv.values[0]
        elif kv.key == 'ProcessInfo':
            global PROCESS_INFO
            PROCESS_INFO = kv.values[0]
        elif kv.key == 'Datapoints':
            global DATAPOINTS
            DATAPOINTS = kv.values[0]
        elif kv.key == 'Utilization':
            global UTILIZATION
            UTILIZATION = kv.values[0]
        elif kv.key == 'DPM':
            global DPM
            DPM = kv.values[0]
        elif kv.key == 'Verbose':
            global DEBUG
            DEBUG = kv.values[0]
            log('setting verbose to %s' % DEBUG)
        elif kv.key == 'URL':
            POST_URLS.extend(kv.values)
        elif kv.key == 'Token':
            global API_TOKEN
            API_TOKENS.extend(kv.values)
        elif kv.key == 'Timeout':
            global TIMEOUT
            TIMEOUT = int(kv.values[0])
        elif kv.key == 'Interval':
            global INTERVAL
            INTERVAL = int(kv.values[0])
        elif kv.key == 'NotifyLevel':
            global NOTIFY_LEVEL
            if string.lower(kv.values[0]) == "okay":
                NOTIFY_LEVEL = 4
            elif string.lower(kv.values[0]) == "warning":
                NOTIFY_LEVEL = 2
            elif string.lower(kv.values[0]) == "failure":
                NOTIFY_LEVEL = 1

    if not POST_URLS:
        POST_URLS = [DEFAULT_POST_URL]

    if API_TOKENS and len(POST_URLS) != len(API_TOKENS):
        log(
            "You have specified a different number of Tokens than URLs, "
            "please fix this")
        sys.exit(0)

    if NOTIFICATIONS:
        log("sending collectd notifications")
        collectd.register_notification(receive_notifications)
    else:
        collectd.register_notification(steal_host_from_notifications)

    collectd.register_write(write)

    if UTILIZATION:
        collectd.register_read(UTILIZATION_INSTANCE.read, 1,
                               name="utilization_reads")

    collectd.register_read(send, INTERVAL)
    set_aws_url(get_aws_info())