root/trunk/FlowHandler.py

Revision 1, 3.5 kB (checked in by ixs, 16 years ago)

initial checkin of RH revision

Line 
1 #
2 # Copyright (C) 2007-2008 Red Hat, Inc.
3 # Author: Andreas Thienemann <athienem@redhat.com>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Library General Public License as published by
7 # the Free Software Foundation; version 2 only
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Library General Public License for more details.
13 #
14 # You should have received a copy of the GNU Library General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # Copyright 2004, 2005 Red Hat, Inc.
18 #
19 # AUTHOR: Andreas Thienemann <athienem@redhat.com>
20 #
21
22 import struct
23 import os
24 import sys
25 import IPy
26 import binascii
27 import pickle
28 import hashlib
29 import shutil
30 import pprint
31 from logging import *
32
33 ## Read the config
34 execfile('config.py')
35
36 class AbstractFlowParserMeta(type):
37     def __init__(cls, name, bases, attrs):
38         if not hasattr(cls, 'plugins'):
39             # This branch only executes when processing the mount point itself.
40             # So, since this is a new plugin type, not an implementation, this
41             # class shouldn't be registered as a plugin. Instead, it sets up a
42             # list where plugins can be registered later.
43             cls.plugins = {}
44         else:
45             # This must be a plugin implementation, which should be registered.
46             # Simply appending it to the list is all that's needed to keep
47             # track of it later.
48             cls.plugins[cls.name] = cls
49
50
51 class AbstractFlowParser(object):
52     """Plugin registry.
53     See http://gulopine.gamemusic.org/2008/jan/10/simple-plugin-framework/"""
54     __metaclass__ = AbstractFlowParserMeta
55
56     def __init__(self, flowsrc, pkgdata):
57         super(AbstractFlowParser, self).__init__()
58         self.flowsrc = flowsrc
59         self.pkgdata = pkgdata
60         self.flowpkg_decode = []
61
62     def writeout(self, flow_data=''):
63
64         debug('Writeout called')
65         # Sanity check, do we have an empty flowset (only a header)?
66         if flow_data == '' and len(self.flowpkg_decode) == 1:
67             debug('Writeout canceled')
68             return
69
70         # We're not being passed anything specific to write, get the default self.flowpkg_decode data
71         if flow_data == '':
72             flow_data = self.flowpkg_decode
73             # Delete the committed data
74             del(self.flowpkg_decode)
75
76         # Build an unique filename
77         filename = hashlib.sha1()
78         filename.update(str(flow_data[0]['sourceIP'])+'-'+str(flow_data[0]['unixtime'])+'-'+str(flow_data[0]['sequence']))
79         filename = filename.hexdigest() + '.pkl'
80
81         # Pickle this
82         fp = open(os.path.join(SPOOLDIR, filename + '.tmp'), "w")
83         pickle.dump(flow_data, fp)
84         fp.close
85
86         # Move the file to it's final name to guarantee atomicity
87         shutil.move(os.path.join(SPOOLDIR, filename + '.tmp'), os.path.join(SPOOLDIR, filename))
88        
89
90 class FlowHandler(object):
91
92     def __init__(self, flowsrc, pkgdata):
93         super(FlowHandler, self).__init__()
94        
95         self.flowsrc = flowsrc
96         self.pkgdata = pkgdata
97
98         klass = AbstractFlowParser.plugins[routers[flowsrc]]
99         parser = klass(flowsrc, pkgdata)
100
101         # Execute the parser
102         parser.parse()
103         # Flush it's cache to disk early
104         parser.writeout()
Note: See TracBrowser for help on using the browser.