Skip to content

Commit 431bdf6

Browse files
committed
fix: use logging instead of fancylogger
1 parent e27a5f0 commit 431bdf6

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

lib/vsc/utils/nagios.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
@author: Luis Fernando Muñoz Mejías (Ghent University)
4242
"""
4343

44+
import logging
4445
import operator
4546
import os
4647
import pwd
@@ -50,12 +51,11 @@
5051
import time
5152

5253
from vsc.utils.cache import FileCache
53-
from vsc.utils.fancylogger import getLogger
54+
from vsc.utils.py2vs3 import is_string, FileNotFoundErrorExc
5455

55-
log = getLogger(__name__)
5656

5757
NAGIOS_CACHE_DIR = '/var/cache'
58-
NAGIOS_CACHE_FILENAME_TEMPLATE = '%s.nagios.json.gz'
58+
NAGIOS_CACHE_FILENAME_TEMPLATE = '%s.nagios'
5959

6060
NAGIOS_OK = 'OK'
6161
NAGIOS_WARNING = 'WARNING'
@@ -151,15 +151,13 @@ def __init__(self, nrange):
151151
@param nrange: nrange in [@][start:][end] format. If it is not a string, it is converted to
152152
string and that string should allow conversion to float.
153153
"""
154-
self.log = getLogger(self.__class__.__name__, fname=False)
155-
156-
if not isinstance(nrange, str):
154+
if not is_string(nrange):
157155
newnrange = str(nrange)
158-
self.log.debug("nrange %s of type %s, converting to string (%s)", str(nrange), type(nrange), newnrange)
156+
logging.debug("nrange %s of type %s, converting to string (%s)", str(nrange), type(nrange), newnrange)
159157
try:
160158
float(newnrange)
161159
except ValueError:
162-
self.log.raiseException("nrange %s (type %s) is not valid after conversion to string (newnrange %s)" %
160+
logging.raiseException("nrange %s (type %s) is not valid after conversion to string (newnrange %s)" %
163161
(str(nrange), type(nrange), newnrange))
164162
nrange = newnrange
165163

@@ -173,7 +171,7 @@ def parse(self, nrange):
173171
r = reg.search(nrange)
174172
if r:
175173
res = r.groupdict()
176-
self.log.debug("parse: nrange %s gave %s", nrange, res)
174+
logging.debug("parse: nrange %s gave %s", nrange, res)
177175

178176
start_txt = res['start']
179177
if start_txt is None:
@@ -184,26 +182,26 @@ def parse(self, nrange):
184182
try:
185183
start = float(start_txt)
186184
except ValueError:
187-
self.log.raiseException("Invalid start txt value %s" % start_txt)
185+
logging.raiseException("Invalid start txt value %s" % start_txt)
188186

189187
end = res['end']
190188
if end is not None:
191189
try:
192190
end = float(end)
193191
except ValueError:
194-
self.log.raiseException("Invalid end value %s" % end)
192+
logging.raiseException("Invalid end value %s" % end)
195193

196194
neg = res['neg'] is not None
197-
self.log.debug("parse: start %s end %s neg %s", start, end, neg)
195+
logging.debug("parse: start %s end %s neg %s", start, end, neg)
198196
else:
199-
self.log.raiseException('parse: invalid nrange %s.' % nrange)
197+
logging.raiseException('parse: invalid nrange %s.' % nrange)
200198

201199
def range_fn(test):
202200
# test inside nrange?
203201
try:
204202
test = float(test)
205203
except ValueError:
206-
self.log.raiseException("range_fn: can't convert test %s (type %s) to float" % (test, type(test)))
204+
logging.raiseException("range_fn: can't convert test %s (type %s) to float" % (test, type(test)))
207205

208206
start_res = True # default: -inf < test
209207
if start is not None:
@@ -219,7 +217,7 @@ def range_fn(test):
219217
if neg:
220218
tmp_res = operator.not_(tmp_res)
221219

222-
self.log.debug("range_fn: test %s start_res %s end_res %s result %s (neg %s)",
220+
logging.debug("range_fn: test %s start_res %s end_res %s result %s (neg %s)",
223221
test, start_res, end_res, tmp_res, neg)
224222
return tmp_res
225223

@@ -261,7 +259,7 @@ def __init__(self, header, filename, threshold, nagios_username="nagios", world_
261259

262260
self.nagios_username = nagios_username
263261

264-
self.log = getLogger(self.__class__.__name__, fname=False)
262+
logging = getLogger(self.__class__.__name__, fname=False)
265263

266264
def report_and_exit(self):
267265
"""Unzips the cache file and reads the JSON data back in, prints the data and exits accordingly.
@@ -271,13 +269,13 @@ def report_and_exit(self):
271269
try:
272270
nagios_cache = FileCache(self.filename, True)
273271
except (IOError, OSError):
274-
self.log.critical("Error opening file %s for reading", self.filename)
272+
logging.critical("Error opening file %s for reading", self.filename)
275273
unknown_exit("%s nagios gzipped JSON file unavailable (%s)" % (self.header, self.filename))
276274

277275
(timestamp, ((nagios_exit_code, nagios_exit_string), nagios_message)) = nagios_cache.load('nagios')
278276

279277
if self.threshold <= 0 or time.time() - timestamp < self.threshold:
280-
self.log.info("Nagios check cache file %s contents delivered: %s", self.filename, nagios_message)
278+
logging.info("Nagios check cache file %s contents delivered: %s", self.filename, nagios_message)
281279
print("%s %s" % (nagios_exit_string, nagios_message))
282280
sys.exit(nagios_exit_code)
283281
else:
@@ -296,10 +294,11 @@ def cache(self, nagios_exit, nagios_message):
296294
nagios_cache = FileCache(self.filename)
297295
nagios_cache.update('nagios', (nagios_exit, nagios_message), 0) # always update
298296
nagios_cache.close()
299-
self.log.info("Wrote nagios check cache file %s at about %s", self.filename, time.ctime(time.time()))
297+
logging.info("Wrote nagios check cache file %s at about %s", self.filename, time.ctime(time.time()))
300298
except (IOError, OSError):
301299
# 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)
300+
logging.error("Cannot save to the nagios gzipped JSON file (%s)" % self.filename)
301+
raise Exception()
303302

304303
try:
305304
p = pwd.getpwnam(self.nagios_username)
@@ -312,10 +311,11 @@ def cache(self, nagios_exit, nagios_message):
312311
if os.geteuid() == 0:
313312
os.chown(self.filename, p.pw_uid, p.pw_gid)
314313
else:
315-
self.log.warn("Not running as root: Cannot chown the nagios check file %s to %s",
314+
logging.warning("Not running as root: Cannot chown the nagios check file %s to %s",
316315
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))
316+
except (OSError, FileNotFoundErrorExc):
317+
logging.error("Cannot chown the nagios check file %s to the nagios user" % (self.filename))
318+
raise Exception()
319319

320320
return True
321321

0 commit comments

Comments
 (0)