class ZaggSender(object):
    """
    collect and create UniqueMetrics and send them to Zagg
    """

    def __init__(self, host, zagg_connection):
        """
        set up the zagg client, pcp_metrics and unique_metrics
        """
        self.zaggclient = ZaggClient(zagg_connection=zagg_connection)
        self.host = host
        self.unique_metrics = []

    def add_pcp_metrics(self, pcp_metrics, host=None):
        """
        Evaluate a list of metrics from pcp using pminfo
        return list of  UniqueMetrics
        """
        if not host:
            host = self.host

        pcp_metric_dict = pminfo.get_metrics(pcp_metrics)

        for metric, value in pcp_metric_dict.iteritems():
            new_unique_metric = UniqueMetric(host, metric, value)
            self.unique_metrics.append(new_unique_metric)

    def add_heartbeat(self, heartbeat, host=None):
        """ create a heartbeat unique metric to send to zagg """

        if not host:
            host = self.host

        hb_metric = UniqueMetric.create_heartbeat(host,
                                                  heartbeat.templates,
                                                  heartbeat.hostgroups,
                                                 )
        self.unique_metrics.append(hb_metric)

    def add_zabbix_keys(self, zabbix_keys, host=None):
        """ create unique metric from zabbix key value pair """

        if not host:
            host = self.host

        zabbix_metrics = []

        for key, value in zabbix_keys.iteritems():
            zabbix_metric = UniqueMetric(host, key, value)
            zabbix_metrics.append(zabbix_metric)

        self.unique_metrics += zabbix_metrics

    def send_metrics(self):
        """
        Send list of Unique Metrics to Zagg
        clear self.unique_metrics
        """
        self.zaggclient.add_metric(self.unique_metrics)
        self.unique_metrics = []
Exemple #2
0
class ZaggSender(GenericMetricSender):
    """
    collect and create UniqueMetrics and send them to Zagg
    """

    # Allow for 6 arguments (including 'self')
    # pylint: disable=too-many-arguments
    def __init__(self,
                 host=None,
                 zagg_connection=None,
                 verbose=False,
                 debug=False,
                 config_file=None):
        """
        set up the zagg client and unique_metrics
        """
        super(ZaggSender, self).__init__()

        if not config_file:
            config_file = '/etc/openshift_tools/metric_sender.yaml'

        self.config_file = config_file
        self.unique_metrics = []
        self.verbose = verbose
        self.debug = debug

        if not host:
            host = self.get_default_host()

        if not zagg_connection:
            zagg_connection = self.get_default_zagg_connection()

        self.host = host
        self.zaggclient = ZaggClient(zagg_connection=zagg_connection)

    def get_default_host(self):
        """ get the 'host' value from the config file """
        self.parse_config()

        return self.config['host']['name']

    def get_default_zagg_connection(self):
        """ get the values and create a zagg_connection """

        self.parse_config()
        zagg_server = self.config['zagg']['url']
        zagg_user = self.config['zagg']['user']
        zagg_password = self.config['zagg']['pass']
        zagg_ssl_verify = self.config['zagg'].get('ssl_verify', False)
        zagg_debug = self.config['zagg'].get('debug', False)

        if isinstance(zagg_ssl_verify, str):
            zagg_ssl_verify = (zagg_ssl_verify == 'True')

        if self.debug:
            zagg_debug = self.debug
        elif isinstance(zagg_debug, str):
            zagg_debug = (zagg_debug == 'True')

        zagg_connection = ZaggConnection(
            url=zagg_server,
            user=zagg_user,
            password=zagg_password,
            ssl_verify=zagg_ssl_verify,
            debug=zagg_debug,
        )

        return zagg_connection

    def add_heartbeat(self, heartbeat, host=None):
        """ create a heartbeat unique metric to send to zagg """

        if not host:
            host = self.host

        hb_metric = UniqueMetric.create_heartbeat(
            host,
            heartbeat.templates,
            heartbeat.hostgroups,
        )
        self.unique_metrics.append(hb_metric)

    def add_metric(self, metrics, host=None, synthetic=False, key_tags=None):
        """ create unique metric from zabbix key value pair """

        if synthetic and not host:
            host = self.config['synthetic_clusterwide']['host']['name']
        elif not host:
            host = self.host

        zabbix_metrics = []

        for key, value in metrics.iteritems():
            zabbix_metric = UniqueMetric(host, key, value, tags=key_tags)
            zabbix_metrics.append(zabbix_metric)

        self.unique_metrics += zabbix_metrics

    # Temporary wrapper for add_metric to support old calls to zagg_sender
    def add_zabbix_keys(self, metrics, host=None, synthetic=False):
        """ Temporary wrapper for add_metric to support old calls to zagg_sender """
        self.add_metric(metrics, host, synthetic)

    # Allow for 6 arguments (including 'self')
    # pylint: disable=too-many-arguments
    def add_dynamic_metric(self,
                           discovery_key,
                           macro_string,
                           macro_array,
                           host=None,
                           synthetic=False):
        """
        This creates a dynamic item prototype that is required
        for low level discovery rules in Zabbix.
        This requires:
        - dicovery key
        - macro string
        - macro name

        This will create a zabbix key value pair that looks like:

        disovery_key = "{"data": [
                          {"{#macro_string}":"macro_array[0]"},
                          {"{#macro_string}":"macro_array[1]"},
                        ]}"
        """

        if synthetic and not host:
            host = self.config['synthetic_clusterwide']['host']['name']
        elif not host:
            host = self.host

        data_array = [{'{%s}' % macro_string: i} for i in macro_array]
        json_data = json.dumps({'data': data_array})

        zabbix_dynamic_item = UniqueMetric(host, discovery_key, json_data)

        self.unique_metrics.append(zabbix_dynamic_item)

    # Temporary wrapper for add_dynamic_metric to support old calls to zagg_sender.
    # Allow for 6 arguments (including 'self')
    # pylint: disable=too-many-arguments
    def add_zabbix_dynamic_item(self,
                                discovery_key,
                                macro_string,
                                macro_array,
                                host=None,
                                synthetic=False):
        """ Temporary wrapper for add_dynamic_metric to support old calls to zagg_sender """
        self.add_dynamic_metric(discovery_key, macro_string, macro_array, host,
                                synthetic)

    def send_metrics(self):
        """
        Send list of Unique Metrics to Zagg
        clear self.unique_metrics
        """
        if self.verbose:
            self.print_unique_metrics_key_value()

        if self.debug:
            self.print_unique_metrics()

        self.zaggclient.add_metric(self.unique_metrics)
        self.unique_metrics = []
class ZaggSender(object):
    """
    collect and create UniqueMetrics and send them to Zagg
    """
    def __init__(self,
                 host=None,
                 zagg_connection=None,
                 verbose=False,
                 debug=False):
        """
        set up the zagg client, pcp_metrics and unique_metrics
        """
        self.unique_metrics = []
        self.config = None
        self.config_file = '/etc/openshift_tools/zagg_client.yaml'
        self.verbose = verbose
        self.debug = debug

        if not host:
            host = self.get_default_host()

        if not zagg_connection:
            zagg_connection = self.get_default_zagg_connecton()

        self.host = host
        self.zaggclient = ZaggClient(zagg_connection=zagg_connection)

    def print_unique_metrics_key_value(self):
        """
        This function prints the key/value pairs the UniqueMetrics that ZaggSender
        currently has stored
        """

        print "\nZaggSender Key/Value pairs:"
        print "=============================="
        for unique_metric in self.unique_metrics:
            print("%s:  %s") % (unique_metric.key, unique_metric.value)
        print "==============================\n"

    def print_unique_metrics(self):
        """
        This function prints all of the information of the UniqueMetrics that ZaggSender
        currently has stored
        """

        print "\nZaggSender UniqueMetrics:"
        print "=============================="
        for unique_metric in self.unique_metrics:
            print unique_metric
        print "==============================\n"

    def parse_config(self):
        """ parse default config file """

        if not self.config:
            if not os.path.exists(self.config_file):
                raise ZaggSenderException(self.config_file +
                                          " does not exist.")
            self.config = yaml.load(file(self.config_file))

    def get_default_host(self):
        """ get the 'host' value from the config file """
        self.parse_config()

        return self.config['host']['name']

    def get_default_zagg_connecton(self):
        """ get the values and create a zagg_connection """

        self.parse_config()
        zagg_server = self.config['zagg']['url']
        zagg_user = self.config['zagg']['user']
        zagg_password = self.config['zagg']['pass']
        zagg_ssl_verify = self.config['zagg'].get('ssl_verify', False)
        zagg_debug = self.config['zagg'].get('debug', False)

        if isinstance(zagg_ssl_verify, str):
            zagg_ssl_verify = (zagg_ssl_verify == 'True')

        if self.debug:
            zagg_debug = self.debug
        elif isinstance(zagg_debug, str):
            zagg_debug = (zagg_debug == 'True')

        zagg_connection = ZaggConnection(
            url=zagg_server,
            user=zagg_user,
            password=zagg_password,
            ssl_verify=zagg_ssl_verify,
            debug=zagg_debug,
        )

        return zagg_connection

    def add_pcp_metrics(self,
                        pcp_metrics,
                        pcp_derived_metrics=None,
                        host=None):
        """
        Evaluate a list of metrics from pcp using pminfo
        return list of  UniqueMetrics
        """
        if not host:
            host = self.host

        pcp_metric_dict = pminfo.get_metrics(
            metrics=pcp_metrics, derived_metrics=pcp_derived_metrics)

        for metric, value in pcp_metric_dict.iteritems():
            new_unique_metric = UniqueMetric(host, metric, value)
            self.unique_metrics.append(new_unique_metric)

    def add_heartbeat(self, heartbeat, host=None):
        """ create a heartbeat unique metric to send to zagg """

        if not host:
            host = self.host

        hb_metric = UniqueMetric.create_heartbeat(
            host,
            heartbeat.templates,
            heartbeat.hostgroups,
        )
        self.unique_metrics.append(hb_metric)

    def add_zabbix_keys(self, zabbix_keys, host=None):
        """ create unique metric from zabbix key value pair """

        if not host:
            host = self.host

        zabbix_metrics = []

        for key, value in zabbix_keys.iteritems():
            zabbix_metric = UniqueMetric(host, key, value)
            zabbix_metrics.append(zabbix_metric)

        self.unique_metrics += zabbix_metrics

    def add_zabbix_dynamic_item(self,
                                discovery_key,
                                macro_string,
                                macro_array,
                                host=None):
        """
        This creates a dynamic item prototype that is required
        for low level discovery rules in Zabbix.
        This requires:
        - dicovery key
        - macro string
        - macro name

        This will create a zabbix key value pair that looks like:

        disovery_key = "{"data": [
                          {"{#macro_string}":"macro_array[0]"},
                          {"{#macro_string}":"macro_array[1]"},
                        ]}"
        """

        if not host:
            host = self.host

        data_array = [{'{%s}' % macro_string: i} for i in macro_array]
        json_data = json.dumps({'data': data_array})

        zabbix_dynamic_item = UniqueMetric(host, discovery_key, json_data)

        self.unique_metrics.append(zabbix_dynamic_item)

    def send_metrics(self):
        """
        Send list of Unique Metrics to Zagg
        clear self.unique_metrics
        """
        if self.verbose:
            self.print_unique_metrics_key_value()

        if self.debug:
            self.print_unique_metrics()

        self.zaggclient.add_metric(self.unique_metrics)
        self.unique_metrics = []
class ZaggSender(object):
    """
    collect and create UniqueMetrics and send them to Zagg
    """

    def __init__(self, host=None, zagg_connection=None):
        """
        set up the zagg client, pcp_metrics and unique_metrics
        """
        self.unique_metrics = []
        self.config = None
        self.config_file = '/etc/openshift_tools/zagg_client.yaml'

        if not host:
            host = self.get_default_host()

        if not zagg_connection:
            zagg_connection = self.get_default_zagg_connecton()

        self.host = host
        self.zaggclient = ZaggClient(zagg_connection=zagg_connection)

    def parse_config(self):
        """ parse default config file """

        if not self.config:
            if not os.path.exists(self.config_file):
                raise ZaggSenderException(self.config_file + " does not exist.")
            self.config = yaml.load(file(self.config_file))

    def get_default_host(self):
        """ get the 'host' value from the config file """
        self.parse_config()

        return self.config['host']['name']

    def get_default_zagg_connecton(self):
        """ get the values and create a zagg_connection """

        self.parse_config()
        zagg_server = self.config['zagg']['url']
        zagg_user = self.config['zagg']['user']
        zagg_password = self.config['zagg']['pass']
        zagg_ssl_verify = self.config['zagg'].get('ssl_verify', False)

        if isinstance(zagg_ssl_verify, str):
            zagg_ssl_verify = (zagg_ssl_verify == 'True')

        zagg_connection = ZaggConnection(url=zagg_server,
                                         user=zagg_user,
                                         password=zagg_password,
                                         ssl_verify=zagg_ssl_verify,
                                        )

        return zagg_connection

    def add_pcp_metrics(self, pcp_metrics, host=None):
        """
        Evaluate a list of metrics from pcp using pminfo
        return list of  UniqueMetrics
        """
        if not host:
            host = self.host

        pcp_metric_dict = pminfo.get_metrics(pcp_metrics)

        for metric, value in pcp_metric_dict.iteritems():
            new_unique_metric = UniqueMetric(host, metric, value)
            self.unique_metrics.append(new_unique_metric)

    def add_heartbeat(self, heartbeat, host=None):
        """ create a heartbeat unique metric to send to zagg """

        if not host:
            host = self.host

        hb_metric = UniqueMetric.create_heartbeat(host,
                                                  heartbeat.templates,
                                                  heartbeat.hostgroups,
                                                 )
        self.unique_metrics.append(hb_metric)

    def add_zabbix_keys(self, zabbix_keys, host=None):
        """ create unique metric from zabbix key value pair """

        if not host:
            host = self.host

        zabbix_metrics = []

        for key, value in zabbix_keys.iteritems():
            zabbix_metric = UniqueMetric(host, key, value)
            zabbix_metrics.append(zabbix_metric)

        self.unique_metrics += zabbix_metrics

    def send_metrics(self):
        """
        Send list of Unique Metrics to Zagg
        clear self.unique_metrics
        """
        self.zaggclient.add_metric(self.unique_metrics)
        self.unique_metrics = []
class ZaggSender(object):
    """
    collect and create UniqueMetrics and send them to Zagg
    """

    def __init__(self, host=None, zagg_connection=None, verbose=False):
        """
        set up the zagg client, pcp_metrics and unique_metrics
        """
        self.unique_metrics = []
        self.config = None
        self.config_file = '/etc/openshift_tools/zagg_client.yaml'
        self.verbose = verbose

        if not host:
            host = self.get_default_host()

        if not zagg_connection:
            zagg_connection = self.get_default_zagg_connecton()

        self.host = host
        self.zaggclient = ZaggClient(zagg_connection=zagg_connection)

    def parse_config(self):
        """ parse default config file """

        if not self.config:
            if not os.path.exists(self.config_file):
                raise ZaggSenderException(self.config_file + " does not exist.")
            self.config = yaml.load(file(self.config_file))

    def get_default_host(self):
        """ get the 'host' value from the config file """
        self.parse_config()

        return self.config['host']['name']

    def get_default_zagg_connecton(self):
        """ get the values and create a zagg_connection """

        self.parse_config()
        zagg_server = self.config['zagg']['url']
        zagg_user = self.config['zagg']['user']
        zagg_password = self.config['zagg']['pass']
        zagg_ssl_verify = self.config['zagg'].get('ssl_verify', False)
        zagg_verbose = self.config['zagg'].get('verbose', False)

        if isinstance(zagg_ssl_verify, str):
            zagg_ssl_verify = (zagg_ssl_verify == 'True')

        if self.verbose:
            zagg_verbose = self.verbose
        elif isinstance(zagg_verbose, str):
            zagg_verbose = (zagg_verbose == 'True')

        zagg_connection = ZaggConnection(url=zagg_server,
                                         user=zagg_user,
                                         password=zagg_password,
                                         ssl_verify=zagg_ssl_verify,
                                         verbose=zagg_verbose,
                                        )

        return zagg_connection

    def add_pcp_metrics(self, pcp_metrics, host=None):
        """
        Evaluate a list of metrics from pcp using pminfo
        return list of  UniqueMetrics
        """
        if not host:
            host = self.host

        pcp_metric_dict = pminfo.get_metrics(pcp_metrics)

        for metric, value in pcp_metric_dict.iteritems():
            new_unique_metric = UniqueMetric(host, metric, value)
            self.unique_metrics.append(new_unique_metric)

    def add_heartbeat(self, heartbeat, host=None):
        """ create a heartbeat unique metric to send to zagg """

        if not host:
            host = self.host

        hb_metric = UniqueMetric.create_heartbeat(host,
                                                  heartbeat.templates,
                                                  heartbeat.hostgroups,
                                                 )
        self.unique_metrics.append(hb_metric)

    def add_zabbix_keys(self, zabbix_keys, host=None):
        """ create unique metric from zabbix key value pair """

        if not host:
            host = self.host

        zabbix_metrics = []

        for key, value in zabbix_keys.iteritems():
            zabbix_metric = UniqueMetric(host, key, value)
            zabbix_metrics.append(zabbix_metric)

        self.unique_metrics += zabbix_metrics

    def add_zabbix_dynamic_item(self, discovery_key, macro_string, macro_array, host=None):
        """
        This creates a dynamic item prototype that is required
        for low level discovery rules in Zabbix.
        This requires:
        - dicovery key
        - macro string
        - macro name

        This will create a zabbix key value pair that looks like:

        disovery_key = "{"data": [
                          {"{#macro_string}":"macro_array[0]"},
                          {"{#macro_string}":"macro_array[1]"},
                        ]}"
        """

        if not host:
            host = self.host

        data_array = [{'{%s}' % macro_string : i} for i in macro_array]
        json_data = json.dumps({'data' : data_array})

        zabbix_dynamic_item = UniqueMetric(host, discovery_key, json_data)

        self.unique_metrics.append(zabbix_dynamic_item)

    def send_metrics(self):
        """
        Send list of Unique Metrics to Zagg
        clear self.unique_metrics
        """
        self.zaggclient.add_metric(self.unique_metrics)
        self.unique_metrics = []