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