mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-05-02 22:51:15 +00:00
lib, *: add a common time interval formatting api
Add a common api that formats a time interval into a string with different output for short and longer intervals. We do this in several places, for cli/ui output. Signed-off-by: Mark Stapp <mjs@voltanet.io>
This commit is contained in:
parent
4c2a712d93
commit
d0636ead31
@ -562,20 +562,12 @@ void vty_multiline(struct vty *vty, const char *prefix, const char *format, ...)
|
||||
|
||||
void vty_out_timestr(struct vty *vty, time_t uptime)
|
||||
{
|
||||
struct tm tm;
|
||||
time_t difftime = time(NULL);
|
||||
char buf[MONOTIME_STRLEN];
|
||||
|
||||
difftime -= uptime;
|
||||
|
||||
gmtime_r(&difftime, &tm);
|
||||
frrtime_to_interval(difftime, buf, sizeof(buf));
|
||||
|
||||
if (difftime < ONE_DAY_SECOND)
|
||||
vty_out(vty, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (difftime < ONE_WEEK_SECOND)
|
||||
vty_out(vty, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
vty_out(vty, "%02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
|
||||
vty_out(vty, " ago");
|
||||
vty_out(vty, "%s ago", buf);
|
||||
}
|
||||
|
@ -115,6 +115,7 @@ DEFUN (no_triggered_csnp,
|
||||
static void lsp_print_flooding(struct vty *vty, struct isis_lsp *lsp)
|
||||
{
|
||||
char lspid[255];
|
||||
char buf[MONOTIME_STRLEN];
|
||||
|
||||
lspid_print(lsp->hdr.lsp_id, lspid, true, true);
|
||||
vty_out(vty, "Flooding information for %s\n", lspid);
|
||||
@ -129,21 +130,10 @@ static void lsp_print_flooding(struct vty *vty, struct isis_lsp *lsp)
|
||||
lsp->flooding_interface : "(null)");
|
||||
|
||||
time_t uptime = time(NULL) - lsp->flooding_time;
|
||||
struct tm tm;
|
||||
|
||||
gmtime_r(&uptime, &tm);
|
||||
frrtime_to_interval(uptime, buf, sizeof(buf));
|
||||
|
||||
if (uptime < ONE_DAY_SECOND)
|
||||
vty_out(vty, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (uptime < ONE_WEEK_SECOND)
|
||||
vty_out(vty, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
vty_out(vty, "%02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7),
|
||||
tm.tm_hour);
|
||||
vty_out(vty, " ago)\n");
|
||||
vty_out(vty, "%s ago)\n", buf);
|
||||
|
||||
if (lsp->flooding_circuit_scoped) {
|
||||
vty_out(vty, " Received as circuit-scoped LSP, so not "
|
||||
|
@ -112,6 +112,26 @@ static inline char *time_to_string(time_t ts, char *buf)
|
||||
return ctime_r(&tbuf, buf);
|
||||
}
|
||||
|
||||
/* Convert interval to human-friendly string, used in cli output e.g. */
|
||||
static inline const char *frrtime_to_interval(time_t t, char *buf,
|
||||
size_t buflen)
|
||||
{
|
||||
struct tm tm;
|
||||
|
||||
gmtime_r(&t, &tm);
|
||||
|
||||
if (t < ONE_DAY_SECOND)
|
||||
snprintf(buf, buflen, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (t < ONE_WEEK_SECOND)
|
||||
snprintf(buf, buflen, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
snprintf(buf, buflen, "%02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
|
||||
return buf;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -131,7 +131,6 @@ void rip_peer_bad_packet(struct rip *rip, struct sockaddr_in *from)
|
||||
static char *rip_peer_uptime(struct rip_peer *peer, char *buf, size_t len)
|
||||
{
|
||||
time_t uptime;
|
||||
struct tm tm;
|
||||
|
||||
/* If there is no connection has been done before print `never'. */
|
||||
if (peer->uptime == 0) {
|
||||
@ -142,17 +141,9 @@ static char *rip_peer_uptime(struct rip_peer *peer, char *buf, size_t len)
|
||||
/* Get current time. */
|
||||
uptime = time(NULL);
|
||||
uptime -= peer->uptime;
|
||||
gmtime_r(&uptime, &tm);
|
||||
|
||||
if (uptime < ONE_DAY_SECOND)
|
||||
snprintf(buf, len, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (uptime < ONE_WEEK_SECOND)
|
||||
snprintf(buf, len, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
snprintf(buf, len, "%02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
|
||||
frrtime_to_interval(uptime, buf, len);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -141,7 +141,6 @@ void ripng_peer_bad_packet(struct ripng *ripng, struct sockaddr_in6 *from)
|
||||
static char *ripng_peer_uptime(struct ripng_peer *peer, char *buf, size_t len)
|
||||
{
|
||||
time_t uptime;
|
||||
struct tm tm;
|
||||
|
||||
/* If there is no connection has been done before print `never'. */
|
||||
if (peer->uptime == 0) {
|
||||
@ -152,17 +151,9 @@ static char *ripng_peer_uptime(struct ripng_peer *peer, char *buf, size_t len)
|
||||
/* Get current time. */
|
||||
uptime = time(NULL);
|
||||
uptime -= peer->uptime;
|
||||
gmtime_r(&uptime, &tm);
|
||||
|
||||
if (uptime < ONE_DAY_SECOND)
|
||||
snprintf(buf, len, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (uptime < ONE_WEEK_SECOND)
|
||||
snprintf(buf, len, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
snprintf(buf, len, "%02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
|
||||
frrtime_to_interval(uptime, buf, len);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -241,25 +241,13 @@ static void vty_show_ip_route_detail(struct vty *vty, struct route_node *rn,
|
||||
vty_out(vty, "\n");
|
||||
|
||||
time_t uptime;
|
||||
struct tm tm;
|
||||
|
||||
uptime = monotime(NULL);
|
||||
uptime -= re->uptime;
|
||||
gmtime_r(&uptime, &tm);
|
||||
|
||||
vty_out(vty, " Last update ");
|
||||
frrtime_to_interval(uptime, buf, sizeof(buf));
|
||||
|
||||
if (uptime < ONE_DAY_SECOND)
|
||||
vty_out(vty, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (uptime < ONE_WEEK_SECOND)
|
||||
vty_out(vty, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
vty_out(vty, "%02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7),
|
||||
tm.tm_hour);
|
||||
vty_out(vty, " ago\n");
|
||||
vty_out(vty, " Last update %s ago\n", buf);
|
||||
|
||||
if (show_ng)
|
||||
vty_out(vty, " Nexthop Group ID: %u\n", re->nhe_id);
|
||||
@ -402,14 +390,15 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn,
|
||||
json_object *json_route = NULL;
|
||||
json_object *json_labels = NULL;
|
||||
time_t uptime;
|
||||
struct tm tm;
|
||||
struct vrf *vrf = NULL;
|
||||
rib_dest_t *dest = rib_dest_from_rnode(rn);
|
||||
struct nexthop_group *nhg;
|
||||
char up_str[MONOTIME_STRLEN];
|
||||
|
||||
uptime = monotime(NULL);
|
||||
uptime -= re->uptime;
|
||||
gmtime_r(&uptime, &tm);
|
||||
|
||||
frrtime_to_interval(uptime, up_str, sizeof(up_str));
|
||||
|
||||
/* If showing fib information, use the fib view of the
|
||||
* nexthops.
|
||||
@ -474,18 +463,8 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn,
|
||||
json_object_int_add(json_route, "internalNextHopActiveNum",
|
||||
nexthop_group_active_nexthop_num(
|
||||
&(re->nhe->nhg)));
|
||||
if (uptime < ONE_DAY_SECOND)
|
||||
sprintf(buf, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (uptime < ONE_WEEK_SECOND)
|
||||
sprintf(buf, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
sprintf(buf, "%02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7),
|
||||
tm.tm_hour);
|
||||
|
||||
json_object_string_add(json_route, "uptime", buf);
|
||||
json_object_string_add(json_route, "uptime", up_str);
|
||||
|
||||
for (ALL_NEXTHOPS_PTR(nhg, nexthop)) {
|
||||
json_nexthop = json_object_new_object();
|
||||
@ -774,17 +753,7 @@ static void vty_show_ip_route(struct vty *vty, struct route_node *rn,
|
||||
sizeof(buf), 1));
|
||||
}
|
||||
|
||||
if (uptime < ONE_DAY_SECOND)
|
||||
vty_out(vty, ", %02d:%02d:%02d", tm.tm_hour,
|
||||
tm.tm_min, tm.tm_sec);
|
||||
else if (uptime < ONE_WEEK_SECOND)
|
||||
vty_out(vty, ", %dd%02dh%02dm", tm.tm_yday,
|
||||
tm.tm_hour, tm.tm_min);
|
||||
else
|
||||
vty_out(vty, ", %02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7),
|
||||
tm.tm_hour);
|
||||
vty_out(vty, "\n");
|
||||
vty_out(vty, ", %s\n", up_str);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -858,7 +858,6 @@ void zserv_event(struct zserv *client, enum zserv_event event)
|
||||
#define ZEBRA_TIME_BUF 32
|
||||
static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
|
||||
{
|
||||
struct tm tm;
|
||||
time_t now;
|
||||
|
||||
assert(buf != NULL);
|
||||
@ -872,17 +871,9 @@ static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
|
||||
|
||||
now = monotime(NULL);
|
||||
now -= *time1;
|
||||
gmtime_r(&now, &tm);
|
||||
|
||||
if (now < ONE_DAY_SECOND)
|
||||
snprintf(buf, buflen, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (now < ONE_WEEK_SECOND)
|
||||
snprintf(buf, buflen, "%dd%02dh%02dm", tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
snprintf(buf, buflen, "%02dw%dd%02dh", tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7) * 7), tm.tm_hour);
|
||||
frrtime_to_interval(now, buf, buflen);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
@ -1001,8 +992,6 @@ static void zebra_show_stale_client_detail(struct vty *vty,
|
||||
struct zserv *client)
|
||||
{
|
||||
char buf[PREFIX2STR_BUFFER];
|
||||
struct tm tm;
|
||||
struct timeval tv;
|
||||
time_t uptime;
|
||||
struct client_gr_info *info = NULL;
|
||||
struct zserv *s = NULL;
|
||||
@ -1028,26 +1017,13 @@ static void zebra_show_stale_client_detail(struct vty *vty,
|
||||
if (ZEBRA_CLIENT_GR_ENABLED(info->capabilities)) {
|
||||
if (info->stale_client_ptr) {
|
||||
s = (struct zserv *)(info->stale_client_ptr);
|
||||
uptime = monotime(&tv);
|
||||
uptime = monotime(NULL);
|
||||
uptime -= s->restart_time;
|
||||
gmtime_r(&uptime, &tm);
|
||||
|
||||
vty_out(vty, "Last restart time : ");
|
||||
if (uptime < ONE_DAY_SECOND)
|
||||
vty_out(vty, "%02d:%02d:%02d",
|
||||
tm.tm_hour, tm.tm_min,
|
||||
tm.tm_sec);
|
||||
else if (uptime < ONE_WEEK_SECOND)
|
||||
vty_out(vty, "%dd%02dh%02dm",
|
||||
tm.tm_yday, tm.tm_hour,
|
||||
tm.tm_min);
|
||||
else
|
||||
vty_out(vty, "%02dw%dd%02dh",
|
||||
tm.tm_yday / 7,
|
||||
tm.tm_yday - ((tm.tm_yday / 7)
|
||||
* 7),
|
||||
tm.tm_hour);
|
||||
vty_out(vty, " ago\n");
|
||||
frrtime_to_interval(uptime, buf, sizeof(buf));
|
||||
|
||||
vty_out(vty, "Last restart time : %s ago\n",
|
||||
buf);
|
||||
|
||||
vty_out(vty, "Stalepath removal time: %d sec\n",
|
||||
info->stale_removal_time);
|
||||
|
Loading…
Reference in New Issue
Block a user