Newer
Older
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
import os
import re
import psutil
from subprocess import Popen, PIPE
from multiprocessing import cpu_count
from platform import platform, machine
from socket import gethostbyaddr, gethostname
class Host(object):
'''
'''
class LocalHost(Host):
'''
'''
ARCH = {
'i386' : 'x86',
'i486' : 'x86',
'i586' : 'x86',
'i686' : 'x86',
'x86_64' : 'x64',
}
def get_hw_serial(self):
serial = None
try:
data = open('/sys/class/dmi/id/product_serial').read().strip()
if data:
serial = data
except:
pass
return serial
def get_hw_vendor(self):
vendor = None
try:
data = open('/sys/class/dmi/id/sys_vendor').read().strip()
if data:
vendor = data
except:
pass
return vendor
def get_hw_product(self):
product = None
try:
data = open('/sys/class/dmi/id/product_name').read().strip()
if data:
product = data
except:
pass
return product
def get_hw_bios(self):
bios = ''
try:
bios_ver = open('/sys/class/dmi/id/bios_version').read().strip()
bios_date = open('/sys/class/dmi/id/bios_date').read().strip()
if bios_ver:
bios += bios_ver
if bios_date:
bios += ' (%s)' % bios_date
if not bios:
bios = None
except:
pass
return bios
def get_name(self):
result = None
try:
hostname = gethostname()
fqdn = gethostbyaddr(hostname)[0]
result = fqdn if fqdn else hostname
except:
pass
return result
def get_uname(self):
uname = None
try:
data = ' '.join(os.uname())
if data:
uname = data
except:
pass
return uname
def get_platform(self):
result = None
try:
p = platform()
if p:
result = p
except:
pass
return result
def get_uptime(self):
uptime = None
try:
data = open("/proc/uptime").read().split()
if data:
uptime = int(float(data[0]))
except:
pass
return uptime
def get_loadavg(self):
return ' '.join('%.2f' % load for load in os.getloadavg())
def get_arch(self):
arch = None
try:
a = machine()
if a in self.ARCH:
arch = ARCH[a]
except:
pass
return arch
def get_cpu(self):
return cpu_count()
def get_cpu_usage(self):
return '%.1f' % psutil.cpu_percent()
def get_mem(self):
return psutil.avail_phymem() + psutil.used_phymem()
def get_mem_free(self):
return psutil.avail_phymem()
def get_mem_used(self):
return psutil.used_phymem()
def get_disks(self):
disks = {}
try:
re_pattern = re.compile(r'([sh]d[a-z]+)')
found = [bd for bd in os.listdir('/sys/block/')
if re_pattern.match(bd)]
for disk in found:
fullname = os.path.join('/sys/block', disk, 'size')
size = int(open(fullname).read())
if size > 0:
disks[disk] = size * 512
except:
pass
return disks
def power_shutdown(self):
return self.execute('/sbin/shutdown -h -P 0')
def power_off(self):
return self.execute('/sbin/shutdown -h -P -n 0')
def power_reboot(self):
return self.execute('/sbin/shutdown -r -f 0')
def power_force_reboot(self):
return self.execute('/sbin/shutdown -r -n 0')
def execute(self, command):
#FIXME: stop using shell=true and parse arguments with shlex.split()
return Popen(command, shell=True,
bufsize=-1,
stdin=PIPE,
stdout=PIPE,
stderr=PIPE).communicate()
class Hypervisor(LocalHost):
'''
'''
def storage(self):
raise NotImplemented
def vm_list(self):
raise NotImplemented
def vm_list_running(self):
raise NotImplemented
def vm_list_stopped(self):
raise NotImplemented
def vm_list_paused(self):
raise NotImplemented
def vm_get(self, name):
raise NotImplemented
class VM(Host):
'''
'''
class Storage(object):
'''
'''
class StoragePool(object):
'''
'''
class StorageVolume(object):
'''
'''