change host structure definition in config file

This commit is contained in:
Sergey Popov 2011-12-19 14:52:57 +04:00
parent d4f632689a
commit 240aa35786

22
yasnd.c
View File

@ -20,7 +20,7 @@ char* recipient_number=NULL; // recipient of sms alerts
bool lpt_enable=false; // control usage of LPT port to reset target devices bool lpt_enable=false; // control usage of LPT port to reset target devices
int lpt_port=0x378; // LPT port in hex(0x378 is usually LPT1) int lpt_port=0x378; // LPT port in hex(0x378 is usually LPT1)
bool loop_locked=false; // flag for locking main loop while config re-reading bool loop_locked=false; // flag for locking main loop while config re-reading
int failures_first=3; // count of failures while hosts checking, that triggers first SMS sending long failures_first=3; // count of failures while hosts checking, that triggers first SMS sending
pid_t mainloop_clone_pid; // pid of clone process, that containt main loop pid_t mainloop_clone_pid; // pid of clone process, that containt main loop
void log_debug(const char *message,int verbosity) void log_debug(const char *message,int verbosity)
@ -81,11 +81,22 @@ void lpt_lock()
outb (1, lpt_port); outb (1, lpt_port);
} }
// Function, that checks that defined host entry is valid
int cb_validate_host(cfg_t *cfg, cfg_opt_t *opt)
{
return 0; // success
}
void init() void init()
{ {
static cfg_opt_t host_opts[] = {
CFG_STR("hostname", 0, CFGF_NONE),
CFG_INT("lpt_pin", 0, CFGF_NONE),
CFG_END()
};
// Initialize config parsing library // Initialize config parsing library
cfg_opt_t opts[] = { cfg_opt_t opts[] = {
CFG_STR_LIST("hosts", "{}", CFGF_NONE), CFG_SEC("host", host_opts, CFGF_MULTI | CFGF_TITLE),
CFG_SIMPLE_INT("debug", &debug_flag), CFG_SIMPLE_INT("debug", &debug_flag),
CFG_SIMPLE_BOOL("lpt_enable", &lpt_enable), CFG_SIMPLE_BOOL("lpt_enable", &lpt_enable),
CFG_SIMPLE_INT("lpt_port", &lpt_port), CFG_SIMPLE_INT("lpt_port", &lpt_port),
@ -97,6 +108,8 @@ void init()
CFG_END() CFG_END()
}; };
cfg = cfg_init(opts, CFGF_NONE); cfg = cfg_init(opts, CFGF_NONE);
// set a validating callback function for host sections
cfg_set_validate_func(cfg, "host", &cb_validate_host);
// 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)
{ {
@ -128,7 +141,7 @@ void init()
} }
} }
// hosts' array must not be empty // hosts' array must not be empty
hosts_count=cfg_size(cfg,"hosts"); hosts_count=cfg_size(cfg,"host");
if (hosts_count==0) if (hosts_count==0)
{ {
log_event("Error: no hosts to check defined in config file"); log_event("Error: no hosts to check defined in config file");
@ -139,7 +152,8 @@ void init()
// ...and fill it with config file content // ...and fill it with config file content
for (int i=0;i<hosts_count;i++) for (int i=0;i<hosts_count;i++)
{ {
hosts[i].hostname=cfg_getnstr(cfg,"hosts",i); cfg_t* host = cfg_getnsec(cfg, "host", i);
hosts[i].hostname=cfg_getstr(host,"hostname");
hosts[i].fail_count=0; hosts[i].fail_count=0;
hosts[i].alert_sent=false; hosts[i].alert_sent=false;
} }