4f58df0667d0322051cb2067e616038388599b76
[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
38 #ifndef CONFIGFILE
39 # define CONFIGFILE "/etc/collection.conf"
40 #endif
41
42 time_t last_read_mtime = 0;
43
44 static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
45 {
46   int i;
47
48   for (i = 0; i < ci->children_num; i++)
49   {
50     oconfig_item_t *child;
51
52     child = ci->children + i;
53     if (strcasecmp ("Graph", child->key) == 0)
54       graph_config_add (child);
55     else
56     {
57       DEBUG ("Unknown config option: %s", child->key);
58     }
59   }
60
61   return (0);
62 } /* }}} int dispatch_config */
63
64 static int internal_read_config (void) /* {{{ */
65 {
66   oconfig_item_t *ci;
67
68   ci = oconfig_parse_file (CONFIGFILE);
69   if (ci == NULL)
70     return (-1);
71
72   dispatch_config (ci);
73
74   oconfig_free (ci);
75
76   gl_config_submit ();
77
78   return (0);
79 } /* }}} int internal_read_config */
80
81 static time_t get_config_mtime (void) /* {{{ */
82 {
83   struct stat statbuf;
84   int status;
85
86   memset (&statbuf, 0, sizeof (statbuf));
87   status = stat (CONFIGFILE, &statbuf);
88   if (status != 0)
89     return (0);
90
91   return (statbuf.st_mtime);
92 } /* }}} time_t get_config_mtime */
93
94 int graph_read_config (void) /* {{{ */
95 {
96   time_t mtime;
97
98   mtime = get_config_mtime ();
99
100   if (mtime <= last_read_mtime)
101     return (0);
102
103   internal_read_config ();
104
105   last_read_mtime = mtime;
106
107   return (0);
108 } /* }}} int graph_read_config */
109
110 int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
111     char **ret_str)
112 {
113   char *tmp;
114
115   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
116     return (EINVAL);
117
118   tmp = strdup (ci->values[0].value.string);
119   if (tmp == NULL)
120     return (ENOMEM);
121
122   free (*ret_str);
123   *ret_str = tmp;
124
125   return (0);
126 } /* }}} int graph_config_get_string */
127
128 int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */
129     _Bool *ret_bool)
130 {
131   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
132     return (EINVAL);
133
134   if (ci->values[0].value.boolean)
135     *ret_bool = 1;
136   else
137     *ret_bool = 0;
138
139   return (0);
140 } /* }}} int graph_config_get_bool */
141
142 /* vim: set sw=2 sts=2 et fdm=marker : */