|
| 1 | +#!/usr/bin/env python3 |
| 2 | +############################################################### |
| 3 | +# Copyright 2019 Lawrence Livermore National Security, LLC |
| 4 | +# (c.f. AUTHORS, NOTICE.LLNS, COPYING) |
| 5 | +# |
| 6 | +# This file is part of the Flux resource manager framework. |
| 7 | +# For details, see https://github.com/flux-framework. |
| 8 | +# |
| 9 | +# SPDX-License-Identifier: LGPL-3.0 |
| 10 | +############################################################### |
| 11 | +# |
| 12 | +# Follow Flux dmesg output until a line matches a pattern |
| 13 | +# |
| 14 | +import argparse |
| 15 | +import re |
| 16 | +import sys |
| 17 | + |
| 18 | +import flux |
| 19 | +from flux.constants import FLUX_RPC_STREAMING |
| 20 | +from flux.core.watchers import TimerWatcher |
| 21 | + |
| 22 | +parser = argparse.ArgumentParser( |
| 23 | + description="watch the flux dmesg log for a given pattern" |
| 24 | +) |
| 25 | +parser.add_argument( |
| 26 | + "-t", |
| 27 | + "--timeout", |
| 28 | + help="Timeout with error after some number of seconds", |
| 29 | + metavar="SEC", |
| 30 | + type=float, |
| 31 | + default=1.0, |
| 32 | +) |
| 33 | +parser.add_argument( |
| 34 | + "-v", |
| 35 | + "--verbose", |
| 36 | + help="Emit each line of dmesg output, not just first matching", |
| 37 | + action="count", |
| 38 | + default=0, |
| 39 | +) |
| 40 | +parser.add_argument("pattern") |
| 41 | +args = parser.parse_args() |
| 42 | + |
| 43 | + |
| 44 | +def timer_cb(h, watcher, revents, _arg): |
| 45 | + print("Timeout!", file=sys.stderr) |
| 46 | + h.reactor_stop_error() |
| 47 | + |
| 48 | + |
| 49 | +def dmesg_cb(rpc, pattern, verbose=False): |
| 50 | + buf = rpc.get_str() |
| 51 | + match = pattern.search(buf) |
| 52 | + if match: |
| 53 | + print(buf) |
| 54 | + rpc.flux_handle.reactor_stop() |
| 55 | + elif verbose: |
| 56 | + print(buf) |
| 57 | + rpc.reset() |
| 58 | + |
| 59 | + |
| 60 | +pattern = re.compile(args.pattern) |
| 61 | + |
| 62 | +h = flux.Flux() |
| 63 | + |
| 64 | +rpc = h.rpc("log.dmesg", {"follow": True}, flags=FLUX_RPC_STREAMING) |
| 65 | +rpc.then(dmesg_cb, pattern, verbose=args.verbose) |
| 66 | + |
| 67 | +TimerWatcher(h, args.timeout, timer_cb).start() |
| 68 | +if h.reactor_run() < 0: |
| 69 | + sys.exit(1) |
0 commit comments