From: Ben Pfaff Date: Tue, 12 May 2009 20:48:26 +0000 (-0700) Subject: cfg-mod: Add --changes option for logging configuration changes. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b0ff8259ab5d4872c9d9164a6ab8c44bfad29999;p=openvswitch cfg-mod: Add --changes option for logging configuration changes. This makes it a lot easier to see what actually changed. --- diff --git a/lib/cfg.c b/lib/cfg.c index 53a71cda..67545f37 100644 --- a/lib/cfg.c +++ b/lib/cfg.c @@ -257,6 +257,14 @@ cfg_read(void) return 0; } +/* Fills 'svec' with the entire configuration file. */ +void +cfg_get_all(struct svec *svec) +{ + svec_clear(svec); + svec_append(svec, &cfg); +} + int cfg_get_cookie(uint8_t *cookie) { diff --git a/lib/cfg.h b/lib/cfg.h index 2b79f439..1216e062 100644 --- a/lib/cfg.h +++ b/lib/cfg.h @@ -44,6 +44,8 @@ void cfg_unlock(void); int cfg_write(void); int cfg_write_data(uint8_t *data, size_t len); +void cfg_get_all(struct svec *); + #define CFG_COOKIE_LEN SHA1HashSize int cfg_get_cookie(uint8_t *cookie); diff --git a/lib/vlog-modules.def b/lib/vlog-modules.def index 33a7d106..b5ac4134 100644 --- a/lib/vlog-modules.def +++ b/lib/vlog-modules.def @@ -4,6 +4,7 @@ VLOG_MODULE(brcompatd) VLOG_MODULE(bridge) VLOG_MODULE(chain) VLOG_MODULE(cfg) +VLOG_MODULE(cfg_mod) VLOG_MODULE(controller) VLOG_MODULE(coverage) VLOG_MODULE(ctlpath) diff --git a/utilities/cfg-mod.8.in b/utilities/cfg-mod.8.in index f97727d7..b2f212fd 100644 --- a/utilities/cfg-mod.8.in +++ b/utilities/cfg-mod.8.in @@ -64,6 +64,22 @@ Queries \fIfile\fR for entries that match \fIkey\fR. Each matching value is printed on a separate line. Duplicates will be printed multiple times. . +.IP "\fB-c\fR, \fB--changes\fR" +. +Logs all of the changes made to the configuration file in a ``unified +diff''-like format. Only actual changes are logged, so that if, for +example, a \fB--del-match\fR action did not match any key-value pairs, +then nothing will be logged due to that action. Furthermore, only the +net effects of changes are logged: if a key-value pair was deleted and +then an identical key-value pair was added back, then nothing would be +logged due to those changes. +. +This action logs changes that have taken effect at the point where it +is inserted. Thus, if it is given before any other action, it will +not log any changes. If \fB--changes\fR is given more than once, +instances after the first log only the changes since the previous +instance. +. .SH "SEE ALSO" .BR vswitchd (8), .BR vswitchd.conf (5) diff --git a/utilities/cfg-mod.c b/utilities/cfg-mod.c index f62ffef8..b09a7ced 100644 --- a/utilities/cfg-mod.c +++ b/utilities/cfg-mod.c @@ -39,6 +39,12 @@ #include "timeval.h" #include "util.h" +#define THIS_MODULE VLM_cfg_mod +#include "vlog.h" + +/* Configuration when we first read the configuration file. */ +static struct svec orig_cfg = SVEC_EMPTY_INITIALIZER; + static void usage(char *prog_name, int exit_code) { @@ -50,7 +56,8 @@ usage(char *prog_name, int exit_code) " -d, --del-entry=ENTRY delete ENTRY\n" " -D, --del-section=KEY delete section matching KEY\n" " --del-match=PATTERN delete entries matching shell PATTERN\n" - " -q, --query=KEY return all entries matching KEY \n", + " -q, --query=KEY return all entries matching KEY\n" + " -c, --log-changes log changes up to this point\n", prog_name); exit(exit_code); } @@ -70,6 +77,8 @@ open_config(char *config_file) if (error) { ovs_fatal(error, "could not lock configuration file\n"); } + + cfg_get_all(&orig_cfg); } static void @@ -85,7 +94,33 @@ print_vals(char *key) printf("%s\n", vals.names[i]); } } - + +static void +log_diffs(void) +{ + struct svec new_cfg, removed, added; + size_t i; + + svec_init(&new_cfg); + cfg_get_all(&new_cfg); + svec_diff(&orig_cfg, &new_cfg, &removed, NULL, &added); + if (removed.n || added.n) { + VLOG_INFO("configuration changes:"); + for (i = 0; i < removed.n; i++) { + VLOG_INFO("-%s", removed.names[i]); + } + for (i = 0; i < added.n; i++) { + VLOG_INFO("+%s", added.names[i]); + } + } else { + VLOG_INFO("configuration unchanged"); + } + svec_destroy(&added); + svec_destroy(&removed); + svec_swap(&new_cfg, &orig_cfg); + svec_destroy(&new_cfg); +} + int main(int argc, char *argv[]) { enum { @@ -98,6 +133,8 @@ int main(int argc, char *argv[]) {"del-section", required_argument, 0, 'D'}, {"del-match", required_argument, 0, OPT_DEL_MATCH}, {"query", required_argument, 0, 'q'}, + {"changes", no_argument, 0, 'c'}, + {"verbose", optional_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}, }; @@ -107,6 +144,7 @@ int main(int argc, char *argv[]) set_program_name(argv[0]); time_init(); + vlog_init(); short_options = long_options_to_short_options(long_options); for (;;) { @@ -152,10 +190,18 @@ int main(int argc, char *argv[]) print_vals(optarg); break; + case 'c': + log_diffs(); + break; + case 'h': usage(argv[0], EXIT_SUCCESS); break; + case 'v': + vlog_set_verbosity(optarg); + break; + case '?': exit(EXIT_FAILURE);