klinfix: new commands show log [FILE] and follow [FILE]

With completion support.  The log command has an optional last arg,
after FILE, tail NUM, which runs tail -n NUM FILE instead of cat.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2023-08-08 16:35:18 +02:00
committed by Tobias Waldekranz
parent 24d9a6dfc6
commit 697a907350
2 changed files with 75 additions and 0 deletions
+34
View File
@@ -1,5 +1,9 @@
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sysrepo.h>
#include <sysrepo_types.h>
@@ -13,6 +17,35 @@
const uint8_t kplugin_klinfix_major = 1;
const uint8_t kplugin_klinfix_minor = 0;
int klix_files(kcontext_t *ctx)
{
const char *path;
struct dirent *d;
DIR *dir;
path = kcontext_script(ctx);
if (!path) {
fprintf(stderr, "Error: missing path argument to file search.\n");
return -1;
}
dir = opendir(path);
if (!dir) {
fprintf(stderr, "Error: %s", strerror(errno));
return -1;
}
while ((d = readdir(dir))) {
if (d->d_type != DT_REG)
continue;
printf("%s\n", d->d_name);
}
closedir(dir);
return 0;
}
int klix_ds_from_str(const char *text, sr_datastore_t *ds)
{
size_t len = strlen(text);
@@ -219,6 +252,7 @@ int kplugin_klinfix_init(kcontext_t *ctx)
kplugin_add_syms(plugin, ksym_new("klix_copy", klix_copy));
kplugin_add_syms(plugin, ksym_new("klix_commit", klix_commit));
kplugin_add_syms(plugin, ksym_new("klix_files", klix_files));
kplugin_add_syms(plugin, ksym_new("klix_rpc", klix_rpc));
return 0;
+41
View File
@@ -87,6 +87,12 @@
<ACTION sym="PLINE_INSERT_TO@sysrepo"/>
</PTYPE>
<PTYPE name="LOGFILES">
<COMPL>
<ACTION sym="klix_files@klinfix">/var/log</ACTION>
</COMPL>
<ACTION sym="STRING"/>
</PTYPE>
<VIEW name="main">
<HOTKEY key="^D" cmd="exit"/>
@@ -220,9 +226,44 @@
</SWITCH>
</COMMAND>
<COMMAND name="log" help="Show log file, default: syslog">
<PARAM name="fn" ptype="/LOGFILES" min="0" help="Optional log file to show"/>
<SWITCH name="optional" min="0">
<COMMAND name="tail" help="Show log tail, last N lines only">
<PARAM name="lines" ptype="/UINT" help="Last number of lines to show"/>
</COMMAND>
</SWITCH>
<ACTION sym="script">
file=${KLISH_PARAM_fn:-syslog}
if [ -n "$KLISH_PARAM_lines" ]; then
tail -n $KLISH_PARAM_lines /log/$file
else
cat /log/$file
fi
</ACTION>
</COMMAND>
<COMMAND name="factory-config" help="Show factory-config">
<ACTION sym="script">cat /cfg/factory-config.cfg | jq .</ACTION>
</COMMAND>
<COMMAND name="running-config" help="Show running-config">
<!-- TODO: add PARAM for xml/json output, and coloring like factory -->
<ACTION sym="srp_show_running@sysrepo"/>
</COMMAND>
<COMMAND name="startup-config" help="Show startup-config">
<ACTION sym="script">cat /cfg/startup-config.cfg | jq .</ACTION>
</COMMAND>
</COMMAND>
<COMMAND name="follow" help="Monitor a log file, use Ctrl-C to abort">
<PARAM name="fn" ptype="/LOGFILES" help="Optional log file to monitor, default: syslog"/>
<ACTION sym="script" interactive="true">
file=${KLISH_PARAM_fn:-syslog}
echo -e "\e[1mPress Ctrl-C to abort ────────────────────────────────────────────\e[0m"
tail -F /log/$file
</ACTION>
</COMMAND>
</VIEW>