-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_ehaelix.py
325 lines (297 loc) · 9.77 KB
/
stats_ehaelix.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""
Class eHaelix
"""
#!/usr/bin/python
import subprocess
import re
from jinja2 import Template, Environment, PackageLoader, FileSystemLoader
class Cmd(object):
"""
Class for execute via ssh a command
"""
def __init__(self, host):
self._host = host
def exec_command_host(self, command):
"""
Exec a command on a physical host
"""
process = subprocess.Popen(r'ssh %s "%s"' % (self._host, command),
stdout=subprocess.PIPE,
stderr=None, shell=True)
output = process.communicate()
return [line for line in output[0].split("\n") if line != '']
def exec_command_on_vz(self, vz_id, command):
"""
Exec a command on a VZ
"""
process = subprocess.Popen(r'ssh %s "vzctl exec %s \"%s\""' %
(self._host, vz_id, command),
stdout=subprocess.PIPE,
stderr=None,
shell=True)
output = process.communicate()
return [line for line in output[0].split("\n") if line != '']
class Ehaelix(object):
"""
Class for get several information about an eHaelix cluster
"""
def __init__(self, host):
self._host = host
self._cmd = Cmd(self._host)
def get_drbd_overview(self):
"""
Return get drbd-overview infos
"""
drbd_overview = {}
fields_name = [
'id_name',
'connection_state',
'role',
'disk_states',
'replication_protocol',
'io_flags',
'mount_point',
'filesystem',
'size',
'free',
'used',
'percent',
]
for line in self._cmd.exec_command_host('drbd-overview'):
_drbd_line = {}
for index, field in enumerate(line.split()):
_drbd_line[fields_name[index]] = field
# Get only name
drbd_name = _drbd_line['id_name'].split(':')[1]
drbd_overview[drbd_name] = _drbd_line
return drbd_overview
def get_vz_list(self):
"""
Return the VZ list of a physical host
"""
vzlist_raw = self._cmd.exec_command_host(
"vzlist -a -H -o ctid,hostname,status")
vztab = []
for vz in vzlist_raw:
vzinfo = vz.split()
if not vzinfo:
continue
vztab.append({
'id': vzinfo[0],
'hostname': vzinfo[1],
'status': vzinfo[2],
'physical_host': self._host,
})
return vztab
def get_cpu_info(self):
"""
Return CPU info of a physical machine
"""
command = "grep 'cpu MHz' /proc/cpuinfo"
#print command
cpus = self._cmd.exec_command_host(command)
cpu = cpus.pop().split()
result = {
'nb': len(cpus) + 1,
'mhz': cpu[3],
'unit': cpu[1],
}
return result
def get_cpu_info_vz(self, vz_id):
"""
Return CPU info of a VZ
"""
command = "grep 'cpu MHz' /proc/cpuinfo"
cpus = self._cmd.exec_command_on_vz(vz_id, command)
cpu = cpus.pop().split()
result = {
'nb': len(cpus) + 1,
'mhz': cpu[3],
'unit': cpu[1],
}
return result
def get_total_mem_info(self):
"""
Return memory info of a physical machine
"""
command = "grep 'MemTotal' /proc/meminfo"
infos = self._cmd.exec_command_host(command)
info = infos.pop().split()
result = {
'name': info[0],
'size': info[1],
'unit': info[2],
}
return result
def get_total_mem_info_vz(self, vz_id):
"""
Return memory info of a VZ
"""
command = "grep 'MemTotal' /proc/meminfo"
infos = self._cmd.exec_command_on_vz(vz_id, command)
info = infos.pop().split()
result = {
'name': info[0],
'size': info[1],
'unit': info[2],
}
return result
def get_vgs_infos(self):
"""
Return informations about VGs on a physical host
"""
lines_raw = self._cmd.exec_command_host("vgs --units G")
# delete first line
lines_raw = lines_raw[1:]
result = []
for line in lines_raw:
lineinfo = line.split()
result.append({
'name': lineinfo[0],
'size': lineinfo[5],
'free': lineinfo[6],
})
return result
def get_lvs_infos(self):
"""
Return informations about LVs on a physicial host
"""
lines_raw = self._cmd.exec_command_host("lvs --units G")
# delete first line
lines_raw = lines_raw[1:]
result = []
for line in lines_raw:
lineinfo = line.split()
result.append({
'name': lineinfo[0],
'vg': lineinfo[1],
'size': lineinfo[3],
})
return result
def get_mount_infos(self):
"""
Return mount information on a physical host
"""
lines_raw = self._cmd.exec_command_host("mount")
result = []
for line in lines_raw:
lineinfo = line.split()
result.append({
'device': lineinfo[0],
'mount': lineinfo[2],
'type': lineinfo[4],
})
return result
def get_hw_model(self):
"""
Return hw model
"""
lines_raw = self._cmd.exec_command_host('dmidecode -s system-manufacturer'
' && dmidecode -s system-product-name')
# One result by line so get therse lines content
result = {
'manufacturer': lines_raw[0].rstrip('\n'),
'product': lines_raw[1].rstrip('\n'),
}
return result
def get_kernel_version(self):
"""
Return kernel version
"""
lines_raw = self._cmd.exec_command_host('uname -r')
return lines_raw.pop().rstrip('\n')
def get_os_version(self):
"""
Return Operating system version (little debian friendly)
"""
lines_raw = self._cmd.exec_command_host('cat /etc/issue.net')
return lines_raw.pop().rstrip('\n')
def get_vz_os_version(self, vz_id):
"""
Return Operating system version (little debian friendly)
"""
lines_raw = self._cmd.exec_command_on_vz(vz_id, 'cat /etc/issue.net')
return lines_raw.pop().rstrip('\n')
def get_df_infos(self):
"""
Return space disk information on a physical host
"""
lines_raw = self._cmd.exec_command_host("df -hP")
# delete first line
lines_raw = lines_raw[1:]
result = []
for line in lines_raw:
lineinfo = line.split()
result.append({
'device': lineinfo[0],
'size': lineinfo[1],
'used': lineinfo[2],
'available': lineinfo[3],
'used_p': lineinfo[4],
'mount': lineinfo[5],
})
return result
def get_df_vz_infos(self, vz_id):
"""
Return disk space informations of a VZ
"""
lines_raw = self._cmd.exec_command_on_vz(vz_id, "df -hP")
# delete first line
lines_raw = lines_raw[1:]
result = []
for line in lines_raw:
lineinfo = line.split()
result.append({
'device': lineinfo[0],
'size': lineinfo[1],
'used': lineinfo[2],
'available': lineinfo[3],
'use%': lineinfo[4],
'mount': lineinfo[5],
})
return result
def get_vz_list_apps(self, vz_id, apps=[
"apache2-mpm-.*",
"nginx",
"tomcat[0-9]",
"varnish",
"postgresql-[0-9]\.[0-9]",
"mysql-server",
r"mariadb-server-[0-9]\.[0-9]",
"mongodb",
"php5-fpm", "php5", "zend-server-.*-php.*"]):
"""
Return an array of apps listed in 'apps' arg,
with status and version number
"""
command = "dpkg -l"
lines_raw = self._cmd.exec_command_on_vz(vz_id, command)
result = {}
for line in lines_raw:
lineinfo = line.split()
if len(lineinfo) >= 3:
for app in apps:
regexp = "^%s$" % (app)
match = re.match(regexp, lineinfo[1])
if match:
result[lineinfo[1]] = {
'name': lineinfo[1],
'status': lineinfo[0],
'version': lineinfo[2],
}
return result
#SRV = Ehaelix("eno-eh9-b2.mut-8.hosting.enovance.com")
#print SRV.get_cpu_info()
#templateLoader = FileSystemLoader( searchpath="./" )
#templateEnv = Environment( loader=templateLoader )
#TEMPLATE_FILE = "./templates/report.rst"
#template = templateEnv.get_template( TEMPLATE_FILE )
#outputText = template.render()
#
#VZLIST = SRV.get_vz_list()
#for vz in VZLIST:
# print vz['hostname']
# vzapps = SRV.get_vz_list_apps(vz['id'])
# print vzapps
# for name, vzapp in vzapps.iteritems():
# print "name: %s\t\tversion: %s" % (vzapp['name'], vzapp['version'])