diff --git a/tools/virt-admin.c b/tools/virt-admin.c index 4275aa37a1bfb477655a6e0bbaf5a043947517c9..d3648bc034e1dbbd0b7a8362f00ec230d6657cde 100644 --- a/tools/virt-admin.c +++ b/tools/virt-admin.c @@ -38,6 +38,7 @@ #include "virstring.h" #include "virthread.h" #include "virgettext.h" +#include "virtime.h" /* Gnulib doesn't guarantee SA_SIGINFO support. */ #ifndef SA_SIGINFO @@ -46,11 +47,64 @@ #define VIRT_ADMIN_PROMPT "virt-admin # " +/* we don't need precision to milliseconds in this module */ +#define VIRT_ADMIN_TIME_BUFLEN VIR_TIME_STRING_BUFLEN - 3 + static char *progname; static const vshCmdGrp cmdGroups[]; static const vshClientHooks hooks; +VIR_ENUM_DECL(virClientTransport) +VIR_ENUM_IMPL(virClientTransport, + VIR_CLIENT_TRANS_LAST, + N_("unix"), + N_("tcp"), + N_("tls")) + +static const char * +vshAdmClientTransportToString(int transport) +{ + const char *str = virClientTransportTypeToString(transport); + return str ? _(str) : _("unknown"); +} + +/* + * vshAdmGetTimeStr: + * + * Produces string representation (local time) of @then + * (seconds since epoch UTC) using format 'YYYY-MM-DD HH:MM:SS+ZZZZ'. + * + * Returns 0 if conversion finished successfully, -1 in case of an error. + * Caller is responsible for freeing the string returned. + */ +static int +vshAdmGetTimeStr(vshControl *ctl, time_t then, char **result) +{ + + char *tmp = NULL; + struct tm timeinfo; + + if (!localtime_r(&then, &timeinfo)) + goto error; + + if (VIR_ALLOC_N(tmp, VIRT_ADMIN_TIME_BUFLEN) < 0) + goto error; + + if (strftime(tmp, VIRT_ADMIN_TIME_BUFLEN, "%Y-%m-%d %H:%M:%S%z", + &timeinfo) == 0) { + VIR_FREE(tmp); + goto error; + } + + *result = tmp; + return 0; + + error: + vshError(ctl, "%s", _("Timestamp string conversion failed")); + return -1; +} + /* * vshAdmCatchDisconnect: * @@ -522,6 +576,87 @@ cmdSrvThreadpoolSet(vshControl *ctl, const vshCmd *cmd) goto cleanup; } +/* ------------------------ + * Command srv-clients-list + * ------------------------ + */ + +static const vshCmdInfo info_srv_clients_list[] = { + {.name = "help", + .data = N_("list clients connected to ") + }, + {.name = "desc", + .data = N_("List all manageable clients connected to .") + }, + {.name = NULL} +}; + +static const vshCmdOptDef opts_srv_clients_list[] = { + {.name = "server", + .type = VSH_OT_DATA, + .flags = VSH_OFLAG_REQ, + .help = N_("server which to list connected clients from"), + }, + {.name = NULL} +}; + +static bool +cmdSrvClientsList(vshControl *ctl, const vshCmd *cmd) +{ + int nclts = 0; + size_t i; + bool ret = false; + const char *srvname = NULL; + unsigned long long id; + virClientTransport transport; + char *timestr = NULL; + virAdmServerPtr srv = NULL; + virAdmClientPtr *clts = NULL; + vshAdmControlPtr priv = ctl->privData; + + if (vshCommandOptStringReq(ctl, cmd, "server", &srvname) < 0) + return false; + + if (!(srv = virAdmConnectLookupServer(priv->conn, srvname, 0))) + goto cleanup; + + /* Obtain a list of clients connected to server @srv */ + if ((nclts = virAdmServerListClients(srv, &clts, 0)) < 0) { + vshError(ctl, _("failed to obtain list of connected clients " + "from server '%s'"), virAdmServerGetName(srv)); + goto cleanup; + } + + vshPrintExtra(ctl, " %-5s %-15s %-15s\n%s\n", _("Id"), _("Transport"), + _("Connected since"), + "-------------------------" + "-------------------------"); + + for (i = 0; i < nclts; i++) { + virAdmClientPtr client = clts[i]; + id = virAdmClientGetID(client); + transport = virAdmClientGetTransport(client); + if (vshAdmGetTimeStr(ctl, virAdmClientGetTimestamp(client), + ×tr) < 0) + goto cleanup; + + vshPrint(ctl, " %-5llu %-15s %-15s\n", + id, vshAdmClientTransportToString(transport), timestr); + VIR_FREE(timestr); + } + + ret = true; + + cleanup: + if (clts) { + for (i = 0; i < nclts; i++) + virAdmClientFree(clts[i]); + VIR_FREE(clts); + } + virAdmServerFree(srv); + return ret; +} + static void * vshAdmConnectionHandler(vshControl *ctl) { @@ -827,6 +962,12 @@ static const vshCmdDef monitoringCmds[] = { .info = info_srv_threadpool_info, .flags = 0 }, + {.name = "srv-clients-list", + .handler = cmdSrvClientsList, + .opts = opts_srv_clients_list, + .info = info_srv_clients_list, + .flags = 0 + }, {.name = NULL} }; diff --git a/tools/virt-admin.pod b/tools/virt-admin.pod index 83a8cea3d48b0c32e769336e05e1568d62576d5e..6f9dd6329f1a19c7059be602e38c0941fb503884 100644 --- a/tools/virt-admin.pod +++ b/tools/virt-admin.pod @@ -220,6 +220,13 @@ The current number of active priority workers in a threadpool. =back +=item B I + +Print a table showing the list of clients connected to , also providing +information about transport type used on client's connection (supported +transports include B, B, and B), as well as providing +information about client's connection time (system local time is used). + =back =head1 ENVIRONMENT