summaryrefslogtreecommitdiffstats
path: root/monitorplugins/disk.py
blob: 6167c84e8cf2ad6ef2858114d7322eda50ba32c2 (plain)
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
# Copyright (C) 2008, 2009 Red Hat, Inc.
# Authors: Phil Knirsch
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#

import os

class DiskMonitor:
	def __init__(self):
		self.devices = {}
		dnames = os.listdir("/sys/block/")
		for d in dnames:
			try:
				v = open("/sys/block/"+d+"/device/vendor").read().strip()
			except:
				v = None
			if v != "ATA" and v != "SCSI":
				continue
			self.devices[d] = {}
			self.devices[d]["new"] = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
			self.devices[d]["max"] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
			self.__updateStat__(d)
			self.devices[d]["max"] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
		print self.devices

	def __calcdiff__(self, dev):
		l = []
		for i in xrange(len(self.devices[dev]["old"])):
			l.append(int(self.devices[dev]["new"][i]) - int(self.devices[dev]["old"][i]))
		return l

	def __updateStat__(self, dev):
		l = open("/sys/block/"+dev+"/stat", "r").read()
		self.devices[dev]["old"] = self.devices[dev]["new"]
		self.devices[dev]["new"] = l.split()
		l = self.__calcdiff__(dev)
		for i in xrange(len(l)):
			if l[i] > self.devices[dev]["max"][i]:
				self.devices[dev]["max"][i] = l[i]

	def __update__(self):
		for dev in self.devices.keys():
			self.__updateStat__(dev)
			self.devices[dev]["diff"] = self.__calcdiff__(dev)

	def init(self, config):
		self.config = config

	def cleanup(self):
		pass

	def getLoad(self):
		self.__update__()
		ret = {}
		ret["DISK"] = {}
		for dev in self.devices.keys():
			ret["DISK"][dev] = {}
			ret["DISK"][dev]["READ"] = float(self.devices[dev]["diff"][1]) / float(self.devices[dev]["max"][1])
			ret["DISK"][dev]["WRITE"] = float(self.devices[dev]["diff"][5]) / float(self.devices[dev]["max"][5])
		return ret

_plugin = DiskMonitor()