<s> import simplejson as json <EOL> import logging . config <EOL> import socket <EOL> import sys <EOL> from datetime import datetime <EOL> from logging . handlers import SocketHandler <EOL> from twisted . internet import reactor <EOL> class Singleton ( type ) : <EOL> _instances = { } <EOL> def __call__ ( cls , * args , ** kwargs ) : <EOL> if cls not in cls . _instances : <EOL> cls . _instances [ cls ] = super ( Singleton , cls ) . __call__ ( * args , ** kwargs ) <EOL> return cls . _instances [ cls ] <EOL> def getLogger ( config ) : <EOL> try : <EOL> d = config . getVal ( '<STR_LIT>' ) <EOL> except Exception as e : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> exit ( <NUM_LIT:1> ) <EOL> classname = d . get ( '<STR_LIT:class>' , None ) <EOL> if classname is None : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> exit ( <NUM_LIT:1> ) <EOL> LoggerClass = globals ( ) . get ( classname , None ) <EOL> if LoggerClass is None : <EOL> print >> sys . stderr , "<STR_LIT>" % classname <EOL> exit ( <NUM_LIT:1> ) <EOL> kwargs = d . get ( '<STR_LIT>' , None ) <EOL> if kwargs is None : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> exit ( <NUM_LIT:1> ) <EOL> try : <EOL> logger = LoggerClass ( config , ** kwargs ) <EOL> except Exception as e : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> print e <EOL> exit ( <NUM_LIT:1> ) <EOL> return logger <EOL> class LoggerBase ( object ) : <EOL> LOG_BASE_BOOT = <NUM_LIT:1000> <EOL> LOG_BASE_MSG = <NUM_LIT> <EOL> LOG_BASE_DEBUG = <NUM_LIT> <EOL> LOG_BASE_ERROR = <NUM_LIT> <EOL> LOG_BASE_PING = <NUM_LIT> <EOL> LOG_BASE_CONFIG_SAVE = <NUM_LIT> <EOL> LOG_BASE_EXAMPLE = <NUM_LIT> <EOL> LOG_FTP_LOGIN_ATTEMPT = <NUM_LIT> <EOL> LOG_HTTP_GET = <NUM_LIT> <EOL> LOG_HTTP_POST_LOGIN_ATTEMPT = <NUM_LIT> <EOL> LOG_SSH_NEW_CONNECTION = <NUM_LIT> <EOL> LOG_SSH_REMOTE_VERSION_SENT = <NUM_LIT> <EOL> LOG_SSH_LOGIN_ATTEMPT = <NUM_LIT> <EOL> LOG_SMB_FILE_OPEN = <NUM_LIT> <EOL> LOG_PORT_SYN = <NUM_LIT> <EOL> LOG_TELNET_LOGIN_ATTEMPT = <NUM_LIT> <EOL> LOG_HTTPPROXY_LOGIN_ATTEMPT = <NUM_LIT> <EOL> LOG_MYSQL_LOGIN_ATTEMPT = <NUM_LIT> <EOL> LOG_MSSQL_LOGIN_SQLAUTH = <NUM_LIT> <EOL> LOG_MSSQL_LOGIN_WINAUTH = <NUM_LIT> <EOL> LOG_TFTP = <NUM_LIT> <EOL> LOG_NTP_MONLIST = <NUM_LIT> <EOL> LOG_VNC = <NUM_LIT> <EOL> LOG_SNMP_CMD = <NUM_LIT> <EOL> LOG_RDP = <NUM_LIT> <EOL> LOG_SIP_REQUEST = <NUM_LIT> <EOL> LOG_USER_0 = <NUM_LIT> <EOL> LOG_USER_1 = <NUM_LIT> <EOL> LOG_USER_2 = <NUM_LIT> <EOL> LOG_USER_3 = <NUM_LIT> <EOL> LOG_USER_4 = <NUM_LIT> <EOL> LOG_USER_5 = <NUM_LIT> <EOL> LOG_USER_6 = <NUM_LIT> <EOL> LOG_USER_7 = <NUM_LIT> <EOL> LOG_USER_8 = <NUM_LIT> <EOL> LOG_USER_9 = <NUM_LIT> <EOL> def sanitizeLog ( self , logdata ) : <EOL> logdata [ '<STR_LIT>' ] = self . node_id <EOL> logdata [ '<STR_LIT>' ] = datetime . utcnow ( ) . strftime ( "<STR_LIT>" ) <EOL> if not logdata . has_key ( '<STR_LIT>' ) : <EOL> logdata [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if not logdata . has_key ( '<STR_LIT>' ) : <EOL> logdata [ '<STR_LIT>' ] = - <NUM_LIT:1> <EOL> if not logdata . has_key ( '<STR_LIT>' ) : <EOL> logdata [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if not logdata . has_key ( '<STR_LIT>' ) : <EOL> logdata [ '<STR_LIT>' ] = - <NUM_LIT:1> <EOL> if not logdata . has_key ( '<STR_LIT>' ) : <EOL> logdata [ '<STR_LIT>' ] = self . LOG_BASE_MSG <EOL> if not logdata . has_key ( '<STR_LIT>' ) : <EOL> logdata [ '<STR_LIT>' ] = { } <EOL> return logdata <EOL> class PyLogger ( LoggerBase ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = Singleton <EOL> def __init__ ( self , config , handlers , formatters = { } ) : <EOL> self . node_id = config . getVal ( '<STR_LIT>' ) <EOL> for h in handlers : <EOL> handlers [ h ] [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> logconfig = { <EOL> "<STR_LIT:version>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : formatters , <EOL> "<STR_LIT>" : handlers , <EOL> "<STR_LIT>" : { <EOL> self . node_id : { <EOL> "<STR_LIT>" : handlers . keys ( ) <EOL> } <EOL> } <EOL> } <EOL> try : <EOL> logging . config . dictConfig ( logconfig ) <EOL> except Exception as e : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> print type ( e ) <EOL> print e <EOL> exit ( <NUM_LIT:1> ) <EOL> self . logger = logging . getLogger ( self . node_id ) <EOL> def error ( self , data ) : <EOL> data [ '<STR_LIT>' ] = datetime . utcnow ( ) . strftime ( "<STR_LIT>" ) <EOL> msg = '<STR_LIT>' % json . dumps ( data , sort_keys = True ) <EOL> print >> sys . stderr , msg <EOL> self . logger . warn ( msg ) <EOL> def log ( self , logdata , retry = True ) : <EOL> logdata = self . sanitizeLog ( logdata ) <EOL> self . logger . warn ( json . dumps ( logdata , sort_keys = True ) ) <EOL> class SocketJSONHandler ( SocketHandler ) : <EOL> """<STR_LIT>""" <EOL> def makeSocket ( self , timeout = <NUM_LIT:1> ) : <EOL> s = SocketHandler . makeSocket ( self , timeout ) <EOL> s . setsockopt ( socket . SOL_SOCKET , socket . SO_KEEPALIVE , <NUM_LIT:1> ) <EOL> return s <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> SocketHandler . __init__ ( self , * args , ** kwargs ) <EOL> self . retryStart = <NUM_LIT:0> <EOL> self . retryMax = <NUM_LIT:0> <EOL> self . retryFactor = <NUM_LIT:0> <EOL> def send ( self , s , attempt = <NUM_LIT:0> ) : <EOL> if attempt >= <NUM_LIT:10> : <EOL> print "<STR_LIT>" <EOL> return <EOL> if self . sock is None : <EOL> self . createSocket ( ) <EOL> if self . sock : <EOL> try : <EOL> self . sock . sendall ( s ) <EOL> return <EOL> except socket . error : <EOL> self . sock . close ( ) <EOL> self . sock = None <EOL> reactor . callLater ( <NUM_LIT> , lambda x : self . send ( s , attempt + <NUM_LIT:1> ) , None ) <EOL> def makePickle ( self , record ) : <EOL> return record . getMessage ( ) + "<STR_LIT:\n>" <EOL> class HpfeedsHandler ( logging . Handler ) : <EOL> def __init__ ( self , host , port , ident , secret , channels ) : <EOL> logging . Handler . __init__ ( self ) <EOL> self . host = str ( host ) <EOL> self . port = int ( port ) <EOL> self . ident = str ( ident ) <EOL> self . secret = str ( secret ) <EOL> self . channels = map ( str , channels ) <EOL> hpc = hpfeeds . new ( self . host , self . port , self . ident , self . secret ) <EOL> hpc . subscribe ( channels ) <EOL> self . hpc = hpc <EOL> def emit ( self , record ) : <EOL> try : <EOL> msg = self . format ( record ) <EOL> self . hpc . publish ( self . channels , msg ) <EOL> except : <EOL> print "<STR_LIT>" </s>
