/* * 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 (vec svec, 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"; for (unsigned int i = 0; i < svec.size (); i++) { service *svc = svec[i]; bool ok = svc->ok (); st << "\n"; } st << "
ServiceDescriptionStatus
" << svc->name () << "" << svc->descr () << "" << (ok ? "up" : "down: ") << (ok ? str ("") : svc->errmsg ()) << "
\n" << "\n"; net_write (fd, st.tosuio (), wrap (&web_status_close, fd)); } static void web_status_read (vec svec, 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 (svec, fd); } static void web_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); fdcb (s, selread, wrap (&web_status_read, svec, s)); } void web_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 (&web_status_accept, svec, fd)); }