Skip to content

Commit 3dd3c9c

Browse files
hirose31Ganglia Development Team
authored andcommitted
new module: diskpart
1 parent 80111af commit 3dd3c9c

3 files changed

Lines changed: 223 additions & 0 deletions

File tree

diskpart/README.mkdn

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
diskpart
2+
===============
3+
4+
python module for ganglia 3.1.
5+
6+
"diskpart" send metrics on disk pattition.
7+
8+
* total size [GB]
9+
* used size [GB]
10+
* total number of inode
11+
* used number of inode
12+
13+
## AUTHOR
14+
15+
HIROSE Masaaki <[email protected]>
16+

diskpart/conf.d/diskpart.conf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
modules {
2+
module {
3+
name = "diskpart"
4+
language = "python"
5+
}
6+
}
7+
8+
collection_group {
9+
collect_every = 20
10+
time_threshold = 90
11+
12+
metric {
13+
name = "diskpart-root-total"
14+
title = "total partition space: root"
15+
}
16+
metric {
17+
name = "diskpart-root-used"
18+
title = "partition space used: root"
19+
}
20+
metric {
21+
name = "diskpart-root-inode-total"
22+
title = "total number of inode: root"
23+
}
24+
metric {
25+
name = "diskpart-root-inode-used"
26+
title = "number of inode used: root"
27+
}
28+
}
29+
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import os
5+
import sys
6+
import threading
7+
import time
8+
9+
descriptors = list()
10+
mount_points = list()
11+
Desc_Skel = {}
12+
_Worker_Thread = None
13+
_Lock = threading.Lock() # synchronization lock
14+
15+
class UpdateMetricThread(threading.Thread):
16+
17+
def __init__(self, params):
18+
threading.Thread.__init__(self)
19+
self.running = False
20+
self.shuttingdown = False
21+
self.refresh_rate = 10
22+
if "refresh_rate" in params:
23+
self.refresh_rate = int(params["refresh_rate"])
24+
self.metric = {}
25+
26+
def shutdown(self):
27+
self.shuttingdown = True
28+
if not self.running:
29+
return
30+
self.join()
31+
32+
def run(self):
33+
self.running = True
34+
35+
while not self.shuttingdown:
36+
_Lock.acquire()
37+
self.update_metric()
38+
_Lock.release()
39+
time.sleep(self.refresh_rate)
40+
41+
self.running = False
42+
43+
def update_metric(self):
44+
for mtp in mount_points:
45+
#print >>sys.stderr, "mtp: ", mtp
46+
st = os.statvfs(mtp)
47+
if mtp == "/":
48+
part = "diskpart-root"
49+
else:
50+
part = "diskpart-" + mtp.replace('/', '_').lstrip('_')
51+
#print >>sys.stderr, "%u %u %u" % (st.f_blocks, st.f_bavail, st.f_bsize)
52+
self.metric[ part+"-total" ] = float(st.f_blocks * st.f_bsize) / 1024/1024/1024
53+
self.metric[ part+"-used" ] = float((st.f_blocks - st.f_bavail) * st.f_bsize) / 1024/1024/1024
54+
55+
self.metric[ part+"-inode-total" ] = st.f_files
56+
self.metric[ part+"-inode-used" ] = st.f_files - st.f_favail
57+
58+
59+
def metric_of(self, name):
60+
val = 0
61+
if name in self.metric:
62+
_Lock.acquire()
63+
val = self.metric[name]
64+
_Lock.release()
65+
return val
66+
67+
def is_remotefs(dev, type):
68+
if dev.find(":") >= 0:
69+
return True
70+
elif dev.startswith("//") and (type == "smbfs" or type == "cifs"):
71+
return True
72+
return False
73+
74+
def metric_init(params):
75+
global descriptors, Desc_Skel, _Worker_Thread, mount_points
76+
77+
print '[diskpart] diskpart'
78+
print params
79+
80+
# initialize skeleton of descriptors
81+
Desc_Skel = {
82+
'name' : 'XXX',
83+
'call_back' : metric_of,
84+
'time_max' : 60,
85+
'value_type' : 'float',
86+
'format' : '%.3f',
87+
'units' : 'GB',
88+
'slope' : 'both',
89+
'description' : 'XXX',
90+
'groups' : 'disk',
91+
}
92+
93+
if "refresh_rate" not in params:
94+
params["refresh_rate"] = 10
95+
96+
# IP:HOSTNAME
97+
if "spoof_host" in params:
98+
Desc_Skel["spoof_host"] = params["spoof_host"]
99+
100+
f = open("/proc/mounts", "r")
101+
# 0 1 2 3
102+
# /dev/sda4 /home ext3 rw,relatime,errors=continue,data=writeback 0 0
103+
for l in f:
104+
(dev, mtp, fstype, opt) = l.split(None, 3)
105+
if is_remotefs(dev, fstype):
106+
continue
107+
elif opt.startswith('ro'):
108+
continue
109+
elif not dev.startswith('/dev/') \
110+
and not (mtp == "/" and fstype == "tmpfs"): # for netboot
111+
continue;
112+
113+
if mtp == "/":
114+
part = "diskpart-root"
115+
else:
116+
part = "diskpart-" + mtp.replace('/', '_').lstrip('_')
117+
#print >>sys.stderr, "dev=%s mount_point=%s part=%s" % (dev, mtp, part)
118+
119+
descriptors.append(create_desc(Desc_Skel, {
120+
"name" : part + "-total",
121+
"description": "total partition space",
122+
}))
123+
descriptors.append(create_desc(Desc_Skel, {
124+
"name" : part + "-used",
125+
"description": "partition space used",
126+
}))
127+
128+
descriptors.append(create_desc(Desc_Skel, {
129+
"name" : part + "-inode-total",
130+
"description": "total number of inode",
131+
"value_type" : "uint",
132+
"format" : "%d",
133+
"units" : "inode",
134+
}))
135+
descriptors.append(create_desc(Desc_Skel, {
136+
"name" : part + "-inode-used",
137+
"description": "total number of inode used",
138+
"value_type" : "uint",
139+
"format" : "%d",
140+
"units" : "inode",
141+
}))
142+
143+
mount_points.append(mtp)
144+
145+
_Worker_Thread = UpdateMetricThread(params)
146+
_Worker_Thread.start()
147+
148+
return descriptors
149+
150+
def create_desc(skel, prop):
151+
d = skel.copy()
152+
for k,v in prop.iteritems():
153+
d[k] = v
154+
return d
155+
156+
def metric_of(name):
157+
return _Worker_Thread.metric_of(name)
158+
159+
def metric_cleanup():
160+
_Worker_Thread.shutdown()
161+
162+
if __name__ == '__main__':
163+
try:
164+
params = {
165+
}
166+
metric_init(params)
167+
while True:
168+
for d in descriptors:
169+
v = d['call_back'](d['name'])
170+
print ('value for %s is '+d['format']) % (d['name'], v)
171+
time.sleep(5)
172+
except KeyboardInterrupt:
173+
time.sleep(0.2)
174+
os._exit(1)
175+
except:
176+
print sys.exc_info()[0]
177+
raise
178+

0 commit comments

Comments
 (0)