]> mj.ucw.cz Git - arexx.git/commitdiff
Logging of statistics, handling of SIGTERM, better scheduling of time syncs
authorMartin Mares <mj@ucw.cz>
Tue, 27 Dec 2011 18:22:56 +0000 (19:22 +0100)
committerMartin Mares <mj@ucw.cz>
Tue, 27 Dec 2011 18:22:56 +0000 (19:22 +0100)
arexxd.c

index 2e8511f34a955f5d37d41b00d324fa5bec2fd79b..68e08d3a65bdfa8db27b79ea619088db3343f7cc 100644 (file)
--- a/arexxd.c
+++ b/arexxd.c
@@ -14,8 +14,7 @@
 #include <time.h>
 #include <getopt.h>
 #include <syslog.h>
-#include <pwd.h>
-#include <grp.h>
+#include <signal.h>
 #include <sys/stat.h>
 #include <libusb-1.0/libusb.h>
 #include <rrd.h>
@@ -150,17 +149,19 @@ static void rrd_point(time_t t, int id, double val)
 
 #define TIME_OFFSET 946681200          // Timestamp of 2000-01-01 00:00:00
 
+static int data_point_counter;
+
 static void cooked_point(time_t t, int id, double val, char *unit, int q)
 {
-       struct tm tm;
-       localtime_r(&t, &tm);
-
-       char tbuf[64];
-       strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
-
-       if (debug_raw_data)
+       if (debug_raw_data) {
+               struct tm tm;
+               localtime_r(&t, &tm);
+               char tbuf[64];
+               strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &tm);
                printf("== %s id=%d val=%.3f unit=%s q=%d\n", tbuf, id, val, unit, q);
+       }
 
+       data_point_counter++;
        rrd_point(t, id, val);
 }
 
@@ -272,7 +273,7 @@ static int find_device(void)
                libusb_device *dev = devlist[i];
                if (!libusb_get_device_descriptor(dev, &desc)) {
                        if (desc.idVendor == 0x0451 && desc.idProduct == 0x3211) {
-                               log_info("Arexx USB receiver found at usb%d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
+                               log_info("Arexx data logger found at usb%d.%d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
                                int err;
                                if (err = libusb_open(dev, &devh)) {
                                        log_error("libusb_open() failed: error %d", err);
@@ -445,6 +446,13 @@ static void set_clock(void)
 
 /*** Main ***/
 
+static volatile sig_atomic_t want_shutdown;
+
+static void sigterm_handler(int sig __attribute__((unused)))
+{
+       want_shutdown = 1;
+}
+
 static const struct option long_options[] = {
        { "debug",              0, NULL, 'd' },
        { "log-packets",        0, NULL, 'p' },
@@ -499,40 +507,66 @@ int main(int argc, char **argv)
                use_syslog = 1;
        }
 
+       struct sigaction sa = { .sa_handler = sigterm_handler };
+       sigaction(SIGTERM, &sa, NULL);
+       sigaction(SIGINT, &sa, NULL);
+
+       sigset_t term_sigs;
+       sigemptyset(&term_sigs);
+       sigaddset(&term_sigs, SIGTERM);
+       sigaddset(&term_sigs, SIGINT);
+       sigprocmask(SIG_BLOCK, &term_sigs, NULL);
+
        int inited = 0;
-       for (;;) {
+       while (!want_shutdown) {
                if (!find_device()) {
                        if (!inited) {
                                inited = 1;
-                               log_error("Receiver not connected, waiting until it appears");
+                               log_error("Data logger not connected, waiting until it appears");
                        }
                        sleep(30);
                        continue;
                }
-               inited = 1;
-
-               int sync_in = 0;
-               for (;;) {
-                       if (!sync_in) {
-                               log_info("Synchronizing receiver time");
+               log_info("Listening");
+
+               time_t last_sync = 0;
+               time_t last_show = 0;
+               int want_stats = 0;
+               int want_sleep = 0;
+               data_point_counter = 0;
+               while (!want_shutdown) {
+                       time_t now = time(NULL);
+                       if (now > last_sync + 900) {
+                               log_info("Synchronizing data logger time");
                                set_clock();
-                               sync_in = 200;
+                               last_sync = now;
                        }
+                       if (want_stats && now > last_show + 300) {
+                               log_info("Stats: received %d data points", data_point_counter);
+                               data_point_counter = 0;
+                               last_show = now;
+                       }
+
                        byte req[64], reply[64];
                        memset(req, 0, sizeof(req));
                        req[0] = 3;
                        err = send_and_receive(req, reply);
                        if (err < 0)
                                break;
+                       want_sleep = 1;
                        if (err > 0 && parse_packet(reply))
-                               continue;
+                               want_sleep = 0;
+                       want_stats = 1;
+                       sigprocmask(SIG_UNBLOCK, &term_sigs, NULL);
                        sleep(4);
-                       sync_in--;
+                       sigprocmask(SIG_BLOCK, &term_sigs, NULL);
                }
 
-               log_info("Disconnecting receiver");
+               log_info("Disconnecting data logger");
                release_device();
+               inited = 0;
        }
 
+       log_info("Terminated");
        return 0;
 }