rework parser loading interface
[logparse.git] / logparse / parsers / zfs.py
index 09db33ee935f730e79b125f3d2775a9262dfef63..ddef5f2594462bdd4cfea878be5bacced1155460 100644 (file)
 import re
 import sys, traceback
 
-from ..formatting import *
-from ..util import readlog, resolve
-from .. import config
-
-import logging
-logger = logging.getLogger(__name__)
-
-def parse_log():
-    output = ''
-    logger.debug("Starting zfs section")
-    output += opentag('div', 1, 'zfs', 'section')
-    zfslog = readlog(config.prefs['logs']['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
-    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)
-    alloc = iostat.group(1)
-    free = iostat.group(2)
-    output += writetitle("zfs")
-    if (scrubdate != None):
-        subtitle = "Scrub of " + pool + " on " + scrubdate
-        data = [scrubrepairs + " repaired", scruberrors + " errors", alloc + " used", free + " free"]
-    else:
-        subtitle = pool
-        data = [alloc + " used", free + " free"]
-    output += writedata(subtitle, data)
-    output += closetag('div', 1)
-    logger.info("Finished zfs section")
-    return output
+from logparse.formatting import *
+from logparse.util import readlog
+from logparse.config import prefs
+from logparse.load_parsers import Parser
+
+class Zfs(Parser):
+
+    def __init__(self):
+        super().__init__()
+        self.name = "zfs"
+        self.info = "Look through ZFS logs to find latest scrub and its output."
+
+    def parse_log(self):
+
+        logger.debug("Starting zfs section")
+        section = Section("zfs")
+
+        zfslog = readlog(prefs.get("logs", "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)
+
+        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)
+
+        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"]
+
+        section.append_data(scrub_data)
+
+        logger.info("Finished zfs section")
+        return section