forked from firaxis/pve-zsync-monitoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsync-zbx.py
executable file
·107 lines (95 loc) · 3.02 KB
/
zsync-zbx.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
#!/usr/bin/env python3
'''
Report pve-zsync jobs status to Zabbix.
'''
import json
import argparse
from datetime import datetime
from socket import getfqdn
from subprocess import call
class zsync(object):
def __init__(self, statefile):
self.statefile = statefile
self.replnames = []
self.replnames_json = []
self.hostname = getfqdn()
with open(self.statefile, "r") as config:
try:
self.replicas = json.load(config)
except ValueError as e:
print(e)
for source, names in self.replicas.items():
for name, config in names.items():
repl = {}
repl['{#REPLICA}'] = name
self.replnames.append(name)
self.replnames_json.append(repl)
def discover(self):
'''
Make a zabbix-compatible json with pve-zsync jobs names
'''
print(json.dumps(
{'data': self.replnames_json},
indent=4,
separators=(',', ':'))
)
def write_status(self):
'''
Write file with pve-zsync jobs stats
'''
with open('/tmp/zsync.status.dat', 'w') as statusfile:
for source, replica in self.replicas.items():
for replname, param in replica.items():
statusfile.write(
'{0} zsync.lsync[{1}] {2}\n'
.format(
self.hostname,
replname,
zsync.calculate_lag(param['lsync'])
)
)
statusfile.write(
'{0} zsync.state[{1}] {2}\n'
.format(
self.hostname,
replname,
param['state']
)
)
def send(self):
'''
Send data to zabbix trapper
'''
with open("/tmp/zsync-zbx.log", 'a') as log:
call([
'zabbix_sender',
'-c',
'/etc/zabbix/zabbix_agentd.conf',
'-i',
'/tmp/zsync.status.dat'],
stdout=log)
print("1")
def calculate_lag(lsync):
'''
Calculate time delta since last job run
'''
datefmt = "%Y-%m-%d_%H:%M:%S"
if lsync == 0:
return 0
else:
return (datetime.now() - datetime.strptime(lsync, datefmt)).seconds
parser = argparse.ArgumentParser()
parser.add_argument("--discover", action="store_true",
help="return a list of pve-zsync jobs "
"in zabbix discovery format")
parser.add_argument("--send", action="store_true",
help="send data via zabbix_sender to zabbix trapper")
args = parser.parse_args()
jobs = zsync(statefile = "sync_state")
if args.discover:
jobs.discover()
elif args.send:
jobs.write_status()
jobs.send()
else:
parser.print_help()