Bundled cherrypy.
[smonitor.git] / main.py
blobcd8a4c9edf66b8d918eee25bf0999a3846473ac1
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Server monitoring system
6 # Copyright © 2011 Rodrigo Eduardo Lazo Paz
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 """Launching script."""
26 __author__ = "rlazo.paz@gmail.com (Rodrigo Lazo)"
27 __version__ = 0.2
30 import argparse
31 import time
33 from multiprocessing import Process, managers
34 from monitor.collector import HttpCollector
35 from monitor import http_display
36 from monitor import datastore
39 MANAGER_PORT = 53170
40 MANAGER_AUTHKEY = 'xlz98'
43 class MonitorManager(managers.SyncManager):
44 """Custom multiprocessing manager."""
45 pass
48 def timed_collector(sources, interval):
49 """Runs collector in intervals.
51 This method executes and infinite loop collecting data each
52 `interval` seconds. The `DataStore` object used to store the
53 retrieved data is a remote object obtained from a `MonitorManager`.
56 Arguments:
57 - `sources`: Iterable, pairs of (hostname, port) to use for collection.
58 - `interval`: Integer, seconds between collections.
60 """
61 manager = MonitorManager(address=('localhost', MANAGER_PORT),
62 authkey=MANAGER_AUTHKEY)
63 manager.connect()
64 store = manager.get_store()
65 collector = HttpCollector(sources)
66 while (True):
67 collected_data = collector.collect()
68 store.insert_dict(collected_data)
69 time.sleep(interval)
72 def displayer(port):
73 """Runs the http exporter interface.
75 This method executes an HTTP server
76 (`monitor.displayer.http_display`). The `DataStore` object used to
77 call this method is a remote object obtained from a
78 `MonitorManager`.
80 """
81 manager = MonitorManager(address=('localhost', MANAGER_PORT),
82 authkey=MANAGER_AUTHKEY)
83 manager.connect()
84 store = manager.get_store()
85 http_display.main(store, port,
86 "/home/rodrigo/workspace/monitor/monitor/www_templates/",
87 "/home/rodrigo/workspace/monitor/monitor/static/")
90 def main():
91 """Main function, launches process for each part of the system."""
92 parser = argparse.ArgumentParser(description="Server monitoring system.")
93 parser.add_argument('--http-port', metavar="PORT", type=int, default=9012,
94 help="Port for the HTTP server (displayer).")
95 parser.add_argument('--collection-interval', metavar="SECONDS",
96 type=int, default=30,
97 help="Interval, in seconds, between data collections.")
99 group = parser.add_mutually_exclusive_group(required=True)
100 group.add_argument('--remote-servers', nargs="+",
101 help=("List, separated by spaces, of remote servers "
102 "(HOST:PORT) to monitor."))
103 group.add_argument('--snapshot', metavar="SNAPSHOT-FILE", type=str,
104 help="Filename of the snapshot to load.")
105 args = parser.parse_args()
107 if args.snapshot:
108 mstore = manager.get_store()
109 mstore.load(args.snapshot)
110 else:
111 sources = [x.split(':') for x in args.remote_servers]
112 collector_p = Process(target=timed_collector,
113 args=(sources, args.collection_interval))
114 collector_p.start()
115 displayer_p = Process(target=displayer, args=(args.http_port,))
116 displayer_p.start()
117 if not args.snapshot:
118 collector_p.join()
119 displayer_p.join()
122 if __name__ == '__main__':
123 store = datastore.DataStore()
124 MonitorManager.register('get_store', callable=lambda: store)
125 manager = MonitorManager(address=('', MANAGER_PORT),
126 authkey=MANAGER_AUTHKEY)
127 manager.start()
128 main()