Package ioids :: Module ioidslogging
[hide private]
[frames] | no frames]

Source Code for Module ioids.ioidslogging

  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  # "singleton" 
 19  _defaultLogger = None 
20 -def getDefaultLogger():
21 """ 22 Singleton implementation. 23 """ 24 global _defaultLogger 25 if not _defaultLogger: 26 _defaultLogger = FileLogger() 27 return _defaultLogger
28 29 LOGSERVER_STATUS = 0 # boot up / shutdown of logging 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] #everything - not used 59
60 -class FileLogger:
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
72 - def __init__(self):
73 """ 74 Open the log file. 75 76 Put a log message in the log file for brining up the g4ds log service. 77 """ 78 from config import LOGGING_FILENAME, LOGGING_LEVEL, ENABLE_SYSLOG, SYSLOG_IDENTIFIER 79 self._logfile = open(LOGGING_FILENAME, 'a') 80 self._level = LOGGING_LEVEL 81 82 self._syslogOn = ENABLE_SYSLOG 83 84 if ENABLE_SYSLOG: 85 syslog.openlog(SYSLOG_IDENTIFIER) 86 87 self.newMessage(LOGSERVER_STATUS, 'IOIDS Logging started (level %d)' %(self._level))
88
89 - def closedown(self):
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
101 - def newMessage(self, category, message):
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 # this log message is not in the class for the given log level - just ignore it
120
121 - def getLatestMessages(self, n):
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 # put it in order 148 back = [] 149 while len(back) < n: 150 i = (i+1) % n 151 back.append(lines[i]) 152 return back
153