/* * Service monitor: notification module * * Nickolai Zeldovich * February 2002 */ #include #include "svcmon.h" static const char zwrite_script_path[] = "/var/ops/svcmon/zwrite.sh"; static str zephyr_buf (""); static timecb_t *zephyr_cb; static str notify_tostr (service *svc) { strbuf m; m << svc->descr () << " is "; if (svc->ok ()) m << "back up\n"; else m << "down: " << svc->errmsg () << "\n"; return m; } void notify_cons (service *svc) { warn << notify_tostr (svc); } static void notify_zephyr_close (int s, bool ok) { close (s); } static void notify_zephyr_flush () { const char *argv[2]; int fds[2]; str buf = zephyr_buf; zephyr_cb = NULL; zephyr_buf = ""; if (pipe (fds) < 0) { warn << "Failed to create pipe: " << strerror (errno) << "\n"; return; } argv[0] = zwrite_script_path; argv[1] = NULL; make_async (fds[1]); close_on_exec (fds[1]); pid_t pid = spawn (argv[0], argv, fds[0]); close (fds[0]); if (pid < 0) { warn << "Failure spawning zwrite: " << strerror (errno) << "\n"; close (fds[1]); return; } net_write (fds[1], strbuf (buf), wrap (¬ify_zephyr_close, fds[1])); } void notify_zephyr (service *svc) { zephyr_buf = zephyr_buf << notify_tostr (svc); if (zephyr_cb == NULL) zephyr_cb = delaycb (2, wrap (¬ify_zephyr_flush)); }