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-service-sysmon.c
23 * @brief system monitoring service, can use libgtop to retrieve system information
24 * in a plattform independent way
25 * @author Matthias Wachs
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
32 #include <glibtop/proclist.h>
33 #include <glibtop/procstate.h>
34 #include <glibtop/procargs.h>
35 #include <glibtop/procmem.h>
36 #include <glibtop/proctime.h>
54 #define V_NUMERIC_STR "numeric"
55 #define V_STRING_STR "string"
64 * A system property to monitor
69 * Next element in in the DLL
71 struct SysmonProperty *next;
74 * Previous element in in the DLL
76 struct SysmonProperty *prev;
78 struct SysmonGtopProcProperty *gtop_proc_head;
79 struct SysmonGtopProcProperty *gtop_proc_tail;
82 * Description used for statistics valuesd
99 struct GNUNET_TIME_Relative interval;
112 * Command execution handle
114 void * cmd_exec_handle;
129 GNUNET_SCHEDULER_TaskIdentifier task_id;
134 GNUNET_SCHEDULER_Task task;
143 * A system property to monitor
145 struct SysmonGtopProcProperty
147 struct SysmonGtopProcProperty *prev;
148 struct SysmonGtopProcProperty *next;
159 * Configuration handle
161 const struct GNUNET_CONFIGURATION_Handle *cfg;
167 struct GNUNET_STATISTICS_Handle *stats;
172 GNUNET_SCHEDULER_TaskIdentifier end_task;
174 struct SysmonProperty *sp_head;
175 struct SysmonProperty *sp_tail;
177 struct SysmonGtopProcProperty *pp_head;
178 struct SysmonGtopProcProperty *pp_tail;
181 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
183 struct SysmonProperty *sp;
184 struct SysmonProperty *next;
185 struct SysmonGtopProcProperty *gt_cur;
186 struct SysmonGtopProcProperty *gt_next;
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysdaemon stopping ... \n");
189 end_task = GNUNET_SCHEDULER_NO_TASK;
193 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
198 while (NULL != (sp = next))
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping `%s' \n", sp->desc);
202 GNUNET_CONTAINER_DLL_remove (sp_head, sp_tail, sp);
203 if (GNUNET_SCHEDULER_NO_TASK != sp->task_id)
205 GNUNET_SCHEDULER_cancel (sp->task_id);
206 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
208 GNUNET_free_non_null (sp->cmd);
209 GNUNET_free_non_null (sp->cmd_args);
210 GNUNET_free (sp->desc);
215 while (NULL != (gt_cur = gt_next))
217 gt_next = gt_cur->next;
218 GNUNET_CONTAINER_DLL_remove (pp_head, pp_tail, gt_cur);
219 GNUNET_free (gt_cur->binary);
220 GNUNET_free (gt_cur);
231 if (GNUNET_SCHEDULER_NO_TASK != end_task)
232 GNUNET_SCHEDULER_cancel (end_task);
233 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
237 to_lower_str (char * str)
240 for (c = 0; c <= strlen (str); c++)
241 str[c] = tolower(str[c]);
245 put_property (struct SysmonProperty *sp)
247 if (v_numeric ==sp->value_type)
249 fprintf (stderr, "%s : %s : %llu\n",
250 GNUNET_STRINGS_absolute_time_to_string(GNUNET_TIME_absolute_get()),
251 sp->desc, (unsigned long long) sp->num_val);
253 else if (v_string ==sp->value_type)
255 fprintf (stderr, "%s : %s : %s\n",
256 GNUNET_STRINGS_absolute_time_to_string(GNUNET_TIME_absolute_get()),
257 sp->desc, sp->str_val);
262 return GNUNET_SYSERR;
268 update_uptime (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
270 struct SysmonProperty *sp = cls;
271 static int first_run = GNUNET_YES;
273 if (GNUNET_YES == first_run)
274 first_run = GNUNET_NO;
276 sp->num_val += sp->interval.rel_value / 1000;
282 exec_cmd_proc (void *cls, const char *line)
284 struct SysmonProperty *sp = cls;
285 unsigned long long tmp;
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property output: `%s'\n", line);
289 GNUNET_OS_command_stop (sp->cmd_exec_handle);
290 sp->cmd_exec_handle = NULL;
294 switch (sp->value_type) {
296 if (1 != sscanf (line, "%llu", &tmp))
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Command output was not a numerical value: `%s'\n", line);
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "NOT IMPLEMENTED\n");
311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property output: `%s'\n", line);
318 exec_cmd (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
320 struct SysmonProperty *sp = cls;
321 GNUNET_assert (NULL != sp->cmd);
323 if (NULL != sp->cmd_exec_handle)
325 GNUNET_OS_command_stop (sp->cmd_exec_handle);
326 sp->cmd_exec_handle = NULL;
329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property `%s': command `%s' `%s'\n", sp->desc, sp->cmd, sp->cmd_args);
330 if (NULL == (sp->cmd_exec_handle = GNUNET_OS_command_run (&exec_cmd_proc, sp,
331 GNUNET_TIME_UNIT_SECONDS,
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Property `%s': command `%s' failed\n", sp->desc, sp->cmd);
340 exec_gtop_proc_mon (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 struct SysmonGtopProcProperty *sp = cls;
343 glibtop_proclist proc_list;
344 glibtop_proc_args proc_args;
345 glibtop_proc_mem proc_mem;
346 glibtop_proc_time proc_time;
351 /* get process list */
352 pids = glibtop_get_proclist(&proc_list, GLIBTOP_KERN_PROC_ALL, 0);
355 fprintf (stderr, "Could not retrieve process list!\n");
360 printf("Found %lu processes\n", (unsigned long) proc_list.number);
361 for (i = 0; i < proc_list.number; ++i)
363 //printf("PID %u:\n", pids[i]);
365 /* get process args */
366 argss = glibtop_get_proc_args (&proc_args, pids[i], 1024);
369 fprintf (stderr, "Could not retrieve process args!\n");
373 //printf ("\targument string: %s\n", argss);
374 if (NULL != strstr (argss, sp->binary))
376 /* get memory info */
377 glibtop_get_proc_mem (&proc_mem, pids[i]);
378 printf ("\tMemory information:\n");
379 printf ("\t%-50s: %llu\n", "total # of pages of memory", (long long unsigned int) proc_mem.size);
380 printf ("\t%-50s: %llu\n", "number of pages of virtual memory", (long long unsigned int) proc_mem.vsize);
381 printf ("\t%-50s: %llu\n", "number of resident set", (long long unsigned int) proc_mem.resident);
382 printf ("\t%-50s: %llu\n", "number of pages of shared (mmap'd) memory", (long long unsigned int) proc_mem.share);
383 printf ("\t%-50s: %llu\n", "resident set size", (long long unsigned int) proc_mem.rss);
386 glibtop_get_proc_time (&proc_time, pids[i]);
387 printf ("\tTime information:\n");
388 printf ("\t%-50s: %llu\n", "real time accumulated by process", (long long unsigned int) proc_time.rtime);
389 printf ("\t%-50s: %llu\n", "user-mode CPU time accumulated by process", (long long unsigned int) proc_time.utime);
390 printf ("\t%-50s: %llu\n", "kernel-mode CPU time accumulated by process", (long long unsigned int) proc_time.stime);
401 load_property (void *cls,
404 struct GNUNET_CONFIGURATION_Handle *properties = cls;
405 struct SysmonProperty *sp;
408 if (NULL == strstr (section, "sysmon-"))
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loading section `%s'\n", section);
413 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section, "TYPE"))
415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
419 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"VALUE"))
421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
425 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"DESCRIPTION"))
427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
428 "DESCRIPTION", section);
431 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"CMD"))
433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing value %s in section `%s'\n",
437 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
440 GNUNET_CONFIGURATION_get_value_string (properties, section, "DESCRIPTION", &sp->desc);
443 GNUNET_CONFIGURATION_get_value_string (properties, section, "CMD", &tmp);
445 if (NULL != strchr (tmp, ' '))
447 args = strchr (tmp, ' ');
448 if (strlen (args) > 1)
455 sp->cmd = GNUNET_strdup (tmp);
456 sp->cmd_args = GNUNET_strdup (args);
458 sp->task = &exec_cmd;
461 GNUNET_CONFIGURATION_get_value_string (properties, section, "TYPE", &tmp);
463 if (0 == strcasecmp(tmp, "static"))
465 else if (0 == strcasecmp(tmp, "continous"))
466 sp->type = t_continous;
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
470 tmp, "TYPE", section);
478 GNUNET_CONFIGURATION_get_value_string (properties, section, "VALUE", &tmp);
480 if (0 == strcasecmp(tmp, V_NUMERIC_STR))
481 sp->value_type = v_numeric;
482 else if (0 == strcasecmp(tmp, V_STRING_STR))
483 sp->value_type = v_string;
486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid value %s for %s in section `%s'\n",
487 tmp, "VALUE", section);
495 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (properties, section,"INTERVAL"))
496 sp->interval = GNUNET_TIME_UNIT_MINUTES;
499 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (properties, section, "INTERVAL", &sp->interval))
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 _("Could not parse execution interval for `%s', set to default 60 sec.\n"), section);
503 sp->interval = GNUNET_TIME_UNIT_MINUTES;
507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded property `%s': %s, %s, interval %llu\n",
508 (NULL != sp->desc) ? sp->desc: "<undefined>",
509 (t_continous == sp->type) ? "continious" : "static",
510 (v_numeric == sp->value_type) ? "numeric" : "string",
511 sp->interval.rel_value);
513 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
518 load_default_properties (void)
520 struct SysmonProperty *sp;
521 /* GNUnet version array */
524 /* GNUnet vcs revision */
525 unsigned int revision;
528 if (3 != sscanf (VERSION, "%u.%u.%u", &ver[0], &ver[1], &ver[2]))
533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse version string `%s'\n", VERSION);
539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Version string is undefined \n");
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Version: %u.%u.%u\n", ver[0], ver[1], ver[2]);
543 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
544 sp->desc = GNUNET_strdup ("GNUnet version");
546 sp->value_type = v_numeric;
547 sp->num_val = 100 * ver[0] + 10 * ver[1] + ver[2];
548 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
551 if (1 != sscanf (VCS_VERSION, "svn-%uM", &revision))
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not parse revision string `%s'\n", VCS_VERSION);
557 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "VCS revision string is undefined \n");
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Revision: %u\n", revision);
561 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
562 sp->desc = GNUNET_strdup ("GNUnet vcs revision");
564 sp->value_type = v_numeric;
565 sp->num_val = (uint64_t) revision;
566 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
569 /* GNUnet startup time */
570 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
571 sp->desc = GNUNET_strdup ("GNUnet startup time");
573 sp->value_type = v_numeric;
574 sp->num_val = (uint64_t) GNUNET_TIME_absolute_get().abs_value;
575 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
578 /* GNUnet sysmon daemon uptime in seconds */
579 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
580 sp->desc = GNUNET_strdup ("GNUnet uptime");
581 sp->type = t_continous;
582 sp->value_type = v_numeric;
583 sp->num_val = (uint64_t) 0;
584 sp->interval = GNUNET_TIME_UNIT_MINUTES;
585 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
586 sp->task = update_uptime;
588 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
594 load_gtop_properties (void)
599 struct SysmonGtopProcProperty *pp;
600 struct SysmonProperty *sp;
601 /* Load network monitoring tasks */
603 /* Load service memory monitoring tasks */
604 if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, "sysmon", "MONITOR_SERVICES"))
607 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "sysmon", "MONITOR_SERVICES", &services))
608 return GNUNET_SYSERR;
610 s = strtok (services, " ");
613 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, s, "BINARY", &binary))
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Monitoring service `%s' with binary `%s'\n", s, binary);
616 pp = GNUNET_malloc (sizeof (struct SysmonGtopProcProperty));
618 GNUNET_CONTAINER_DLL_insert (pp_head, pp_tail, pp);
620 /* GNUnet sysmon daemon uptime in seconds */
622 sp = GNUNET_malloc (sizeof (struct SysmonProperty));
623 GNUNET_asprintf(&sp->desc, "Process Monitoring for service %s", s);
624 sp->type = t_continous;
625 sp->value_type = v_numeric;
626 sp->num_val = (uint64_t) 0;
627 sp->interval = GNUNET_TIME_UNIT_MINUTES;
628 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
629 sp->task = exec_gtop_proc_mon;
631 GNUNET_CONTAINER_DLL_insert (sp_head, sp_tail, sp);
633 s = strtok (NULL, " ");
635 GNUNET_free (services);
642 run_property (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
644 struct SysmonProperty *sp = cls;
645 sp->task_id = GNUNET_SCHEDULER_NO_TASK;
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running continous property `%s' \n", sp->desc);
647 sp->task (sp->task_cls, tc);
648 sp->task_id = GNUNET_SCHEDULER_add_delayed (sp->interval, &run_property, sp);
653 run_properties (void)
655 struct SysmonProperty *sp;
657 for (sp = sp_head; NULL != sp; sp = sp->next)
659 if (t_static == sp->type)
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running static property `%s' \n", sp->desc);
666 if (NULL == sp->task)
671 sp->task_id = GNUNET_SCHEDULER_add_now (&run_property, sp);
679 * Process template requests.
682 * @param server the initialized server
683 * @param mycfg configuration to use
686 run (void *cls, struct GNUNET_SERVER_Handle *server,
687 const struct GNUNET_CONFIGURATION_Handle *mycfg)
689 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
690 /* FIXME: add handlers here! */
693 /* FIXME: do setup here */
694 GNUNET_SERVER_add_handlers (server, handlers);
696 struct GNUNET_CONFIGURATION_Handle *properties;
699 end_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sysmon starting ... \n");
704 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_filename (mycfg, "sysmon", "CFGFILE", &file))
706 properties = GNUNET_CONFIGURATION_create();
707 if (NULL == properties)
714 if ((GNUNET_YES == GNUNET_DISK_file_test(file)) &&
715 (GNUNET_OK == GNUNET_CONFIGURATION_load (properties, file)))
716 GNUNET_CONFIGURATION_iterate_sections (properties, &load_property, properties);
718 GNUNET_CONFIGURATION_destroy (properties);
721 /* Creating statistics */
722 stats = GNUNET_STATISTICS_create ("sysmon", mycfg);
732 /* load properties */
733 if (GNUNET_SYSERR == load_default_properties ())
742 if (NULL != glibtop_init())
743 if ( GNUNET_SYSERR == load_gtop_properties ())
745 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to load gtop properties \n");
750 if (GNUNET_SYSERR == run_properties ())
761 * The main function for the sysmon service.
763 * @param argc number of arguments from the command line
764 * @param argv command line arguments
765 * @return 0 ok, 1 on error
768 main (int argc, char *const *argv)
771 GNUNET_SERVICE_run (argc, argv, "sysmon",
772 GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
775 /* end of gnunet-service-sysmon.c */