option for log verbosity level
This commit is contained in:
parent
c93b612597
commit
bc7c1edb41
43
yasnd.c
43
yasnd.c
@ -12,11 +12,9 @@
|
||||
#include <confuse.h>
|
||||
// #include <gammu/gammu.h>
|
||||
|
||||
// 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;i<cfg_size(cfg,"hosts");i++)
|
||||
for (int i=0;i<hosts_count;i++)
|
||||
{
|
||||
hosts[i].hostname=cfg_getnstr(cfg,"hosts",i);
|
||||
hosts[i].fail_count=0;
|
||||
@ -72,14 +78,14 @@ void check_host(int num,host_decl* host)
|
||||
{
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
log_event("can not fork child :(");
|
||||
log_event("Error: can not fork child :(");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// If pid == 0, then we are in the child
|
||||
if (pid == 0) {
|
||||
char tmp[50];
|
||||
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);
|
||||
// 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<NUM_HOSTS;i++)
|
||||
for (int i=0;i<hosts_count;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 result = waitpid(hosts[i].helper_pid, &pid_status, NULL);
|
||||
int result = waitpid(hosts[i].helper_pid, &pid_status, 0);
|
||||
if (result > 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)
|
||||
|
Loading…
Reference in New Issue
Block a user