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 **/
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>
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"
39 #ifndef CONFIGFILE
40 # define CONFIGFILE "/etc/collection.conf"
41 #endif
43 time_t last_read_mtime = 0;
45 static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
46 {
47 int i;
49 for (i = 0; i < ci->children_num; i++)
50 {
51 oconfig_item_t *child;
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 }
64 return (0);
65 } /* }}} int dispatch_config */
67 static int internal_read_config (void) /* {{{ */
68 {
69 oconfig_item_t *ci;
71 ci = oconfig_parse_file (CONFIGFILE);
72 if (ci == NULL)
73 return (-1);
75 dispatch_config (ci);
77 oconfig_free (ci);
79 gl_config_submit ();
81 return (0);
82 } /* }}} int internal_read_config */
84 static time_t get_config_mtime (void) /* {{{ */
85 {
86 struct stat statbuf;
87 int status;
89 memset (&statbuf, 0, sizeof (statbuf));
90 status = stat (CONFIGFILE, &statbuf);
91 if (status != 0)
92 return (0);
94 return (statbuf.st_mtime);
95 } /* }}} time_t get_config_mtime */
97 int graph_read_config (void) /* {{{ */
98 {
99 time_t mtime;
101 mtime = get_config_mtime ();
103 if (mtime <= last_read_mtime)
104 return (0);
106 internal_read_config ();
108 last_read_mtime = mtime;
110 return (0);
111 } /* }}} int graph_read_config */
113 int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
114 char **ret_str)
115 {
116 char *tmp;
118 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
119 return (EINVAL);
121 tmp = strdup (ci->values[0].value.string);
122 if (tmp == NULL)
123 return (ENOMEM);
125 free (*ret_str);
126 *ret_str = tmp;
128 return (0);
129 } /* }}} int graph_config_get_string */
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);
137 if (ci->values[0].value.boolean)
138 *ret_bool = 1;
139 else
140 *ret_bool = 0;
142 return (0);
143 } /* }}} int graph_config_get_bool */
145 /* vim: set sw=2 sts=2 et fdm=marker : */