forked from selivan/bacula_zabbix_integration
-
Notifications
You must be signed in to change notification settings - Fork 5
/
notify_operator.py
executable file
·62 lines (50 loc) · 1.95 KB
/
notify_operator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
import sys
import os
import subprocess
import argparse
from argparse import RawTextHelpFormatter
import smtplib
import logging
from email.mime.text import MIMEText
from zbxsend import Metric, send_to_zabbix
# Settings
from conf import conf
def sendmail(msg, recipients):
subject = "{0}: message for operator".format(conf['type'].title())
logging.debug( "sending email ({0}) to '{1}'".format(subject, recipients) )
msg = MIMEText(msg)
msg['Subject'] = subject
msg['From'] = conf['email_from']
msg['To'] = ', '.join(recipients)
s = smtplib.SMTP( conf['email_server'] )
s.sendmail( conf['email_from'], recipients, msg.as_string() )
s.quit()
logging.basicConfig(
format=u'%(levelname)-8s [%(asctime)s] %(message)s',
level=logging.DEBUG,
filename=u'{0}/{1}.log'.format(conf['log_dir'], os.path.basename(__file__))
)
logging.info('sys.argv: ' + repr(sys.argv))
parser = argparse.ArgumentParser(
formatter_class=RawTextHelpFormatter,
description=
"""Simple script to send {1} operator reports to Zabbix.
Should be used in {1}-dir config instead of mail command:
mail = root@localhost,admin@domain = all, !skipped
operatorcommand = "{0} [--recipients '%r']"
Hostnames in Zabbix and {1} must correspond
""".format(os.path.realpath(__file__), conf['type'].title())
)
parser.add_argument('--recipients',
action='store',
type=lambda x: x.split(),
default=[],
help='space-separated list of report recipients (%%r)')
args = parser.parse_args()
msg = sys.stdin.read()
metrics = [ Metric(conf['hostname'], "{0}.custommessage".format(conf['type']), msg) ]
logging.info( "sending custom message to '{0}': '{1}'".format(conf['zabbix_server'], metrics) )
send_to_zabbix(metrics, conf['zabbix_server'], 10051, 20)
if args.recipients:
sendmail(msg, args.recipients)