SyslogLogging
From NetBurner Wiki
Contents |
[edit] Overview
Simple Syslog data logging with python.
[edit] The Why
If you come across a scenario where you need to log large amounts of data over a long term, then post process, the GUI tools tend to fall down. This is where scripting comes in. Thankfully Netburner's syslog facility is easy to tap. The following quick python script will log any incoming syslog data from the network to stdio, which can then be easily manipulated via any of the standard *nix tooling.
[edit] The How
import os
from socket import *
# Main
if __name__=='__main__':
sock = socket(AF_INET, SOCK_DGRAM)
sock.bind(("0.0.0.0", 514))
while(1):
data,addr = sock.recvfrom(4096)
if not data:
print "<!> : recv failed"
else:
print "<%s> : %s" % addr, data
[edit] Usage
logs to stdio
python <scriptname>
redirect to file
python <scriptname> > log_file &
follow the log file
tail -f log_file
[edit] Other info
I use the following diagnostic print code to handle debug prints. It routes all diag logging through both the terminal, via the iprintf, and out the UDP SysLog port(514) via the SysLog function.
//Simple platform diagnostic logging function. Sends out serial and UDP port
unsigned platformDiag(const char *fmt, ...) {
va_list vl;
va_start(vl,fmt);
char str[150];
vsnprintf(str,sizeof(str),fmt,vl);
iprintf(str);
SysLog(str);
va_end(vl);
return 1;
}
Then, I add the following into a common header file, and use the LOG() function for all my logging needs. I can then toggle the LOG_ENABLED defined to determine whether or not to build in debug printing. Disabling it reduces the resulting executable by a considerable amount.
#define LOG_ENABLED 1 #if LOG_ENABLED //Standard debug log #define LOG(fmt,...) (platformDiag(fmt, ##__VA_ARGS__)) //Debug printf. Prints out if the debugVar is set to >0. #define UASSERT(expr,fmt,...) ( !(expr) && platformDiag(fmt, ##__VA_ARGS__) ) #else #define LOG(fmt,...) #define UASSERT(expr,...) (expr) #endif