-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathproxy_monitor_restart.py
More file actions
54 lines (41 loc) · 1.51 KB
/
proxy_monitor_restart.py
File metadata and controls
54 lines (41 loc) · 1.51 KB
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
"""
Script to parse haproxy.cfg file and restart dead squid instances
"""
import re
import os
import time
import utils
server_re = re.compile(r'server\s+([a-zA-Z0-9]+)\s+(\d+\.\d+\.\d+\.\d+)\:(\d+)*')
network_test_cmd = 'nc %s %d -w 5 -zv 2>/dev/null'
squid_restart_cmd = 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ubuntu@%s "sudo squid3 -f /etc/squid3/squid.conf"'
def parse_config(filename='/etc/haproxy/haproxy.cfg'):
""" Parse HAproxy configuration file """
restarted = {}
for line in open(filename).readlines():
line = line.strip()
if line == '': continue
if line.startswith('server'):
# Get the server name out
match = server_re.match(line)
server_name, ip_address, port = match.groups()
# Test for access via nc
print ip_address, port
cmd = network_test_cmd % (ip_address, int(port))
if os.system(cmd) != 0:
# This squid instance is down
cmd = squid_restart_cmd % ip_address
print 'Restarting squid on',ip_address,'...'
if os.system(cmd) == 0:
restarted[ip_address] = 1
print 'Restarted',len(restarted),'squid instances.'
def main():
utils.daemonize('monitor.pid', logfile='monitor.log')
while True:
parse_config()
time.sleep(300)
if __name__ == "__main__":
import sys
if len(sys.argv)>1:
parse_config(sys.argv[1])
else:
main()