summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2012-01-27 12:48:21 -0800
committerBen Pfaff <blp@nicira.com>2012-02-02 16:24:51 -0800
commit8f46c9bb842d2564297939aef369b4e4baaaaa43 (patch)
tree92594bc050b6e32e364926002a3d3d3f4be9b2df
parentc2158145f3c7ea1653e5fbc69389ffdaceecd143 (diff)
downloadopenvswitch-8f46c9bb842d2564297939aef369b4e4baaaaa43.tar.gz
ovsdb-client: Add optional timestamps to "monitor" command output.
Suggestion #9347. Suggested-by: Alan Shieh <ashieh@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
-rw-r--r--NEWS3
-rw-r--r--lib/table.c43
-rw-r--r--lib/table.h4
-rw-r--r--ovsdb/ovsdb-client.1.in7
-rw-r--r--ovsdb/ovsdb-client.c13
5 files changed, 67 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 592e01359..682d83bd1 100644
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,9 @@ post-v1.5.0
should maintain by updating with ofproto_rule_update_used().
- The default MAC learning timeout has been increased from 60 seconds
to 300 seconds. The MAC learning timeout is now configurable.
+ - ovsdb-client:
+ - The new option --timestamp causes the "monitor" command to print
+ a timestamp with every update.
v1.5.0 - xx xxx xxxx
diff --git a/lib/table.c b/lib/table.c
index 537fae97c..8141677c3 100644
--- a/lib/table.c
+++ b/lib/table.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
#include "json.h"
#include "ovsdb-data.h"
#include "ovsdb-error.h"
+#include "timeval.h"
#include "util.h"
struct column {
@@ -123,6 +124,14 @@ table_set_caption(struct table *table, char *caption)
table->caption = caption;
}
+/* Turns printing a timestamp along with 'table' on or off, according to
+ * 'timestamp'. */
+void
+table_set_timestamp(struct table *table, bool timestamp)
+{
+ table->timestamp = timestamp;
+}
+
/* Adds a new column to 'table' just to the right of any existing column, with
* 'heading' as a title for the column. 'heading' must be a valid printf()
* format specifier.
@@ -212,6 +221,24 @@ table_print_table_line__(struct ds *line)
}
static void
+table_format_timestamp__(char *s, size_t size)
+{
+ time_t now = time_wall();
+ strftime(s, size, "%Y-%m-%d %H:%M:%S", localtime(&now));
+}
+
+static void
+table_print_timestamp__(const struct table *table)
+{
+ if (table->timestamp) {
+ char s[32];
+
+ table_format_timestamp__(s, sizeof s);
+ puts(s);
+ }
+}
+
+static void
table_print_table__(const struct table *table, const struct table_style *style)
{
static int n = 0;
@@ -223,6 +250,8 @@ table_print_table__(const struct table *table, const struct table_style *style)
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
@@ -286,6 +315,8 @@ table_print_list__(const struct table *table, const struct table_style *style)
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
@@ -357,6 +388,8 @@ table_print_html__(const struct table *table, const struct table_style *style)
{
size_t x, y;
+ table_print_timestamp__(table);
+
fputs("<table border=1>\n", stdout);
if (table->caption) {
@@ -427,6 +460,8 @@ table_print_csv__(const struct table *table, const struct table_style *style)
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
@@ -465,6 +500,12 @@ table_print_json__(const struct table *table, const struct table_style *style)
if (table->caption) {
json_object_put_string(json, "caption", table->caption);
}
+ if (table->timestamp) {
+ char s[32];
+
+ table_format_timestamp__(s, sizeof s);
+ json_object_put_string(json, "time", s);
+ }
headings = json_array_create_empty();
for (x = 0; x < table->n_columns; x++) {
diff --git a/lib/table.h b/lib/table.h
index 146d4dfe3..c29d7e31f 100644
--- a/lib/table.h
+++ b/lib/table.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,11 +32,13 @@ struct table {
size_t n_rows, allocated_rows;
size_t current_column;
char *caption;
+ bool timestamp;
};
void table_init(struct table *);
void table_destroy(struct table *);
void table_set_caption(struct table *, char *caption);
+void table_set_timestamp(struct table *, bool timestamp);
void table_add_column(struct table *, const char *heading, ...)
PRINTF_FORMAT(2, 3);
diff --git a/ovsdb/ovsdb-client.1.in b/ovsdb/ovsdb-client.1.in
index e513a3bb1..0065ced3b 100644
--- a/ovsdb/ovsdb-client.1.in
+++ b/ovsdb/ovsdb-client.1.in
@@ -40,6 +40,7 @@ ovsdb\-client \- command-line interface to \fBovsdb-server\fR(1)
[\fB\-\-pretty\fR]
[\fB\-\-bare\fR]
[\fB\-\-no\-heading\fR]
+[\fB\-\-timestamp\fR]
.so lib/daemon-syn.man
.so lib/vlog-syn.man
.so lib/ssl-syn.man
@@ -137,6 +138,12 @@ The following options controlling output formatting:
.ds TD (default)
.so lib/table.man
.
+.IP "\fB\-\-timestamp\fR"
+For the \fB\-\-monitor\fR command, adds a timestamp to each table
+update. Most output formats add the timestamp on a line of its own
+just above the table. The JSON output format puts the timestamp in a
+member of the top-level JSON object named \fBtime\fR.
+.
.SS "Daemon Options"
The daemon options apply only to the \fBmonitor\fR command. With any
other command, they have no effect.
diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c
index d2a9de10e..afa61c3d2 100644
--- a/ovsdb/ovsdb-client.c
+++ b/ovsdb/ovsdb-client.c
@@ -64,6 +64,9 @@ struct ovsdb_client_command {
int argc, char *argv[]);
};
+/* --timestamp: Print a timestamp before each update on "monitor" command? */
+static bool timestamp;
+
/* Format for table output. */
static struct table_style table_style = TABLE_STYLE_DEFAULT;
@@ -160,6 +163,7 @@ parse_options(int argc, char *argv[])
{
enum {
OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
+ OPT_TIMESTAMP,
DAEMON_OPTION_ENUMS,
TABLE_OPTION_ENUMS
};
@@ -167,6 +171,7 @@ parse_options(int argc, char *argv[])
{"verbose", optional_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
+ {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
DAEMON_LONG_OPTIONS,
#ifdef HAVE_OPENSSL
{"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
@@ -207,6 +212,10 @@ parse_options(int argc, char *argv[])
stream_ssl_set_ca_cert_file(optarg, true);
break;
+ case OPT_TIMESTAMP:
+ timestamp = true;
+ break;
+
case '?':
exit(EXIT_FAILURE);
@@ -256,7 +265,8 @@ usage(void)
" (\"table\", \"html\", \"csv\", "
"or \"json\")\n"
" --no-headings omit table heading row\n"
- " --pretty pretty-print JSON in output");
+ " --pretty pretty-print JSON in output\n"
+ " --timestamp timestamp \"monitor\" output");
daemon_usage();
vlog_usage();
printf("\nOther options:\n"
@@ -532,6 +542,7 @@ monitor_print(struct json *table_updates,
size_t i;
table_init(&t);
+ table_set_timestamp(&t, timestamp);
if (table_updates->type != JSON_OBJECT) {
ovs_error(0, "<table-updates> is not object");