/* * Service monitor: status report * * Nickolai Zeldovich * February 2002 */ #include #include "svcmon.h" static void status_close (int s, bool ok) { close (s); } static void status_accept (vec svec, int fd) { sockaddr_in sin; bzero (&sin, sizeof (sin)); socklen_t sinlen = sizeof (sin); int s = accept (fd, (sockaddr *) &sin, &sinlen); if (s < 0) return; make_async (s); strbuf st ("%-45s %s\n", " Service Description", "Status"); for (unsigned int i = 0; i < svec.size (); i++) { service *svc = svec[i]; bool ok = svc->ok (); st << strbuf ("%-45s %s%s\n", svc->descr ().cstr (), ok ? "up" : "down: ", ok ? "" : svc->errmsg ().cstr ()); } net_write (s, st, wrap (&status_close, s)); } void status_init (int port, vec svec) { int fd = inetsocket (SOCK_STREAM, port, INADDR_ANY); if (fd < 0) fatal ("could not bind TCP port %d: %m\n", port); close_on_exec (fd); listen (fd, 5); fdcb (fd, selread, wrap (&status_accept, svec, fd)); }