1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <errno.h>
9 #include "graph_config.h"
10 #include "graph.h"
11 #include "graph_list.h"
12 #include "oconfig.h"
13 #include "common.h"
15 #ifndef CONFIGFILE
16 # define CONFIGFILE "/etc/collection.conf"
17 #endif
19 time_t last_read_mtime = 0;
21 static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
22 {
23 int i;
25 for (i = 0; i < ci->children_num; i++)
26 {
27 oconfig_item_t *child;
29 child = ci->children + i;
30 if (strcasecmp ("Graph", child->key) == 0)
31 graph_config_add (child);
32 else
33 {
34 DEBUG ("Unknown config option: %s", child->key);
35 }
36 }
38 return (0);
39 } /* }}} int dispatch_config */
41 static int internal_read_config (void) /* {{{ */
42 {
43 oconfig_item_t *ci;
45 ci = oconfig_parse_file (CONFIGFILE);
46 if (ci == NULL)
47 return (-1);
49 dispatch_config (ci);
51 oconfig_free (ci);
53 gl_config_submit ();
55 return (0);
56 } /* }}} int internal_read_config */
58 static time_t get_config_mtime (void) /* {{{ */
59 {
60 struct stat statbuf;
61 int status;
63 memset (&statbuf, 0, sizeof (statbuf));
64 status = stat (CONFIGFILE, &statbuf);
65 if (status != 0)
66 return (0);
68 return (statbuf.st_mtime);
69 } /* }}} time_t get_config_mtime */
71 int graph_read_config (void) /* {{{ */
72 {
73 time_t mtime;
75 mtime = get_config_mtime ();
77 if (mtime <= last_read_mtime)
78 return (0);
80 internal_read_config ();
82 last_read_mtime = mtime;
84 return (0);
85 } /* }}} int graph_read_config */
87 int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
88 char **ret_str)
89 {
90 char *tmp;
92 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
93 return (EINVAL);
95 tmp = strdup (ci->values[0].value.string);
96 if (tmp == NULL)
97 return (ENOMEM);
99 free (*ret_str);
100 *ret_str = tmp;
102 return (0);
103 } /* }}} int graph_config_get_string */
105 int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */
106 _Bool *ret_bool)
107 {
108 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
109 return (EINVAL);
111 if (ci->values[0].value.boolean)
112 *ret_bool = 1;
113 else
114 *ret_bool = 0;
116 return (0);
117 } /* }}} int graph_config_get_bool */
119 /* vim: set sw=2 sts=2 et fdm=marker : */