/* * SIPB service monitor: web-based status report * * Nickolai Zeldovich * April 2002 */ #include #include "sipbmon.h" static void web_status_close (int s, bool ok) { close (s); } static void web_send_response (struct sm_service *svc, int fd) { strbuf st; st << "HTTP/1.0 200 OK\n" << "Connection: close\n" << "Content-Type: text/html\n" << "\n" << "" << "SIPB service monitor: status\n" << "

SIPB service monitor: status

\n" << "\n" << "" << "\n"; while (svc) { st << "\n"; svc = svc->next; } st << "
HostPortServiceStatus
" << svc->host << "" << svc->port << "" << svc->name << "" << (svc->up ? "up" : "down: ") << (svc->up ? str ("") : svc->errmsg) << "
\n" << "\n"; net_write (fd, st.tosuio (), wrap (&web_status_close, fd)); } static void web_status_read (struct sm_service *svc, int fd) { char buf[1024]; fdcb (fd, selread, NULL); /* Don't actually care about request -- just placate the browser */ read (fd, buf, sizeof (buf)); web_send_response (svc, fd); } static void web_status_accept (struct sm_service *svc, 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); fdcb (s, selread, wrap (&web_status_read, svc, s)); } void web_status_init (int port, struct sm_service *svc_head) { 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 (&web_status_accept, svc_head, fd)); }