Browse Source

Better use of logging framework

tags/0.3.2
Johann Schmitz 2 years ago
parent
commit
f98a22881a
1 changed files with 15 additions and 9 deletions
  1. 15
    9
      smartcheck/check.py

+ 15
- 9
smartcheck/check.py View File

@@ -6,6 +6,8 @@ import yaml
6 6
 import re
7 7
 import os
8 8
 
9
+logger = logging.getLogger(__name__)
10
+
9 11
 DEFAULT_DISKS_FILE=os.path.join(os.path.dirname(__file__), 'disks.yaml')
10 12
 
11 13
 INFORMATION_SECTION_START = '=== START OF INFORMATION SECTION ==='
@@ -133,9 +135,9 @@ class SMARTCheck(object):
133 135
         for dev in self.database:
134 136
             device_regexprs = dev['model'] if isinstance(dev['model'], list) else [dev['model']]
135 137
             if any(re.match(r, device_model, re.IGNORECASE) for r in device_regexprs):
136
-                logging.debug("Device exists in database (one of %s matches %s)" % (device_regexprs, self.device_model))
138
+                logger.debug("Device exists in database (one of %s matches %s)", device_regexprs, self.device_model)
137 139
                 return dev['attributes']
138
-        logging.debug("Device does not exist in database")
140
+        logger.debug("Device does not exist in database")
139 141
         return None
140 142
 
141 143
     def parse(self):
@@ -172,7 +174,7 @@ class SMARTCheck(object):
172 174
 
173 175
     def parse_data_section(self, s):
174 176
         if DATA_SECTION_START not in s:
175
-            logging.info("No data section found")
177
+            logger.info("No data section found")
176 178
             return {}
177 179
 
178 180
         start = s.index(DATA_SECTION_START)
@@ -244,7 +246,7 @@ class SMARTCheck(object):
244 246
         failed_attributes = {}
245 247
 
246 248
         for attrid, name, flag, value, worst, thresh, attr_type, updated, when_failed, raw_value in self.smart_data['attributes']:
247
-            logging.debug("Attribute %s (%s): value=%s, raw value=%s" % (attrid, name, value, raw_value))
249
+            logger.debug("Attribute %s (%s): value=%s, raw value=%s", attrid, name, value, raw_value)
248 250
             attrid = int(attrid)
249 251
             attr_name = (name or '').lower()
250 252
             int_value = toint(value)
@@ -318,8 +320,7 @@ class SMARTCheck(object):
318 320
                                                                     raw_value,
319 321
                                                                     "Attribute value dropped below threshold of %s" % int_thresh)
320 322
 
321
-
322
-        logging.debug("Failed generic attributes: %s" % (failed_attributes, ))
323
+        logger.debug("Failed generic attributes: %s" % failed_attributes)
323 324
         return failed_attributes
324 325
 
325 326
     def check_device_attributes(self):
@@ -343,9 +344,14 @@ class SMARTCheck(object):
343 344
                     check_value = value if value_field == "VALUE" else raw_value
344 345
                     check_value = int(check_value or -1)
345 346
 
347
+                    logger.debug("Value field: %s, min: %s, max: %s, value: %s, raw_value: %s",
348
+                        value, min_value, max_value, value, raw_value
349
+                    )
350
+
346 351
                     if not (int(min_value) <= check_value <= int(max_value)):
347
-                        logging.info("Attribute %s (%s) failed: not %s <= %s <= %s" % (attrid, name, min_value, check_value, max_value))
352
+                        logger.info("Attribute %s (%s) failed: not %s <= %s <= %s", attrid, name, min_value, check_value, max_value)
348 353
                         failed_attributes[(attrid, name)] = AttributeWarning(AttributeWarning.Critical, name, check_value)
354
+
349 355
                 elif isinstance(db_attrs, dict):
350 356
                     value_field = db_attrs.get('field', 'RAW_VALUE')
351 357
                     check_value = value if value_field == "VALUE" else raw_value
@@ -368,12 +374,12 @@ class SMARTCheck(object):
368 374
                                 (to_m and check_value <= int(to_m.group(1))) or \
369 375
                                 (from_to_m and (int(from_to_m.group(1)) <= check_value <= int(from_to_m.group(2)))):
370 376
 
371
-                                logging.info("Attribute %s (%s) failed with %s: not within treshold %s" % (attrid, name, failure_type, v))
377
+                                logger.info("Attribute %s (%s) failed with %s: not within treshold %s", attrid, name, failure_type, v)
372 378
                                 failed_attributes[(attrid, name)] = AttributeWarning(failure_type, name, check_value)
373 379
                     else:
374 380
                         if (min_value is not None and check_value >= int(min_value)) or \
375 381
                             (max_value is not None and check_value <= int(max_value)):
376
-                            logging.info("Attribute %s (%s) failed: not %s >= %s <= %s" % (attrid, name, min_value, check_value, max_value))
382
+                            logger.info("Attribute %s (%s) failed: not %s >= %s <= %s", attrid, name, min_value, check_value, max_value)
377 383
                             failed_attributes[(attrid, name)] = AttributeWarning(AttributeWarning.Critical, name, check_value)
378 384
 
379 385
                 else:

Loading…
Cancel
Save