U Ó gGã@sšdZdgZddlZddlZddlZddlmZddlmZm Z m Z m Z da da dadadd „ZGd d„deƒZdd d „Zdd d„Zddd„Zddd„ZdS)zh!This module runs the NCO dbn_alert program, or logs dbn_alert messages if run with dbn alerts disabled.ÚDBNAlertéN)ÚRunner)ÚcheckrunÚbatchexeÚaliasÚrunTFcCsRtj}| dd¡dkr0tttj |dd¡ƒƒStsBt  d¡dattdƒƒSdS)zv!Locates the dbn_alert executable based on environment variables, and returns it as a produtil.prog.Runner object.ÚDBNROOTÚz bin/dbn_alertz>$DBNROOT is not set. Will search for dbn_alert in your $PATH.TÚ dbn_alertN) ÚosÚenvironÚgetrrÚpathÚjoinÚno_DBNROOT_warnÚlogÚwarning)ÚENV©rú>/lfs/h1/ops/prod/packages/hmon.v3.2.7/ush/produtil/dbnalert.pyÚfind_dbn_alert!s rc@s(eZdZdZejdfdd„Zdd„ZdS)rz÷!This class represents a call to dbn_alert, as a callable Python object. It allows the instructions on how to make the call to be stored for later use by a produtil.datastore.Product object's add_callback and call_callbacks functions.NcCs„t|tƒst|tƒstdƒ‚|dk r@t|tƒs@t|tƒs@tdƒ‚dd„|Dƒ|_t|tƒrbt|ƒ}||_|jdkrzt ƒ|_||_ dS)a!Create a new DBNAlert object that can be used to send an alert later on. @param args The arguments to dbn_alert. @param alert_exe The dbn_alert executable name. @param loglevel A Python logging level to log messages before each alert.z[In DBNAlert(), the first argument must be a list or tuple of arguments to send to dbn_alertNzhIn DBNAlert(), the alert_exe argument must be a string executable name, or a produtil.prog.Runner objectcSsg|] }t|ƒ‘qSr)Ústr©Ú.0ÚsrrrÚ Bsz%DBNAlert.__init__..) Ú isinstanceÚlistÚtupleÚ TypeErrorrrÚ alert_argsrÚ alert_exerÚloglevel)ÚselfÚargsr"r!rrrÚ__init__5sÿ  zDBNAlert.__init__c sÊtdk s t‚ttƒˆd<‡fdd„|jDƒ}tr t|jtƒrLt|jƒ|}n |j|}t t j dt |ƒ¡t |ƒ}|dkr‚t j nt j }t |dt |ƒf¡n&t |jd¡t |jd d  |¡¡dS) a*!Expands strings specified in the constructor and calls dbn_alert with the results. If dbn alerts are disabled, then the fact that a dbn alert would be run is logged, but dbn_alert is NOT called. @param kwargs string formatting variables for the dbn alert argumentsNÚjobcsg|]}|jfˆŽ‘qSr)Úformatr©ÚkwargsrrrXsz%DBNAlert.__call__..z DBN Alert: rzExit status %s from dbn_alert.zdbn_alert is disabledzwould run: dbn_alert ú )r&ÚAssertionErrorrr Úsend_dbn_alertsrr!rrÚloggingÚINFOÚreprrÚERRORr"r)r#r)r ÚcmdÚretZexit_log_levelrr(rÚ__call__Ps    zDBNAlert.__call__)Ú__name__Ú __module__Ú __qualname__Ú__doc__r-ÚWARNINGr%r3rrrrr0scCs0|dkrt d¡}nt|tƒr(t |¡}|adS)z‹!Initializes logging for this module. The argument is either a logging.Logger to log to, or the string name of the logging domain.Nr )r-Ú getLoggerrrr)ÚloggerrrrÚ init_logginggs    r;cCsÆ|dk rt|ƒadStj}d|kr2t|dƒadSd\}}dD],}||kr>||dkr>tj ||¡}qlq>|dkrxd}dD]$}||kr|||dkr|t||ƒ}q||dkr¶tt ¡ƒ}d||fadS) z}!Sets the job string (for dbn_alerts) to the specified value, or if unspecified, tries to guess one from the environment.Nr&)NN)Ú LSB_JOBNAMEÚ PBS_JOBNAMEÚ MOAB_JOBNAMEr Úunknown)Ú LSB_JOBIDÚ PBS_JOBIDÚ MOAB_JOBIDzLL%s.%s)rr&r r rÚbasenameÚgetpid)ÚjobnamerÚnameZiidZnamevarZiidvarrrrÚinit_jobstringss(  rGcCsðt d¡}tj}da|dk r(|s(|adSd|kr<| d¡n||d ¡dkrb| d|df¡nV|dkršd| d d ¡ ¡katrŽ| d ¡q¸| d ¡n|atr®| d ¡n | d¡d|krÐ| d¡dan| d¡dkrì| d¡dadS)a{!DBN alert initialization helper function. This is part of the implementation of init_module: it decides whether to send DBNet alerts, and sets the module-scope send_dbn_alerts variable accordingly. That will then be used by DBNAlert objects to decide whether to actually call the dbn_alert program. @protected @param send_dbn Do we send dbn alerts?r FNZ RUN_ENVIRz*RUN_ENVIR is unset. Disabling DBN alerts.ZNCOz1RUN_ENVIR=%s is not "NCO". Disabling DBN alerts.ÚYESZSENDDBNÚNOz'Enabling dbn_alerts because SENDDBN=YESz/Disabling dbn_alerts because SENDDBN is not YESzLEnabling dbn_alerts because the code manually turned them on (send_dbn=True)zODisabling dbn_alerts because the code manually turned them off (send_dbn=False)rz(DBNROOT is unset. Disabling DBN alerts.Z DBNROOT_OFFz5DBNROOT is set to DBNROOT_OFF. Disabling DBN alerts.) r-r9r r r,rÚupperÚinfor )Úsend_dbnr:rrrrÚinit_dbn_alert”s:   ÿ     ÿrMcCst|ƒt|ƒt|ƒdS)aÎ!Call to initialize this module. Initializes the logging and job string for this module. @param logger Either a logging.Logger object to receive log messages, or the string name of a logger domain. @param jobname The dbn_alert job string for this job. @param send_dbn Optional. If specified, this controls whether dbn_alert is actually run (True) or not (False). If unspecified, then the SENDDBN environment variable is used.N)rGr;rM)r:rErLrrrÚ init_moduleÃs rN)N)N)N)NNN)r7Ú__all__r-r Ú produtil.runÚprodutilÚ produtil.progrrrrrrr&r,rrÚobjectrr;rGrMrNrrrrÚs 7 ! /