-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpywatchd
More file actions
executable file
·79 lines (57 loc) · 1.96 KB
/
Copy pathpywatchd
File metadata and controls
executable file
·79 lines (57 loc) · 1.96 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
from watchd.metrics import aggregated_elb
from watchd.server import socket_server
from watchd import datastore
import yaml
import datetime
import time
import traceback
import os
collectd_types = """
aggregated_metric average:GAUGE:0:U sigma:GAUGE:0:U one_tenth:GAUGE:0:U five_mins:GAUGE:0:U
aggregated_elb average:GAUGE:0:U sigma:GAUGE:0:U one_tenth:GAUGE:0:U five_mins:GAUGE:0:U out:GAUGE:0:U count:GAUGE:0:U
"""
if __name__ == '__main__' :
if len(os.sys.argv) == 2 and os.sys.argv[1] == "--collectd" :
print collectd_types
os.sys.exit(0)
if len(os.sys.argv) != 1 :
print "Usage: %s [--collectd]" % os.sys.argv[0].split('.')[-1]
os.sys.exit(2)
pidfile = "/var/run/watchd.pid"
if os.path.isfile(pidfile) :
print "PID file exists, another watchd instance is likely running"
os.sys.exit(1)
newpid = os.fork()
if newpid :
with open( pidfile , 'w' ) as fd :
fd.write( "%d\n" % newpid )
os.sys.exit(0)
state = { 'serving':True }
server = socket_server(state)
server.start()
config = 'watchd.yml'
if not os.path.isfile( config ) :
config = os.path.join( '/etc' , config )
with open( config ) as fd :
config = yaml.load( fd )
state['metrics'] = []
for name in config :
state['metrics'].append( aggregated_elb(name, config) )
sock = datastore.collectd()
while state['serving'] :
for metric in state['metrics'] :
sock = datastore.collectd()
try :
metric.update( sock )
metric.check_thresholds()
except Exception , ex :
os.sys.stdout.write( "Exception happened at %s\n%s\n" %( datetime.datetime.now() , ex ) )
traceback.print_exc()
os.sys.stdout.write( "\n" )
os.sys.stdout.flush()
with open('/var/lib/nagios3/rw/nagios.cmd', 'a+') as fd :
fd.write( "[%d] PROCESS_SERVICE_CHECK_RESULT;admin11;watchd;0;watchd OK - service running\n" % time.time() )
time.sleep(60)
server.close()
os.unlink( pidfile )