src/graph_instance.[ch]: Implement "inst_data_to_json".
[collection4.git] / src / graph_config.c
1 /**
2  * collection4 - graph_config.c
3  * Copyright (C) 2010  Florian octo Forster
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA
19  *
20  * Authors:
21  *   Florian octo Forster <ff at octo.it>
22  **/
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31
32 #include "graph_config.h"
33 #include "graph.h"
34 #include "graph_list.h"
35 #include "oconfig.h"
36 #include "common.h"
37 #include "data_provider.h"
38
39 #ifndef CONFIGFILE
40 # define CONFIGFILE "/etc/collection.conf"
41 #endif
42
43 time_t last_read_mtime = 0;
44
45 static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
46 {
47   int i;
48
49   for (i = 0; i < ci->children_num; i++)
50   {
51     oconfig_item_t *child;
52
53     child = ci->children + i;
54     if (strcasecmp ("Graph", child->key) == 0)
55       graph_config_add (child);
56     else if (strcasecmp ("DataProvider", child->key) == 0)
57       data_provider_config (child);
58     else
59     {
60       DEBUG ("Unknown config option: %s", child->key);
61     }
62   }
63
64   return (0);
65 } /* }}} int dispatch_config */
66
67 static int internal_read_config (void) /* {{{ */
68 {
69   oconfig_item_t *ci;
70
71   ci = oconfig_parse_file (CONFIGFILE);
72   if (ci == NULL)
73     return (-1);
74
75   dispatch_config (ci);
76
77   oconfig_free (ci);
78
79   gl_config_submit ();
80
81   return (0);
82 } /* }}} int internal_read_config */
83
84 static time_t get_config_mtime (void) /* {{{ */
85 {
86   struct stat statbuf;
87   int status;
88
89   memset (&statbuf, 0, sizeof (statbuf));
90   status = stat (CONFIGFILE, &statbuf);
91   if (status != 0)
92     return (0);
93
94   return (statbuf.st_mtime);
95 } /* }}} time_t get_config_mtime */
96
97 int graph_read_config (void) /* {{{ */
98 {
99   time_t mtime;
100
101   mtime = get_config_mtime ();
102
103   if (mtime <= last_read_mtime)
104     return (0);
105
106   internal_read_config ();
107
108   last_read_mtime = mtime;
109
110   return (0);
111 } /* }}} int graph_read_config */
112
113 int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
114     char **ret_str)
115 {
116   char *tmp;
117
118   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
119     return (EINVAL);
120
121   tmp = strdup (ci->values[0].value.string);
122   if (tmp == NULL)
123     return (ENOMEM);
124
125   free (*ret_str);
126   *ret_str = tmp;
127
128   return (0);
129 } /* }}} int graph_config_get_string */
130
131 int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */
132     _Bool *ret_bool)
133 {
134   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
135     return (EINVAL);
136
137   if (ci->values[0].value.boolean)
138     *ret_bool = 1;
139   else
140     *ret_bool = 0;
141
142   return (0);
143 } /* }}} int graph_config_get_bool */
144
145 /* vim: set sw=2 sts=2 et fdm=marker : */