This repository has been archived by the owner on Oct 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmonit.py
122 lines (107 loc) · 4.02 KB
/
monit.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
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
# -*- coding: utf-8 -*-
'''
Interface to the Monit system manager and monitor (http://mmonit.com/monit/)
Usage:
>>> mon = Monit(username='admin', password='monit')
>>> # mon is a dict:
>>> a_service_name = mon.keys()[0]
>>> mon[a_service_name].monitor() # other actions include start, stop, unmonitor
...
>>> mon[a_service_name].monitored # see also 'running', 'type', 'name'
True
'''
# monit.py
# Python to Monit HTTP interface
# Camilo Polymeris, 2015
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import requests
import time
class Monit(dict):
def __init__(self, host='localhost', port=2812, username=None, password='', https=False):
self.baseurl = (https and 'https://%s:%i' or 'http://%s:%i') % (host, port)
self.s = requests.Session()
self.s.auth = None
if username:
self.s.auth = requests.auth.HTTPBasicAuth(username, password)
self.update()
def update(self):
"""
Update Monit deamon and services status.
"""
url = self.baseurl + '/_status?format=xml'
response = self.s.get(url)
response.raise_for_status()
from xml.etree.ElementTree import XML
root = XML(response.text)
for serv_el in root.iter('service'):
serv = Monit.Service(self, serv_el)
self[serv.name] = serv
# Pendingaction occurs when a service is stopping
if self[serv.name].pendingaction:
time.sleep(1)
return Monit.update(self)
# Monitor == 2 when service in startup
if self[serv.name].monitorState == 2:
time.sleep(1)
return Monit.update(self)
class Service:
"""
Describes a Monit service, i.e. a monitored resource.
"""
def __init__(self, daemon, xml):
"""
Parse service from XML element.
"""
self.name = xml.find('name').text
self.type = {
0: 'filesystem',
1: 'directory',
2: 'file',
3: 'process',
4: 'connection',
5: 'system'
}.get(int(xml.attrib['type']), 'unknown')
self.daemon = daemon
self.running = None
if self.type != 'system':
if xml.find('pid') is not None:
self.running = True
else:
self.running = False
self.monitored = bool(int(xml.find('monitor').text))
self.pendingaction = bool(int(xml.find('pendingaction').text))
self.monitorState = int(xml.find('monitor').text)
def start(self):
self._action('start')
def restart(self):
self._action('restart')
def stop(self):
self._action('stop')
def monitor(self, monitor=True):
if not monitor:
return self.unmonitor()
self._action('monitor')
def unmonitor(self):
self._action('unmonitor')
def _action(self, action):
url = self.daemon.baseurl + '/' + self.name
if self.daemon.s.auth:
postdata = {'securitytoken': self.daemon.s.cookies['securitytoken'], 'action': action}
else:
postdata = {'action': action}
response = self.daemon.s.post(url, data=postdata)
response.raise_for_status()
self.daemon.update()
def __repr__(self):
repr = self.type.capitalize()
if not self.running is None:
repr += self.running and ', running' or ', stopped'
if not self.monitored is None:
repr += self.monitored and ', monitored' or ', not monitored'
return repr
if __name__ == "__main__":
import doctest
doctest.testmod()