new parser class structure
[logparse.git] / logparse / parsers / httpd.py
index 0abf6edeb2a05432d59d05489b7fe3759e9f32de..2e6ae2ff6e8c3dec92f11b8d8fedb4271d34e7bd 100644 (file)
@@ -13,9 +13,7 @@ import re
 from logparse.formatting import *
 from logparse.util import readlog, resolve
 from logparse import config
-
-import logging
-logger = logging.getLogger(__name__)
+from logparse.load_parsers import Parser
 
 ACCESS_REGEX = "^\s*(\S+).*\"GET (\S+) HTTP(?:\/\d\.\d)?\" (\d{3}) (\d*) \".+\" \"(.*)\""
 
@@ -31,58 +29,65 @@ class AccessLine(object):
         self.bytes = int(fields.group(4))
         self.useragent = fields.group(5)
 
-def parse_log():
-
-    logger.debug("Starting httpd section")
-    section = Section("httpd")
-
-    accesslog = readlog(prefs("logs", "httpd-access"))
-
-    errorlog= readlog(prefs("logs", "httpd-error"))
-    total_errors = len(errorlog.splitlines())
+class Httpd(Parser):
 
-    logger.debug("Retrieved log data")
+    def __init__(self):
+        super().__init__()
+        self.name = "httpd"
+        self.info = "Analyse Apache (httpd) server logs, including data transferred, requests, clients, and errors."
 
-    logger.debug("Searching through access log")
+    def parse_log(self):
 
-    accesses = []
+        logger.debug("Starting httpd section")
+        section = Section("httpd")
 
-    for line in accesslog.splitlines():
-        if "GET" in line:
-            accesses.append(AccessLine(line))
+        accesslog = readlog(prefs("logs", "httpd-access"))
 
-    total_requests = len(accesses)
-    
-    section.append_data(Data("Total of " + plural("request", total_requests)))
-    section.append_data(Data(plural("error", total_errors)))
+        errorlog= readlog(prefs("logs", "httpd-error"))
+        total_errors = len(errorlog.splitlines())
 
-    size = Data()
-    size.subtitle = "Transferred " + parsesize(sum([ac.bytes for ac in accesses]))
-    section.append_data(size)
+        logger.debug("Retrieved log data")
 
-    clients = Data()
-    clients.items = [resolve(ac.client, config.prefs.get("httpd", "resolve-domains")) for ac in accesses]
-    clients.orderbyfreq()
-    clients.subtitle = "Received requests from " + plural("client", len(clients.items))
-    clients.truncl(config.prefs.getint("logparse", "maxlist"))
-    section.append_data(clients)
+        logger.debug("Searching through access log")
 
-    files = Data()
-    files.items = [ac.file for ac in accesses]
-    files.orderbyfreq()
-    files.subtitle = plural("file", len(files.items)) + " requested"
-    files.truncl(config.prefs.getint("logparse", "maxlist"))
-    section.append_data(files)
+        accesses = []
 
-    useragents = Data()
-    useragents.items = [ac.useragent for ac in accesses]
-    useragents.orderbyfreq()
-    useragents.subtitle = plural("user agent", len(useragents.items))
-    useragents.truncl(config.prefs.getint("logparse", "maxlist"))
-    section.append_data(useragents)
+        for line in accesslog.splitlines():
+            if "GET" in line:
+                accesses.append(AccessLine(line))
 
-    logger.info("httpd has received " + str(total_requests) + " requests with " + str(total_errors) + " errors")
-
-
-    logger.info("Finished httpd section")
-    return section
+        total_requests = len(accesses)
+        
+        section.append_data(Data("Total of " + plural("request", total_requests)))
+        section.append_data(Data(plural("error", total_errors)))
+
+        size = Data()
+        size.subtitle = "Transferred " + parsesize(sum([ac.bytes for ac in accesses]))
+        section.append_data(size)
+
+        clients = Data()
+        clients.items = [resolve(ac.client, config.prefs.get("httpd", "httpd-resolve-domains")) for ac in accesses]
+        clients.orderbyfreq()
+        clients.subtitle = "Received requests from " + plural("client", len(clients.items))
+        clients.truncl(config.prefs.getint("logparse", "maxlist"))
+        section.append_data(clients)
+
+        files = Data()
+        files.items = [ac.file for ac in accesses]
+        files.orderbyfreq()
+        files.subtitle = plural("file", len(files.items)) + " requested"
+        files.truncl(config.prefs.getint("logparse", "maxlist"))
+        section.append_data(files)
+
+        useragents = Data()
+        useragents.items = [ac.useragent for ac in accesses]
+        useragents.orderbyfreq()
+        useragents.subtitle = plural("user agent", len(useragents.items))
+        useragents.truncl(config.prefs.getint("logparse", "maxlist"))
+        section.append_data(useragents)
+
+        logger.info("httpd has received " + str(total_requests) + " requests with " + str(total_errors) + " errors")
+
+
+        logger.info("Finished httpd section")
+        return section