- 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 = malloc(sizeof(struct monitor_host *) * host_count);
+ for (int i=0; i < host_count; i++) {
+ /* Allocate a 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++] = ':';
+
+ hosts[i] = malloc(sizeof(struct monitor_host));
+ for (int k=0; k<6; k++) {
+ key_buf[section_len] = '\0'; /* Reuse the section name and colon on each pass through this loop. */
+ switch (k) {
+ case 0:
+ strncat(key_buf, "host", MAXCONFKEYLEN);
+ hosts[i]->name = strdup(iniparser_getstring(conf, key_buf, NULL));
+ break;
+ case 1:
+ strncat(key_buf, "interval", MAXCONFKEYLEN);
+ hosts[i]->ping_interval = iniparser_getint(conf, key_buf, -1);
+ break;
+ case 2:
+ strncat(key_buf, "max_delay", MAXCONFKEYLEN);
+ hosts[i]->max_delay = iniparser_getint(conf, key_buf, -1);
+ break;
+ case 3:
+ strncat(key_buf, "up_cmd", MAXCONFKEYLEN);
+ hosts[i]->upcmd = strdup(iniparser_getstring(conf, key_buf, NULL));
+ break;
+ case 4:
+ strncat(key_buf, "down_cmd", MAXCONFKEYLEN);
+ hosts[i]->downcmd = strdup(iniparser_getstring(conf, key_buf, NULL));
+ break;
+ case 5:
+ /* TODO: Parse for up/down/auto in start_condition. */
+ /* TODO: Do a host up/down check if necessary. */
+ hosts[i]->hostup = true;
+ break;