forked from LeastAuthority/leastauthority.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiservercheck.py
executable file
·72 lines (55 loc) · 2.31 KB
/
multiservercheck.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
#!/usr/bin/python
import sys, os
from cStringIO import StringIO
from twisted.python.filepath import FilePath
from twisted.python.failure import Failure
from twisted.internet import reactor
from lae_automation.config import Config
from lae_automation.monitor import check_servers, read_serverinfo, compare_servers_to_local, \
send_monitoring_report
from lae_automation.aws.queryapi import wait_for_EC2_properties, ServerInfoParser
endpoint_uri = 'https://ec2.us-east-1.amazonaws.com/'
config = Config()
ec2secretpath='../secret_config/ec2secret'
ec2accesskeyid = str(config.other['ec2_access_key_id'])
ec2secretkey = FilePath(ec2secretpath).getContent().strip()
serverinfocsvpath = '../serverinfo.csv'
lasterrorspath = '../lasterrors.txt'
monitor_privkey_path = str(config.other['monitor_privkey_path'])
stderr = StringIO()
serverinfotuple = read_serverinfo(serverinfocsvpath)
localstate = {}
for propertytuple in serverinfotuple:
(launch_time, instance_id, publichost, status) = propertytuple
localstate[instance_id] = (launch_time, publichost, status)
lasterrors = None
lasterrorsfp = FilePath(lasterrorspath)
if lasterrorsfp.exists():
lasterrors = lasterrorsfp.getContent()
POLL_TIME = 10
ADDRESS_WAIT_TIME = 60
d = wait_for_EC2_properties(ec2accesskeyid, ec2secretkey, endpoint_uri,
ServerInfoParser(('launchTime', 'instanceId'), ('dnsName', 'instanceState.name')),
POLL_TIME, ADDRESS_WAIT_TIME, sys.stdout, stderr)
d.addCallback(lambda remoteproperties: compare_servers_to_local(remoteproperties, localstate, sys.stdout, stderr))
d.addCallback(lambda host_list: check_servers(host_list, monitor_privkey_path, sys.stdout, stderr))
def cb(x):
if isinstance(x, Failure):
print >>stderr, str(x)
if hasattr(x.value, 'response'):
print >>stderr, x.value.response
errors = stderr.getvalue()
print >>sys.stderr, errors
if errors != lasterrors:
d2 = send_monitoring_report(errors)
def _sent(ign):
lasterrorsfp.setContent(errors)
raise Exception("Sent failure report.")
def _err(f):
print >>sys.stderr, str(f)
return f
d2.addCallbacks(_sent, _err)
return d2
d.addBoth(cb)
d.addCallbacks(lambda ign: os._exit(0), lambda ign: os._exit(1))
reactor.run()