Initial commit: rrdd, rrdc, librrdc.so
[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 (RRDD_SOCK_PATH);
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     char **argv_copy;
82
83     fprintf (stdout, "RRDc 1.0.0  Copyright 2008 by Florian Forster "
84         "<octo at verplant.org>\n\n");
85     fflush (stdout);
86
87     /* Copy argv so we can assure that it is NULL terminated. */
88     argv_copy = (char **) malloc (sizeof (char *) * (argc + 1));
89     if (argv_copy == NULL)
90     {
91       fprintf (stderr, "malloc failed.\n");
92       return (1);
93     }
94     memcpy (argv_copy, argv, sizeof (char *) * argc);
95     /* argv_copy[0] = "rrdtool"; */
96     argv_copy[argc] = NULL;
97
98     status = execvp ("rrdtool", argv_copy);
99     fprintf (stderr, "execv failed with status %i.\n", status);
100     return (1);
101   }
102 } /* int main */
103
104 /*
105  * vim: set sw=2 sts=2 ts=8 et fdm=marker :
106  */