-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinger.py
87 lines (70 loc) · 2.03 KB
/
pinger.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
#! /usr/bin/python
"""
@fileoverview Ping message sending script
@author David Parlevliet
@version 20130305
@preserve Copyright 2013 David Parlevliet.
Pinger
======
Sends ping messages to all known Elastic Firewall servers
"""
import os
import sys
import json
import socket
import re
import time
from ext.encryption import Encrypt
log_path = '/var/log/elastic-firewall/pinger.log'
debug = True
def log(output):
output = "[%s] %s" % (time.ctime(), output)
if debug:
print output
open(log_path, 'a').write("\n%s" % output)
def ping(ip, port, salt, api):
log("Pinging: %s:%s" % (ip, port))
command = {
"api_key": api,
"area": "ping"
}
try:
client_sock = socket.socket()
client_sock.connect((ip, port))
client_sock.send(Encrypt(json.dumps(command), salt))
client_sock.close()
except:
log("Connection refused")
def main():
try:
config = json.loads(open('/usr/local/share/elastic-firewall/config.json').read())
except Exception, e:
log("Unable to load config: %s" % e)
return 1
# I hate exec. Keep an eye out for better solutions to this
exec "from api.%s import Api" % config['server_group']
try:
api = Api()
for key in config[config['server_group']]:
setattr(api, key, config[config['server_group']][key])
api.grab_servers()
except Exception, e:
log("Error: %s" % e)
return 1
hostname = socket.gethostname()
for c_hostname in config['hostnames']:
if not re.match(c_hostname, hostname):
continue
log('Config found at: %s' % c_hostname)
server_rules = config['hostnames'][c_hostname]
if 'ping' in server_rules:
for server in server_rules['ping']:
for c_hostname in config['hostnames']:
if not re.match(c_hostname, server):
continue
receiver_rules = config['hostnames'][c_hostname]
for ip in api.get_servers(server):
ping(ip, server_rules['server_port'], receiver_rules['bsalt'], receiver_rules['api_key'])
return 0
if __name__ == '__main__':
sys.exit(main())