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
"""This module acts as a little framework for defining tags in a simple way.
Just define a string or a function and it will be introspected and used as a
tag value.
"""
import re
import os as os_
import platform as platform_
from multiprocessing import cpu_count
from socket import gethostname, gethostbyaddr
import psutil
from ccnode.tags import Tag, DynamicTags
def h():
"""Hostname tag."""
hn = gethostname()
return gethostbyaddr(hn)[0] or hn
h.ttl = 3600 * 24 # one day
# CPU related tags
def arch():
"""Hardware CPU architecture."""
return {
u'i386': u'x86',
u'i486': u'x86',
u'i586': u'x86',
u'i686': u'x86',
u'x86_64': u'x64',
u'': None,
}[platform_.machine()]
def cpu():
"""Number of CPU (core) on the host."""
try:
return unicode(cpu_count())
except NotImplementedError:
return None
def cpuuse():
"""CPU usage in percentage."""
return u'%.1f' % psutil.cpu_percent()
cpuuse.ttl = 5
# memory related tags
def mem():
"""Total physical memory available on system."""
return unicode(psutil.avail_phymem() + psutil.used_phymem())
mem.ttl = -1 # FIXME validate ttl
def memfree():
"""Available physical memory on system."""
return unicode(psutil.avail_phymem())
memfree.ttl = 60
def memused():
"""Used physical memory on system."""
return unicode(psutil.used_phymem())
memused.ttl = 60
# disks related tags
def disk():
"""List of disk devices on the host."""
disk_pattern = re.compile(r'[sh]d[a-z]+')
return u' '.join(d for d in os_.listdir(
'/sys/block/') if disk_pattern.match(d))
def _disk_tag_value(disk_name):
def size():
s = open(os_.path.join('/sys/block', disk_name, 'size')).read().strip()
try:
s = int(s)
if s > 0:
return s * 512
else:
return None
except ValueError:
return None
return size
class DynamicDisks(DynamicTags):
def __init__(self):
DynamicTags.__init__(self)
for d in disk().split():
self.tags.append(Tag(u'disk%s_size' % d, _disk_tag_value(d), 3600))
disks_size = DynamicDisks()
# other hardware related tags
def chaserial():
"""Blade chassis serial number."""
return open('/sys/class/dmi/id/chassis_serial').read().strip() or None
def chaasset():
"""Blade chassis asset tag."""
return open('/sys/class/dmi/id/chassis_asset_tag').read().strip() or None
def hmodel():
"""Host hardware model."""
return open('/sys/class/dmi/id/product_name').read().strip() or None
def hserial():
"""Host hardware serial number."""
return open('/sys/class/dmi/id/product_serial').read().strip() or None
def hvendor():
"""Host hardware vendor."""
return open('/sys/class/dmi/id/sys_vendor').read().strip() or None
def hbios():
"""Host BIOS version."""
return u'%s (%s)' % (
open('/sys/class/dmi/id/bios_version').read().strip() or None,
open('/sys/class/dmi/id/bios_date').read().strip() or None,
)
# Operating system related tags
def os():
"""Operating system (linux/windows)."""
return unicode(platform_.system().lower())
def platform():
"""Python platform.platform() info."""
return unicode(platform_.platform())
def uname():
"""As uname command (see python os.uname)."""
return u' '.join(os_.uname()) or None
def uptime():
"""Uptime of the system in seconds."""
return open('/proc/uptime').read().split()[0].split(u'.')[0] or None
uptime.ttl = 5
def load():
"""Average of the number of processes in the run queue over the last 1, 5
and 15 minutes."""
load_ = None
try:
load_ = u' '.join(unicode(l) for l in os_.getloadavg())
except OSError:
pass
return load_
load.ttl = 5