-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitoring.py
executable file
·66 lines (63 loc) · 1.59 KB
/
monitoring.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
#!/usr/bin/python
import socket
import time
import struct
import pickle
import urllib2
import json
class Graphite:
def __init__(self):
self.payload = []
self.hostname = socket.gethostname()
# set a couple sane defaults
self.set_port(2004)
self.set_prefix(self.hostname)
def set_port(self,port):
self.port = port
def set_prefix(self,prefix):
self.prefix = prefix
def add(self,key,val):
if key is None or val is None:
return False
key = "%s.%s" % (self.prefix,key)
self.payload.append((str(key), (self.now(), int(val))))
def now(self):
return int(time.time())
def get(self,t):
return getattr(self,t)
def flush(self):
self.payload = []
def send(self,host):
p = pickle.dumps(self.payload, protocol=2)
header = struct.pack("!L", len(p))
msg = header + p
sock = socket.socket()
sock.connect((host, self.port))
sock.sendall(msg)
sock.close()
self.flush()
class Nagios:
def __init__(self,apiKey):
self.payload = []
self.apiKey = apiKey
self.set_proto("http")
self.set_port("8000")
self.set_host("TARGETHOST")
self.set_uri("/api/alert")
def set_host(self,host):
self.host = host
def set_port(self,port):
self.port = port
def set_uri(self,uri):
self.uri = uri
def set_proto(self,protocol):
self.protocol = protocol
def add(self,key,comment,code):
self.payload.append({ "apiKey":self.apiKey, "status_code":code, "key":key, "comment":comment })
def flush(self):
self.payload = []
def send(self):
target = "%s://%s:%s%s" % (self.protocol,self.host,self.port,self.uri)
for d in self.payload:
urllib2.urlopen(target,json.dumps(d))
self.flush()