def _send(self):
        """
        Take metrics from queue and send it to whale API
        """
        metrics = []

        while len(self.queue) > 0:
            metric = self.queue.popleft()

            path = '%s.%s' % (
                metric.getCollectorPath(),
                metric.getMetricPath()
            )
            timestamp, value, host = metric.timestamp, metric.value, metric.host
            metric_type = metric.metric_type.lower()

            logging.debug(
                "Sending.. metric[%s], value[%s], timestamp[%s]",
                path,
                value,
                timestamp
            )

            metrics.append({
                'metric': path,
                'host': host,
                'metrric_type': metric_type,
                'points': [
                    {'timestamp': converter.epoch_to_iso_8601(timestamp), 'value': value}
                ]
            })

        if len(metrics) > 0:
            self.api.Metric.send(metrics=metrics)
Example #2
0
    def handle(self):
        whaleapi.initialize(self.api_token(), self.api_host())

        timestamp = self.event.get('timestamp')
        hostname = self.event['client'].get('name')
        event_status = self.event['check'].get('status', 2)
        check = self.event['check'].get('name')
        text = self.event['check'].get('output')

        if timestamp is None or hostname is None or check is None or text is None:
            return self.bail('Could not send event to Whale Monitoring. Some data is missing.')

        whaleapi.api.Event.send(
            title='%s %s on %s' % (self.whale_alert_type(event_status).title(), check,
                                   str(hostname).lower()),
            text=text,
            timestamp=converter.epoch_to_iso_8601(timestamp),
            host=hostname,
            alert_type=self.whale_alert_type(event_status),
            aggregation_key=str('sensu.%s.%s' % (hostname, check)).lower(),
            source_type_name='sensu'
        )