2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file sysmon/gnunet-daemon-sysmon.c
23 * @brief system monitoring daemon
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_statistics_service.h"
44 struct SysmonProperty *next;
45 struct SysmonProperty *prev;
50 struct GNUNET_TIME_Relative interval;
55 GNUNET_SCHEDULER_TaskIdentifier task_id;
56 GNUNET_SCHEDULER_Task task;
66 * Configuration handle
68 const struct GNUNET_CONFIGURATION_Handle *cfg;
74 struct GNUNET_STATISTICS_Handle *stats;
80 GNUNET_SCHEDULER_TaskIdentifier end_task;
82 struct SysmonProperty *sp_head;
83 struct SysmonProperty *sp_tail;
86 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
88 struct SysmonProperty *sp;
89 struct SysmonProperty *next;
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon stopping ... \n");
93 end_task = GNUNET_SCHEDULER_NO_TASK;
97 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
102 while (NULL != (sp = next))
104 GNUNET_CONTAINER_DLL_remove (sp_head, sp_tail, sp);
106 if (GNUNET_SCHEDULER_NO_TASK != sp->task_id)
108 GNUNET_SCHEDULER_cancel (sp->task_id);
109 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
111 GNUNET_free_non_null (sp->desc);
120 if (GNUNET_SCHEDULER_NO_TASK != end_task)
121 GNUNET_SCHEDULER_cancel (end_task);
122 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
126 to_lower_str (char * str)
129 for (c = 0; c <= strlen (str); c++)
130 str[c] = tolower(str[c]);
134 put_property (struct SysmonProperty *sp)
136 if (v_numeric ==sp->value_type)
138 GNUNET_STATISTICS_set (stats, sp->desc, sp->num_val, GNUNET_NO);
140 else if (v_string ==sp->value_type)
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NOT IMPLEMENTED\n");
147 return GNUNET_SYSERR;
153 load_property (void *cls,
156 struct GNUNET_CONFIGURATION_Handle *properties = cls;
157 struct SysmonProperty *sp;
160 if (NULL == strstr (section, "sysmon-"))
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading section `%s'\n", section);
165 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value(properties, section, "TYPE"))
167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
171 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value(properties, section,"VALUE"))
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
177 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
180 GNUNET_CONFIGURATION_get_value_string(properties, section, "DESCRIPTION", &sp->desc);
183 GNUNET_CONFIGURATION_get_value_string(properties, section, "TYPE", &tmp);
185 if (0 == strcasecmp(tmp, "static"))
187 else if (0 == strcasecmp(tmp, "continous"))
188 sp->type = t_continous;
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
192 tmp, "TYPE", section);
200 GNUNET_CONFIGURATION_get_value_string(properties, section, "VALUE", &tmp);
202 if (0 == strcasecmp(tmp, "numeric"))
203 sp->value_type = v_numeric;
204 else if (0 == strcasecmp(tmp, "string"))
205 sp->value_type = v_string;
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
209 tmp, "VALUE", section);
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded property `%s': type %u, value %u,\n",
219 (NULL != sp->desc) ? sp->desc: "<undefined>",
220 sp->type, sp->value_type);
222 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
227 update_uptime (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct SysmonProperty *sp = cls;
230 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
233 sp->task_id = GNUNET_SCHEDULER_add_delayed (sp->interval, sp->task, sp);
237 load_default_properties (void)
239 struct SysmonProperty *sp;
240 /* GNUnet version array */
243 /* GNUnet vcs revision */
244 unsigned int revision;
248 if (3 != sscanf (VERSION, "%u.%u.%u", &ver[0], &ver[1], &ver[2]))
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse version string `%s'\n", VERSION);
259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Version string is undefined \n");
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Version: %u.%u.%u\n", ver[0], ver[1], ver[2]);
263 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
264 sp->desc = GNUNET_strdup ("GNUnet version");
266 sp->value_type = v_numeric;
267 sp->num_val = 100 * ver[0] + 10 * ver[1] + ver[2];
268 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
272 if (1 != sscanf (VCS_VERSION, "svn-%uM", &revision))
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse revision string `%s'\n", VCS_VERSION);
278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "VCS revision string is undefined \n");
281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Revision: %u\n", revision);
283 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
284 sp->desc = GNUNET_strdup ("GNUnet vcs revision");
286 sp->value_type = v_numeric;
287 sp->num_val = (uint64_t) revision;
288 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
291 /* GNUnet startup time */
292 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
293 sp->desc = GNUNET_strdup ("GNUnet startup time");
295 sp->value_type = v_numeric;
296 sp->num_val = (uint64_t) GNUNET_TIME_absolute_get().abs_value;
297 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
300 /* GNUnet sysmon daemon uptime */
301 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
302 sp->desc = GNUNET_strdup ("GNUnet uptime");
303 sp->type = t_continous;
304 sp->value_type = v_numeric;
305 sp->num_val = (uint64_t) 0;
306 sp->interval = GNUNET_TIME_UNIT_SECONDS;
307 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
308 sp->task = update_uptime;
309 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
315 run_properties (void)
317 struct SysmonProperty *sp;
319 for (sp = sp_head; NULL != sp; sp = sp->next)
321 if (t_static == sp->type)
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running static property `%s' \n", sp->desc);
328 if (NULL == sp->task)
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running continous property `%s' \n", sp->desc);
334 sp->task_id = GNUNET_SCHEDULER_add_now (&update_uptime, sp);
341 * Main function that will be run by the scheduler.
344 * @param args remaining command-line arguments
345 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
346 * @param cfg configuration
349 run (void *cls, char *const *args, const char *cfgfile,
350 const struct GNUNET_CONFIGURATION_Handle *mycfg)
352 struct GNUNET_CONFIGURATION_Handle *properties;
355 end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon starting ... \n");
360 if (GNUNET_SYSERR ==GNUNET_CONFIGURATION_get_value_filename (mycfg, "sysmon", "CFGFILE", &file))
362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sysmon configuration file not set, exit! \n");
368 properties = GNUNET_CONFIGURATION_create();
369 if (NULL == properties)
376 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (properties, file))
379 GNUNET_CONFIGURATION_destroy (properties);
386 GNUNET_CONFIGURATION_iterate_sections (properties, &load_property, properties);
388 GNUNET_CONFIGURATION_destroy (properties);
390 /* Creating statistics */
391 stats = GNUNET_STATISTICS_create ("sysmon", mycfg);
400 /* load properties */
401 if (GNUNET_SYSERR == load_default_properties ())
410 if (GNUNET_SYSERR == run_properties ())
424 * @param argc number of arguments from the command line
425 * @param argv command line arguments
426 * @return 0 ok, 1 on error
429 main (int argc, char *const *argv)
431 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
432 GNUNET_GETOPT_OPTION_END
434 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
438 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-sysmon",
439 gettext_noop ("GNUnet system monitoring and information daemon"), options, &run,
441 GNUNET_free ((void*) argv);
445 /* end of gnunet-daemon-sysmon.c */