1 """
2 Provides Logging facilities.
3
4 Inter-Organisational Intrusion Detection System (IOIDS)
5
6 This is just a copy from the G4DS logging facilities with changes applied to make it suitable
7 for IOIDS use. (looks like, it should be extracted in a tools module ;))
8
9 @author: Michael Pilgermann
10 @contact: mailto:mpilgerm@glam.ac.uk
11 @license: GPL (General Public License)
12 """
13
14 from time import strftime
15 import string
16 import syslog
17
18
19 _defaultLogger = None
28
29 LOGSERVER_STATUS = 0
30
31 EVENTTRIGGER_STATUS = 199
32 EVENTTRIGGER_UPDATE = 198
33 EVENTTRIGGER_UPDATE_DETAILS = 197
34
35 DATAENGINE_ERROR_GENERIC = 200
36 DATAENGINE_STATUS = 299
37 DATAENGINE_PROCESSING_DETAILS = 298
38 DATAENGINE_POLICY_STATUS = 290
39 DATAENGINE_POLICY_INFORMATION = 289
40
41 G4DS_CONNECTOR_STATUS = 299
42 G4DS_CONNECTOR_ERROR_GENERIC = 200
43 G4DS_CONNECTOR_INCOMING_MSG = 211
44 G4DS_CONNECTOR_INCOMING_MSG_DETAILS = 212
45 G4DS_CONNECTOR_OUTGOING_MSG = 221
46 G4DS_CONNECTOR_OUTGOING_MSG_DETAILS = 222
47
48 CLASS={}
49 CLASS[0] = [LOGSERVER_STATUS]
50 CLASS[1] = [EVENTTRIGGER_STATUS, DATAENGINE_STATUS, G4DS_CONNECTOR_STATUS, DATAENGINE_POLICY_STATUS]
51 CLASS[1].extend(CLASS[0])
52 CLASS[2] = [DATAENGINE_ERROR_GENERIC, G4DS_CONNECTOR_ERROR_GENERIC]
53 CLASS[2].extend(CLASS[1])
54 CLASS[3] = [EVENTTRIGGER_UPDATE, G4DS_CONNECTOR_INCOMING_MSG, G4DS_CONNECTOR_OUTGOING_MSG]
55 CLASS[3].extend(CLASS[2])
56 CLASS[4] = [G4DS_CONNECTOR_INCOMING_MSG_DETAILS, G4DS_CONNECTOR_OUTGOING_MSG_DETAILS]
57 CLASS[4].extend(CLASS[3])
58 CLASS[5] = [EVENTTRIGGER_UPDATE_DETAILS, DATAENGINE_PROCESSING_DETAILS, DATAENGINE_POLICY_INFORMATION]
59
61 """
62 All messages are equipped with a timestamp and line wise written to a log file.
63
64 Addtionally, this class supports logging into syslog facilities.
65
66 @ivar _logfile: Reference to the file instance
67 @type _logfile: C{File}
68 @ivar _level: Log level to be used for the instance (defined in config file)
69 @type _level: C{int}
70 """
71
88
90 """
91 Shutdown logging.
92
93 Put a log message in the log file for closing down g4ds logging and finally close the log file.
94 """
95 self.newMessage(LOGSERVER_STATUS, 'IOIDS Logging shut down')
96 self._logfile.close()
97
98 if self._syslogOn:
99 syslog.closelog()
100
102 """
103 New entry for the log system.
104
105 A check is performed, whether the given category is to be logged in the activated log level. If so,
106 a message is generated, made up by a time stamp, the category value and the message itself.
107 """
108 try:
109 if self._level != 5:
110 CLASS[self._level].index(category)
111 st = strftime('%Y-%m-%d %H:%M:%S').ljust(17) + ' ' + string.zfill(category, 3) + ' ' + str(message) + '\n'
112 self._logfile.write(st)
113 self._logfile.flush()
114
115 if self._syslogOn:
116 syslog.syslog(string.zfill(category, 3) + ' ' + str(message))
117
118 except ValueError:
119 pass
120
122 """
123 Returns the last 'n' lines of the log file.
124
125 @param n: Number of lines requested
126 @type n: C{int}
127 @return: The last lines - each line as a string - together in a list
128 @rtype: C{List} of C{String}
129 """
130 from config import LOGGING_FILENAME, LOGGING_LEVEL
131 logfile = open(LOGGING_FILENAME, 'r')
132 lines = []
133 s = logfile.readline().rstrip()
134 i = -1
135 while s != '':
136 i = (i+1) % n
137 if len(lines) > i:
138 lines[i] = s
139 else:
140 lines.append(s)
141 s = logfile.readline().rstrip()
142 logfile.close()
143
144 if len(lines) == i+1:
145 return lines
146
147
148 back = []
149 while len(back) < n:
150 i = (i+1) % n
151 back.append(lines[i])
152 return back
153