add averaging
authorTeodor Sigaev <teodor@sigaev.ru>
Mon, 4 Apr 2016 21:25:15 +0000 (00:25 +0300)
committerTeodor Sigaev <teodor@sigaev.ru>
Mon, 4 Apr 2016 21:25:15 +0000 (00:25 +0300)
main.c
munin.plugin/trinket
rc.d/trinketd

diff --git a/main.c b/main.c
index 0fb4f1a..7c48a37 100644 (file)
--- a/main.c
+++ b/main.c
@@ -38,7 +38,8 @@
 #include <trinket.h>
 
 static char*   dbdir = NULL;
-static int             period = (300); /* in seconds */
+static int             period = 300; /* in seconds */
+static int             tics = 1;
 static char*   pidfile = NULL;
 static char*   logfile = NULL;
 static bool            daemonize = false;
@@ -52,7 +53,7 @@ static void
 usage(const char *errmsg) {
        puts("trinketd - collecting info from AtomPro");
        puts("Copyright (c) 2016, Teodor Sigaev <teodor@sigaev.ru>");
-       puts("trinketd [-d] [-l logfile] [-p pidfile] [-P period] [-D datadir] [-w]");
+       puts("trinketd [-d] [-l logfile] [-p pidfile] [-P period] [-D datadir] [-i tics] [-w]");
 
        if (errmsg) {
                puts("");
@@ -64,11 +65,19 @@ usage(const char *errmsg) {
 
 static void
 main_loop() {
-       double  prevDose,
+       double  *prevDose,
                        curDose;
+       int             i;
+       int             collected;
+
+       prevDose = malloc(sizeof(*prevDose) * (tics + 1));
+       if (!prevDose) {
+               fprintf(stderr, "could not allocate array, exiting...\n");
+               return;
+       }
 
 restart:
-       prevDose = -1;
+       collected = 0;
 
        if (trinketOpen() != ERR_OK) {
                fprintf(stderr, "trinketOpen fails\n");
@@ -77,6 +86,7 @@ restart:
        }
 
        while(42) {
+
                if (workaround)
                        trinketPing();
 
@@ -87,9 +97,18 @@ restart:
                        goto restart;
                }
 
-               if (prevDose >= 0.0) {
-                       double  counts = curDose - prevDose;
-                       double  radlevel = 1e6 * coefficient * counts * 3600.0 / (double)period;
+               if (collected <= tics) {
+                       prevDose[collected++] = curDose;
+               } else {
+                       for (i=1; i<=tics; i++)
+                               prevDose[i - 1] = prevDose[i];
+                       prevDose[tics] = curDose;
+               }
+
+               if (collected > 1) {
+                       double  counts = curDose - prevDose[0];
+                       double  radlevel = 1e6 * coefficient * counts * 3600.0 /
+                                                               (double)(period * (collected - 1));
                        FILE    *fh = stdout;
                        bool    successOpen = true;
 
@@ -103,6 +122,7 @@ restart:
                        if (successOpen) {
                                fprintf(fh, "total: %lld\n", (long long)curDose);
                                fprintf(fh, "counts: %lld\n", (long long)counts);
+                               fprintf(fh, "tics: %d\n", collected - 1);
                                fprintf(fh, "radlevel: %.02f\n", radlevel);
                        }
 
@@ -113,7 +133,6 @@ restart:
                        }
                }
 
-               prevDose = curDose;
                sleep(period);
        }
 
@@ -131,7 +150,7 @@ main(int argn, char* argv[]) {
                logfd;
 
        opterr = 0;
-       while((i=getopt(argn,argv,"dD:l:p:P:wh")) != EOF) {
+       while((i=getopt(argn,argv,"dD:i:l:p:P:wh")) != EOF) {
                switch(i) {
                        case 'd':
                                daemonize = true;
@@ -139,6 +158,9 @@ main(int argn, char* argv[]) {
                        case 'D':
                                dbdir = strdup(optarg);
                                break;
+                       case 'i':
+                               tics = atoi(optarg);
+                               break;
                        case 'l':
                                logfile = strdup(optarg);
                                break;
@@ -163,6 +185,9 @@ main(int argn, char* argv[]) {
        if (period <= 0)
                usage("Collecting period could not be zero nor negative");
 
+       if (tics <= 0)
+               usage("Number of tics could not be zero nor negative");
+
        if (daemonize && !dbdir)
                usage("trinketd: it is useless to use -d without -D");
 
index c16a8b1..c2b3ab4 100755 (executable)
@@ -48,9 +48,7 @@ fi
 if [ "$1" = "config" ]; then
     echo 'multigraph rad'
     echo "graph_title Radiation level, mkR/h"
-#    echo 'graph_args --upper-limit 3000 -l 0'
     echo 'graph_vlabel mkR/h'
-#    echo 'graph_scale no'
     echo 'graph_category sensors'
     echo "rad.label Radiation level, mkR/h"
     rad_warning=${rad_warning:-50}
index d21ba82..b9540e0 100755 (executable)
@@ -21,7 +21,7 @@ dbdir="/var/db/trinket"
 procname="/usr/local/sbin/trinketd"
 
 command="${procname}"
-command_args=" -d -p ${pidfile} -P 300 -D ${dbdir} -l ${logfile} -w"
+command_args=" -d -p ${pidfile} -P 300 -i 12 -D ${dbdir} -l ${logfile} -w"
 
 load_rc_config $name