From 8856d7def89bebef924ef90d04a0baf088744bbc Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sat, 7 Aug 2010 09:43:33 +0200 Subject: [PATCH] =?utf8?q?collectd-flush:=20Do=20not=20use=20=E2=80=98geto?= =?utf8?q?pt=5Flong()=E2=80=99.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is a GNU extension and, thus, not portable. Also, pimped the usage output a bit. --- src/collectd-flush.c | 78 ++++++++++++++++++++++++++++---------------------- src/collectd-flush.pod | 32 ++++++++++----------- 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/src/collectd-flush.c b/src/collectd-flush.c index 165c073a..7b9326bf 100644 --- a/src/collectd-flush.c +++ b/src/collectd-flush.c @@ -80,22 +80,35 @@ static int flush ( } static void exit_usage (const char *name, int status) { - fprintf ((status == 0) ? stdout : stderr, "Usage: %s [options]\n" - "\n" - "Valid options are:\n" - " -h, --help Display this help message.\n" - " -s, --socket= Path to collectd's UNIX socket. Default: /var/run/collectd-unixsock\n" - " -p, --plugin= Plugin to flush _to_ (not from). Example: rrdtool\n" - " -i, --identifier=\n" - " Only flush data specified by , which has the format: \n" - "\n" - " [/][-]/[-]\n" - "\n" - " Hostname defaults to the local hostname if omitted.\n" - " No error is returned if the specified identifier does not exist.\n" - " Examples: uptime/uptime\n" - " somehost/cpu-0/cpu-wait\n" - " -t, --timeout= Only flush values older than this timeout.\n", name); + fprintf ((status == 0) ? stdout : stderr, + "Usage: %s [options]\n\n" + + "Available options:\n" + " -s Path to collectd's UNIX socket.\n" + " Default: /var/run/collectd-unixsock\n" + " -p Plugin to be flushed.\n" + " -i Flush data identified by only (see below).\n" + " -t Flush values older than this value only.\n" + + "\n -h Display this help and exit.\n" + + "\nIdentfiers:\n\n" + + "An identifier (as accepted by the -i option) has the following\n" + "format:\n\n" + + " [/][-]/[-]\n\n" + + "Hostname defaults to the local hostname if omitted (e.g., uptime/uptime).\n" + "No error is returned if the specified identifier does not exist.\n" + + "\nExample:\n\n" + + " collectd-flush -p rrdtool -i somehost/cpu-0/cpu-wait\n\n" + + "Flushes all CPU wait RRD values of the first CPU of the local host.\n" + "I.e., writes all pending RRD updates of that data-source to disk.\n" + , name); exit (status); } @@ -120,45 +133,41 @@ int main (int argc, char **argv) { char ident_str[1024] = ""; int timeout = -1; char hostname[1024]; - char c; - static struct option long_options[] = - { - {"help", no_argument, 0, 'h'}, - {"socket", required_argument, 0, 's'}, - {"plugin", required_argument, 0, 'p'}, - {"identifier", required_argument, 0, 'i'}, - {"timeout", required_argument, 0, 't'} - }; - int option_index = 0; + while (42) { + int c; + c = getopt (argc, argv, "s:p:i:ht:"); + + if (c == -1) + break; - while ((c = getopt_long (argc, argv, "s:p:i:ht:", long_options, &option_index)) != -1) { switch (c) { case 's': snprintf (address, sizeof (address), "unix:%s", optarg); + address[sizeof (address) - 1] = '\0'; break; case 'p': plugin = optarg; break; case 'i': - if(charoccurences(optarg, '/') == 1) { + if (charoccurences (optarg, '/') == 1) { /* The user has omitted the hostname part of the identifier * (there is only one '/' in the identifier) * Let's add the local hostname */ - if(gethostname(hostname, sizeof(hostname)) != 0) { - fprintf (stderr, "Could not get local hostname: %s", strerror(errno)); + if (gethostname (hostname, sizeof (hostname)) != 0) { + fprintf (stderr, "Could not get local hostname: %s", strerror (errno)); return 1; } /* Make sure hostname is zero-terminated */ - hostname[sizeof(hostname)-1] = '\0'; + hostname[sizeof (hostname) - 1] = '\0'; snprintf (ident_str, sizeof (ident_str), "%s/%s", hostname, optarg); /* Make sure ident_str is zero terminated */ - ident_str[sizeof(ident_str)-1] = '\0'; + ident_str[sizeof(ident_str) - 1] = '\0'; } else { - strncpy(ident_str, optarg, sizeof (ident_str)); + strncpy (ident_str, optarg, sizeof (ident_str)); /* Make sure identifier is zero terminated */ - ident_str[sizeof(ident_str)-1] = '\0'; + ident_str[sizeof (ident_str) - 1] = '\0'; } break; case 't': @@ -166,6 +175,7 @@ int main (int argc, char **argv) { break; case 'h': exit_usage (argv[0], 0); + break; default: exit_usage (argv[0], 1); } diff --git a/src/collectd-flush.pod b/src/collectd-flush.pod index cf7b178b..f3fb866a 100644 --- a/src/collectd-flush.pod +++ b/src/collectd-flush.pod @@ -8,33 +8,29 @@ collectd-flush I<[options]> =head1 DESCRIPTION -This small command line utitilty uses C to flush collectd -through a socket from the L. Useful if you want to be sure -you have the latest values in your RRD files before graphing them or copying +This small command line utitilty uses C to flush collectd +through a socket from the L. Useful if you want to be sure +you have the latest values in your RRD files before graphing them or copying them somewhere else. =head1 ARGUMENTS AND OPTIONS The following arguments and options are understood by collectd-flush. The order -of the arguments generally doesn't matter, as long as no argument is passed +of the arguments generally doesn't matter, as long as no argument is passed more than once. =over 4 -=item B<-h>, B<--help> +=item B<-s> I -Display information about the options. - -=item B<-s>, B<--socket=>I - -Path to the UNIX socket opened by collectd's C. +Path to the UNIX socket opened by collectd's C. Default: /var/run/collectd-unixsock -=item B<-p>, B<--plugin=>I +=item B<-p> I Plugin to flush I. Example: B. -=item B<-i>, B<--identifier=>I +=item B<-i> I If this option is present, only the data specified by I will be flushed. I has the following format: @@ -47,14 +43,18 @@ Examples: otherhost/memory/memory-used Hostname defaults to the local hostname if omitted. No error is returned if the -specified identifier does not exist (this is a limitation in the -C library).You can only specify one identifier each time you +specified identifier does not exist (this is a limitation in the +C library). You can only specify one identifier each time you run this program (even though L supports multiple identifiers). -=item B<-t>, B<--timeout=>I +=item B<-t> I + +Only flush values older than I seconds. + +=item B<-h> -Only flush values older than I. +Display usage information and exit. =back -- 2.11.0