]> git.ipfire.org Git - collecty.git/blame - collecty/plugins/base.py
Add some basic logging.
[collecty.git] / collecty / plugins / base.py
CommitLineData
eed405de
MT
1#!/usr/bin/python
2###############################################################################
3# #
4# collecty - A system statistics collection daemon for IPFire #
5# Copyright (C) 2012 IPFire development team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22from threading import Thread
23
24from ..i18n import _
25
26class Plugin(Thread):
27 def __init__(self, collecty, **kwargs):
28 Thread.__init__(self)
29 self.collecty = collecty
30
31 self.interval = int(kwargs.get("interval", 60))
32
33 # Keepalive options
34 self.heartbeat = 2
35 self.killed = False
36
37 self.wakeup = self.interval / self.heartbeat
38
39 self.file = kwargs.get("file", None)
40 if not self.file.startswith("/"):
41 self.file = os.path.join("/var/rrd", self.file)
42
43 self.data = []
44
45 self.create()
46
47 def __repr__(self):
48 return "<Plugin %s>" % self._type
49
50 def __str__(self):
51 return "Plugin %s %s" % (self._type, self.file)
52
53 def run(self):
54 self.collecty.debug("%s started..." % self)
55
56 c = 0
57 while True:
58 if self.killed:
59 self.update()
60 self.collecty.debug("%s stoppped..." % self)
61 return
62
63 if c == 0:
64 self.data.append(self.collect())
65 self.collecty.debug("%s collectd: %s..." % (self, self.data[-1]))
66
67 self.update()
68
69 c = self.wakeup
70
71 c = c - 1
72 time.sleep(self.heartbeat)
73
74 def shutdown(self):
75 self.killed = True
76
77 def time(self):
78 return int(time.time()) # Should return time as int in UTC
79
80 def create(self):
81 if not os.path.exists(self.file):
82 rrdtool.create(self.file, *self._rrd)
83
84 def update(self):
85 if self.data:
86 self.collecty.debug("%s saving data..." % self)
87 rrdtool.update(self.file, *self.data)
88 self.data = []
89
90 def collect(self):
91 raise Exception, "Not implemented"
92
93 def graph(self, file, interval=None):
94 args = [ "--imgformat", "PNG",
95 "-w", "580", # Width of the graph
96 "-h", "240", # Height of the graph
97 "--interlaced", "--slope-mode", ]
98
99 intervals = { None : "-3h",
100 "hour" : "-1h",
101 "day" : "-25h",
102 "week" : "-360h" }
103
104 args.append("--start")
105 if intervals.has_key(interval):
106 args.append(intervals[interval])
107 else:
108 args.append(interval)
109
110 info = { "file" : self.file }
111 for item in self._graph:
112 try:
113 args.append(item % info)
114 except TypeError:
115 args.append(item)
116
117 rrdtool.graph(file, *args)
118
119 def info(self):
120 return rrdtool.info(self.file)
121