src/{lib,}rrdc.c: Make `rrdd_connect' use the default path if path == NULL.
[rrdd.git] / src / rrdc.c
1 /**
2  * collectd - src/rrdc.c
3  * Copyright (C) 2008 Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "rrdd.h"
23 #include "rrdc.h"
24
25 #include <libgen.h>
26
27 static void exit_usage (char *name)
28 {
29   printf ("Usage: %s <command> [options]\n"
30       "\n"
31       "Commands handled by rrdc:\n"
32       "  update\n"
33       "  help\n"
34       "\n"
35       "All other commands are passed to rrdtool(1).\n"
36       "\n", basename (name));
37   exit (0);
38 } /* void exit_usage */
39
40 int main (int argc, char **argv)
41 {
42   char *name = "rrdc";
43   int status;
44
45   if (argc > 0)
46     name = argv[0];
47
48   if ((argc < 2)
49       || (strcmp (argv[1], "help") == 0))
50     exit_usage (name);
51
52   /* rrdc update <file> <value> [<value> ..] */
53   if ((argc >= 4) && (strcmp (argv[1], "update") == 0))
54   {
55     status = rrdd_connect (/* path = */ NULL);
56     if (status != 0)
57     {
58       fprintf (stderr, "rrdd_connect failed: %s\n",
59           strerror (status));
60       return (1);
61     }
62
63     status = rrdd_update (argv[2], argc - 3, (void *) (argv + 3));
64     if (status < 0)
65     {
66       fprintf (stderr, "rrdd_update failed.\n");
67       status = 1;
68     }
69     else if (status > 0)
70     {
71       fprintf (stderr, "rrdd_update failed: %s\n",
72           strerror (status));
73       status = 1;
74     }
75
76     rrdd_disconnect ();
77     return (status);
78   }
79   else
80   {
81     fprintf (stdout, "RRDc %s  Copyright 2008 by Florian Forster "
82         "<octo at verplant.org>\n\n",
83         PACKAGE_VERSION);
84     fflush (stdout);
85
86     assert (argv[argc] == NULL);
87
88     status = execvp ("rrdtool", argv);
89     fprintf (stderr, "execv failed with status %i.\n", status);
90     return (1);
91   }
92 } /* int main */
93
94 /*
95  * vim: set sw=2 sts=2 ts=8 et fdm=marker :
96  */