option for log verbosity level

This commit is contained in:
Sergey Popov 2011-11-25 17:35:33 +04:00
parent c93b612597
commit bc7c1edb41

41
yasnd.c
View File

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