]> git.ipfire.org Git - collecty.git/blame - collecty/__init__.py
Let the main thread regularly submit all data to disk.
[collecty.git] / collecty / __init__.py
CommitLineData
a49a4b46 1#!/usr/bin/python
cd57e2f3
MT
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###############################################################################
a49a4b46
MT
21
22import signal
49ce926e 23import time
a49a4b46
MT
24
25import ConfigParser as configparser
26
27import plugins
28
49ce926e
MT
29from i18n import _
30
5a2ff959
MT
31# Initialize logging.
32import logging
33log = logging.getLogger("collecty")
34log.level = logging.DEBUG
35
36handler = logging.StreamHandler()
6b8cd578 37handler.setLevel(logging.DEBUG)
5a2ff959
MT
38log.handlers.append(handler)
39
6b8cd578
MT
40formatter = logging.Formatter("%(asctime)s | %(name)-20s - %(levelname)-6s | %(message)s")
41handler.setFormatter(formatter)
42
a49a4b46
MT
43class ConfigError(Exception):
44 pass
45
46class Collecty(object):
4dc6b0c9
MT
47 # The default interval, when all data is written to disk.
48 SUBMIT_INTERVAL = 300
49
50 HEARTBEAT = 2
51
a49a4b46
MT
52 def __init__(self):
53 self.config = configparser.ConfigParser()
54 self.instances = []
55
4dc6b0c9
MT
56 # Indicates whether this process should be running or not.
57 self.running = True
58
13b7bc46
MT
59 # Add all automatic plugins.
60 self.add_autocreate_plugins()
61
6b8cd578
MT
62 log.info(_("Collecty successfully initialized."))
63
13b7bc46
MT
64 def add_autocreate_plugins(self):
65 for plugin in plugins.registered_plugins:
66 if not hasattr(plugin, "autocreate"):
67 continue
68
69 ret = plugin.autocreate(self)
70 if not ret:
71 continue
72
73 if not type(ret) == type([]):
74 ret = [ret,]
75
76 log.debug(_("Plugin '%(name)s' registered %(number)s instance(s).") % \
77 { "name" : plugin.name, "number" : len(ret) })
78
79 self.instances += ret
80
a49a4b46
MT
81 def read_config(self, config):
82 self.config.read(config)
83
84 for section in self.config.sections():
85 try:
86 plugin = self.config.get(section, "plugin")
87 plugin = plugins.find(plugin)
88 except configparser.NoOptionError:
89 raise ConfigError, "Syntax error in configuration: plugin option is missing."
90 except:
91 raise Exception, "Plugin configuration error: Maybe plugin wasn't found? %s" % plugin
92
93 kwargs = {}
94 for (key, value) in self.config.items(section):
95 if key == "plugin":
96 continue
97
98 kwargs[key] = value
99 kwargs["file"] = section
100
101 i = plugin(self, **kwargs)
102 self.instances.append(i)
103
a49a4b46 104 def run(self):
49ce926e
MT
105 # Register signal handlers.
106 self.register_signal_handler()
a49a4b46 107
49ce926e 108 # Start all plugin instances.
a49a4b46
MT
109 for i in self.instances:
110 i.start()
111
4dc6b0c9
MT
112 # Regularly submit all data to disk.
113 counter = self.SUBMIT_INTERVAL / self.HEARTBEAT
114 while self.running:
115 time.sleep(self.HEARTBEAT)
116 counter -= 1
117
118 if counter == 0:
119 self.submit_all()
120 counter = self.SUBMIT_INTERVAL / self.HEARTBEAT
121
122 # Wait until all instances are finished.
123 while self.instances:
124 for instance in self.instances[:]:
125 if not instance.isAlive():
126 log.debug(_("%s is not alive anymore. Removing.") % instance)
127 self.instances.remove(instance)
128
129 # Wait a bit.
130 time.sleep(0.1)
49ce926e
MT
131
132 log.debug(_("No thread running. Exiting main thread."))
133
4dc6b0c9
MT
134 def submit_all(self):
135 """
136 Submit all data right now.
137 """
138 for i in self.instances:
139 i._submit()
140
a49a4b46 141 def shutdown(self):
49ce926e
MT
142 log.debug(_("Received shutdown signal"))
143
4dc6b0c9
MT
144 self.running = False
145
49ce926e 146 # Propagating shutdown to all threads.
a49a4b46 147 for i in self.instances:
a49a4b46 148 i.shutdown()
49ce926e
MT
149
150 def register_signal_handler(self):
151 for s in (signal.SIGTERM, signal.SIGINT):
152 signal.signal(s, self.signal_handler)
153
154 def signal_handler(self, *args, **kwargs):
155 # Shutdown this application.
156 self.shutdown()