new parser class structure
[logparse.git] / logparse / parsers / zfs.py
index 75d414eccbe59c031dd31bfab43084b7d26ca643..ddef5f2594462bdd4cfea878be5bacced1155460 100644 (file)
 import re
 import sys, traceback
 
-from formatting import *
+from logparse.formatting import *
 from logparse.util import readlog
 from logparse.config import prefs
+from logparse.load_parsers import Parser
 
-import logging
-logger = logging.getLogger(__name__)
+class Zfs(Parser):
 
-def parse_log():
+    def __init__(self):
+        super().__init__()
+        self.name = "zfs"
+        self.info = "Look through ZFS logs to find latest scrub and its output."
 
-    logger.debug("Starting zfs section")
-    section = Section("zfs")
+    def parse_log(self):
 
-    zfslog = readlog(prefs.get("logs", "zfs"))
+        logger.debug("Starting zfs section")
+        section = Section("zfs")
 
-    logger.debug("Analysing zpool log")
-    pool = re.search('.*---\n(\w*)', zfslog).group(1)
-    scrub = re.search('.* scrub repaired (\d+\s*\w+) in .* with (\d+) errors on (\w+)\s+(\w+)\s+(\d+)\s+(\d{1,2}:\d{2}):\d+\s+(\d{4})', zfslog)
-    logger.debug("Found groups {0}".format(scrub.groups()))
-    iostat = re.search('.*---\n\w*\s*(\S*)\s*(\S*)\s', zfslog)
-    scrubrepairs = scruberrors = scrubdate = None
-    alloc = iostat.group(1)
-    free = iostat.group(2)
+        zfslog = readlog(prefs.get("logs", "zfs"))
 
-    try:
-        scrubrepairs = scrub.group(1)
-        scruberrors = scrub.group(2)
-        scrubdate = ' '.join(scrub.groups()[2:-1])
-    except Exception as e:
-        logger.debug("Error getting scrub data: " + str(e))
-        traceback.print_exc(limit=2, file=sys.stdout)
+        logger.debug("Analysing zpool log")
+        pool = re.search('.*---\n(\w*)', zfslog).group(1)
+        scrub = re.search('.* scrub repaired (\d+\s*\w+) in .* with (\d+) errors on (\w+)\s+(\w+)\s+(\d+)\s+(\d{1,2}:\d{2}):\d+\s+(\d{4})', zfslog)
+        logger.debug("Found groups {0}".format(scrub.groups()))
+        iostat = re.search('.*---\n\w*\s*(\S*)\s*(\S*)\s', zfslog)
+        scrubrepairs = scruberrors = scrubdate = None
+        alloc = iostat.group(1)
+        free = iostat.group(2)
 
-    if (scrubdate != None):
-        scrub_data = Data("Scrub of " + pool + " on " + scrubdate)
-        scrub_data.items = [scrubrepairs + " repaired", scruberrors + " errors", alloc + " used", free + " free"]
-    else:
-        scrub_data = Data(pool)
-        scrub_data.items = [alloc + " used", free + " free"]
+        try:
+            scrubrepairs = scrub.group(1)
+            scruberrors = scrub.group(2)
+            scrubdate = ' '.join(scrub.groups()[2:-1])
+        except Exception as e:
+            logger.debug("Error getting scrub data: " + str(e))
+            traceback.print_exc(limit=2, file=sys.stdout)
 
-    section.append_data(scrub_data)
+        if (scrubdate != None):
+            scrub_data = Data("Scrub of " + pool + " on " + scrubdate)
+            scrub_data.items = [scrubrepairs + " repaired", scruberrors + " errors", alloc + " used", free + " free"]
+        else:
+            scrub_data = Data(pool)
+            scrub_data.items = [alloc + " used", free + " free"]
 
-    logger.info("Finished zfs section")
-    return section
+        section.append_data(scrub_data)
+
+        logger.info("Finished zfs section")
+        return section