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_list.h"
11 #include "oconfig.h"
12 #include "common.h"
14 #ifndef CONFIGFILE
15 # define CONFIGFILE "/etc/collection.conf"
16 #endif
18 time_t last_read_mtime = 0;
20 static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
21 {
22 int i;
24 for (i = 0; i < ci->children_num; i++)
25 {
26 oconfig_item_t *child;
28 child = ci->children + i;
29 if (strcasecmp ("Graph", child->key) == 0)
30 graph_config_add (child);
31 else
32 {
33 DEBUG ("Unknown config option: %s", child->key);
34 }
35 }
37 return (0);
38 } /* }}} int dispatch_config */
40 static int internal_read_config (void) /* {{{ */
41 {
42 oconfig_item_t *ci;
44 ci = oconfig_parse_file (CONFIGFILE);
45 if (ci == NULL)
46 return (-1);
48 dispatch_config (ci);
50 oconfig_free (ci);
52 gl_config_submit ();
54 return (0);
55 } /* }}} int internal_read_config */
57 static time_t get_config_mtime (void) /* {{{ */
58 {
59 struct stat statbuf;
60 int status;
62 memset (&statbuf, 0, sizeof (statbuf));
63 status = stat (CONFIGFILE, &statbuf);
64 if (status != 0)
65 return (0);
67 return (statbuf.st_mtime);
68 } /* }}} time_t get_config_mtime */
70 int graph_read_config (void) /* {{{ */
71 {
72 time_t mtime;
74 mtime = get_config_mtime ();
76 if (mtime <= last_read_mtime)
77 return (0);
79 internal_read_config ();
81 last_read_mtime = mtime;
83 return (0);
84 } /* }}} int graph_read_config */
86 int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
87 char **ret_str)
88 {
89 char *tmp;
91 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
92 return (EINVAL);
94 tmp = strdup (ci->values[0].value.string);
95 if (tmp == NULL)
96 return (ENOMEM);
98 free (*ret_str);
99 *ret_str = tmp;
101 return (0);
102 } /* }}} int graph_config_get_string */
104 int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */
105 _Bool *ret_bool)
106 {
107 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
108 return (EINVAL);
110 if (ci->values[0].value.boolean)
111 *ret_bool = 1;
112 else
113 *ret_bool = 0;
115 return (0);
116 } /* }}} int graph_config_get_bool */
118 /* vim: set sw=2 sts=2 et fdm=marker : */