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_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping `%s' \n", sp->desc);
105 GNUNET_CONTAINER_DLL_remove (sp_head, sp_tail, sp);
107 if (GNUNET_SCHEDULER_NO_TASK != sp->task_id)
109 GNUNET_SCHEDULER_cancel (sp->task_id);
110 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
112 GNUNET_free (sp->desc);
121 if (GNUNET_SCHEDULER_NO_TASK != end_task)
122 GNUNET_SCHEDULER_cancel (end_task);
123 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
127 to_lower_str (char * str)
130 for (c = 0; c <= strlen (str); c++)
131 str[c] = tolower(str[c]);
135 put_property (struct SysmonProperty *sp)
137 if (v_numeric ==sp->value_type)
139 GNUNET_STATISTICS_set (stats, sp->desc, sp->num_val, GNUNET_NO);
141 else if (v_string ==sp->value_type)
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NOT IMPLEMENTED\n");
148 return GNUNET_SYSERR;
154 load_property (void *cls,
157 struct GNUNET_CONFIGURATION_Handle *properties = cls;
158 struct SysmonProperty *sp;
161 if (NULL == strstr (section, "sysmon-"))
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading section `%s'\n", section);
166 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section, "TYPE"))
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
172 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"VALUE"))
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
178 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"DESCRIPTION"))
180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
181 "DESCRIPTION", section);
184 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
187 GNUNET_CONFIGURATION_get_value_string (properties, section, "DESCRIPTION", &sp->desc);
190 GNUNET_CONFIGURATION_get_value_string (properties, section, "TYPE", &tmp);
192 if (0 == strcasecmp(tmp, "static"))
194 else if (0 == strcasecmp(tmp, "continous"))
195 sp->type = t_continous;
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
199 tmp, "TYPE", section);
207 GNUNET_CONFIGURATION_get_value_string (properties, section, "VALUE", &tmp);
209 if (0 == strcasecmp(tmp, "numeric"))
210 sp->value_type = v_numeric;
211 else if (0 == strcasecmp(tmp, "string"))
212 sp->value_type = v_string;
215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
216 tmp, "VALUE", section);
224 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"INTERVAL"))
225 sp->interval = GNUNET_TIME_UNIT_MINUTES;
228 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (properties, section, "INTERVAL", &sp->interval))
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 _("Could not parse execution interval for `%s', set to default 60 sec.\n"), section);
232 sp->interval = GNUNET_TIME_UNIT_MINUTES;
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded property `%s': type %u, value %u, interval %llu\n",
237 (NULL != sp->desc) ? sp->desc: "<undefined>",
238 sp->type, sp->value_type, sp->interval.rel_value);
240 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
245 update_uptime (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
247 struct SysmonProperty *sp = cls;
253 load_default_properties (void)
255 struct SysmonProperty *sp;
256 /* GNUnet version array */
259 /* GNUnet vcs revision */
260 unsigned int revision;
264 if (3 != sscanf (VERSION, "%u.%u.%u", &ver[0], &ver[1], &ver[2]))
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse version string `%s'\n", VERSION);
275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Version string is undefined \n");
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Version: %u.%u.%u\n", ver[0], ver[1], ver[2]);
279 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
280 sp->desc = GNUNET_strdup ("GNUnet version");
282 sp->value_type = v_numeric;
283 sp->num_val = 100 * ver[0] + 10 * ver[1] + ver[2];
284 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
288 if (1 != sscanf (VCS_VERSION, "svn-%uM", &revision))
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse revision string `%s'\n", VCS_VERSION);
294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "VCS revision string is undefined \n");
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Revision: %u\n", revision);
299 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
300 sp->desc = GNUNET_strdup ("GNUnet vcs revision");
302 sp->value_type = v_numeric;
303 sp->num_val = (uint64_t) revision;
304 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
307 /* GNUnet startup time */
308 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
309 sp->desc = GNUNET_strdup ("GNUnet startup time");
311 sp->value_type = v_numeric;
312 sp->num_val = (uint64_t) GNUNET_TIME_absolute_get().abs_value;
313 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
316 /* GNUnet sysmon daemon uptime */
317 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
318 sp->desc = GNUNET_strdup ("GNUnet uptime");
319 sp->type = t_continous;
320 sp->value_type = v_numeric;
321 sp->num_val = (uint64_t) 0;
322 sp->interval = GNUNET_TIME_UNIT_SECONDS;
323 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
324 sp->task = update_uptime;
325 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
332 run_property (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
334 struct SysmonProperty *sp = cls;
335 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
337 sp->task_id = GNUNET_SCHEDULER_add_delayed (sp->interval, &run_property, sp);
342 run_properties (void)
344 struct SysmonProperty *sp;
346 for (sp = sp_head; NULL != sp; sp = sp->next)
348 if (t_static == sp->type)
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running static property `%s' \n", sp->desc);
355 if (NULL == sp->task)
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running continous property `%s' \n", sp->desc);
361 sp->task_id = GNUNET_SCHEDULER_add_now (&run_property, sp);
368 * Main function that will be run by the scheduler.
371 * @param args remaining command-line arguments
372 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
373 * @param cfg configuration
376 run (void *cls, char *const *args, const char *cfgfile,
377 const struct GNUNET_CONFIGURATION_Handle *mycfg)
379 struct GNUNET_CONFIGURATION_Handle *properties;
382 end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon starting ... \n");
387 if (GNUNET_SYSERR ==GNUNET_CONFIGURATION_get_value_filename (mycfg, "sysmon", "CFGFILE", &file))
389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sysmon configuration file not set, exit! \n");
395 properties = GNUNET_CONFIGURATION_create();
396 if (NULL == properties)
403 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (properties, file))
406 GNUNET_CONFIGURATION_destroy (properties);
413 GNUNET_CONFIGURATION_iterate_sections (properties, &load_property, properties);
415 GNUNET_CONFIGURATION_destroy (properties);
417 /* Creating statistics */
418 stats = GNUNET_STATISTICS_create ("sysmon", mycfg);
427 /* load properties */
428 if (GNUNET_SYSERR == load_default_properties ())
437 if (GNUNET_SYSERR == run_properties ())
451 * @param argc number of arguments from the command line
452 * @param argv command line arguments
453 * @return 0 ok, 1 on error
456 main (int argc, char *const *argv)
458 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
459 GNUNET_GETOPT_OPTION_END
461 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
465 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-sysmon",
466 gettext_noop ("GNUnet system monitoring and information daemon"), options, &run,
468 GNUNET_free ((void*) argv);
472 /* end of gnunet-daemon-sysmon.c */