| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
####################################################################### |
| 4 |
# |
| 5 |
# A script to parse samba (especially winbind) logfiles. |
| 6 |
# Trace files should be in a non-syslog format (debug syslog format = no). |
| 7 |
# |
| 8 |
# --traceid ... Specify the traceid of the request to parse |
| 9 |
# --pid ... Specify the pid |
| 10 |
# --breakdown ... Break to separate files per each traceid |
| 11 |
# --merge-by-timestamp ... Merge logs by timestamp |
| 12 |
# --flow ... Show the request/sub-request call flow |
| 13 |
# --flow-compact ... Show the request/sub-request call flow without dcerpc |
| 14 |
# |
| 15 |
# |
| 16 |
# Copyright (c) 2023 Andreas Schneider <[email protected]> |
| 17 |
# Copyright (c) 2023 Pavel Filipenský <[email protected]> |
| 18 |
# |
| 19 |
# This program is free software: you can redistribute it and/or modify |
| 20 |
# it under the terms of the GNU General Public License as published by |
| 21 |
# the Free Software Foundation, either version 3 of the License, or |
| 22 |
# (at your option) any later version. |
| 23 |
# |
| 24 |
# This program is distributed in the hope that it will be useful, |
| 25 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 |
# GNU General Public License for more details. |
| 28 |
# |
| 29 |
# You should have received a copy of the GNU General Public License |
| 30 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 31 |
# |
| 32 |
####################################################################### |
| 33 |
# |
| 34 |
# Requires: ??? |
| 35 |
|
| 36 |
import sys |
| 37 |
import os |
| 38 |
import re |
| 39 |
from argparse import ArgumentParser |
| 40 |
from collections import defaultdict |
| 41 |
|
| 42 |
# Trace record consists of a trace header followed by one or more text lines. |
| 43 |
# |
| 44 |
# This tool expects trace header format based on these smb.conf parameters: |
| 45 |
# |
| 46 |
# debug syslog format = no |
| 47 |
# debug hires timestamp = yes |
| 48 |
# winbind debug traceid = yes |
| 49 |
# |
| 50 |
# If 'winbind debug traceid = no' is set, then the option --merge-by-timestamp |
| 51 |
# still can be used. |
| 52 |
# |
| 53 |
# Each trace header contains a traceid, which is the main identifier for this |
| 54 |
# tool. A single traceid is either provided via command line option --traceid |
| 55 |
# or a list of traceids is derived from the PID specified via option --pid. |
| 56 |
# Creating and evaluating list of traceids from PID can be tricky: |
| 57 |
# The traceid can appear in a trace record before trace record containing the |
| 58 |
# PID is processed. So when we see a new traceid we are not sure if it belongs |
| 59 |
# to the traced PID. |
| 60 |
# The PID appears only in the main winbind process (log.winbind). If a |
| 61 |
# directory with many log files should be processed, we process the files in |
| 62 |
# random order. |
| 63 |
# It might happen that e.g. log.wb-ADDOMAIN is processed before log.winbind so |
| 64 |
# we do not know the list of traceids yet. |
| 65 |
# To make all this easy we put into memory all trace records and do the final |
| 66 |
# traceid filtering only after all files are read. This can require lot of |
| 67 |
# memory if files are big. |
| 68 |
|
| 69 |
|
| 70 |
def process_file_no_traceid(record_list, fname): |
| 71 |
with open(fname, "r") as infile: |
| 72 |
try: |
| 73 |
data = infile.readlines() |
| 74 |
except UnicodeDecodeError: |
| 75 |
print("This file is not Unicode encoded: ", fname, file=sys.stderr) |
| 76 |
return |
| 77 |
|
| 78 |
date = "" |
| 79 |
record_lines = [] |
| 80 |
|
| 81 |
RE_HEADER_NO_TRACEID = re.compile( |
| 82 |
r"^\[(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}\.\d{6}).*") |
| 83 |
for line in data: |
| 84 |
header = RE_HEADER_NO_TRACEID.search(line) |
| 85 |
if header: |
| 86 |
# Append all previous trace lines of a record |
| 87 |
if record_lines: |
| 88 |
record_list.append((date, None, record_lines, fname)) |
| 89 |
record_lines = [] |
| 90 |
# Remember the new date |
| 91 |
date = header.group(1) |
| 92 |
record_lines.append(line) |
| 93 |
|
| 94 |
|
| 95 |
def process_file(record_list, traceid_set, fname, opid, otraceid): |
| 96 |
with open(fname, "r") as infile: |
| 97 |
try: |
| 98 |
data = infile.readlines() |
| 99 |
except UnicodeDecodeError: |
| 100 |
print("This file is not Unicode encoded: ", fname, file=sys.stderr) |
| 101 |
return |
| 102 |
|
| 103 |
pid = None |
| 104 |
traceid = 0 |
| 105 |
traceid_prev = None |
| 106 |
undecided_traceid = False |
| 107 |
date = "" |
| 108 |
record_lines = [] |
| 109 |
|
| 110 |
# If traceid option was provided the traceid_set will contain just it |
| 111 |
if otraceid: |
| 112 |
traceid_set.add(otraceid) |
| 113 |
|
| 114 |
RE_HEADER = re.compile( |
| 115 |
r"^\[(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}\.\d{6}).*?, .*, " |
| 116 |
r"traceid=([0-9]+).*\]") |
| 117 |
RE_INTERFACE_VERSION = re.compile( |
| 118 |
r"^\s+winbindd_interface_version: \[\S* \((\d+)\)\]") |
| 119 |
RE_ASYNC_REQUEST = re.compile( |
| 120 |
r"^\s+process_request_send: " |
| 121 |
r"\[\S* \((\d+)\)\] Handling async request:") |
| 122 |
# Example of a header line |
| 123 |
# [2023/05/01 07:40:45.439049, 3, pid=418844, effective(0, 0), real(0, 0), class=winbind, traceid=37] ../../source3/winbindd/winbindd_misc.c:355(winbindd_interface_version) |
| 124 |
for line in data: |
| 125 |
header = RE_HEADER.search(line) |
| 126 |
if header: |
| 127 |
# Append all previous trace lines of a record if the traceid is in |
| 128 |
# the list. |
| 129 |
if record_lines: |
| 130 |
record_list.append((date, traceid, record_lines, fname)) |
| 131 |
record_lines = [] |
| 132 |
# Remember the new date and the new traceid |
| 133 |
date = header.group(1) |
| 134 |
traceid = header.group(2) |
| 135 |
if traceid != traceid_prev: |
| 136 |
traceid_prev = traceid |
| 137 |
undecided_traceid = True |
| 138 |
if opid: |
| 139 |
# Search for lines that identify a new winbind client and the |
| 140 |
# client PID |
| 141 |
|
| 142 |
# winbindd_interface_version: [nss_winbind (500725)]: request interface version (version = 32) |
| 143 |
# process_request_send: [nss_winbind (500725)] Handling async request: SETPWENT |
| 144 |
interface_version = RE_INTERFACE_VERSION.search(line) |
| 145 |
async_request = RE_ASYNC_REQUEST.search(line) |
| 146 |
if interface_version: |
| 147 |
pid = interface_version.group(1) |
| 148 |
if undecided_traceid: |
| 149 |
if pid == opid: |
| 150 |
traceid_set.add(traceid) |
| 151 |
undecided_traceid = False |
| 152 |
if async_request: |
| 153 |
pid = async_request.group(1) |
| 154 |
if undecided_traceid: |
| 155 |
if pid == opid: |
| 156 |
traceid_set.add(traceid) |
| 157 |
undecided_traceid = False |
| 158 |
# For --breakdown add every traceid |
| 159 |
if not opid and not otraceid: |
| 160 |
traceid_set.add(traceid) |
| 161 |
|
| 162 |
record_lines.append(line) |
| 163 |
|
| 164 |
|
| 165 |
def filter_traceids(record_list, traceid_set): |
| 166 |
llist = [] |
| 167 |
for (d, t, li, f) in record_list: |
| 168 |
if t in traceid_set: |
| 169 |
llist.append((d, t, li, f)) |
| 170 |
return llist |
| 171 |
|
| 172 |
|
| 173 |
def filter_flow(record_list): |
| 174 |
local_list = [] |
| 175 |
for (date, traceid, lines, filename) in record_list: |
| 176 |
for line in lines: |
| 177 |
isflow = re.search(r"^(\s+)flow: (.*)", line) |
| 178 |
if isflow: |
| 179 |
local_list.append(isflow.group(1) + isflow.group(2)) |
| 180 |
return local_list |
| 181 |
|
| 182 |
|
| 183 |
def filter_flowcompact(flist): |
| 184 |
local_list = [] |
| 185 |
end_marker = None |
| 186 |
for fl in flist: |
| 187 |
if not end_marker: |
| 188 |
local_list.append(fl) |
| 189 |
dcerpc_start = re.search(r"^(\s+)-> dcerpc_", fl) |
| 190 |
if dcerpc_start: |
| 191 |
end_marker = dcerpc_start.group(1) |
| 192 |
else: |
| 193 |
dcerpc_end = re.search(r"^" + end_marker + "<- dcerpc_", fl) |
| 194 |
if dcerpc_end: |
| 195 |
end_marker = None |
| 196 |
local_list.append(fl) |
| 197 |
return local_list |
| 198 |
|
| 199 |
|
| 200 |
def print_record_list(record_list, file): |
| 201 |
f_prev = None |
| 202 |
for (date, traceid, lines, filename) in record_list: |
| 203 |
# Inform about filename change |
| 204 |
if filename != f_prev: |
| 205 |
print("-" * 72, file=file) |
| 206 |
print("FILE: ", filename, file=file) |
| 207 |
print("-" * 72, file=file) |
| 208 |
for line in lines: |
| 209 |
print(line, end='', file=file) |
| 210 |
f_prev = filename |
| 211 |
|
| 212 |
# record_list ... list of quadruplets <date, traceid, [trace lines], filename> |
| 213 |
# flow_list ... lines from record_list with 'flow' traces |
| 214 |
# traceid_set ... list of traceids we want to trace |
| 215 |
# with --traceid ... there is a single traceids |
| 216 |
# with --pid ... there are all traceids for the PID |
| 217 |
# with --breakdown ... there are all traceids |
| 218 |
|
| 219 |
|
| 220 |
def setup_parser(): |
| 221 |
parser = ArgumentParser() |
| 222 |
|
| 223 |
parser.add_argument( |
| 224 |
"path", |
| 225 |
type=str, |
| 226 |
help="logfile or directory" |
| 227 |
) |
| 228 |
parser.add_argument( |
| 229 |
"--traceid", |
| 230 |
dest="traceid", |
| 231 |
help="specify the traceid of the trace records", |
| 232 |
metavar="ID" |
| 233 |
) |
| 234 |
parser.add_argument( |
| 235 |
"--pid", |
| 236 |
dest="pid", |
| 237 |
help="specify the pid of winbind client", |
| 238 |
metavar="PID" |
| 239 |
) |
| 240 |
parser.add_argument( |
| 241 |
"--breakdown", |
| 242 |
action="store_true", |
| 243 |
dest="breakdown", |
| 244 |
default=False, |
| 245 |
help="breakdown the traces into per traceid files" |
| 246 |
) |
| 247 |
parser.add_argument( |
| 248 |
"--merge-by-timestamp", |
| 249 |
action="store_true", |
| 250 |
dest="merge", |
| 251 |
default=False, |
| 252 |
help="merge logs by timestamp" |
| 253 |
) |
| 254 |
parser.add_argument( |
| 255 |
"--flow", |
| 256 |
action="store_true", |
| 257 |
dest="flow", |
| 258 |
default=False, |
| 259 |
help="show the request/sub-request flow traces" |
| 260 |
) |
| 261 |
parser.add_argument( |
| 262 |
"--flow-compact", |
| 263 |
action="store_true", |
| 264 |
dest="flowcompact", |
| 265 |
default=False, |
| 266 |
help="show the request/sub-request flow traces without dcerpc details" |
| 267 |
) |
| 268 |
return parser |
| 269 |
|
| 270 |
|
| 271 |
def main(): # noqa |
| 272 |
record_list = [] |
| 273 |
flow_list = [] |
| 274 |
traceid_set = set() |
| 275 |
|
| 276 |
parser = setup_parser() |
| 277 |
options = parser.parse_args() |
| 278 |
|
| 279 |
if (not options.traceid and not options.pid and not options.breakdown |
| 280 |
and not options.merge): |
| 281 |
print("One of --traceid or --pid is needed" |
| 282 |
" or --breakdown or --merge-by-timestamp.") |
| 283 |
sys.exit(1) |
| 284 |
elif options.traceid and options.pid: |
| 285 |
print("Only one of --traceid or --pid is allowed.") |
| 286 |
sys.exit(1) |
| 287 |
elif options.breakdown and (options.traceid or options.pid): |
| 288 |
print("--breakdown cannot be combined with --traceid and --pid.") |
| 289 |
sys.exit(1) |
| 290 |
|
| 291 |
if options.flow and not options.traceid: |
| 292 |
print("Option --flow can be used only together with --traceid.") |
| 293 |
sys.exit(1) |
| 294 |
|
| 295 |
if options.flowcompact and not options.traceid: |
| 296 |
print("Option --flow-compact can be used only together with " |
| 297 |
"--traceid.") |
| 298 |
sys.exit(1) |
| 299 |
|
| 300 |
if options.flow and options.flowcompact: |
| 301 |
print("Only one of --flow or --flow-compact is allowed.") |
| 302 |
sys.exit(1) |
| 303 |
|
| 304 |
if not options.path: |
| 305 |
print("Path to logfile or directory with logs is needed.") |
| 306 |
sys.exit(1) |
| 307 |
|
| 308 |
merge_with_no_traceid = (not options.traceid and not options.pid |
| 309 |
and not options.breakdown) and options.merge |
| 310 |
|
| 311 |
path = options.path |
| 312 |
if os.path.isdir(path): |
| 313 |
for root, dirs, files in os.walk(path): |
| 314 |
for name in files: |
| 315 |
if merge_with_no_traceid: |
| 316 |
process_file_no_traceid( |
| 317 |
record_list, |
| 318 |
os.path.join(root, name) |
| 319 |
) |
| 320 |
else: |
| 321 |
process_file( |
| 322 |
record_list, |
| 323 |
traceid_set, |
| 324 |
os.path.join(root, name), |
| 325 |
options.pid, |
| 326 |
options.traceid, |
| 327 |
) |
| 328 |
elif os.path.isfile(path): |
| 329 |
if merge_with_no_traceid: |
| 330 |
process_file_no_traceid( |
| 331 |
record_list, |
| 332 |
path |
| 333 |
) |
| 334 |
else: |
| 335 |
process_file( |
| 336 |
record_list, |
| 337 |
traceid_set, |
| 338 |
path, |
| 339 |
options.pid, |
| 340 |
options.traceid |
| 341 |
) |
| 342 |
else: |
| 343 |
print(path, "Path is neither file or directory.") |
| 344 |
sys.exit(1) |
| 345 |
|
| 346 |
# Sort only using timestamps, no use of traceid |
| 347 |
if merge_with_no_traceid: |
| 348 |
record_list.sort() |
| 349 |
print_record_list(record_list, sys.stdout) |
| 350 |
sys.exit(0) |
| 351 |
|
| 352 |
# Keep only records with matching traceids |
| 353 |
if not options.breakdown: |
| 354 |
record_list = filter_traceids(record_list, traceid_set) |
| 355 |
|
| 356 |
if options.breakdown: |
| 357 |
for traceid in traceid_set: |
| 358 |
# Full |
| 359 |
with open("%s.full" % traceid, "w") as full_f: |
| 360 |
full_l = filter_traceids(record_list, {traceid}) |
| 361 |
if options.merge: |
| 362 |
full_l.sort() |
| 363 |
print_record_list(full_l, full_f) |
| 364 |
# Flow |
| 365 |
with open("%s.flow" % traceid, "w") as flow_f: |
| 366 |
flow_l = filter_flow(full_l) |
| 367 |
for fl in flow_l: |
| 368 |
print(fl, file=flow_f) |
| 369 |
# Flow compact |
| 370 |
with open("%s.flowcompact" % traceid, "w") as flowcompact_f: |
| 371 |
flowcompact_l = filter_flowcompact(flow_l) |
| 372 |
for fl in flowcompact_l: |
| 373 |
print(fl, file=flowcompact_f) |
| 374 |
elif options.flow: |
| 375 |
flow_list = filter_flow(record_list) |
| 376 |
for fl in flow_list: |
| 377 |
print(fl) |
| 378 |
elif options.flowcompact: |
| 379 |
flow_list = filter_flow(record_list) |
| 380 |
flow_list = filter_flowcompact(flow_list) |
| 381 |
for fl in flow_list: |
| 382 |
print(fl) |
| 383 |
else: |
| 384 |
if options.merge: |
| 385 |
record_list.sort() |
| 386 |
print_record_list(record_list, sys.stdout) |
| 387 |
|
| 388 |
sys.exit(0) |
| 389 |
|
| 390 |
|
| 391 |
if __name__ == "__main__": |
| 392 |
main() |