diff --git a/yasnd.c b/yasnd.c index d5a342c..d6c8a65 100644 --- a/yasnd.c +++ b/yasnd.c @@ -12,11 +12,9 @@ #include // #include -// Target hosts count -#define NUM_HOSTS 3 - -// Should we do excessive debug logging or not -#define DEBUG +// Debug verbosity +#define DEBUG_BASE 1 +#define DEBUG_ALL 2 typedef struct { char* hostname; // address of host @@ -24,13 +22,19 @@ typedef struct { int fail_count; // how many times in a row host was unreachable } host_decl; -host_decl* hosts=NULL; +host_decl* hosts=NULL; // structure with hosts' definitions +int hosts_count=0; // count of hosts +int debug_flag=0; + +void log_debug(const char *message,int verbosity) +{ + if (debug_flag>=verbosity) + syslog(LOG_INFO,"%s",message); +} void log_event(const char *message) { - #ifdef DEBUG - syslog(LOG_INFO,"%s",message); - #endif + syslog(LOG_INFO,"%s",message); } void* allocate_memory(int bytes) @@ -48,6 +52,7 @@ void init() // Initialize config parsing library cfg_opt_t opts[] = { CFG_STR_LIST("hosts", "{}", CFGF_NONE), + CFG_SIMPLE_INT("debug", &debug_flag), CFG_END() }; cfg_t *cfg; @@ -55,13 +60,14 @@ void init() // check if config file is valid and parse it if (cfg_parse(cfg, "/etc/yasnd.conf") == CFG_PARSE_ERROR) { - log_event("error parsing config file"); + log_event("Error parsing config file"); exit(EXIT_FAILURE); } // allocate memory for hosts array... - hosts=allocate_memory(cfg_size(cfg,"hosts")*sizeof(host_decl)); + hosts_count=cfg_size(cfg,"hosts"); + hosts=allocate_memory(hosts_count*sizeof(host_decl)); // ...and fill it with config file content - for (int i=0;ihostname); - log_event(tmp); + log_debug(tmp,DEBUG_BASE); execl("/bin/ping","/bin/ping","-c 4","-n",host->hostname,(char*) 0); // STUB: check result of exec call exit(EXIT_SUCCESS); @@ -90,21 +96,21 @@ void check_host(int num,host_decl* host) void loop_function() { - for (int i=0;i 0) { if (WIFEXITED(pid_status)) { char tmp[50]; sprintf(tmp, "Finished child %d with result %d", result, WEXITSTATUS(pid_status)); - log_event(tmp); + log_debug(tmp,DEBUG_ALL); // if host is alive - reset failure counter // otherwise - increment it if (WEXITSTATUS(pid_status) == 0) @@ -118,6 +124,7 @@ void loop_function() void termination_handler(int signum) { + log_event("SIGTERM captured, daemon stopping"); closelog(); // free hosts structures memory if (hosts!=NULL)