<s> """<STR_LIT>""" <EOL> from zope . interface import classProvides <EOL> from twisted . internet import error as neterror <EOL> from twisted . python import failure <EOL> from snapy import netsnmp <EOL> from snapy . twisted import Session as SnmpSession <EOL> from nagcat import errors , query , util <EOL> class SNMPCommon ( query . Query ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , nagcat , conf ) : <EOL> super ( SNMPCommon , self ) . __init__ ( nagcat , conf ) <EOL> protocol = conf . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if protocol not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise errors . ConfigError ( conf , <EOL> "<STR_LIT>" % conf [ '<STR_LIT>' ] ) <EOL> if protocol == '<STR_LIT>' : <EOL> self . conf [ '<STR_LIT>' ] = '<STR_LIT>' % conf [ '<STR_LIT:path>' ] <EOL> else : <EOL> self . conf [ '<STR_LIT>' ] = '<STR_LIT>' % ( protocol , <EOL> self . addr , int ( conf . get ( '<STR_LIT:port>' , <NUM_LIT> ) ) ) <EOL> self . conf [ '<STR_LIT:version>' ] = str ( conf . get ( '<STR_LIT:version>' , '<STR_LIT>' ) ) <EOL> if self . conf [ '<STR_LIT:version>' ] not in ( '<STR_LIT:1>' , '<STR_LIT>' ) : <EOL> raise errors . ConfigError ( conf , <EOL> "<STR_LIT>" % conf [ '<STR_LIT:version>' ] ) <EOL> self . conf [ '<STR_LIT>' ] = conf . get ( '<STR_LIT>' , None ) <EOL> if not self . conf [ '<STR_LIT>' ] : <EOL> raise errors . ConfigError ( conf , "<STR_LIT>" ) <EOL> def check_oid ( self , conf , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> oid = netsnmp . OID ( conf [ key ] ) <EOL> except netsnmp . OIDValueError , ex : <EOL> raise errors . ConfigError ( conf , str ( ex ) ) <EOL> return oid <EOL> _use_bulk = True <EOL> @ staticmethod <EOL> def use_bulk ( value ) : <EOL> """<STR_LIT>""" <EOL> SNMPCommon . _use_bulk = bool ( value ) <EOL> class SNMPQuery ( SNMPCommon ) : <EOL> """<STR_LIT>""" <EOL> classProvides ( query . IQuery ) <EOL> name = "<STR_LIT>" <EOL> def __init__ ( self , nagcat , conf ) : <EOL> super ( SNMPQuery , self ) . __init__ ( nagcat , conf ) <EOL> if '<STR_LIT>' in conf : <EOL> if ( "<STR_LIT>" in conf or "<STR_LIT>" in conf or "<STR_LIT:key>" in conf ) : <EOL> raise errors . ConfigError ( conf , <EOL> "<STR_LIT>" ) <EOL> self . conf [ '<STR_LIT>' ] = self . check_oid ( conf , '<STR_LIT>' ) <EOL> conf [ '<STR_LIT>' ] = False <EOL> self . query_oid = nagcat . new_query ( conf , qcls = SNMPCombined ) <EOL> self . addDependency ( self . query_oid ) <EOL> elif ( "<STR_LIT>" in conf and "<STR_LIT>" in conf and "<STR_LIT:key>" in conf ) : <EOL> if "<STR_LIT>" in conf : <EOL> raise errors . ConfigError ( conf , <EOL> "<STR_LIT>" ) <EOL> self . conf [ '<STR_LIT>' ] = self . check_oid ( conf , '<STR_LIT>' ) <EOL> self . conf [ '<STR_LIT>' ] = self . check_oid ( conf , '<STR_LIT>' ) <EOL> self . conf [ '<STR_LIT:key>' ] = conf [ '<STR_LIT:key>' ] <EOL> conf [ '<STR_LIT>' ] = True <EOL> base = conf . copy ( ) <EOL> base [ '<STR_LIT>' ] = self . conf [ '<STR_LIT>' ] <EOL> self . query_base = nagcat . new_query ( base , qcls = SNMPCombined ) <EOL> self . addDependency ( self . query_base ) <EOL> key = conf . copy ( ) <EOL> key [ '<STR_LIT>' ] = self . conf [ '<STR_LIT>' ] <EOL> self . query_key = nagcat . new_query ( key , qcls = SNMPCombined ) <EOL> self . addDependency ( self . query_key ) <EOL> else : <EOL> raise errors . ConfigError ( conf , <EOL> "<STR_LIT>" ) <EOL> if "<STR_LIT>" in conf : <EOL> self . conf [ '<STR_LIT>' ] = self . check_oid ( conf , '<STR_LIT>' ) <EOL> scale = conf . copy ( ) <EOL> scale [ '<STR_LIT>' ] = self . conf [ '<STR_LIT>' ] <EOL> self . query_scale = nagcat . new_query ( scale , qcls = SNMPCombined ) <EOL> self . addDependency ( self . query_scale ) <EOL> else : <EOL> self . query_scale = None <EOL> def _start ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if "<STR_LIT>" in self . conf : <EOL> return self . _get_result ( ) <EOL> else : <EOL> return self . _get_result_set ( ) <EOL> except : <EOL> return errors . Failure ( ) <EOL> def _do_scale ( self , scale , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> scale = util . MathString ( scale ) <EOL> value = util . MathString ( value ) <EOL> except util . MathError : <EOL> raise errors . TestCritical ( <EOL> "<STR_LIT>" ) <EOL> return value * scale <EOL> def _get_result ( self ) : <EOL> """<STR_LIT>""" <EOL> result = self . query_oid . result <EOL> if isinstance ( result , failure . Failure ) : <EOL> return result <EOL> oid = self . conf [ '<STR_LIT>' ] <EOL> result = dict ( self . query_oid . result ) <EOL> if oid not in result : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ( oid , ) ) <EOL> result = result [ oid ] <EOL> if self . query_scale : <EOL> oid = self . conf [ '<STR_LIT>' ] <EOL> scale = dict ( self . query_scale . result ) <EOL> if oid not in result : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ( oid , ) ) <EOL> scale = scale [ oid ] <EOL> result = self . _do_scale ( scale , result ) <EOL> return str ( result ) <EOL> def _get_result_set ( self ) : <EOL> """<STR_LIT>""" <EOL> class Return ( Exception ) : <EOL> pass <EOL> def filter_result ( result , root ) : <EOL> if isinstance ( result , failure . Failure ) : <EOL> raise Return ( result ) <EOL> new = { } <EOL> for key , value in result : <EOL> if key . startswith ( self . conf [ root ] ) : <EOL> new [ key ] = value <EOL> if not new : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ( root , ) ) <EOL> return new <EOL> try : <EOL> base = filter_result ( self . query_base . result , "<STR_LIT>" ) <EOL> keys = filter_result ( self . query_key . result , "<STR_LIT>" ) <EOL> if self . query_scale : <EOL> scale = filter_result ( self . query_scale . result , "<STR_LIT>" ) <EOL> except Return , ex : <EOL> return ex . args [ <NUM_LIT:0> ] <EOL> final = None <EOL> for oid , value in keys . iteritems ( ) : <EOL> if value == self . conf [ "<STR_LIT:key>" ] : <EOL> index = oid [ len ( self . conf [ "<STR_LIT>" ] ) : ] <EOL> final = self . conf [ '<STR_LIT>' ] + index <EOL> if self . query_scale : <EOL> final_scale = self . conf [ '<STR_LIT>' ] + index <EOL> break <EOL> if final is None : <EOL> raise errors . TestCritical ( "<STR_LIT>" % self . conf [ "<STR_LIT:key>" ] ) <EOL> if final not in base : <EOL> raise errors . TestCritical ( "<STR_LIT>" % ( final , ) ) <EOL> result = base [ final ] <EOL> if self . query_scale : <EOL> if final_scale not in scale : <EOL> raise errors . TestCritical ( <EOL> "<STR_LIT>" % ( final_scale , ) ) <EOL> scale = scale [ final_scale ] <EOL> result = self . _do_scale ( scale , result ) <EOL> return str ( result ) <EOL> class SNMPCombined ( SNMPCommon ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> def __init__ ( self , nagcat , conf ) : <EOL> """<STR_LIT>""" <EOL> super ( SNMPCombined , self ) . __init__ ( nagcat , conf ) <EOL> self . oids = set ( ) <EOL> self . update ( conf ) <EOL> self . conf [ '<STR_LIT>' ] = conf [ '<STR_LIT>' ] <EOL> if self . conf [ '<STR_LIT:version>' ] == "<STR_LIT:1>" : <EOL> self . conf [ '<STR_LIT>' ] = self . oids <EOL> try : <EOL> self . client = SnmpSession ( <EOL> version = self . conf [ '<STR_LIT:version>' ] , <EOL> community = self . conf [ '<STR_LIT>' ] , <EOL> timeout = <NUM_LIT:1> , retrys = int ( self . conf [ '<STR_LIT>' ] ) , <EOL> peername = self . conf [ '<STR_LIT>' ] , <EOL> _use_bulk = self . _use_bulk ) <EOL> except netsnmp . SnmpError , ex : <EOL> raise errors . InitError ( "<STR_LIT>" % ex ) <EOL> def update ( self , conf ) : <EOL> """<STR_LIT>""" <EOL> super ( SNMPCombined , self ) . update ( conf ) <EOL> self . oids . add ( self . check_oid ( conf , '<STR_LIT>' ) ) <EOL> def _start ( self ) : <EOL> try : <EOL> self . client . open ( ) <EOL> if self . conf [ '<STR_LIT>' ] : <EOL> deferred = self . client . walk ( self . oids , strict = True ) <EOL> else : <EOL> deferred = self . client . get ( self . oids ) <EOL> except : <EOL> return errors . Failure ( ) <EOL> deferred . addBoth ( self . _handle_close ) <EOL> deferred . addErrback ( self . _handle_error ) <EOL> return deferred <EOL> @ errors . callback <EOL> def _handle_close ( self , result ) : <EOL> """<STR_LIT>""" <EOL> self . client . close ( ) <EOL> return result <EOL> @ errors . callback <EOL> def _handle_error ( self , result ) : <EOL> if isinstance ( result . value , neterror . TimeoutError ) : <EOL> raise errors . TestCritical ( "<STR_LIT>" ) <EOL> return result </s>
