-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathmonit.py
More file actions
132 lines (110 loc) · 4.93 KB
/
monit.py
File metadata and controls
132 lines (110 loc) · 4.93 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# coding=utf-8
"""
Collect the monit stats and report on cpu/memory for monitored processes
#### Dependencies
* monit serving up /_status
"""
import urllib2
import base64
import sys
import ssl
from xml.dom.minidom import parseString
import diamond.collector
from diamond.collector import str_to_bool
class MonitCollector(diamond.collector.Collector):
def get_default_config_help(self):
config_help = super(MonitCollector, self).get_default_config_help()
config_help.update({
'send_totals': 'Send cpu and memory totals',
})
return config_help
def get_default_config(self):
"""
Returns the default collector settings
"""
config = super(MonitCollector, self).get_default_config()
config.update({
'host': '127.0.0.1',
'port': 2812,
'user': 'monit',
'passwd': 'monit',
'path': 'monit',
'byte_unit': ['byte'],
'send_totals': False,
'scheme': 'http',
'selfsigned': False,
})
return config
def collect(self):
url = '%s://%s:%i/_status?format=xml' % (self.config['scheme'],
self.config['host'],
int(self.config['port']))
if (
self.config['selfsigned']
# 0x020709f0 exactly equal python 2.7.9 final
and sys.hexversion >= 0x020709f0
and hasattr(ssl, '_create_unverified_context')
):
ssl._create_default_https_context = ssl._create_unverified_context
try:
request = urllib2.Request(url)
#
# shouldn't need to check this
base64string = base64.encodestring('%s:%s' % (
self.config['user'], self.config['passwd'])).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
response = urllib2.urlopen(request)
except urllib2.HTTPError, err:
self.log.error("%s: %s", err, url)
return
metrics = {}
try:
dom = parseString("".join(response.readlines()))
except:
self.log.error("Got an empty response from the monit server")
return
for svc in dom.getElementsByTagName('service'):
if int(svc.getAttribute('type')) == 3:
name = svc.getElementsByTagName('name')[0].firstChild.data
status = svc.getElementsByTagName('status')[0].firstChild.data
monitor = svc.getElementsByTagName(
'monitor')[0].firstChild.data
if status == '0' and monitor == '1':
try:
uptime = svc.getElementsByTagName(
'uptime')[0].firstChild.data
metrics["%s.uptime" % name] = uptime
cpu = svc.getElementsByTagName(
'cpu')[0].getElementsByTagName(
'percent')[0].firstChild.data
metrics["%s.cpu.percent" % name] = cpu
if str_to_bool(self.config['send_totals']):
cpu_total = svc.getElementsByTagName(
'cpu')[0].getElementsByTagName(
'percenttotal')[0].firstChild.data
metrics["%s.cpu.percent_total" % name] = cpu_total
mem = int(svc.getElementsByTagName(
'memory')[0].getElementsByTagName(
'kilobyte')[0].firstChild.data)
for unit in self.config['byte_unit']:
metrics["%s.memory.%s_usage" % (name, unit)] = (
diamond.convertor.binary.convert(
value=mem,
oldUnit='kilobyte',
newUnit=unit))
metrics["%s.uptime" % name] = uptime
if str_to_bool(self.config['send_totals']):
mem_total = int(svc.getElementsByTagName(
'memory')[0].getElementsByTagName(
'kilobytetotal')[0].firstChild.data)
for unit in self.config['byte_unit']:
metrics["%s.memory_total.%s_usage" % (
name, unit)] = (
diamond.convertor.binary.convert(
value=mem_total,
oldUnit='kilobyte',
newUnit=unit))
except:
pass
for key in metrics:
self.publish(key, metrics[key])