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"
50 * A system property to monitor
55 * Next element in in the DLL
57 struct SysmonProperty *next;
60 * Previous element in in the DLL
62 struct SysmonProperty *prev;
65 * Description used for statistics valuesd
82 struct GNUNET_TIME_Relative interval;
95 * Command execution handle
97 void * cmd_exec_handle;
112 GNUNET_SCHEDULER_TaskIdentifier task_id;
117 GNUNET_SCHEDULER_Task task;
127 * Configuration handle
129 const struct GNUNET_CONFIGURATION_Handle *cfg;
135 struct GNUNET_STATISTICS_Handle *stats;
140 GNUNET_SCHEDULER_TaskIdentifier end_task;
142 struct SysmonProperty *sp_head;
143 struct SysmonProperty *sp_tail;
146 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
148 struct SysmonProperty *sp;
149 struct SysmonProperty *next;
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon stopping ... \n");
153 end_task = GNUNET_SCHEDULER_NO_TASK;
157 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
162 while (NULL != (sp = next))
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping `%s' \n", sp->desc);
165 GNUNET_CONTAINER_DLL_remove (sp_head, sp_tail, sp);
167 if (GNUNET_SCHEDULER_NO_TASK != sp->task_id)
169 GNUNET_SCHEDULER_cancel (sp->task_id);
170 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
172 GNUNET_free_non_null (sp->cmd);
173 GNUNET_free_non_null (sp->cmd_args);
174 GNUNET_free (sp->desc);
183 if (GNUNET_SCHEDULER_NO_TASK != end_task)
184 GNUNET_SCHEDULER_cancel (end_task);
185 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
189 to_lower_str (char * str)
192 for (c = 0; c <= strlen (str); c++)
193 str[c] = tolower(str[c]);
197 put_property (struct SysmonProperty *sp)
199 if (v_numeric ==sp->value_type)
201 GNUNET_STATISTICS_set (stats, sp->desc, sp->num_val, GNUNET_NO);
203 else if (v_string ==sp->value_type)
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NOT IMPLEMENTED\n");
210 return GNUNET_SYSERR;
216 update_uptime (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 struct SysmonProperty *sp = cls;
224 exec_cmd_proc (void *cls, const char *line)
226 struct SysmonProperty *sp = cls;
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property output: `%s'\n", line);
230 GNUNET_OS_command_stop (sp->cmd_exec_handle);
231 sp->cmd_exec_handle = NULL;
235 switch (sp->value_type) {
237 if (1 != sscanf (line, "%lu", &sp->num_val))
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Command output was not a numerical value: `%s'\n", line);
244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NOT IMPLEMENTED\n");
250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property output: `%s'\n", line);
257 exec_cmd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 struct SysmonProperty *sp = cls;
260 GNUNET_assert (NULL != sp->cmd);
262 if (NULL != sp->cmd_exec_handle)
264 GNUNET_OS_command_stop (sp->cmd_exec_handle);
265 sp->cmd_exec_handle = NULL;
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property `%s': command `%s' `%s'\n", sp->desc, sp->cmd, sp->cmd_args);
269 if (NULL == (sp->cmd_exec_handle = GNUNET_OS_command_run (&exec_cmd_proc, sp,
270 GNUNET_TIME_UNIT_SECONDS,
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property `%s': command `%s' failed\n", sp->desc, sp->cmd);
278 load_property (void *cls,
281 struct GNUNET_CONFIGURATION_Handle *properties = cls;
282 struct SysmonProperty *sp;
285 if (NULL == strstr (section, "sysmon-"))
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading section `%s'\n", section);
290 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section, "TYPE"))
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
296 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"VALUE"))
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
302 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"DESCRIPTION"))
304 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
305 "DESCRIPTION", section);
308 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"CMD"))
310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
314 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
317 GNUNET_CONFIGURATION_get_value_string (properties, section, "DESCRIPTION", &sp->desc);
320 GNUNET_CONFIGURATION_get_value_string (properties, section, "CMD", &tmp);
322 if (NULL != strchr (tmp, ' '))
324 args = strchr (tmp, ' ');
325 if (strlen (args) > 1)
331 sp->cmd = GNUNET_strdup (tmp);
332 sp->cmd_args = GNUNET_strdup (args);
334 sp->task = &exec_cmd;
337 GNUNET_CONFIGURATION_get_value_string (properties, section, "TYPE", &tmp);
339 if (0 == strcasecmp(tmp, "static"))
341 else if (0 == strcasecmp(tmp, "continous"))
342 sp->type = t_continous;
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
346 tmp, "TYPE", section);
354 GNUNET_CONFIGURATION_get_value_string (properties, section, "VALUE", &tmp);
356 if (0 == strcasecmp(tmp, "numeric"))
357 sp->value_type = v_numeric;
358 else if (0 == strcasecmp(tmp, "string"))
359 sp->value_type = v_string;
362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
363 tmp, "VALUE", section);
371 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"INTERVAL"))
372 sp->interval = GNUNET_TIME_UNIT_MINUTES;
375 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (properties, section, "INTERVAL", &sp->interval))
377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378 _("Could not parse execution interval for `%s', set to default 60 sec.\n"), section);
379 sp->interval = GNUNET_TIME_UNIT_MINUTES;
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded property `%s': %s, %s, interval %llu\n",
384 (NULL != sp->desc) ? sp->desc: "<undefined>",
385 (t_continous == sp->type) ? "continious" : "static",
386 (v_numeric == sp->value_type) ? "numeric" : "string",
387 sp->interval.rel_value);
389 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
394 load_default_properties (void)
396 struct SysmonProperty *sp;
397 /* GNUnet version array */
400 /* GNUnet vcs revision */
401 unsigned int revision;
405 if (3 != sscanf (VERSION, "%u.%u.%u", &ver[0], &ver[1], &ver[2]))
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse version string `%s'\n", VERSION);
416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Version string is undefined \n");
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Version: %u.%u.%u\n", ver[0], ver[1], ver[2]);
420 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
421 sp->desc = GNUNET_strdup ("GNUnet version");
423 sp->value_type = v_numeric;
424 sp->num_val = 100 * ver[0] + 10 * ver[1] + ver[2];
425 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
429 if (1 != sscanf (VCS_VERSION, "svn-%uM", &revision))
432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse revision string `%s'\n", VCS_VERSION);
435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "VCS revision string is undefined \n");
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Revision: %u\n", revision);
440 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
441 sp->desc = GNUNET_strdup ("GNUnet vcs revision");
443 sp->value_type = v_numeric;
444 sp->num_val = (uint64_t) revision;
445 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
448 /* GNUnet startup time */
449 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
450 sp->desc = GNUNET_strdup ("GNUnet startup time");
452 sp->value_type = v_numeric;
453 sp->num_val = (uint64_t) GNUNET_TIME_absolute_get().abs_value;
454 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
457 /* GNUnet sysmon daemon uptime */
458 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
459 sp->desc = GNUNET_strdup ("GNUnet uptime");
460 sp->type = t_continous;
461 sp->value_type = v_numeric;
462 sp->num_val = (uint64_t) 0;
463 sp->interval = GNUNET_TIME_UNIT_SECONDS;
464 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
465 sp->task = update_uptime;
466 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
473 run_property (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
475 struct SysmonProperty *sp = cls;
476 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running continous property `%s' \n", sp->desc);
479 sp->task_id = GNUNET_SCHEDULER_add_delayed (sp->interval, &run_property, sp);
484 run_properties (void)
486 struct SysmonProperty *sp;
488 for (sp = sp_head; NULL != sp; sp = sp->next)
490 if (t_static == sp->type)
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running static property `%s' \n", sp->desc);
497 if (NULL == sp->task)
502 sp->task_id = GNUNET_SCHEDULER_add_now (&run_property, sp);
509 * Main function that will be run by the scheduler.
512 * @param args remaining command-line arguments
513 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
514 * @param cfg configuration
517 run (void *cls, char *const *args, const char *cfgfile,
518 const struct GNUNET_CONFIGURATION_Handle *mycfg)
520 struct GNUNET_CONFIGURATION_Handle *properties;
523 end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon starting ... \n");
528 if (GNUNET_SYSERR ==GNUNET_CONFIGURATION_get_value_filename (mycfg, "sysmon", "CFGFILE", &file))
530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sysmon configuration file not set, exit! \n");
536 properties = GNUNET_CONFIGURATION_create();
537 if (NULL == properties)
544 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (properties, file))
547 GNUNET_CONFIGURATION_destroy (properties);
554 GNUNET_CONFIGURATION_iterate_sections (properties, &load_property, properties);
556 GNUNET_CONFIGURATION_destroy (properties);
558 /* Creating statistics */
559 stats = GNUNET_STATISTICS_create ("sysmon", mycfg);
568 /* load properties */
569 if (GNUNET_SYSERR == load_default_properties ())
578 if (GNUNET_SYSERR == run_properties ())
592 * @param argc number of arguments from the command line
593 * @param argv command line arguments
594 * @return 0 ok, 1 on error
597 main (int argc, char *const *argv)
599 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
600 GNUNET_GETOPT_OPTION_END
602 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
606 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-sysmon",
607 gettext_noop ("GNUnet system monitoring and information daemon"), options, &run,
609 GNUNET_free ((void*) argv);
613 /* end of gnunet-daemon-sysmon.c */