| 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 |
|
| 25 |
from ipapython import admintool |
| 26 |
from ipalib.facts import is_ipa_configured |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
TIME_RE = re.compile( |
| 31 |
r'\[(?P<date>.*)\] \[.*\].* \[pid \d+:tid \d+\] \[remote .*\] ' |
| 32 |
r'ipa: DEBUG: FINAL: Hits (?P<hits>\d+) Misses (?P<misses>\d+) ' |
| 33 |
r'Size (?P<size>\d+)' |
| 34 |
) |
| 35 |
|
| 36 |
DATE_FORMAT = '%a %b %d %H:%M:%S.%f %Y' |
| 37 |
|
| 38 |
logger = logging.getLogger(__name__) |
| 39 |
|
| 40 |
|
| 41 |
class cachelog(admintool.AdminTool): |
| 42 |
command_name = "cachelog" |
| 43 |
|
| 44 |
usage = "%prog [options]" |
| 45 |
description = "Parse the Apache error log for cache 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(cachelog, self).__init__(options, args) |
| 51 |
self.since = None |
| 52 |
|
| 53 |
@classmethod |
| 54 |
def add_options(cls, parser): |
| 55 |
super(cachelog, cls).add_options(parser, debug_option=True) |
| 56 |
parser.add_option( |
| 57 |
"--command", |
| 58 |
dest="command", |
| 59 |
action="store", |
| 60 |
default=None, |
| 61 |
help="Command to analyze", |
| 62 |
) |
| 63 |
parser.add_option( |
| 64 |
"--start-time", |
| 65 |
dest="start_time", |
| 66 |
action="store", |
| 67 |
default=None, |
| 68 |
help="time to begin analyzing logfile from, e.g. " |
| 69 |
"Fri May 7 16:33:08.0 2021", |
| 70 |
) |
| 71 |
parser.add_option( |
| 72 |
"--file", |
| 73 |
dest="file", |
| 74 |
action="store", |
| 75 |
default="/var/log/httpd/error_log", |
| 76 |
help="Log file to parse", |
| 77 |
) |
| 78 |
|
| 79 |
def validate_options(self): |
| 80 |
super(cachelog, self).validate_options(needs_root=True) |
| 81 |
|
| 82 |
if self.options.start_time: |
| 83 |
self.since = datetime.strptime( |
| 84 |
self.options.start_time, |
| 85 |
DATE_FORMAT |
| 86 |
) |
| 87 |
|
| 88 |
def run(self): |
| 89 |
super(cachelog, self).run() |
| 90 |
|
| 91 |
if not is_ipa_configured(): |
| 92 |
logger.error("IPA server is not configured on this system.") |
| 93 |
raise admintool.ScriptError() |
| 94 |
|
| 95 |
with open(self.options.file, 'r') as f: |
| 96 |
data = f.read() |
| 97 |
|
| 98 |
matches = list(re.finditer(TIME_RE, data)) |
| 99 |
|
| 100 |
hits = 0 |
| 101 |
misses = 0 |
| 102 |
count = 0 |
| 103 |
|
| 104 |
for match in matches: |
| 105 |
if self.since: |
| 106 |
logtime = datetime.strptime(match.group('date'), DATE_FORMAT) |
| 107 |
if logtime < self.since: |
| 108 |
continue |
| 109 |
hits += int(match.group('hits')) |
| 110 |
misses += int(match.group('misses')) |
| 111 |
count += 1 |
| 112 |
|
| 113 |
print('Total reads %d, hits %d, misses %d, avg %1.4f' % |
| 114 |
(hits + misses, hits, misses, hits / (hits + misses))) |
| 115 |
|
| 116 |
|
| 117 |
if __name__ == '__main__': |
| 118 |
cachelog.run_cli() |