# # httpd.py # # Analyse Apache (httpd) server logs, including data transferred, requests, # clients, and errors. Note that Apache's logs can get filled up very quickly # with the default verbosity, leading to logparse taking a very long time to # analyse them. In general the default verbosity is good, but logs should be # cleared as soon as they are analysed (make sure 'rotate' is set to 'y'). # import re from ..formatting import * from ..util import readlog, resolve from .. import config import logging logger = logging.getLogger(__name__) def parse_log(): output = '' logger.debug("Starting httpd section") output += opentag('div', 1, 'httpd', 'section') accesslog = readlog(config['logs']['httpd'] + '/access.log') a = len(accesslog.split('\n')) errorlog = readlog(config['logs']['httpd'] + '/error.log') e = len(errorlog.split('\n')) data_b = 0 ips = [] files = [] useragents = [] errors = [] notfound = [] unprivileged = [] logger.debug("Searching through access log") for line in accesslog.split('\n'): fields = re.search('^(\S*) .*GET (\/.*) HTTP/\d\.\d\" 200 (\d*) \"(.*)\".*\((.*)\;', line) try: ips.append(resolve(fields.group(1), fqdn=config.prefs['httpd']['resolve-domains'])) files.append(fields.group(2)) useragents.append(fields.group(5)) data_b += int(fields.group(3)) except Exception as error: if type(error) is AttributeError: # this line is not an access log pass else: logger.warning("Error processing httpd access log: " + str(error)) traceback.print_exc() data_h = parsesize(data_b) output += writetitle("apache") logger.info("httpd has transferred " + str(data_b) + " bytes in response to " + str(a) + " requests with " + str(e) + " errors") if (a > 0): logger.debug("Parsing request statistics (this might take a while)") files = addtag(files, 'code') files = orderbyfreq(files) files = truncl(files, config.prefs['maxlist']) output += writedata(plural(" request", a), files) if (ips != None): logger.debug("Parsing client statistics") ips = addtag(ips, 'code') ips = orderbyfreq(ips) n_ip = str(len(ips)) ips = truncl(ips, config.prefs['maxlist']) output += writedata(plural(" client", n_ip), ips) if (useragents != None): logger.debug("Parsing user agent statistics") useragents = addtag(useragents, 'code') useragents = orderbyfreq(useragents) n_ua = str(len(useragents)) useragents = truncl(useragents, config.prefs['maxlist']) output += writedata(plural(" device", n_ua), useragents) output += writedata(data_h + " transferred") output += writedata(plural(" error", e)) output += closetag('div', 1) logger.info("Finished httpd section") return output