1 |
#!/usr/bin/python |
---|
2 |
# |
---|
3 |
# Copyright (C) 2007-2008 Red Hat, Inc. |
---|
4 |
# Author: Andreas Thienemann <athienem@redhat.com> |
---|
5 |
# |
---|
6 |
# This program is free software; you can redistribute it and/or modify |
---|
7 |
# it under the terms of the GNU Library General Public License as published by |
---|
8 |
# the Free Software Foundation; version 2 only |
---|
9 |
# |
---|
10 |
# This program is distributed in the hope that it will be useful, |
---|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 |
# GNU Library General Public License for more details. |
---|
14 |
# |
---|
15 |
# You should have received a copy of the GNU Library General Public License |
---|
16 |
# along with this program; if not, write to the Free Software |
---|
17 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
18 |
# Copyright 2004, 2005 Red Hat, Inc. |
---|
19 |
# |
---|
20 |
# AUTHOR: Andreas Thienemann <athienem@redhat.com> |
---|
21 |
# |
---|
22 |
|
---|
23 |
import os |
---|
24 |
import sys |
---|
25 |
import SocketServer |
---|
26 |
import FlowHandler |
---|
27 |
import optparse |
---|
28 |
from signal import signal, SIGTERM |
---|
29 |
import atexit |
---|
30 |
from logging import * |
---|
31 |
from common import * |
---|
32 |
import pprint |
---|
33 |
import traceback |
---|
34 |
import gamin |
---|
35 |
|
---|
36 |
def cleanup(): |
---|
37 |
info('Collector shutdown') |
---|
38 |
|
---|
39 |
|
---|
40 |
class handler(SocketServer.DatagramRequestHandler): |
---|
41 |
'''Connection handler for the UDP Server''' |
---|
42 |
def handle(self): |
---|
43 |
flowsrc = self.client_address[0] |
---|
44 |
# Only accept data from configured probes |
---|
45 |
if flowsrc not in routers: |
---|
46 |
# raise UnknownRouterException(flowsrc) |
---|
47 |
debug('Package from non-configured system %s received' %flowsrc) |
---|
48 |
return |
---|
49 |
|
---|
50 |
flowpkg = self.request[0] |
---|
51 |
x = FlowHandler.FlowHandler(flowsrc, flowpkg) |
---|
52 |
|
---|
53 |
def main(): |
---|
54 |
|
---|
55 |
# CLI Options |
---|
56 |
usage = "usage: %prog [options] arg" |
---|
57 |
parser = optparse.OptionParser(usage) |
---|
58 |
parser.add_option("-c", "--config", dest="configfile", default="config.py", |
---|
59 |
help="Use FILENAME to read initial configuration from") |
---|
60 |
parser.add_option("-D", "--daemon", |
---|
61 |
action="store_true", dest="daemon", |
---|
62 |
help="Run as daemon") |
---|
63 |
parser.add_option("-d", "--loglevel=debug", |
---|
64 |
action="store_const", const="DEBUG", dest="loglevel", |
---|
65 |
help="DEBUG loglevel", default="INFO") |
---|
66 |
(options, args) = parser.parse_args() |
---|
67 |
|
---|
68 |
|
---|
69 |
# Read in configuration |
---|
70 |
execfile(options.configfile, globals()) |
---|
71 |
|
---|
72 |
# Set up logging |
---|
73 |
basicConfig(level=eval(options.loglevel), |
---|
74 |
format='%(asctime)s %(levelname)s %(module)s %(message)s', |
---|
75 |
filename=LOGFILE_COLLECTOR, filemode='a+') |
---|
76 |
|
---|
77 |
# Dynamically load the Parser Modules in the FlowParser sub-directory |
---|
78 |
# This will import all files with the .py extension |
---|
79 |
for file in os.listdir('FlowParser'): |
---|
80 |
if file.split('.')[-1] == 'py': |
---|
81 |
__import__('FlowParser/' + file[:-3]) |
---|
82 |
|
---|
83 |
if options.daemon: |
---|
84 |
daemonize() |
---|
85 |
|
---|
86 |
try: |
---|
87 |
atexit.register(cleanup) |
---|
88 |
signal(SIGTERM, lambda signum, stack_frame: exit(1)) |
---|
89 |
s = SocketServer.ThreadingUDPServer(('',PORTNO), handler) |
---|
90 |
info('Collector startup success') |
---|
91 |
info('UDPServer startup successful. Listening on ' + str(PORTNO) + '...') |
---|
92 |
s.serve_forever() |
---|
93 |
except (KeyboardInterrupt, SystemExit): |
---|
94 |
pass |
---|
95 |
except: |
---|
96 |
raise |
---|
97 |
|
---|
98 |
if __name__ == "__main__": |
---|
99 |
main() |
---|