diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2011-10-12 15:30:26 +0200 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2011-10-12 15:30:26 +0200 |
commit | 934919ff6ad143eb585943799f6685a34bd403b0 (patch) | |
tree | c920d13567cb64fd5ae209ca4e5ee08d8513b0fd | |
parent | c2af879f64bac393df12e593b1bf16cd56517095 (diff) | |
download | pingu-934919ff6ad143eb585943799f6685a34bd403b0.tar.bz2 pingu-934919ff6ad143eb585943799f6685a34bd403b0.tar.xz |
pingu_adm: implement host-status and gateway-status
-rw-r--r-- | pingu.lua | 20 | ||||
-rw-r--r-- | pingu_adm.c | 32 | ||||
-rw-r--r-- | pingu_host.c | 4 | ||||
-rw-r--r-- | pingu_host.h | 2 | ||||
-rw-r--r-- | pingu_iface.c | 15 | ||||
-rw-r--r-- | pingu_iface.h | 2 |
6 files changed, 62 insertions, 13 deletions
@@ -2,20 +2,28 @@ module(..., package.seeall) -local function status(self) - self.handle:write("status\n") +local function run_command(self, cmd) + self.handle:write(cmd.."\n") self.handle:flush() local t = {} local line = self.handle:read("*line") while line ~= "" do - local host, status = string.match(line, "^(.*): (.*)$") - t[host] = status + local key, value = string.match(line, "^(.*): (.*)$") + t[key] = value line = self.handle:read("*line") end return t end +local function host_status(self) + return self:run_command("host-status") +end + +local function gateway_status(self) + return self:run_command("gateway-status") +end + local function close(self) return self.handle:close() end @@ -31,7 +39,9 @@ function connect(socket_path) end return { ["handle"] = fh, - ["status"] = status, + ["run_command"] = run_command, + ["host_status"] = host_status, + ["gateway_status"] = gateway_status, ["close"] = close } end diff --git a/pingu_adm.c b/pingu_adm.c index eb1e070..27f685f 100644 --- a/pingu_adm.c +++ b/pingu_adm.c @@ -13,6 +13,7 @@ #include "list.h" #include "log.h" #include "pingu_adm.h" +#include "pingu_iface.h" #include "pingu_host.h" static struct ev_io accept_io; @@ -34,11 +35,21 @@ static void pingu_adm_free_conn(struct ev_loop *loop, struct adm_conn *rm) free(rm); } +static struct { + const char *command; + void (*handler)(int fd, char *args); +} adm_handler[] = { + { "host-status", pingu_host_dump_status }, + { "gateway-status", pingu_iface_dump_status }, + { NULL, NULL } +}; + static void pingu_adm_recv_cb (struct ev_loop *loop, struct ev_io *w, int revents) { struct adm_conn *conn = container_of(w, struct adm_conn, io); - int len; + int len, i; + char *args; len = recv(conn->io.fd, conn->cmd, sizeof(conn->cmd) - conn->num_read, MSG_DONTWAIT); @@ -55,15 +66,24 @@ static void pingu_adm_recv_cb (struct ev_loop *loop, struct ev_io *w, conn->num_read--; conn->cmd[conn->num_read] = '\0'; - - if (strncmp(conn->cmd, "status", len) == 0) { + + args = strchr(conn->cmd, ' '); + if (args != NULL) + *args++ = '\0'; + + for (i = 0; adm_handler[i].command != NULL; i++) { + if (strncmp(conn->cmd, adm_handler[i].command, len) != 0) + continue; log_debug("Admin command: %s", conn->cmd); - pingu_host_dump_status(conn->io.fd); + adm_handler[i].handler(conn->io.fd, args); conn->cmd[0] = '\0'; conn->num_read = 0; - } else { - log_error("unknown adm command"); + break; } + + if (adm_handler[i].command == NULL) + log_error("%s: unknown admim command", conn->cmd); + return; err: diff --git a/pingu_host.c b/pingu_host.c index c153e3d..81719a6 100644 --- a/pingu_host.c +++ b/pingu_host.c @@ -109,11 +109,13 @@ struct pingu_host *pingu_host_new(char *hoststr, float burst_interval, return host; } -void pingu_host_dump_status(int fd) +void pingu_host_dump_status(int fd, char *filter) { struct pingu_host *host; char buf[512]; list_for_each_entry(host, &host_list, host_list_entry) { + if (filter != NULL && strcmp(filter, host->label) != 0) + continue; snprintf(buf, sizeof(buf), "%s: %i\n", host->label, host->status); write(fd, buf, strlen(buf)); } diff --git a/pingu_host.h b/pingu_host.h index a7a879d..731d64e 100644 --- a/pingu_host.h +++ b/pingu_host.h @@ -38,6 +38,6 @@ struct pingu_host *pingu_host_new(char *hoststr, float burst_interval, int pingu_host_set_status(struct pingu_host *host, int status); int pingu_host_init(struct ev_loop *loop); int pingu_host_verify_status(struct ev_loop *loop, struct pingu_host *host); -void pingu_host_dump_status(int fd); +void pingu_host_dump_status(int fd, char *filter); #endif diff --git a/pingu_iface.c b/pingu_iface.c index 820546d..f92bc5d 100644 --- a/pingu_iface.c +++ b/pingu_iface.c @@ -243,6 +243,21 @@ int pingu_iface_set_route_table(struct pingu_iface *iface, int table) return table; } +void pingu_iface_dump_status(int fd, char *filter) +{ + struct pingu_iface *iface; + char buf[512]; + list_for_each_entry(iface, &iface_list, iface_list_entry) { + if (filter != NULL && strcmp(filter, iface->label) != 0) + continue; + snprintf(buf, sizeof(buf), "%s: %i\n", + iface->label != NULL ? iface->label : iface->name, + pingu_iface_gw_is_online(iface)); + write(fd, buf, strlen(buf)); + } + write(fd, "\n", 1); +} + int pingu_iface_init(struct ev_loop *loop) { struct pingu_iface *iface; diff --git a/pingu_iface.h b/pingu_iface.h index e6c8b24..815f4e9 100644 --- a/pingu_iface.h +++ b/pingu_iface.h @@ -51,6 +51,8 @@ int pingu_iface_set_route_table(struct pingu_iface *iface, int table); int pingu_iface_gw_is_online(struct pingu_iface *iface); void pingu_iface_gw_action(struct pingu_iface *iface, struct pingu_route *gw, int action); + +void pingu_iface_dump_status(int fd, char *filter); void pingu_iface_update_routes(struct pingu_iface *iface, int action); void pingu_iface_cleanup(void); |