# # 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(): logger.debug("Starting httpd section") section = Section("httpd") accesslog = readlog(config.prefs['logs']['httpd'] + '/access.log') a = len(accesslog.split('\n')) errorlog = readlog(config.prefs['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) 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)") request_data = Data() request_data.items = backticks(files) request_data.orderbyfreq() request_data.truncl(config.prefs['maxlist']) request_data.subtitle = plural(" request", a) section.append_data(request_data) if (ips != None): logger.debug("Parsing client statistics") client_data = Data() client_data.items = ips client_data.orderbyfreq() client_data.subtitle = plural(" client", str(len(ips))) client_data.truncl(config.prefs['maxlist']) section.append_data(client_data) if (useragents != None): logger.debug("Parsing user agent statistics") ua_data = Data() ua_data.items = useragents ua_data.orderbyfreq() n_ua = str(len(ua_data.items)) ua_data.truncl(config.prefs['maxlist']) ua_data.subtitle = plural(" user agent", n_ua) section.append_data(ua_data) section.append_data(Data(data_h + " transferred")) section.append_data(Data(plural(" error", e))) logger.info("Finished httpd section") return section