[LUAU] watchdog to make sure a log file is growing

Matt Darnell mattdarnell at gmail.com
Wed Mar 30 11:21:38 PST 2005


> You could probably write something shorter in Perl or Python, but this
> is Unix, so we'll use 'C'.
> (I should write this in lisp just to show off how short programs are in
> lisp, but I doubt there are
> three other lisp hackers in the state.)
> 
> You'll want to look for the following code and do something other than
> print a diagnostic, likely.
> 
>      /* you'll want to do something different here */
>       printf("oops!! %s: \tsize: %d\n", filename, (int) buf.st_size);
> 
> jim
> 
> Matt Darnell wrote:
> 
> /home/jim 341>: cat watch4matt.c
> /*
>  * watch4mat.c
>  *
>  * Inspired by:
>  *
>  * From: mattdarnell at gmail.com
>  * Subject: [LUAU] watchdog to make sure a log file is growing
>  *
>  * Aloha,
>  *
>  * Does anyone know of a daemon or script that can be run to make sure
>  * that a log file is growing?
>  *
>  * This log file grows to over 300MB in 24 hours & growth is guaranteed
>  * second to second.
>  *
>  * I would like to now when the file does not grow for a five second
>  * period.  Sending out emails when it occurs would be best.
>  *
>  * I found lots of links for log watchers - like here
>  * http://www.linuxlinks.com/Software/Monitoring/Logs/ - but none seem to
>  * just make sure the log is growing.
>  *
>  * It is a Debian box.
>  *
>  */
> 
> #include <stdio.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <unistd.h>
> #include <errno.h>
> #include <stdlib.h>
> #include <libgen.h>
> 
> void usage();
> char *progname;
> 
> int
> main(int argc, char *argv[])
> {
>   int ret, c, errflag = 0, lastsize;
>   int timeout = 5; /* five second default */
>   char *filename = NULL;
>   struct stat buf;
> 
>   progname = basename(argv[0]);
> 
>   while ((c = getopt(argc, argv, "t:f:")) != -1) {
>     switch (c) {
>       case 't':
>         if ((timeout = (int)strtol(optarg, (char **)NULL, 10)) <= 0) {
>           (void) fprintf(stderr, "%s: illegal timeout value: %s\n",
>              progname, optarg);
>           errflag++;
>         }
>         break;
>       case 'f': /* if we can't stat the file the first time, exit */
>         filename = optarg; /* just points into argv[] */
>         break;
>       case ':':       /* -f or -o without operand */
>         fprintf(stderr, "Option -%c requires an operand\n", optopt);
>         errflag++;
>       case '?':
>         fprintf(stderr, "Unrecognized option: -%c\n", optopt);
>         errflag++;
>       default:
>         errflag++;
>     }
>   }
> 
>   if (errflag) {
>     usage();
>   }
> 
>   /* file to watch must be named */
>   if (!filename && !errflag) {
>     fprintf(stderr, "filename not set\n");
>     usage();
>   } else {
>     /* and we must be able to stat it */
>     if ((ret = stat(filename, &buf)) != 0) {
>         perror("stat failed:");
>         exit(1);
>     }
> 
>     /* and its senseless to watch anything but a normal file */
>     if (S_ISREG(buf.st_mode)) {
>       lastsize = buf.st_size; /* record last size */
>     } else {
>       fprintf(stderr, "%s is not a regular file\n", filename);
>       exit(1);
>     }
>   }
> 
>   while (1) {
>     usleep(timeout * 1000000);
>     if ((ret = stat(filename, &buf)) != 0) {
>       perror("stat failed:");
>       exit(ret);
>     }
>     if (buf.st_size <= lastsize) {
>       /* you'll want to do something different here */
>       printf("oops!! %s: \tsize: %d\n", filename, (int) buf.st_size);
>     }
>     lastsize = buf.st_size;
>   }
>   exit(0);
> }
> 
> void
> usage()
> {
>   fprintf(stderr, "Usage: %s [-t timeout] -f filename\n", progname);
>   exit(1);
> }
> 

Jim, 

Thanks for this.

Must be nice to be able to whip up  a C program that fast!

-Matt



More information about the LUAU mailing list