From: claudio Date: Mon, 3 May 2021 14:01:56 +0000 (+0000) Subject: Print the timestamps both formatted and as a time in seconds field in X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=fd18fec492a868e2cb2cdc589d6915b12c23de99;p=openbsd Print the timestamps both formatted and as a time in seconds field in the various JSON object that have time values. OK benno@ --- diff --git a/usr.sbin/bgpctl/bgpctl.c b/usr.sbin/bgpctl/bgpctl.c index 02fc5ab0535..d99661595f9 100644 --- a/usr.sbin/bgpctl/bgpctl.c +++ b/usr.sbin/bgpctl/bgpctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpctl.c,v 1.266 2021/04/15 14:12:05 claudio Exp $ */ +/* $OpenBSD: bgpctl.c,v 1.267 2021/05/03 14:01:56 claudio Exp $ */ /* * Copyright (c) 2003 Henning Brauer @@ -510,6 +510,20 @@ show(struct imsg *imsg, struct parse_result *res) return (0); } +time_t +get_monotime(time_t t) +{ + struct timespec ts; + + if (t == 0) + return -1; + if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) + err(1, "clock_gettime"); + if (t > ts.tv_sec) /* time in the future is not possible */ + t = ts.tv_sec; + return (ts.tv_sec - t); +} + char * fmt_peer(const char *descr, const struct bgpd_addr *remote_addr, int masklen) @@ -596,16 +610,12 @@ fmt_timeframe(time_t t) const char * fmt_monotime(time_t t) { - struct timespec ts; + t = get_monotime(t); - if (t == 0) + if (t == -1) return ("Never"); - if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) - err(1, "clock_gettime"); - if (t > ts.tv_sec) /* time in the future is not possible */ - t = ts.tv_sec; - return (fmt_timeframe(ts.tv_sec - t)); + return (fmt_timeframe(t)); } const char * diff --git a/usr.sbin/bgpctl/bgpctl.h b/usr.sbin/bgpctl/bgpctl.h index 25b28db38ec..cf544df9ae2 100644 --- a/usr.sbin/bgpctl/bgpctl.h +++ b/usr.sbin/bgpctl/bgpctl.h @@ -41,6 +41,7 @@ extern const size_t pt_sizes[]; #define EOL0(flag) ((flag & F_CTL_SSV) ? ';' : '\n') +time_t get_monotime(time_t); char *fmt_peer(const char *, const struct bgpd_addr *, int); const char *fmt_timeframe(time_t); const char *fmt_monotime(time_t); diff --git a/usr.sbin/bgpctl/output_json.c b/usr.sbin/bgpctl/output_json.c index 4d19dd1971b..1afa4425754 100644 --- a/usr.sbin/bgpctl/output_json.c +++ b/usr.sbin/bgpctl/output_json.c @@ -1,4 +1,4 @@ -/* $OpenBSD: output_json.c,v 1.9 2021/04/15 14:12:05 claudio Exp $ */ +/* $OpenBSD: output_json.c,v 1.10 2021/05/03 14:01:56 claudio Exp $ */ /* * Copyright (c) 2020 Claudio Jeker @@ -104,7 +104,9 @@ json_neighbor_stats(struct peer *p) { json_do_object("stats"); json_do_printf("last_read", "%s", fmt_monotime(p->stats.last_read)); + json_do_int("last_read_sec", get_monotime(p->stats.last_read)); json_do_printf("last_write", "%s", fmt_monotime(p->stats.last_write)); + json_do_int("last_write_sec", get_monotime(p->stats.last_write)); json_do_object("prefixes"); json_do_uint("sent", p->stats.prefix_out_cnt); @@ -267,6 +269,7 @@ json_neighbor(struct peer *p, struct parse_result *res) } json_do_printf("state", "%s", statenames[p->state]); json_do_printf("last_updown", "%s", fmt_monotime(p->stats.last_updown)); + json_do_int("last_updown_sec", get_monotime(p->stats.last_updown)); switch (res->action) { case SHOW: @@ -827,6 +830,7 @@ json_rib(struct ctl_show_rib *r, u_char *asdata, size_t aslen, json_do_uint("localpref", r->local_pref); json_do_uint("weight", r->weight); json_do_printf("last_update", "%s", fmt_timeframe(r->age)); + json_do_int("last_update_sec", r->age); /* keep the object open for communities and attribuites */ } @@ -927,6 +931,7 @@ json_rib_set(struct ctl_show_set *set) json_do_printf("name", "%s", set->name); json_do_printf("type", "%s", fmt_set_type(set)); json_do_printf("last_change", "%s", fmt_monotime(set->lastchange)); + json_do_int("last_change_sec", get_monotime(set->lastchange)); if (set->type == ASNUM_SET) { json_do_uint("num_ASnum", set->as_cnt); } else {