forked from paleg/bareos_zabbix_integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.py
executable file
·145 lines (118 loc) · 4.67 KB
/
notify.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
import sys
import subprocess
import re
import os
import argparse
from argparse import RawTextHelpFormatter
import logging
import smtplib
from email.mime.text import MIMEText
# Settings
from conf import conf
def sendmail(jname, jtype, jlevel, jexit_code, jmsg, recipients):
subject = "Bareos: {0} {1} of {2} {3}".format(jlevel, jtype, jname, jexit_code)
logging.debug( "sending email ({0}) to '{1}'".format(subject, recipients) )
msg = MIMEText(jmsg)
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 Bareos reports to Zabbix.
Should be used in Bareos-dir config instead of mail command:
mail = <admin@localhost> = all, !skipped
mailcommand = "{0} '%n' '%t' '%l' '%e' [--recipients '%r'] [--email-on-fail] [--email-on-success]"
Hostnames in Zabbix and Bareos must correspond
""".format(os.path.realpath(__file__))
)
parser.add_argument('job_name', help='job name (%%n)')
parser.add_argument('job_type', help='job type (%%t)')
parser.add_argument('job_level', help='job level (%%l)')
parser.add_argument('job_exit_code', help='job exit code (%%e)')
parser.add_argument('--recipients',
action='store',
type=lambda x: x.split(),
default=[],
help='space-separated list of report recipients (%%r)')
parser.add_argument('--email-on-fail',
action='store_true',
help='Send email about failed jobs to recipients')
parser.add_argument('--email-on-success',
action='store_true',
help='Send email about successed jobs to recipients')
args = parser.parse_args()
# Define how to get values from input
tests = (
("\s*FD Files Written:\s+([0-9]+)\s*",
"{0}.fd_fileswritten".format(conf['type']),
lambda x: x.group(1)),
("\s*SD Files Written:\s+([0-9]+)\s*",
"{0}.sd_fileswritten".format(conf['type']),
lambda x: x.group(1)),
("\s*FD Bytes Written:\s+([0-9][,0-9]*)\s+\(.*\)\s*",
"{0}.fd_byteswritten".format(conf['type']),
lambda x: x.group(1).translate(None, ",")),
("\s*SD Bytes Written:\s+([0-9][,0-9]*)\.*",
"{0}.sd_byteswritten".format(conf['type']),
lambda x: x.group(1).translate(None, ",")),
("\s*Last Volume Bytes:\s+([0-9][,0-9]*).*",
"{0}.lastvolumebytes".format(conf['type']),
lambda x: x.group(1).translate(None, ",")),
("\s*Files Examined:\s+([0-9][,0-9]*)\s*",
"{0}.verify_filesexamined".format(conf['type']),
lambda x: x.group(1).translate(None, ",")),
("\s*Non-fatal FD errors:\s+([0-9]+)\s*",
"{0}.fd_errors_non_fatal".format(conf['type']),
lambda x: x.group(1)),
("\s*SD Errors:\s+([0-9]+)\s*",
"{0}.sd_errors".format(conf['type']),
lambda x: x.group(1))
)
result = {}
in_msg = ""
# Get values from input
for line in sys.stdin.readlines():
in_msg += line
for regexp, key, value in tests:
match = re.match(regexp, line)
if match:
# DEBUG
logging.debug(line)
result[key] = value(match)
continue
if not result:
# TODO: send email?
logging.info("It is not a message about job")
exit(0)
result['{0}.job_exit_code'.format(conf['type'])] = args.job_exit_code
logging.debug(repr(in_msg))
# DEBUG
logging.debug(repr(result))
metrics = []
for key, value in result.items():
metrics+=conf['hostname']
metrics+=' "' + key + '[' + args.job_name + ']"'
metrics+=' ' + value + '\n'
# Send result to zabbix
logging.info( "sending metrics to '{0}': '{1}'".format(conf['zabbix_server'], metrics) )
zs = 'zabbix_sender -vv -z ' + "{0}".format(conf['zabbix_server']) + ' -i -'
p = subprocess.Popen(zs.split(), stdout=subprocess.PIPE, stdin=subprocess.PIPE)
stdoutdata, stderrdata = p.communicate(metrics)
logging.info( stdoutdata )
logging.debug( stderrdata )
# Send emails (if requested)
if (args.recipients and
((args.job_exit_code == 'OK' and args.email_on_success) or
(args.job_exit_code != 'OK' and args.email_on_fail))):
sendmail(args.job_name, args.job_type, args.job_level, args.job_exit_code, in_msg, args.recipients)