Debian Codegraph @

gmond/python_modules/disk/multidisk.py

raw
1 #/*******************************************************************************
2 #* Portions Copyright (C) 2007 Novell, Inc. All rights reserved.
3 #*
4 #* Redistribution and use in source and binary forms, with or without
5 #* modification, are permitted provided that the following conditions are met:
6 #*
7 #* - Redistributions of source code must retain the above copyright notice,
8 #* this list of conditions and the following disclaimer.
9 #*
10 #* - Redistributions in binary form must reproduce the above copyright notice,
11 #* this list of conditions and the following disclaimer in the documentation
12 #* and/or other materials provided with the distribution.
13 #*
14 #* - Neither the name of Novell, Inc. nor the names of its
15 #* contributors may be used to endorse or promote products derived from this
16 #* software without specific prior written permission.
17 #*
18 #* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19 #* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 #* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 #* ARE DISCLAIMED. IN NO EVENT SHALL Novell, Inc. OR THE CONTRIBUTORS
22 #* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 #* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 #* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 #* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 #* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 #* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 #* POSSIBILITY OF SUCH DAMAGE.
29 #*
30 #* Author: Brad Nicholes (bnicholes novell.com)
31 #******************************************************************************/
32
33 import statvfs
34 import os
35 import ganglia
36
37 descriptors = list()
38
39
40 def Find_Metric(name):
41 '''Find the metric definition data given the metric name.
42 The metric name should always be unique.'''
43 for d in descriptors:
44 if d['name'] == name:
45 return d
46 pass
47
48
49 def Remote_Mount(device, type):
50 '''Determine if the device specifed is a local or remote device.'''
51 return ((device.rfind(':') != -1)
52 or ((type == "smbfs") and device.startswith('//'))
53 or type.startswith('nfs') or (type == 'autofs')
54 or (type == 'gfs') or (type == 'none'))
55
56
57 def DiskTotal_Handler(name):
58 '''Calculate the total disk space for the device that is associated
59 with the metric name.'''
60 d = Find_Metric(name)
61 if not d:
62 return 0
63
64 st = os.statvfs(d['mount'])
65 size = st[statvfs.F_BLOCKS]
66 blocksize = st[statvfs.F_BSIZE]
67 vv = (size * blocksize) / 1e9
68 return vv
69
70
71 def DiskUsed_Handler(name):
72 '''Calculate the used disk space for the device that is associated
73 with the metric name.'''
74 d = Find_Metric(name)
75 if not d:
76 return float(0)
77
78 st = os.statvfs(d['mount'])
79 free = st[statvfs.F_BAVAIL]
80 size = st[statvfs.F_BLOCKS]
81
82 if size:
83 return ((size - free) / float(size)) * 100
84 else:
85 return float(0)
86
87
88 def Init_Metric(line, name, tmax, type, units, slope, fmt, desc, handler):
89 '''Create a metric definition dictionary object for a device.'''
90 metric_name = line[0] + '-' + name
91
92 d = {'name': metric_name.replace('/', '-').lstrip('-'),
93 'call_back': handler,
94 'time_max': tmax,
95 'value_type': type,
96 'units': units,
97 'slope': slope,
98 'format': fmt,
99 'description': desc,
100 'groups': 'disk',
101 'mount': line[1]}
102 return d
103
104
105 def metric_init(params):
106 '''Discover all of the local disk devices on the system and create
107 a metric definition dictionary object for each.'''
108 global descriptors
109 f = open('/proc/mounts', 'r')
110
111 for l in f:
112 line = l.split()
113 if line[3].startswith('ro'):
114 continue
115 elif Remote_Mount(line[0], line[2]):
116 continue
117 elif (not line[0].startswith('/dev/')) and (not line[0].startswith('/dev2/')):
118 continue
119
120 if ganglia.get_debug_msg_level() > 1:
121 print 'Discovered device %s' % line[1]
122
123 descriptors.append(Init_Metric(line, 'disk_total', int(1200),
124 'double', 'GB', 'both', '%.3f',
125 'Available disk space', DiskTotal_Handler))
126 descriptors.append(Init_Metric(line, 'disk_used', int(180),
127 'float', '%', 'both', '%.1f',
128 'Percent used disk space', DiskUsed_Handler))
129
130 f.close()
131 return descriptors
132
133
134 def metric_cleanup():
135 '''Clean up the metric module.'''
136 pass
137
138 #This code is for debugging and unit testing
139 if __name__ == '__main__':
140 metric_init(None)
141 for d in descriptors:
142 v = d['call_back'](d['name'])
143 print 'value for %s is %f' % (d['name'], v)