diff options
author | Christian Hesse <mail@eworm.de> | 2015-06-19 14:30:24 +0200 |
---|---|---|
committer | Christian Hesse <mail@eworm.de> | 2015-06-19 14:30:24 +0200 |
commit | 4007862e9f521cefed577e68bf9fc5df9b28085e (patch) | |
tree | 471c963b1f5a799b3c49604bfd67df06953d0ca1 | |
parent | c34fac8a2841f036f9c7b3074c1078e98ff739c1 (diff) | |
download | journal-notify-4007862e9f521cefed577e68bf9fc5df9b28085e.tar.gz journal-notify-4007862e9f521cefed577e68bf9fc5df9b28085e.tar.zst |
initial support for throttling
-rw-r--r-- | journal-notify.c | 23 | ||||
-rw-r--r-- | journal-notify.h | 1 |
2 files changed, 21 insertions, 3 deletions
diff --git a/journal-notify.c b/journal-notify.c index 2be80ce..cdbcac0 100644 --- a/journal-notify.c +++ b/journal-notify.c @@ -113,6 +113,9 @@ int main(int argc, char **argv) { pid_t child_pid, wpid; int status; + struct timeval tv_last = (struct timeval){ 0 }, tv_now = (struct timeval){ 0 }; + unsigned int notification_count = 0; + program = argv[0]; /* get command line options - part I @@ -159,7 +162,7 @@ int main(int argc, char **argv) { /* reinitialize getopt() by resetting optind to 0 */ optind = 0; - /* get command line options - part II*/ + /* get command line options - part II */ while ((i = getopt_long(argc, argv, optstring, options_long, NULL)) != -1) { switch (i) { case 'a': @@ -237,6 +240,7 @@ int main(int argc, char **argv) { } } + /* main loop */ while (1) { if ((rc = sd_journal_next(journal)) < 0) { fprintf(stderr, "Failed to iterate to next entry: %s\n", strerror(-rc)); @@ -252,9 +256,22 @@ int main(int argc, char **argv) { continue; } - /* Looks like there is a but in libsystemd journal handling + gettimeofday(&tv_now, NULL); + + if (tv_now.tv_sec == tv_last.tv_sec) { + if (notification_count >= 5) { + fprintf(stderr, "Already showed five notifications, ignoring!\n"); + continue; + } + notification_count++; + } else { + tv_last = tv_now; + notification_count = 1; + } + + /* Looks like there is a bug in libsystemd journal handling * that jumps to wrong entries. Just skip old entries until we - * have a recent ones. */ + * have a recent one. */ if (old_entry > 0) { fprintf(stderr, "This is an old entry, ignoring.\n"); continue; diff --git a/journal-notify.h b/journal-notify.h index 0dd2962..d840236 100644 --- a/journal-notify.h +++ b/journal-notify.h @@ -16,6 +16,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <sys/time.h> #include <sys/wait.h> #include <systemd/sd-journal.h> |