-
Notifications
You must be signed in to change notification settings - Fork 1
/
mk_zope2
executable file
·192 lines (174 loc) · 6.28 KB
/
mk_zope2
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
#
# check-mk-agent-logwatch
# Call with -d for debug mode: colored output, no saving of status
import sys, os, re, time, psutil, urllib2
if sys.version_info < (2,6):
import socket
if '-d' in sys.argv[1:]:
tty_red = '\033[1;31m'
tty_green = '\033[1;32m'
tty_yellow = '\033[1;33m'
tty_blue = '\033[1;34m'
tty_normal = '\033[0m'
debug = True
else:
tty_red = ''
tty_green = ''
tty_yellow = ''
tty_blue = ''
tty_normal = ''
debug = False
status_dir = "."
config_dir = "." #TODO
print "<<<zope2>>>"
config_filename = status_dir + "/zope.cfg"
status_filename = status_dir + "/zope.state"
# structure of config file
# [instancename]
# conf = /opt/zope/parts/instance1/etc/zope.conf
# port = 8081
# secret = gjhfkukyuyk6
# ...
def read_config():
# TODO
return None
# structure of statusfile
# # ZOPE.CONF PORT
# /opt/zope/parts/instance1/etc/zope.conf|8081
# /opt/zope/parts/instance2/etc/zope.conf|8082
def read_status():
status = {}
for line in file(status_filename):
parts = line.split('|')
conffile = parts[0]
port = parts[1]
status[conffile] = int(port)
return status
def save_status(status):
f = file(status_filename, "w")
## if we want the possibility to have the instance status:Down
## we could keep the instance previous conf, and so writing
## the new status file only if staus.items() is not empty.
## This will permit to have the status:Down of the instance.
# if status.items():
for conffile, port in status.items():
f.write("%s|%d\n" % (conffile, port))
# rt.zps
def guess_zope_instances():
instances = set()
for p in psutil.get_process_list():
zconf = [arg for arg in p.cmdline if "zope.conf" in arg]
if zconf:
if zconf[0][0] == '/':
zconf = zconf[0]
else:
zconf = "%s/%s" % (p.getcwd(), zconf[0])
if os.path.exists(zconf):
instances.add(zconf)
return instances
# rt.zps
def load_zopeconf(conffile):
currentnode = None
muninsecret = None
portbase = 0
for line in file(conffile).readlines():
if "<http-server>" in line:
currentnode = 'http-server'
if currentnode == 'http-server' and line.split() and 'address' == line.split()[0]:
port = int(line.split()[-1].split(':')[-1])
if "<product-config munin.zope>" in line:
currentnode = 'munin.zope'
if currentnode == 'munin.zope' and line.split() and 'secret' == line.split()[0]:
muninsecret = line.split()[-1].strip()
if "</http-server>" in line:
currentnode = None
if "</product-config>" in line:
currentnode = None
if not currentnode and line.split() and 'port-base' == line.split()[0]:
portbase = int(line.split()[-1].split(':')[-1])
return dict(
port=port+portbase,
muninsecret=muninsecret,
)
def zope_monitor(conffile):
worst = 1
# TODO: assert (status[conffile].port == read_zopeconf(conffile).port)
config = load_zopeconf(conffile)
port = config['port']
muninsecret = config['muninsecret']
# munin.zope plugin
## Timeout, min limit 0.006s. Max limit 1s.
## If timeout is set to 0.005s it may fails every 5-10 times.
## If timeout is set lower thane 0.005s it may fails quite often.
## If timeout is set bigger then 1s it may generate spwaning of mk_zope2 processess
## 0.1 s it seems reasonable. YMMV!.
timeout = 0.3
outputtxt = ''
## Check Instance
plugin = 'zopeinstance'
url = 'http://localhost:%d/' % (port,)
try:
if sys.version_info > (2,6):
urllib2.urlopen(url, None, timeout).readlines()
else:
socket.setdefaulttimeout(timeout)
urllib2.urlopen(url, None ).readlines()
level = 'I'
pluginout = 'status:OK'
except:
level = 'C'
pluginout = 'status:CRIT'
color = {'C': tty_red, 'W': tty_yellow, 'I': tty_blue, '.': ''}[level]
outputtxt = "%s%s %s=%s%s\n" % (color, level, plugin, pluginout, tty_normal)
## Check munin.zope
for plugin in ('zopecache', 'zodbactivity',
'zopethreads', 'zopememory'):
url = 'http://localhost:%d/@@munin.zope.plugins/%s' % (port, plugin)
if muninsecret:
url = "%s?%s" % (url, muninsecret)
# print ">>> %s" % url
pluginout = []
try:
## timeout act as this level
if sys.version_info > (2,6):
lines = urllib2.urlopen(url, None, timeout).readlines()
else:
socket.setdefaulttimeout(timeout)
lines = urllib2.urlopen(url, None ).readlines()
for line in lines:
if len(line.split(':')) == 2:
k, v = line.split(':')
# print ">>> %s" % k
pluginout.append(line.strip())
level = 'I'
color = {'C': tty_red, 'W': tty_yellow, 'I': tty_blue, '.': ''}[level]
outputtxt += "%s%s %s=%s%s\n" % (color, level, plugin, ','.join(pluginout), tty_normal)
except:
## TODO: set the right level
level = '.'
color = tty_yellow
## TODO: set the right "missing_or_invalid_plugin" string for the correct server side parsing.
outputtxt += "%s%s %s=%s%s\n" % (color, level, plugin, 'missing_or_invalid_plugin:', tty_normal)
status[conffile] = port
print "[[[%s]]]" % '-'.join([x for x in conffile.split('/') if x not in ('opt','var','srv','etc','parts','zope.conf','')])
if worst > 0:
sys.stdout.write(outputtxt)
sys.stdout.flush()
try:
config = read_config()
except Exception, e:
print "CANNOT READ CONFIG FILE: %s" % e
sys.exit(1)
# Simply ignore errors in the status file. In case of a corrupted status file we simply begin
# with an empty status. That keeps the monitoring up and running - even if we might loose a
# message in the extreme case of a corrupted status file.
try:
status = read_status()
except Exception, e:
status = {}
for zconf in guess_zope_instances():
zope_monitor(zconf)
if not debug:
save_status(status)