-
- if (cfg->nelements) {
- hosts = malloc(sizeof(struct monitor_host *) * cfg->nelements);
- for (i = 0; i < cfg->nelements; i++) {
- if (cfg->dict[i]->nvalues < 4) {
- fprintf(stderr, "ERROR: Not enough fields in record %d of cfg file. Got %d.\n", n, cfg->dict[i]->nvalues+1);
- exit(EXIT_FAILURE);
- } else if (cfg->dict[i]->nvalues>5) {
- fprintf(stderr, "ERROR: Too many fields in record %d of cfg file. Got %d.\n", n, cfg->dict[i]->nvalues+1);
- exit(EXIT_FAILURE);
- }
-
- hosts[n] = malloc(sizeof(struct monitor_host));
- hosts[n]->name = strdup(cfg->dict[i]->name);
- hosts[n]->ping_interval = atoi (cfg->dict[i]->value[0]);
- hosts[n]->max_delay = atoi (cfg->dict[i]->value[1]);
- hosts[n]->upcmd = strdup(cfg->dict[i]->value[2]);
- hosts[n]->downcmd = strdup(cfg->dict[i]->value[3]);
-
- if (cfg->dict[i]->nvalues == 4) {
- hosts[n]->down = 0;
- hosts[n]->up = 1;
- } else if (strcmp(cfg->dict[i]->value[4], "up") == 0) {
- hosts[n]->down = 0;
- hosts[n]->up = 1;
- } else if (strcmp(cfg->dict[i]->value[4], "down") == 0) {
- hosts[n]->down = 1;
- hosts[n]->up = 0;
- } else if (strcmp(cfg->dict[i]->value[4], "auto") == 0) {
- hosts[n]->down = 1;
- hosts[n]->up = 1;
- } else if (strcmp(cfg->dict[i]->value[4], "none") == 0) {
- hosts[n]->down = 0;
- hosts[n]->up = 0;
- } else {
- fprintf(stderr, "ERROR: Illegal value %s in record %d for startup condition.\n", cfg->dict[i]->value[4], n);
+
+ int host_count = iniparser_getnsec(conf);
+ if (host_count < 1 ) {
+ fprintf(stderr, "ERROR: Unable to determine number of hosts in configuration file.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ struct host_entry * host_list_end = NULL;
+ for (int i=0; i < host_count; i++) {
+ /* Allocate a reusable buffer large enough to hold the full 'section:key' string. */
+ int section_len = strlen(iniparser_getsecname(conf, i));
+ char * key_buf = malloc(section_len + 1 + MAXCONFKEYLEN + 1);
+ strcpy(key_buf, iniparser_getsecname(conf, i));
+ key_buf[section_len++] = ':';
+
+ struct host_entry * cur_host = malloc(sizeof(struct host_entry));
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "host", MAXCONFKEYLEN);
+ cur_host->name = strdup(iniparser_getstring(conf, key_buf, NULL));
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "interval", MAXCONFKEYLEN);
+ cur_host->ping_interval = iniparser_getint(conf, key_buf, -1);
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "max_delay", MAXCONFKEYLEN);
+ cur_host->max_delay = iniparser_getint(conf, key_buf, -1);
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "up_cmd", MAXCONFKEYLEN);
+ cur_host->up_cmd = strdup(iniparser_getstring(conf, key_buf, NULL));
+
+ key_buf[section_len] = '\0';
+ strncat(key_buf, "down_cmd", MAXCONFKEYLEN);
+ cur_host->down_cmd = strdup(iniparser_getstring(conf, key_buf, NULL));
+
+ key_buf[section_len] = '\0';
+ /* TODO: Parse for up/down/auto in start_condition. */
+ /* TODO: Do a host up/down check if necessary. */
+ cur_host->host_up = true;
+
+ /* TODO: Do I want to make any checks for start_condition? */
+ if (cur_host->name == NULL || cur_host->ping_interval == -1 || cur_host->max_delay == -1) {
+ fprintf(stderr, "ERROR: Problems parsing section %s.\n", iniparser_getsecname(conf, i));