|
| 1 | +import logging |
| 2 | +import sys |
| 3 | + |
| 4 | +from pyzabbix import ZabbixMetric, ZabbixSender |
| 5 | + |
| 6 | +from mongodb_consistent_backup.Errors import NotifyError, OperationError |
| 7 | +from mongodb_consistent_backup.Pipeline import Task |
| 8 | + |
| 9 | + |
| 10 | +class Zabbix(Task): |
| 11 | + def __init__(self, manager, config, timer, base_dir, backup_dir, **kwargs): |
| 12 | + super(Zabbix, self).__init__(self.__class__.__name__, manager, config, timer, base_dir, backup_dir, **kwargs) |
| 13 | + self.zabbix_server = self.config.notify.zabbix.server |
| 14 | + self.zabbix_port = self.config.notify.zabbix.port |
| 15 | + self.zabbix_use_config = self.config.notify.zabbix.use_config |
| 16 | + self.zabbix_key = self.config.notify.zabbix.key |
| 17 | + self.zabbix_nodename = self.config.notify.zabbix.node |
| 18 | + |
| 19 | + req_attrs = ['zabbix_key'] |
| 20 | + for attr in req_attrs: |
| 21 | + if not getattr(self, attr): |
| 22 | + raise OperationError('Zabbix notifier module requires attribute: %s!' % attr) |
| 23 | + |
| 24 | + try: |
| 25 | + self.notifier = ZabbixSender( |
| 26 | + use_config=self.zabbix_use_config, |
| 27 | + zabbix_server=self.zabbix_server, |
| 28 | + zabbix_port=self.zabbix_port, |
| 29 | + ) |
| 30 | + except Exception, e: |
| 31 | + logging.error("Error initiating ZabbixSender! Error: %s" % e) |
| 32 | + raise OperationError(e) |
| 33 | + |
| 34 | + def close(self): |
| 35 | + pass |
| 36 | + |
| 37 | + def run(self, ret_code): |
| 38 | + if self.notifier: |
| 39 | + logging.info("Sending Zabbix metric to item '%s:%s' to Zabbix Server" % ( |
| 40 | + self.zabbix_nodename, |
| 41 | + self.zabbix_key, |
| 42 | + )) |
| 43 | + logging.debug("Zabbix metric return code: '%s', item key: '%s', node name: '%s'" % (ret_code, self.zabbix_key, self.zabbix_nodename)) |
| 44 | + |
| 45 | + try: |
| 46 | + metrics = [ZabbixMetric(self.zabbix_nodename, self.zabbix_key, ret_code)] |
| 47 | + response = self.notifier.send(metrics) |
| 48 | + |
| 49 | + if response.failed > 0: |
| 50 | + raise NotifyError("%s metric(s) failed out of %s" % (response.failed, response.total)) |
| 51 | + except Exception, e: |
| 52 | + logging.error("Failed to send Zabbix metric to host" % (sys.exc_info()[1])) |
| 53 | + raise NotifyError(e) |
| 54 | + finally: |
| 55 | + logging.info("Zabbix report processed. Processed: %s, Failed: %s, Total: %s, Seconds spent: %s" % ( |
| 56 | + response.processed, |
| 57 | + response.failed, |
| 58 | + response.total, |
| 59 | + response.time |
| 60 | + )) |
0 commit comments