diff options
-rw-r--r-- | pingu.c | 74 |
1 files changed, 45 insertions, 29 deletions
@@ -1,4 +1,6 @@ +#include <sys/queue.h> + #include <err.h> #include <errno.h> #include <stdio.h> @@ -12,14 +14,17 @@ int pingu_verbose = 0; struct provider { + char *router; char *name; char *interface; - char *pinghost; char *up_action; char *down_action; int status; + SLIST_ENTRY(provider) provider_list; }; +SLIST_HEAD(provider_list, provider); + #if 0 int skip(char **str, int whitespace) { @@ -74,16 +79,15 @@ void parse_line(char *line, char **key, char **value) (*value) = p; } -struct provider **read_config(const char *file) +int read_config(const char *file, struct provider_list *head) { FILE *f = fopen(file, "r"); struct provider *p = NULL; - struct provider **list = NULL; int i = 0, lineno = 0; char line[256]; if (f == NULL) { log_perror(file); - return NULL; + return -1; } while (fgets(line, sizeof(line), f)) { char *key, *value; @@ -91,19 +95,19 @@ struct provider **read_config(const char *file) parse_line(line, &key, &value); if (key == NULL) continue; - printf("DEBUG: lineno=%i, key='%s', val='%s'\n", - lineno, key, value); +// printf("DEBUG: lineno=%i, key='%s', val='%s'\n", +// lineno, key, value); - if (strcmp(key, "interface") == 0) { - list = xrealloc(list, (i + 2) * sizeof(struct provider *)); + if (strcmp(key, "router") == 0) { p = xmalloc(sizeof(struct provider)); - p->name = xstrdup(value); - list[i] = p; - i++; + memset(p, 0, sizeof(struct provider)); + p->router = xstrdup(value); + p->status = 1; /* online by default */ + SLIST_INSERT_HEAD(head, p, provider_list); + } else if (p && strcmp(key, "interface") == 0) { + p->interface = xstrdup(value); } else if (p && strcmp(key, "provider") == 0) { p->name = xstrdup(value); - } else if (p && strcmp(key, "pinghost") == 0) { - p->pinghost = xstrdup(value); } else if (p && strcmp(key, "up-action") == 0) { p->up_action = xstrdup(value); } else if (p && strcmp(key, "down-action") == 0) { @@ -114,14 +118,13 @@ struct provider **read_config(const char *file) log_error("provider not specified"); } } - list[i] = NULL; - return list; + return 0; } static int ping(const char *host) { char cmd[280]; - snprintf(cmd, sizeof(cmd), "ping -c 1 -q %s", host); + snprintf(cmd, sizeof(cmd), "ping -c 1 -q %s >/dev/null 2>&1", host); return system(cmd); } @@ -131,16 +134,29 @@ void usage(int retcode) exit(retcode); } -void ping_loop(char *hosts[], int offline[], int count, int interval) +void dump_provider(struct provider *p) +{ + printf("router: %s\n" + "provider: %s\n" + "interface: %s\n" + "up-action: %s\n" + "down-action: %s\n" + "p->status: %i\n" + "\n", + p->router, p->name, p->interface, + p->up_action, p->down_action, p->status); +} + +void ping_loop(struct provider_list *head, int interval) { - int i; + struct provider *p; while (1) { - for (i = 0; i < count; i++) { - int status = (ping(hosts[i]) != 0); - if (status != offline[i]) { - offline[i] = status; + SLIST_FOREACH(p, head, provider_list) { + int status = (ping(p->router) != 0); + if (status != p->status) { + p->status = status; printf("status changed for %s to %i\n", - hosts[i], status); + p->router, status); } } sleep(interval); @@ -156,7 +172,7 @@ int main(int argc, char *argv[]) int hosts_count; int interval = 30; const char *script = NULL; - struct provider **providers; + struct provider_list providers; while ((c = getopt(argc, argv, "c:i:s:")) != -1) { switch (c) { @@ -175,17 +191,17 @@ int main(int argc, char *argv[]) argc -= optind; argv += optind; - providers = read_config(config); - if (providers == NULL) + SLIST_INIT(&providers); + if (read_config(config, &providers) == -1) return 1; - if (argc == 0) - usage(EXIT_FAILURE); +// if (argc == 0) +// usage(EXIT_FAILURE); offline = xmalloc(sizeof(int) * argc); memset(offline, 0, sizeof(int) * argc); - ping_loop(argv, offline, argc, interval); + ping_loop(&providers, interval); free(hosts); return 0; |