| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import division |
| 20 |
|
| 21 |
from datetime import datetime |
| 22 |
import logging |
| 23 |
import re |
| 24 |
from statistics import mean |
| 25 |
|
| 26 |
from ipapython import admintool |
| 27 |
from ipalib.facts import is_ipa_configured |
| 28 |
|
| 29 |
|
| 30 |
TIME_RE = re.compile( |
| 31 |
r'\[(?P<date>.*)\] \[.*\].* \[pid \d+:tid \d+\] \[remote .*\] ' |
| 32 |
r'ipa: DEBUG: \[jsonserver_session\] (?P<principal>\S+): ' |
| 33 |
r'(?P<command>\S+)/1\(.*\): (?P<result>\S+) etime=(?P<etime>\d+)' |
| 34 |
) |
| 35 |
|
| 36 |
DATE_FORMAT = '%a %b %d %H:%M:%S.%f %Y' |
| 37 |
|
| 38 |
logger = logging.getLogger(__name__) |
| 39 |
|
| 40 |
|
| 41 |
class parselog(admintool.AdminTool): |
| 42 |
command_name = "parselog" |
| 43 |
|
| 44 |
usage = "%prog [options]" |
| 45 |
description = "Parse the Apache error log for performance data. " \ |
| 46 |
"Enable debugging by creating /etc/ipa/server.conf with " \ |
| 47 |
"the contents: [global]\\ndebug = True" |
| 48 |
|
| 49 |
def __init__(self, options, args): |
| 50 |
super(parselog, self).__init__(options, args) |
| 51 |
self.times = {} |
| 52 |
self.exceptions = {} |
| 53 |
self.since = None |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def add_options(cls, parser): |
| 57 |
super(parselog, cls).add_options(parser, debug_option=True) |
| 58 |
parser.add_option( |
| 59 |
"--command", |
| 60 |
dest="command", |
| 61 |
action="store", |
| 62 |
default=None, |
| 63 |
help="Command to analyze", |
| 64 |
) |
| 65 |
parser.add_option( |
| 66 |
"--start-time", |
| 67 |
dest="start_time", |
| 68 |
action="store", |
| 69 |
default=None, |
| 70 |
help="time to begin analyzing logfile from", |
| 71 |
) |
| 72 |
parser.add_option( |
| 73 |
"--file", |
| 74 |
dest="file", |
| 75 |
action="store", |
| 76 |
default="/var/log/httpd/error_log", |
| 77 |
help="Log file to parse", |
| 78 |
) |
| 79 |
|
| 80 |
def validate_options(self): |
| 81 |
super(parselog, self).validate_options(needs_root=True) |
| 82 |
|
| 83 |
if self.options.start_time: |
| 84 |
self.since = datetime.strptime( |
| 85 |
self.options.start_time, |
| 86 |
DATE_FORMAT |
| 87 |
) |
| 88 |
|
| 89 |
def display_times(self, data, title, empty): |
| 90 |
print(title) |
| 91 |
output = False |
| 92 |
for command in data: |
| 93 |
if not data[command]: |
| 94 |
continue |
| 95 |
|
| 96 |
if len(data[command]) > 5: |
| 97 |
meantime = mean(sorted(data[command])[1:-1]) |
| 98 |
num = len(data[command]) - 2 |
| 99 |
else: |
| 100 |
meantime = mean(data[command]) |
| 101 |
num = len(data[command]) |
| 102 |
print( |
| 103 |
' Mean %s: %0.f ns of %d executions' % ( |
| 104 |
command, |
| 105 |
meantime, |
| 106 |
num) |
| 107 |
) |
| 108 |
output = True |
| 109 |
if not output: |
| 110 |
print(f' No {empty} found') |
| 111 |
|
| 112 |
def run(self): |
| 113 |
super(parselog, self).run() |
| 114 |
|
| 115 |
if not is_ipa_configured(): |
| 116 |
logger.error("IPA server is not configured on this system.") |
| 117 |
raise admintool.ScriptError() |
| 118 |
|
| 119 |
with open(self.options.file, 'r') as f: |
| 120 |
data = f.read() |
| 121 |
|
| 122 |
matches = list(re.finditer(TIME_RE, data)) |
| 123 |
|
| 124 |
if self.options.command: |
| 125 |
command = self.options.command.replace('-', '_') |
| 126 |
else: |
| 127 |
command = None |
| 128 |
|
| 129 |
for match in matches: |
| 130 |
if self.since: |
| 131 |
logtime = datetime.strptime(match.group('date'), DATE_FORMAT) |
| 132 |
if logtime < self.since: |
| 133 |
continue |
| 134 |
if command is None or match.group('command') == command: |
| 135 |
cmd = match.group('command') |
| 136 |
if cmd not in self.times: |
| 137 |
self.times[cmd] = [] |
| 138 |
if cmd not in self.exceptions: |
| 139 |
self.exceptions[cmd] = [] |
| 140 |
if match.group('result') == 'SUCCESS': |
| 141 |
self.times[cmd].append(float(match.group('etime'))) |
| 142 |
else: |
| 143 |
self.exceptions[cmd].append(float(match.group('etime'))) |
| 144 |
|
| 145 |
if self.times or self.exceptions: |
| 146 |
self.display_times(self.times, "Successful commands:", "commands") |
| 147 |
self.display_times(self.exceptions, "Exceptions:", "exceptions") |
| 148 |
else: |
| 149 |
print('No commands or exceptions found') |
| 150 |
|
| 151 |
|
| 152 |
if __name__ == '__main__': |
| 153 |
parselog.run_cli() |