]> git.ipfire.org Git - collecty.git/blame - collecty/__init__.py
Format log messages, so one can see the origin of the message.
[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):
47 def __init__(self):
48 self.config = configparser.ConfigParser()
49 self.instances = []
50
6b8cd578
MT
51 log.info(_("Collecty successfully initialized."))
52
a49a4b46
MT
53 def read_config(self, config):
54 self.config.read(config)
55
56 for section in self.config.sections():
57 try:
58 plugin = self.config.get(section, "plugin")
59 plugin = plugins.find(plugin)
60 except configparser.NoOptionError:
61 raise ConfigError, "Syntax error in configuration: plugin option is missing."
62 except:
63 raise Exception, "Plugin configuration error: Maybe plugin wasn't found? %s" % plugin
64
65 kwargs = {}
66 for (key, value) in self.config.items(section):
67 if key == "plugin":
68 continue
69
70 kwargs[key] = value
71 kwargs["file"] = section
72
73 i = plugin(self, **kwargs)
74 self.instances.append(i)
75
a49a4b46 76 def run(self):
49ce926e
MT
77 # Register signal handlers.
78 self.register_signal_handler()
a49a4b46 79
49ce926e 80 # Start all plugin instances.
a49a4b46
MT
81 for i in self.instances:
82 i.start()
83
49ce926e
MT
84 # As long as at least one thread is alive, the main process
85 # is in a while loop.
86 while any([i.isAlive() for i in self.instances]):
87 time.sleep(0.5)
88
89 log.debug(_("No thread running. Exiting main thread."))
90
a49a4b46 91 def shutdown(self):
49ce926e
MT
92 log.debug(_("Received shutdown signal"))
93
94 # Propagating shutdown to all threads.
a49a4b46 95 for i in self.instances:
a49a4b46 96 i.shutdown()
49ce926e
MT
97
98 def register_signal_handler(self):
99 for s in (signal.SIGTERM, signal.SIGINT):
100 signal.signal(s, self.signal_handler)
101
102 def signal_handler(self, *args, **kwargs):
103 # Shutdown this application.
104 self.shutdown()