add more docstrings
[logparse.git] / logparse / parsers / httpd.py
index 518af7adac3daa52418048a5f5dcf0c960c44f52..b86f1c1bd5b4830788ee5b627e00a196af6d835d 100644 (file)
 
 import re
 
-from ..formatting import *
-from ..util import readlog, resolve
-from .. import config
-
-import logging
-logger = logging.getLogger(__name__)
+from logparse.formatting import *
+from logparse.util import readlog, resolve
+from logparse import config
+from logparse.load_parsers import Parser
 
 ACCESS_REGEX = "^\s*(\S+).*\"GET (\S+) HTTP(?:\/\d\.\d)?\" (\d{3}) (\d*) \".+\" \"(.*)\""
 
@@ -31,62 +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(config.prefs['logs']['httpd'] + '/access.log')
-
-    errorlog = readlog(config.prefs['logs']['httpd'] + '/error.log')
-    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."
 
-    errors = []
-    notfound = []
-    unprivileged = []
+    def parse_log(self):
 
-    logger.debug("Searching through access log")
+        logger.debug("Starting httpd section")
+        section = Section("httpd")
 
-    accesses = []
+        accesslog = readlog(config.prefs.get("logs", "httpd-access"))
 
-    for line in accesslog.splitlines():
-        if "GET" in line:
-            accesses.append(AccessLine(line))
+        errorlog= readlog(config.prefs.get("logs", "httpd-error"))
+        total_errors = len(errorlog.splitlines())
 
-    total_requests = len(accesses)
-    
-    section.append_data(Data("Total of " + plural("request", total_requests)))
-    section.append_data(Data(plural("error", total_errors)))
+        logger.debug("Retrieved log data")
 
-    size = Data()
-    size.subtitle = "Transferred " + parsesize(sum([ac.bytes for ac in accesses]))
-    section.append_data(size)
+        logger.debug("Searching through access log")
 
-    clients = Data()
-    clients.items = [resolve(ac.client, "fqdn") for ac in accesses]
-    clients.orderbyfreq()
-    clients.subtitle = "Received requests from " + plural("client", len(clients.items))
-    clients.truncl(config.prefs['maxlist'])
-    section.append_data(clients)
+        accesses = []
 
-    files = Data()
-    files.items = [ac.file for ac in accesses]
-    files.orderbyfreq()
-    files.subtitle = plural("file", len(files.items)) + " requested"
-    files.truncl(config.prefs['maxlist'])
-    section.append_data(files)
+        for line in accesslog.splitlines():
+            if "GET" in line:
+                accesses.append(AccessLine(line))
 
-    useragents = Data()
-    useragents.items = [ac.useragent for ac in accesses]
-    useragents.orderbyfreq()
-    useragents.subtitle = plural("user agent", len(useragents.items))
-    useragents.truncl(config.prefs['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
+        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