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