forked from domogik/domogik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagnostic.py
executable file
·306 lines (240 loc) · 9 KB
/
diagnostic.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Diagnostic script for Domogik
#
import tempfile
import traceback
import os
import linux_metrics
import socket
FILE=os.path.join(tempfile.gettempdir(), "domogik_diagnostic.log")
# LOG FUNCTIONS
#######################################################################################################
is_warning = False
is_error = False
def title(msg):
print(u"===========================================================")
print(u" {0}".format(msg))
print(u"===========================================================")
def ok(msg):
print(u"OK : {0}".format(msg))
def info(msg):
print(u"INFO : {0}".format(msg))
def warning(msg):
print(u"WARNING : {0}".format(msg))
is_warning = True
def error(msg):
print(u"ERROR : {0}".format(msg))
is_error = True
def solution(msg):
print(u"SOLUTION : {0}".format(msg))
# SERVER RELATED FUNCTIONS
#######################################################################################################
def get_current_metrics():
info("System informations :")
cpu_data = linux_metrics.cpu_stat.cpu_info()
info("- cpu - model : {0}".format(cpu_data['model name']))
info("- cpu - num processors : {0}".format(cpu_data['processor_count']))
info("- cpu - num cores : {0}".format(cpu_data['cpu cores']))
num_cores = cpu_data['cpu cores']
info("System usage :")
cpu_info = linux_metrics.cpu_stat.cpu_percents()
info("- cpu (idle) : {0}".format(cpu_info['idle']))
info("- cpu (system) : {0}".format(cpu_info['system']))
info("- cpu (user) : {0}".format(cpu_info['user']))
info("- cpu (iowait) : {0}".format(cpu_info['iowait']))
load_avg = linux_metrics.cpu_stat.load_avg()
info("- load average : {0} / {1} / {2}".format(load_avg[0], load_avg[1], load_avg[2]))
load_warn = False
if load_avg[0] > num_cores:
warning("The system load is important")
load_warn = True
if not load_warn and load_avg[1] > num_cores:
warning("The system load is important")
load_warn = True
if not load_warn and load_avg[2] > num_cores:
warning("The system load is important")
load_warn = True
if not load_warn:
ok("System load is ok")
mem = linux_metrics.mem_stat.mem_stats()
used, total, _, _, _, _ = mem
used = used/(1024*1024)
total = total/(1024*1024)
info("- memory - total (Mb) : {0}".format(total))
info("- memory - used (Mb) : {0}".format(used))
if total < 900:
error("Domogik needs at least 1Gb of memory on the system to run")
elif total < 1800:
warning("Your system have less than 2Gb of memory. Depending of what is already installed on your server, it may not be enough")
else:
ok("There is enough memory on the system (if others applications does not use all of it!)")
if total - used < 100:
warning("There is less than 100Mb of memory free.")
# UTIL FUNCTIONS
#######################################################################################################
def get_install_path():
try:
import domogik
path = domogik.__path__[0]
getParentFolder = lambda fh: os.path.abspath(os.path.join(os.path.normpath(fh),os.path.pardir))
pp = getParentFolder(getParentFolder(path))
info(u"Install path : {0}".format(pp))
return pp
except:
error("Error while getting domogik module path : {0}".format(traceback.format_exc()))
return None
# BASIC TESTS
#######################################################################################################
def test_import_domogik():
try:
import domogik
ver = domogik.__version__
ok("Domogik module can be imported (version is {0})".format(ver))
return True
except:
error("Error while importing domogik module")
solution("Make sure the Domogik installation is OK")
return False
def test_config_files():
# TODO : /etc/default
### /etc/domogik/domogik.cfg
from domogik.common.configloader import Loader, CONFIG_FILE
try:
cfg = Loader('domogik')
config = cfg.load()
conf = dict(config[1])
ok("Configuration file '{0}' can be read".format(CONFIG_FILE))
return True
except:
error(u"Error while reading the configuration file '{0}' : {1}".format(CONFIG_FILE, traceback.format_exc()))
solution("Make sure the Domogik installation is OK")
return False
def test_free_disk_space():
# TODO
pass
def test_total_memory():
# TODO
pass
def test_load_average():
# TODO
pass
def test_grants():
# TODO
# - get the domogik user
# - check all files have the grants
# - check /var/log/domogik
# - check /var/run/domogik
pass
# PORT TESTS
#######################################################################################################
def test_ports_are_open():
# TODO
#print(test_a_port("192.68.1.10", 40406))
pass
def test_a_port(ip, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((ip, port))
if result == 0:
return True
else:
return False
# XPL TEST
#######################################################################################################
def is_hub_alive():
# TODO
pass
def send_xpl_and_get_it():
# TODO :
# - launch dmg_dump in a thread for 30 seconds and log in a file
# - use dmg_send to send a xpl message
# - check in dmg_dump logs id the xpl message was sent
pass
# MQ TESTS - GLOBAL
#######################################################################################################
def test_mq_mmi_services():
# Test the broker services
import zmq
from zmq.eventloop.ioloop import IOLoop
from domogikmq.reqrep.client import MQSyncReq
from domogikmq.message import MQMessage
cli = MQSyncReq(zmq.Context())
info("Looking for MQ mmi services (broker)")
res = cli.rawrequest('mmi.services', '', timeout=10)
if res == None:
error("No response from MQ mmi.services request : MQ req/rep is KO")
return
info("MQ mmi services response is : {0}".format(res))
components = res[0].replace(" ", "").split(",")
for core in ['manager', 'dbmgr', 'admin', 'scenario', 'butler', 'xplgw']:
if core in components:
ok("MQ rep/req service OK for component : {0}".format(core))
else:
error("MQ rep/req service KO for component : {0}".format(core))
def test_mq_pubsub():
# TODO
# - subscribe
# - publish
# - check we got the MQ msg
info("If this is the last line displayed, there is an issue with MQ pub/sub : no plugin.status message is received from Domogik. If so, please check Domogik is up and runnin before launching the diagnostic script")
import zmq
from domogikmq.pubsub.subscriber import MQSyncSub
# test subscription is OK
class TestMQSub(MQSyncSub):
def __init__(self):
MQSyncSub.__init__(self, zmq.Context(), 'diag', ['plugin.status'])
try:
msg = self.wait_for_event()
if "content" in msg:
ok("MQ : plugin.status message received. MS pub/sub is working")
else:
warning("MQ : plugin.status message received but the content is not good")
except:
error("Error while waiting for a domogik component MQ status message. Error is : {0}".format(traceback.format_exc()))
t = TestMQSub()
def test_reqrep():
# TODO :
# - in a thread wait for some req and do the rep
# - do the req
# - chekc we get the rep
pass
# MQ TESTS - SOME DOMOGIK MESSAGE WHILE DOMOGIK IS RUNNING
#######################################################################################################
def test_clients_list():
# TODO
# request the clients list and check core are not dead
pass
# ADVANCED TESTS
#######################################################################################################
def test_plugin():
# TODO
# - install the domogik-plugin-test
# - launch its tests
pass
# LAUNCH TESTS
#######################################################################################################
if __name__ == "__main__":
# System informations
title("System informations")
get_current_metrics()
# Domogik package and configuration
title("Domogik installations checks")
test_import_domogik()
get_install_path()
test_config_files()
# Ports
title("Domogik web services up ?")
test_ports_are_open()
# MQ
title("Domogik Message Queue (MQ) checks")
test_mq_mmi_services() # test broker services
test_mq_pubsub()
# Summary
title("Summary")
if is_warning:
print(u"There were some warnings !")
if is_error:
print(u"There were some errors !")
if not is_warning and not is_error:
print(u"All seems OK")