logparse / parsers / zfs.pyon commit better log formatting and limit lines to 80 char (a12c41f)
   1#
   2#   zfs.py
   3#
   4#   Look through ZFS logs to find latest scrub and its output.
   5#   Note that ZFS doesn't normally produce logs in /var/log, so for this to
   6#   work, we must set up a cron job to dump `zpool iostat` into a file (hourly
   7#   is best):
   8#
   9#       zpool iostat > /var/log/zpool.log && zpool status >> /var/log/zpool.log
  10#
  11#   The file gets overwritten every hour, so if more than one scrub occurs
  12#   between logparse runs, it will only get the latest one.
  13#
  14#   TODO: add feature to specify pools to check in config file
  15#   TODO: set critical value for scrub data repair
  16#
  17
  18import re
  19import sys, traceback
  20
  21from logparse.formatting import *
  22from logparse.util import readlog
  23from logparse.config import prefs
  24from logparse.load_parsers import Parser
  25
  26class Zfs(Parser):
  27
  28    def __init__(self):
  29        super().__init__()
  30        self.name = "zfs"
  31        self.info = "Look through ZFS logs to find latest scrub and its output."
  32
  33    def parse_log(self):
  34
  35        logger.debug("Starting zfs section")
  36        section = Section("zfs")
  37
  38        zfslog = readlog(prefs.get("logs", "zfs"))
  39
  40        logger.debug("Analysing zpool log")
  41        pool = re.search('.*---\n(\w*)', zfslog).group(1)
  42        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)
  43        logger.debug("Found groups {0}".format(scrub.groups()))
  44        iostat = re.search('.*---\n\w*\s*(\S*)\s*(\S*)\s', zfslog)
  45        scrubrepairs = scruberrors = scrubdate = None
  46        alloc = iostat.group(1)
  47        free = iostat.group(2)
  48
  49        try:
  50            scrubrepairs = scrub.group(1)
  51            scruberrors = scrub.group(2)
  52            scrubdate = ' '.join(scrub.groups()[2:-1])
  53        except Exception as e:
  54            logger.debug("Error getting scrub data: " + str(e))
  55            traceback.print_exc(limit=2, file=sys.stdout)
  56
  57        if (scrubdate != None):
  58            scrub_data = Data("Scrub of " + pool + " on " + scrubdate)
  59            scrub_data.items = [scrubrepairs + " repaired", scruberrors + " errors", alloc + " used", free + " free"]
  60        else:
  61            scrub_data = Data(pool)
  62            scrub_data.items = [alloc + " used", free + " free"]
  63
  64        section.append_data(scrub_data)
  65
  66        logger.info("Finished zfs section")
  67        return section