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;
54 void * cmd_exec_handle;
59 GNUNET_SCHEDULER_TaskIdentifier task_id;
60 GNUNET_SCHEDULER_Task task;
70 * Configuration handle
72 const struct GNUNET_CONFIGURATION_Handle *cfg;
78 struct GNUNET_STATISTICS_Handle *stats;
84 GNUNET_SCHEDULER_TaskIdentifier end_task;
86 struct SysmonProperty *sp_head;
87 struct SysmonProperty *sp_tail;
90 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 struct SysmonProperty *sp;
93 struct SysmonProperty *next;
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon stopping ... \n");
97 end_task = GNUNET_SCHEDULER_NO_TASK;
101 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
106 while (NULL != (sp = next))
108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping `%s' \n", sp->desc);
109 GNUNET_CONTAINER_DLL_remove (sp_head, sp_tail, sp);
111 if (GNUNET_SCHEDULER_NO_TASK != sp->task_id)
113 GNUNET_SCHEDULER_cancel (sp->task_id);
114 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
116 GNUNET_free_non_null (sp->cmd);
117 GNUNET_free_non_null (sp->cmd_args);
118 GNUNET_free (sp->desc);
127 if (GNUNET_SCHEDULER_NO_TASK != end_task)
128 GNUNET_SCHEDULER_cancel (end_task);
129 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
133 to_lower_str (char * str)
136 for (c = 0; c <= strlen (str); c++)
137 str[c] = tolower(str[c]);
141 put_property (struct SysmonProperty *sp)
143 if (v_numeric ==sp->value_type)
145 GNUNET_STATISTICS_set (stats, sp->desc, sp->num_val, GNUNET_NO);
147 else if (v_string ==sp->value_type)
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NOT IMPLEMENTED\n");
154 return GNUNET_SYSERR;
160 update_uptime (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 struct SysmonProperty *sp = cls;
168 exec_cmd_proc (void *cls, const char *line)
170 struct SysmonProperty *sp = cls;
173 GNUNET_OS_command_stop (sp->cmd_exec_handle);
174 sp->cmd_exec_handle = NULL;
180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property output: `%s'\n", line);
184 exec_cmd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
186 struct SysmonProperty *sp = cls;
187 GNUNET_assert (NULL != sp->cmd);
189 if (NULL != sp->cmd_exec_handle)
191 GNUNET_OS_command_stop (sp->cmd_exec_handle);
192 sp->cmd_exec_handle = NULL;
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Trying to exec : `%s'\n", sp->cmd);
197 if (NULL == (sp->cmd_exec_handle = GNUNET_OS_command_run (&exec_cmd_proc, sp,
198 GNUNET_TIME_UNIT_SECONDS,
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property `%s': command `%s' failed\n", sp->desc, sp->cmd);
204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property `%s': command `%s' DONE\n", sp->desc, sp->cmd);
208 load_property (void *cls,
211 struct GNUNET_CONFIGURATION_Handle *properties = cls;
212 struct SysmonProperty *sp;
215 if (NULL == strstr (section, "sysmon-"))
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading section `%s'\n", section);
220 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section, "TYPE"))
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
226 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"VALUE"))
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
232 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"DESCRIPTION"))
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
235 "DESCRIPTION", section);
238 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"CMD"))
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
244 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
247 GNUNET_CONFIGURATION_get_value_string (properties, section, "DESCRIPTION", &sp->desc);
250 GNUNET_CONFIGURATION_get_value_string (properties, section, "CMD", &tmp);
252 if (NULL != strchr (tmp, ' '))
254 args = strchr (tmp, ' ');
255 if (strlen (args) > 1)
261 sp->cmd = GNUNET_strdup (tmp);
262 sp->cmd_args = GNUNET_strdup (args);
264 sp->task = &exec_cmd;
267 GNUNET_CONFIGURATION_get_value_string (properties, section, "TYPE", &tmp);
269 if (0 == strcasecmp(tmp, "static"))
271 else if (0 == strcasecmp(tmp, "continous"))
272 sp->type = t_continous;
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
276 tmp, "TYPE", section);
284 GNUNET_CONFIGURATION_get_value_string (properties, section, "VALUE", &tmp);
286 if (0 == strcasecmp(tmp, "numeric"))
287 sp->value_type = v_numeric;
288 else if (0 == strcasecmp(tmp, "string"))
289 sp->value_type = v_string;
292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
293 tmp, "VALUE", section);
301 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"INTERVAL"))
302 sp->interval = GNUNET_TIME_UNIT_MINUTES;
305 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (properties, section, "INTERVAL", &sp->interval))
307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308 _("Could not parse execution interval for `%s', set to default 60 sec.\n"), section);
309 sp->interval = GNUNET_TIME_UNIT_MINUTES;
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded property `%s': %s, %s, interval %llu\n",
314 (NULL != sp->desc) ? sp->desc: "<undefined>",
315 (t_continous == sp->type) ? "continious" : "static",
316 (v_numeric == sp->value_type) ? "numeric" : "string",
317 sp->interval.rel_value);
319 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
324 load_default_properties (void)
326 struct SysmonProperty *sp;
327 /* GNUnet version array */
330 /* GNUnet vcs revision */
331 unsigned int revision;
335 if (3 != sscanf (VERSION, "%u.%u.%u", &ver[0], &ver[1], &ver[2]))
340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse version string `%s'\n", VERSION);
346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Version string is undefined \n");
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Version: %u.%u.%u\n", ver[0], ver[1], ver[2]);
350 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
351 sp->desc = GNUNET_strdup ("GNUnet version");
353 sp->value_type = v_numeric;
354 sp->num_val = 100 * ver[0] + 10 * ver[1] + ver[2];
355 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
359 if (1 != sscanf (VCS_VERSION, "svn-%uM", &revision))
362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse revision string `%s'\n", VCS_VERSION);
365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "VCS revision string is undefined \n");
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Revision: %u\n", revision);
370 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
371 sp->desc = GNUNET_strdup ("GNUnet vcs revision");
373 sp->value_type = v_numeric;
374 sp->num_val = (uint64_t) revision;
375 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
378 /* GNUnet startup time */
379 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
380 sp->desc = GNUNET_strdup ("GNUnet startup time");
382 sp->value_type = v_numeric;
383 sp->num_val = (uint64_t) GNUNET_TIME_absolute_get().abs_value;
384 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
387 /* GNUnet sysmon daemon uptime */
388 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
389 sp->desc = GNUNET_strdup ("GNUnet uptime");
390 sp->type = t_continous;
391 sp->value_type = v_numeric;
392 sp->num_val = (uint64_t) 0;
393 sp->interval = GNUNET_TIME_UNIT_SECONDS;
394 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
395 sp->task = update_uptime;
396 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
403 run_property (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
405 struct SysmonProperty *sp = cls;
406 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running continous property `%s' \n", sp->desc);
409 sp->task_id = GNUNET_SCHEDULER_add_delayed (sp->interval, &run_property, sp);
414 run_properties (void)
416 struct SysmonProperty *sp;
418 for (sp = sp_head; NULL != sp; sp = sp->next)
420 if (t_static == sp->type)
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running static property `%s' \n", sp->desc);
427 if (NULL == sp->task)
432 sp->task_id = GNUNET_SCHEDULER_add_now (&run_property, sp);
439 * Main function that will be run by the scheduler.
442 * @param args remaining command-line arguments
443 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
444 * @param cfg configuration
447 run (void *cls, char *const *args, const char *cfgfile,
448 const struct GNUNET_CONFIGURATION_Handle *mycfg)
450 struct GNUNET_CONFIGURATION_Handle *properties;
453 end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon starting ... \n");
458 if (GNUNET_SYSERR ==GNUNET_CONFIGURATION_get_value_filename (mycfg, "sysmon", "CFGFILE", &file))
460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Sysmon configuration file not set, exit! \n");
466 properties = GNUNET_CONFIGURATION_create();
467 if (NULL == properties)
474 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (properties, file))
477 GNUNET_CONFIGURATION_destroy (properties);
484 GNUNET_CONFIGURATION_iterate_sections (properties, &load_property, properties);
486 GNUNET_CONFIGURATION_destroy (properties);
488 /* Creating statistics */
489 stats = GNUNET_STATISTICS_create ("sysmon", mycfg);
498 /* load properties */
499 if (GNUNET_SYSERR == load_default_properties ())
508 if (GNUNET_SYSERR == run_properties ())
522 * @param argc number of arguments from the command line
523 * @param argv command line arguments
524 * @return 0 ok, 1 on error
527 main (int argc, char *const *argv)
529 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
530 GNUNET_GETOPT_OPTION_END
532 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
536 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-sysmon",
537 gettext_noop ("GNUnet system monitoring and information daemon"), options, &run,
539 GNUNET_free ((void*) argv);
543 /* end of gnunet-daemon-sysmon.c */