@@ -84,13 +84,13 @@ def _real_exit(message, code, metrics=''):
8484 message = message .split ('|' )
8585 msg = message [0 ]
8686 if len (message ) > 1 :
87- metrics = '|%s' % message [1 ]
87+ metrics = f'| { message [1 ]} '
8888 if len (msg ) > NAGIOS_MAX_MESSAGE_LENGTH :
8989 # log long message but print truncated message
9090 log .info ("Nagios report %s: %s%s" , exit_text , msg , metrics )
9191 msg = msg [:NAGIOS_MAX_MESSAGE_LENGTH - 3 ] + '...'
9292
93- print ("%s %s%s" % ( exit_text , msg , metrics ) )
93+ print (f" { exit_text } { msg } { metrics } " )
9494 sys .exit (exit_code )
9595
9696
@@ -140,7 +140,7 @@ def exit_from_errorcode(errorcode, msg, error_map=None):
140140 try :
141141 NAGIOS_EXIT_MAP [e_map [errorcode ]](msg )
142142 except (IndexError , KeyError ):
143- unknown_exit (msg + " (errorcode {0 } not found in {1}" . format ( errorcode , e_map ) )
143+ unknown_exit (f" { msg } (errorcode { errorcode } not found in { e_map } " )
144144
145145
146146class NagiosRange (object ):
@@ -158,9 +158,13 @@ def __init__(self, nrange):
158158 self .log .debug ("nrange %s of type %s, converting to string (%s)" , str (nrange ), type (nrange ), newnrange )
159159 try :
160160 float (newnrange )
161- except ValueError :
162- self .log .raiseException ("nrange %s (type %s) is not valid after conversion to string (newnrange %s)" %
163- (str (nrange ), type (nrange ), newnrange ))
161+ except ValueError as exc :
162+ msg = (
163+ f"nrange { str (nrange )} (type { type (nrange )} ) is not valid after"
164+ f" conversion to string (newnrange { newnrange } )"
165+ )
166+ self .log .exception (msg )
167+ raise ValueError (msg ) from exc
164168 nrange = newnrange
165169
166170 self .range_fn = self .parse (nrange )
@@ -183,27 +187,35 @@ def parse(self, nrange):
183187 else :
184188 try :
185189 start = float (start_txt )
186- except ValueError :
187- self .log .raiseException ("Invalid start txt value %s" % start_txt )
190+ except ValueError as exc :
191+ msg = f"Invalid start txt value { start_txt } "
192+ self .log .exception (msg )
193+ raise ValueError (msg ) from exc
188194
189195 end = res ['end' ]
190196 if end is not None :
191197 try :
192198 end = float (end )
193- except ValueError :
194- self .log .raiseException ("Invalid end value %s" % end )
199+ except ValueError as exc :
200+ msg = f"Invalid end value { end } "
201+ self .log .Exception ("msg" )
202+ raise ValueError (msg ) from exc
195203
196204 neg = res ['neg' ] is not None
197205 self .log .debug ("parse: start %s end %s neg %s" , start , end , neg )
198206 else :
199- self .log .raiseException ('parse: invalid nrange %s.' % nrange )
207+ msg = f"parse: invalid nrange { nrange } ."
208+ self .log .Error (msg )
209+ raise ValueError (nrange )
200210
201211 def range_fn (test ):
202212 # test inside nrange?
203213 try :
204214 test = float (test )
205- except ValueError :
206- self .log .raiseException ("range_fn: can't convert test %s (type %s) to float" % (test , type (test )))
215+ except ValueError as exc :
216+ msg = f"range_fn: can't convert test { test } (type { type (test )} ) to float"
217+ self .log .Exception (msg )
218+ raise ValueError (msg ) from exc
207219
208220 start_res = True # default: -inf < test
209221 if start is not None :
@@ -272,16 +284,16 @@ def report_and_exit(self):
272284 nagios_cache = FileCache (self .filename , True )
273285 except (IOError , OSError ):
274286 self .log .critical ("Error opening file %s for reading" , self .filename )
275- unknown_exit ("%s nagios gzipped JSON file unavailable (%s)" % ( self .header , self . filename ) )
287+ unknown_exit (f" { self . header } nagios gzipped JSON file unavailable ({ self .filename } )" )
276288
277289 (timestamp , ((nagios_exit_code , nagios_exit_string ), nagios_message )) = nagios_cache .load ('nagios' )
278290
279291 if self .threshold <= 0 or time .time () - timestamp < self .threshold :
280292 self .log .info ("Nagios check cache file %s contents delivered: %s" , self .filename , nagios_message )
281- print ("%s %s" % ( nagios_exit_string , nagios_message ) )
293+ print (f" { nagios_exit_string } { nagios_message } " )
282294 sys .exit (nagios_exit_code )
283295 else :
284- unknown_exit ("%s gzipped JSON file too old (timestamp = %s)" % ( self . header , time .ctime (timestamp )) )
296+ unknown_exit (f" { self . header } gzipped JSON file too old (timestamp = { time .ctime (timestamp )} )" )
285297
286298 def cache (self , nagios_exit , nagios_message ):
287299 """Store the result in the cache file with a timestamp.
@@ -297,9 +309,11 @@ def cache(self, nagios_exit, nagios_message):
297309 nagios_cache .update ('nagios' , (nagios_exit , nagios_message ), 0 ) # always update
298310 nagios_cache .close ()
299311 self .log .info ("Wrote nagios check cache file %s at about %s" , self .filename , time .ctime (time .time ()))
300- except (IOError , OSError ):
312+ except (IOError , OSError ) as exc :
301313 # raising an error is ok, since we usually do this as the very last thing in the script
302- self .log .raiseException ("Cannot save to the nagios gzipped JSON file (%s)" % self .filename )
314+ msg = f"Cannot save to the nagios gzipped JSON file ({ self .filename } )"
315+ self .log .Exception (msg )
316+ raise OSError (msg ) from exc
303317
304318 try :
305319 p = pwd .getpwnam (self .nagios_username )
@@ -312,10 +326,12 @@ def cache(self, nagios_exit, nagios_message):
312326 if os .geteuid () == 0 :
313327 os .chown (self .filename , p .pw_uid , p .pw_gid )
314328 else :
315- self .log .warn ("Not running as root: Cannot chown the nagios check file %s to %s" ,
329+ self .log .warning ("Not running as root: Cannot chown the nagios check file %s to %s" ,
316330 self .filename , self .nagios_username )
317- except (OSError , FileNotFoundError ):
318- self .log .raiseException ("Cannot chown the nagios check file %s to the nagios user" % (self .filename ))
331+ except (OSError , FileNotFoundError ) as exc :
332+ msg = f"Cannot chown the nagios check file { self .filename } to the nagios user"
333+ self .log .Exception (msg )
334+ raise (OSError (msg )) from exc
319335
320336 return True
321337
@@ -399,11 +415,10 @@ def __str__(self):
399415 perf = []
400416 for k , v in sorted (processed_dict .items ()):
401417 if ' ' in k :
402- k = "'%s'" % k
403- perf .append ("%s=%s%s;%s;%s;" % (k , v .get ('value' , '' ), v .get ('unit' , '' ),
404- v .get ('warning' , '' ), v .get ('critical' , '' )))
418+ k = f"'{ k } '"
419+ perf .append (f"{ k } ={ v .get ('value' , '' )} { v .get ('unit' , '' )} ;{ v .get ('warning' , '' )} ;{ v .get ('critical' , '' )} ;" )
405420
406- return "%s | %s" % ( self .message , ' ' .join (perf ))
421+ return f" { self .message } | { ' ' .join (perf )} "
407422
408423
409424class SimpleNagios (NagiosResult ):
0 commit comments