From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Eduardo Habkost" <eduardo@habkost.net>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"John Snow" <jsnow@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Cleber Rosa" <crosa@redhat.com>
Subject: [PATCH 2/2] python: support recording QMP session to a file
Date: Mon, 17 Jan 2022 14:11:03 +0000 [thread overview]
Message-ID: <20220117141103.157288-3-berrange@redhat.com> (raw)
In-Reply-To: <20220117141103.157288-1-berrange@redhat.com>
When running QMP commands with very large response payloads, it is often
not easy to spot the info you want. If we can save the response to a
file then tools like 'grep' or 'jq' can be used to extract information.
For convenience of processing, we merge the QMP command and response
dictionaries together:
{
"arguments": {},
"execute": "query-kvm",
"return": {
"enabled": false,
"present": true
}
}
Example usage
$ ./scripts/qmp/qmp-shell-wrap -l q.log -p -- ./build/qemu-system-x86_64 -display none
Welcome to the QMP low-level shell!
Connected
(QEMU) query-kvm
{
"return": {
"enabled": false,
"present": true
}
}
(QEMU) query-mice
{
"return": [
{
"absolute": false,
"current": true,
"index": 2,
"name": "QEMU PS/2 Mouse"
}
]
}
$ jq --slurp '. | to_entries[] | select(.value.execute == "query-kvm") |
.value.return.enabled' < q.log
false
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
python/qemu/qmp/qmp_shell.py | 27 ++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/python/qemu/qmp/qmp_shell.py b/python/qemu/qmp/qmp_shell.py
index 12f7d28afc..a50c3f1bc7 100644
--- a/python/qemu/qmp/qmp_shell.py
+++ b/python/qemu/qmp/qmp_shell.py
@@ -165,7 +165,8 @@ class QMPShell(qmp.QEMUMonitorProtocol):
def __init__(self, address: qmp.SocketAddrT,
pretty: bool = False,
verbose: bool = False,
- server: bool = False):
+ server: bool = False,
+ logfile: str = None):
super().__init__(address, server=server)
self._greeting: Optional[QMPMessage] = None
self._completer = QMPCompleter()
@@ -175,6 +176,10 @@ def __init__(self, address: qmp.SocketAddrT,
'.qmp-shell_history')
self.pretty = pretty
self.verbose = verbose
+ self.logfile = None
+
+ if logfile is not None:
+ self.logfile = open(logfile, "w")
def close(self) -> None:
# Hook into context manager of parent to save shell history.
@@ -315,11 +320,11 @@ def _build_cmd(self, cmdline: str) -> Optional[QMPMessage]:
self._cli_expr(cmdargs[1:], qmpcmd['arguments'])
return qmpcmd
- def _print(self, qmp_message: object) -> None:
+ def _print(self, qmp_message: object, fh=sys.stdout) -> None:
jsobj = json.dumps(qmp_message,
indent=4 if self.pretty else None,
sort_keys=self.pretty)
- print(str(jsobj))
+ print(str(jsobj), file=fh)
def _execute_cmd(self, cmdline: str) -> bool:
try:
@@ -342,6 +347,9 @@ def _execute_cmd(self, cmdline: str) -> bool:
print('Disconnected')
return False
self._print(resp)
+ if self.logfile is not None:
+ cmd = {**qmpcmd, **resp}
+ self._print(cmd, fh=self.logfile)
return True
def connect(self, negotiate: bool = True) -> None:
@@ -409,8 +417,9 @@ class HMPShell(QMPShell):
def __init__(self, address: qmp.SocketAddrT,
pretty: bool = False,
verbose: bool = False,
- server: bool = False):
- super().__init__(address, pretty, verbose, server)
+ server: bool = False,
+ logfile: str = None):
+ super().__init__(address, pretty, verbose, server, logfile)
self._cpu_index = 0
def _cmd_completion(self) -> None:
@@ -503,6 +512,8 @@ def main() -> None:
help='Verbose (echo commands sent and received)')
parser.add_argument('-p', '--pretty', action='store_true',
help='Pretty-print JSON')
+ parser.add_argument('-l', '--logfile',
+ help='Save log of all QMP messages to PATH')
default_server = os.environ.get('QMP_SOCKET')
parser.add_argument('qmp_server', action='store',
@@ -521,7 +532,7 @@ def main() -> None:
parser.error(f"Bad port number: {args.qmp_server}")
return # pycharm doesn't know error() is noreturn
- with shell_class(address, args.pretty, args.verbose) as qemu:
+ with shell_class(address, args.pretty, args.verbose, args.logfile) as qemu:
try:
qemu.connect(negotiate=not args.skip_negotiation)
except qmp.QMPConnectError:
@@ -545,6 +556,8 @@ def main_wrap() -> None:
help='Verbose (echo commands sent and received)')
parser.add_argument('-p', '--pretty', action='store_true',
help='Pretty-print JSON')
+ parser.add_argument('-l', '--logfile',
+ help='Save log of all QMP messages to PATH')
parser.add_argument('command', nargs=argparse.REMAINDER,
help='QEMU command line to invoke')
@@ -568,7 +581,7 @@ def main_wrap() -> None:
parser.error(f"Bad port number: {socketpath}")
return # pycharm doesn't know error() is noreturn
- with shell_class(address, args.pretty, args.verbose, True) as qemu:
+ with shell_class(address, args.pretty, args.verbose, True, args.logfile) as qemu:
qemuproc = Popen(cmd)
try:
--
2.33.1
next prev parent reply other threads:[~2022-01-17 14:42 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-17 14:11 [PATCH 0/2] python: a few improvements to qmp-shell Daniel P. Berrangé
2022-01-17 14:11 ` [PATCH 1/2] python: introduce qmp-shell-wrap convenience tool Daniel P. Berrangé
2022-01-17 23:27 ` John Snow
2022-01-18 5:13 ` Philippe Mathieu-Daudé via
2022-01-20 12:58 ` Beraldo Leal
2022-01-18 10:06 ` Daniel P. Berrangé
2022-01-18 18:04 ` John Snow
2022-01-20 13:33 ` Philippe Mathieu-Daudé via
2022-01-20 13:40 ` Daniel P. Berrangé
2022-01-20 21:34 ` John Snow
2022-01-17 14:11 ` Daniel P. Berrangé [this message]
2022-01-17 15:04 ` [PATCH 2/2] python: support recording QMP session to a file Philippe Mathieu-Daudé via
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220117141103.157288-3-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=crosa@redhat.com \
--cc=eduardo@habkost.net \
--cc=jsnow@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.