-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.py
executable file
·98 lines (80 loc) · 2.52 KB
/
stat.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
#!/usr/bin/env python3
import socket
from configparser import ConfigParser
import os
from time import time
from pathlib import Path
from urllib.request import urlopen
import csv
workdir = Path(__file__).resolve().parent
config = ConfigParser()
config.read(workdir / 'haproxy-tools.cfg')
timestamp = int(time())
pidfile = workdir / 'haproxy-stat.pid'
# check what instance for this TLD not running on same host
def check_pid(pid):
try:
os.kill(int(pid), 0)
except OSError:
return False
else:
return True
if os.path.isfile(pidfile):
with open(pidfile, 'r') as pidfile_obj:
last_pid = pidfile_obj.read()
if check_pid(last_pid):
print(f"Already running: {last_pid}")
exit(1)
else:
with open(pidfile, 'w') as pidfile_obj:
pidfile_obj.write(str(os.getpid()))
else:
with open(pidfile, 'w') as pidfile_obj:
pidfile_obj.write(str(os.getpid()))
# read data from haproxy
with urlopen(config['haproxy']['url'], timeout=10) as stat_req:
s_lines = list()
for l in stat_req:
s_lines.append(l.decode('ASCII').strip('# '))
csv_obj = csv.DictReader(s_lines, delimiter=',')
data = [line for line in csv_obj]
proxy_up = 0
proxy_down = 0
for item in data:
if item['pxname'] == 'proxy':
if item['status'].startswith('UP'):
proxy_up += 1
elif item['status'].startswith('DOWN'):
proxy_down += 1
def get_general(g_item: str):
for i in data:
if i['pxname'] == 'stats' and i['svname'] == 'FRONTEND':
return i[g_item]
return 0
graphite_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send_graphite(item, value):
graphite_message = '%s %s %i\n' % (
config['graphite']['prefix'] + item,
str(value),
timestamp)
graphite_sock.sendto(
graphite_message.encode(),
(
config['graphite']['host'],
int(config['graphite']['port'])
)
)
for item in config['graphite']['general_values'].split(','):
send_graphite(item, get_general(item))
send_graphite('proxies.up', proxy_up)
send_graphite('proxies.down', proxy_down)
lastsess = list()
for i in data:
if i['pxname'] == 'proxy' and i['status'] == 'UP' and int(i['lastsess']) > 0:
lastsess.append(int(i['lastsess']))
if len(lastsess) > 1:
send_graphite('lastsess.min', min(lastsess))
send_graphite('lastsess.max', max(lastsess))
send_graphite('lastsess.avg', sum(lastsess) // len(lastsess))
graphite_sock.close()
pidfile.unlink()