<s> from datetime import timedelta <EOL> from expvar . stats import stats <EOL> import logging <EOL> from oid_translate import ObjectId <EOL> from pyasn1 . codec . ber import decoder <EOL> from pyasn1 . type . error import ValueConstraintError <EOL> from pysnmp . proto import api <EOL> from pysnmp . proto . error import ProtocolError <EOL> import socket <EOL> from sqlalchemy . exc import IntegrityError , InvalidRequestError , OperationalError <EOL> from trapperkeeper . dde import DdeNotification <EOL> from trapperkeeper . constants import SNMP_VERSIONS <EOL> from trapperkeeper . models import Notification <EOL> from trapperkeeper . utils import parse_time_string , send_trap_email <EOL> try : <EOL> from trapperkeeper_dde_plugin import run as dde_run <EOL> except ImportError as err : <EOL> def dde_run ( notification ) : <EOL> pass <EOL> class TrapperCallback ( object ) : <EOL> def __init__ ( self , conn , template_env , config , resolver , community ) : <EOL> self . conn = conn <EOL> self . template_env = template_env <EOL> self . config = config <EOL> self . hostname = socket . gethostname ( ) <EOL> self . resolver = resolver <EOL> self . community = community <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> try : <EOL> self . _call ( * args , ** kwargs ) <EOL> except Exception as err : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . exception ( "<STR_LIT>" , err ) <EOL> def _send_mail ( self , handler , trap , is_duplicate ) : <EOL> if is_duplicate and not handler [ "<STR_LIT>" ] : <EOL> return <EOL> mail = handler [ "<STR_LIT>" ] <EOL> if not mail : <EOL> return <EOL> recipients = handler [ "<STR_LIT>" ] . get ( "<STR_LIT>" ) <EOL> if not recipients : <EOL> return <EOL> subject = handler [ "<STR_LIT>" ] [ "<STR_LIT>" ] % { <EOL> "<STR_LIT>" : trap . oid , <EOL> "<STR_LIT>" : ObjectId ( trap . oid ) . name , <EOL> "<STR_LIT>" : trap . host , <EOL> "<STR_LIT>" : self . resolver . hostname_or_ip ( trap . host ) , <EOL> } <EOL> ctxt = dict ( trap = trap , dest_host = self . hostname ) <EOL> try : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> send_trap_email ( recipients , "<STR_LIT>" , <EOL> subject , self . template_env , ctxt ) <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> except socket . error as err : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . warning ( "<STR_LIT>" , err ) <EOL> def _call ( self , transport_dispatcher , transport_domain , transport_address , whole_msg ) : <EOL> if not whole_msg : <EOL> return <EOL> msg_version = int ( api . decodeMessageVersion ( whole_msg ) ) <EOL> if msg_version in api . protoModules : <EOL> proto_module = api . protoModules [ msg_version ] <EOL> else : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . error ( "<STR_LIT>" , msg_version ) <EOL> return <EOL> host = transport_address [ <NUM_LIT:0> ] <EOL> version = SNMP_VERSIONS [ msg_version ] <EOL> try : <EOL> req_msg , whole_msg = decoder . decode ( whole_msg , asn1Spec = proto_module . Message ( ) , ) <EOL> except ( ProtocolError , ValueConstraintError ) as err : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . warning ( "<STR_LIT>" , version , host , err ) <EOL> return <EOL> req_pdu = proto_module . apiMessage . getPDU ( req_msg ) <EOL> community = proto_module . apiMessage . getCommunity ( req_msg ) <EOL> if self . community and community != self . community : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . warning ( "<STR_LIT>" , host , community ) <EOL> return <EOL> if not req_pdu . isSameTypeWith ( proto_module . TrapPDU ( ) ) : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . warning ( "<STR_LIT>" , host ) <EOL> return <EOL> if msg_version not in ( api . protoVersion1 , api . protoVersion2c ) : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . warning ( "<STR_LIT>" ) <EOL> return <EOL> trap = Notification . from_pdu ( host , proto_module , version , req_pdu ) <EOL> if trap is None : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . warning ( "<STR_LIT>" , host , req_pdu ) <EOL> return <EOL> dde = DdeNotification ( trap , self . config . handlers [ trap . oid ] ) <EOL> dde_run ( dde ) <EOL> handler = dde . handler <EOL> trap . severity = handler [ "<STR_LIT>" ] <EOL> trap . manager = self . hostname <EOL> if handler . get ( "<STR_LIT>" , None ) : <EOL> expires = parse_time_string ( handler [ "<STR_LIT>" ] ) <EOL> expires = timedelta ( ** expires ) <EOL> trap . expires = trap . sent + expires <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> objid = ObjectId ( trap . oid ) <EOL> if handler . get ( "<STR_LIT>" , False ) : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> logging . debug ( "<STR_LIT>" , objid . name , host ) <EOL> return <EOL> logging . info ( "<STR_LIT>" , objid . name , host ) <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> duplicate = False <EOL> try : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . conn . add ( trap ) <EOL> self . conn . commit ( ) <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> except OperationalError as err : <EOL> self . conn . rollback ( ) <EOL> logging . warning ( "<STR_LIT>" , err ) <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> except InvalidRequestError as err : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> self . conn . rollback ( ) <EOL> logging . warning ( "<STR_LIT>" , err ) <EOL> except IntegrityError as err : <EOL> stats . incr ( "<STR_LIT>" , <NUM_LIT:1> ) <EOL> duplicate = True <EOL> self . conn . rollback ( ) <EOL> logging . info ( "<STR_LIT>" , objid . name , host ) <EOL> logging . debug ( err ) <EOL> self . _send_mail ( handler , trap , duplicate ) </s>
