| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (C) 2008, 2010 Red Hat Inc. |
| 4 |
# |
| 5 |
# This file is part of systemtap, and is free software. You can |
| 6 |
# redistribute it and/or modify it under the terms of the GNU General |
| 7 |
# Public License (GPL); either version 2, or (at your option) any |
| 8 |
# later version. |
| 9 |
|
| 10 |
import sys |
| 11 |
import os |
| 12 |
import os.path |
| 13 |
import time |
| 14 |
import select |
| 15 |
from config_opts import config_opts |
| 16 |
|
| 17 |
def run_module(): |
| 18 |
# Find the current size of /var/log/messages |
| 19 |
logfile = '/var/log/messages' |
| 20 |
print >>sys.stderr, "Getting size of %s..." % logfile |
| 21 |
|
| 22 |
start_pos = os.path.getsize(logfile) |
| 23 |
l = open(logfile, 'r') |
| 24 |
l.seek(start_pos) |
| 25 |
|
| 26 |
# Insert the module |
| 27 |
print >>sys.stderr, "Inserting module..." |
| 28 |
rc = os.system("/sbin/insmod %s" % os.path.join(os.getcwd(), |
| 29 |
'kprobe_module.ko')) |
| 30 |
if os.WEXITSTATUS(rc) != 0: |
| 31 |
# This is actually semi-OK, which is why there is no error |
| 32 |
# message here, only a notice. This might mean that every |
| 33 |
# probe tried cannot be registered. |
| 34 |
print >>sys.stderr, "Notice: insmod failed" |
| 35 |
else: |
| 36 |
# Run the load generate commands (if any). Note we ignore the |
| 37 |
# return values, since we don't really care if the commands |
| 38 |
# succeed or not. |
| 39 |
if config_opts.has_key('load_cmds'): |
| 40 |
print >>sys.stderr, "Running commands..." |
| 41 |
num_waits = 0 |
| 42 |
|
| 43 |
# Redirect the output to 'run_module.log' by modifying |
| 44 |
# what stdout/stderr points to. |
| 45 |
sys.stdout.flush() |
| 46 |
old_stdout = os.dup(sys.stdout.fileno()) |
| 47 |
sys.stderr.flush() |
| 48 |
old_stderr = os.dup(sys.stderr.fileno()) |
| 49 |
fd = os.open('run_module.log', os.O_CREAT | os.O_WRONLY) |
| 50 |
os.dup2(fd, sys.stdout.fileno()) |
| 51 |
os.dup2(fd, sys.stderr.fileno()) |
| 52 |
|
| 53 |
# Run the commands. |
| 54 |
pid_list = list() |
| 55 |
for cmd in config_opts['load_cmds']: |
| 56 |
pid = os.spawnvp(os.P_NOWAIT, cmd[0], cmd) |
| 57 |
pid_list.append(pid) |
| 58 |
for pid in pid_list: |
| 59 |
(childpid, status) = os.waitpid(pid, 0) |
| 60 |
|
| 61 |
# Restore old value of stdout/stderr. |
| 62 |
os.close(fd) |
| 63 |
os.dup2(old_stdout, sys.stdout.fileno()) |
| 64 |
os.dup2(old_stderr, sys.stderr.fileno()) |
| 65 |
|
| 66 |
# Remove the module |
| 67 |
print >>sys.stderr, "Removing module..." |
| 68 |
rc = os.system("/sbin/rmmod kprobe_module") |
| 69 |
if os.WEXITSTATUS(rc) != 0: |
| 70 |
print >>sys.stderr, "Error: rmmod failed" |
| 71 |
return -1 |
| 72 |
|
| 73 |
# Now we have to wait until everything is flushed to the logfile |
| 74 |
print >>sys.stderr, "Looking for output..." |
| 75 |
rc = 0 |
| 76 |
f = open(config_opts['probes_result'], 'w') |
| 77 |
attempts = 0 |
| 78 |
while 1: |
| 79 |
# Find the ending size of /var/log/messages |
| 80 |
end_pos = os.path.getsize(logfile) |
| 81 |
|
| 82 |
if end_pos < start_pos: |
| 83 |
# The log files have been rotated. Read any leftover data, |
| 84 |
# then reopen file. |
| 85 |
data = l.read() |
| 86 |
if data: |
| 87 |
f.write(data) |
| 88 |
|
| 89 |
# See if we can find 'kprobe_module unloaded' in the |
| 90 |
# data we just read. |
| 91 |
if data.find('kprobe_module unloaded') != -1: |
| 92 |
break |
| 93 |
|
| 94 |
l.close() |
| 95 |
l = open(logfile, 'r') |
| 96 |
start_pos = 0 |
| 97 |
continue |
| 98 |
|
| 99 |
# Try to wait until data is available |
| 100 |
while 1: |
| 101 |
try: |
| 102 |
input, output, exc = select.select([l.fileno()], [], [], 60) |
| 103 |
break |
| 104 |
except select.error, err: |
| 105 |
if err[0] != EINTR: |
| 106 |
raise |
| 107 |
|
| 108 |
# Get the new stuff logged to /var/log/messages |
| 109 |
data = l.read(end_pos - start_pos + 1) |
| 110 |
if not data: |
| 111 |
# ignore EOF |
| 112 |
time.sleep(2) |
| 113 |
attempts += 1 |
| 114 |
if attempts < 30: |
| 115 |
continue |
| 116 |
else: |
| 117 |
print >>sys.stderr, "Error: Couldn't find data" |
| 118 |
rc = -1 |
| 119 |
break |
| 120 |
|
| 121 |
# Write results data |
| 122 |
f.write(data) |
| 123 |
|
| 124 |
# See if we can find 'kprobe_module unloaded' in the data we |
| 125 |
# just read. |
| 126 |
if data.find('kprobe_module unloaded') == -1: |
| 127 |
start_pos = end_pos |
| 128 |
else: |
| 129 |
break |
| 130 |
|
| 131 |
l.close() |
| 132 |
f.close() |
| 133 |
return rc |
| 134 |
|
| 135 |
def main(): |
| 136 |
rc = run_module() |
| 137 |
sys.exit(rc) |
| 138 |
|
| 139 |
if __name__ == "__main__": |
| 140 |
main() |