Skip to content

Commit b17634a

Browse files
corey-hammertontimvaillancourt
authored andcommitted
Feature: Add Zabbix Server notification support (#264)
* Feature: Add Zabbix Server notification support * conf: Adding a note about the option notify.zabbix.use_config
1 parent cedcff7 commit b17634a

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Features
3434
- Rsync (over SSH) secure backup uploads (*optional*)
3535
- `Nagios NSCA <https://sourceforge.net/p/nagios/nsca>`__ push
3636
notification support (*optional*)
37+
- `Zabbix <https://www.zabbix.com/>`__ sender notification support (*optional*)
3738
- Modular backup, archiving, upload and notification components
3839
- Support for MongoDB Authentication and SSL database connections
3940
- Support for Read Preference Tags for selecting specific nodes for backup

conf/mongodb-consistent-backup.example.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ production:
6060
# password: [password]
6161
# check_host: [nagios host]
6262
# check_name: [nagios check name]
63+
# zabbix:
64+
# use_config: true (If zabbix-agent is installed, use its config)
65+
# server: [zabbix host] (unused if use_config == true. default: 127.0.0.1)
66+
# port: [zabbix port] (unused if use_config == true. default: 10051)
67+
# node: [zabbix node display name] (default: node hostname)
68+
# key: [zabbix item key]
6369
upload:
6470
method: none
6571
# remove_uploaded: [true|false] (default: false)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import socket
2+
3+
4+
def config(parser):
5+
parser.add_argument("--notify.zabbix.use_config", dest="notify.zabbix.use_config",
6+
help="Use Zabbix Agent configuration (default: True)",
7+
default=True, choices=[True, False])
8+
parser.add_argument("--notify.zabbix.server", dest="notify.zabbix.server",
9+
help="Zabbix Server hostname/ip address, not used if notify.zabbix.use_config is True (default: none)",
10+
default='127.0.0.1', type=str)
11+
parser.add_argument("--notify.zabbix.port", dest="notify.zabbix.port",
12+
help="Zabbix Server port, not used if notify.zabbix.use_config is True (default: none)",
13+
default=10051, type=int)
14+
parser.add_argument("--notify.zabbix.key", dest="notify.zabbix.key",
15+
help="Zabbix Server item key", default=None, type=str)
16+
parser.add_argument("--notify.zabbix.node", dest="notify.zabbix.node",
17+
help="Node name monitored by Zabbix Server (default: nodename)",
18+
default=socket.gethostname(), type=str)
19+
return parser

mongodb_consistent_backup/Notify/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33

44
def config(parser):
5-
parser.add_argument("--notify.method", dest="notify.method", help="Notifier method (default: none)", default='none', choices=['nsca', 'none'])
5+
parser.add_argument("--notify.method", dest="notify.method", help="Notifier method (default: none)", default='none', choices=['nsca', 'zabbix', 'none'])
66
return parser

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ python-dateutil==2.6.1
99
yconf==0.3.4
1010
google_compute_engine==2.7.2
1111
progress==1.3
12+
py-zabbix==1.1.3

0 commit comments

Comments
 (0)