Error setting python_path fixed.
[smonitor.git] / main.py
blobe7a577817a233e3d47b28df3986cd75c3a45cc1c
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
32 import sys
33 import os
35 sys.path.append(os.path.join(os.path.dirname(__file__), "lib"))
37 from multiprocessing import Process, managers
38 from monitor.collector import HttpCollector
39 from monitor import http_display
40 from monitor import datastore
43 MANAGER_PORT = 53170
44 MANAGER_AUTHKEY = 'xlz98'
47 class MonitorManager(managers.SyncManager):
48 """Custom multiprocessing manager."""
49 pass
52 def timed_collector(sources, interval):
53 """Runs collector in intervals.
55 This method executes and infinite loop collecting data each
56 `interval` seconds. The `DataStore` object used to store the
57 retrieved data is a remote object obtained from a `MonitorManager`.
60 Arguments:
61 - `sources`: Iterable, pairs of (hostname, port) to use for collection.
62 - `interval`: Integer, seconds between collections.
64 """
65 manager = MonitorManager(address=('localhost', MANAGER_PORT),
66 authkey=MANAGER_AUTHKEY)
67 manager.connect()
68 store = manager.get_store()
69 collector = HttpCollector(sources)
70 while (True):
71 collected_data = collector.collect()
72 store.insert_dict(collected_data)
73 time.sleep(interval)
76 def displayer(port):
77 """Runs the http exporter interface.
79 This method executes an HTTP server
80 (`monitor.displayer.http_display`). The `DataStore` object used to
81 call this method is a remote object obtained from a
82 `MonitorManager`.
84 """
85 manager = MonitorManager(address=('localhost', MANAGER_PORT),
86 authkey=MANAGER_AUTHKEY)
87 manager.connect()
88 store = manager.get_store()
89 http_display.main(store, port,
90 "/home/rodrigo/workspace/monitor/monitor/www_templates/")
93 def main():
94 """Main function, launches process for each part of the system."""
95 parser = argparse.ArgumentParser(description="Server monitoring system.")
96 parser.add_argument('--http-port', metavar="PORT", type=int, default=9012,
97 help="Port for the HTTP server (displayer).")
98 parser.add_argument('--collection-interval', metavar="SECONDS",
99 type=int, default=30,
100 help="Interval, in seconds, between data collections.")
102 group = parser.add_mutually_exclusive_group(required=True)
103 group.add_argument('--remote-servers', nargs="+",
104 help=("List, separated by spaces, of remote servers "
105 "(HOST:PORT) to monitor."))
106 group.add_argument('--snapshot', metavar="SNAPSHOT-FILE", type=str,
107 help="Filename of the snapshot to load.")
108 args = parser.parse_args()
110 if args.snapshot:
111 mstore = manager.get_store()
112 mstore.load(args.snapshot)
113 else:
114 sources = [x.split(':') for x in args.remote_servers]
115 collector_p = Process(target=timed_collector,
116 args=(sources, args.collection_interval))
117 collector_p.start()
118 displayer_p = Process(target=displayer, args=(args.http_port,))
119 displayer_p.start()
120 if not args.snapshot:
121 collector_p.join()
122 displayer_p.join()
125 if __name__ == '__main__':
126 store = datastore.DataStore()
127 MonitorManager.register('get_store', callable=lambda: store)
128 manager = MonitorManager(address=('', MANAGER_PORT),
129 authkey=MANAGER_AUTHKEY)
130 manager.start()
131 main()