httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Randy Terbush" <ra...@covalent.net>
Subject RE: small contrib
Date Fri, 23 Jul 1999 21:41:29 GMT
How about adding another parameter that would allow setting a command
for the post processing. Could be gzip, a log analyzer, etc. The hard
coded path to /bin/gzip will not be portable.

Also, want to get fancy? Write a pid file and trap SIGUSR1 to tell it
when to perform the rotation.


> -----Original Message-----
> From: new-httpd-owner@apache.org
> [mailto:new-httpd-owner@apache.org]On
> Behalf Of Tom Vogt
> Sent: Thursday, July 22, 1999 7:14 AM
> To: new-httpd@apache.org
> Subject: Re: small contrib
>
>
> Jim Jagielski wrote:
> >
> > If small enough to post, please do so at
> new-httpd@apache.org. If not,
> > then you can post a URL to it.
> >
>
> I think it can be posted. here it comes, please comment on
> everything
> you think can be improved (i.e. most of it :) ):
>
> /*
>  * Logbox - daily logrotation.
>  *
>  * Tom Vogt <tom@lemuria.org>
>  * slightly based on Ben Laurie's rotatelogs.c
>  *
>  * Add something like
>  * TransferLog "|/path/to/logbox /some/where 4" common
>  * to httpd.conf. The generated name will be
> /some/where.date where date
> is
>  * the date of the logfile. The number should tell where
> the date can be
>  * found in the logfile lines.
>  * I assume space-seperated fields and a date in
> [date:time] format. If
> yours
>  * is different, patch this file.
> */
>
> #define BUFSIZE         65536
> #define MAX_PATH        1024
>
> /*
> #include "ap_config.h"
> */
> #include <unistd.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> #include <errno.h>
> #include <fcntl.h>
> #include <syslog.h>
>
> int main (int argc, char **argv)
> {
>     char buf[BUFSIZE], buf2[MAX_PATH];
>     char lastdate[64]={" - Restart - "}, nowdate[64], fname[64];
>     char *ptr, *delptr;
>     int nLogFD = -1;
>     int nRead;
>     char *szLogRoot;
>     int nField;
>     int count;
>
>     if (argc != 3) {
>         fprintf(stderr,"Usage: %s <logfile>
> <datefield>\n\n",argv[0]);
> 	exit(1);
>     }
>
>     szLogRoot = argv[1];
>     nField = atoi(argv[2]);
>
>     for (;;) {
> 	nRead = read(0, buf, sizeof buf);
>         if (nRead == 0) {
>             openlog("Logbox",LOG_CONS|LOG_PID,LOG_DAEMON);
>             syslog(LOG_ALERT,"Logbox dead, empty input stream!");
>             exit(3);
>         }
> 	if (nRead < 0)
>             if (errno != EINTR) {
>                 openlog("Logbox",LOG_CONS|LOG_PID,LOG_DAEMON);
>                 syslog(LOG_ALERT,"Logbox dead, error in
> input stream!");
>                 exit(4);
>             }
>
>         count=1;
>         ptr=buf;
>         while (count<nField&&*ptr!='\x0')
>             if (*ptr++==' ') count++;
>         if (*ptr=='\x0') {
>             openlog("Logbox",LOG_CONS|LOG_PID,LOG_DAEMON);
>             syslog(LOG_ERR,"Trouble in input stream. Trying to hang
> on.");
>             continue;
>         }
>         delptr=strchr(ptr,':');
>         strncpy(nowdate,ptr,(delptr-ptr));
>
>         if (nLogFD >= 0 && (strcmp(nowdate,lastdate)!=0 ||
> nRead < 0)) {
>             close(nLogFD);
>             nLogFD = -1;
>             switch (fork()) {
>             case 0: /* child process */
>                 openlog("Logbox",LOG_CONS|LOG_PID,LOG_DAEMON);
>                 syslog(LOG_INFO,"Zipping %s.",buf2);
>                 execl("/bin/gzip","gzip",buf2,NULL);
>                 syslog(LOG_INFO,"Trouble executing gzip.");
>                 exit(0);
>                 break;
>             case -1: /* error */
>                 openlog("Logbox",LOG_CONS|LOG_PID,LOG_DAEMON);
>                 syslog(LOG_ERR,"Trouble forking to zip old
> logfile.");
>                 break;
>             default: /* parent process - do nothing */
>             }
>         }
>         if (nLogFD < 0) {
>             strcpy(lastdate,nowdate);
>             strcpy(fname,nowdate+1);
>             ptr=fname;
>             while (*ptr!='\x0') {
>                 if (*ptr=='/') *ptr='-';
>                 ptr++;
>             }
> 	    sprintf(buf2, "%s.%s", szLogRoot, fname);
> 	    nLogFD = open(buf2, O_WRONLY | O_CREAT | O_APPEND, 0666);
> 	    if (nLogFD < 0) {
> 		perror(buf2);
> 		exit(2);
> 	    }
> 	}
> 	if (write(nLogFD, buf, nRead) != nRead) {
> 	    perror(buf2);
> 	    exit(5);
> 	}
>     }
> }
>


Mime
View raw message