Portál AbcLinuxu, 9. listopadu 2025 02:32
U me v Gentoo to napr. vypada takhle:
/var/log/apache2/*log {
missingok
notifempty
sharedscripts
postrotate
/etc/init.d/apache2 reload > /dev/null 2>&1 || true
endscript
jo. stop posle pouze signal a na poravdu pretizenych servrech to muze trvat i nekolik vterin, nez skonci vsechny httpd procesy. cronolog mi jeste porad nevyhovoval, tak jsem misto toho sesmolil (pouzivame to uz pres rok) todle: -u include chybi < > protoze se me s tim nechtelo prepisovat HOWG. -
#define _GNU_SOURCE
#include stdio.h
#include time.h
#include string.h
#include unistd.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include sys/time.h
#include errno.h
#include stdlib.h
#define DEBUG fprintf
extern int errno;
static int log= -1;
static char *jmeno=NULL;
static char *webal=NULL;
static int firstmem=1;
char last='\n';
int findnewline=0;
int newlinereset(void);
void cas(struct tm *plogtime);
void konci( int);
void linkwebal(struct tm *caslogu, char *webal, char *prefix);
void pojmenuj(struct tm *caslogu, char *prefix);
void otevri( char *jmeno, char *param);
int main(int argc, char **argv)
{
struct tm caslogu, casted;
struct stat staty;
char buff[4096];
int ret;
buff[0]='\0';
/* neblokuj pri cteni vstupu */
fcntl( fileno(stdin), O_NONBLOCK);
/* kontrola parametru */
if ( argc!=2) {
DEBUG(stderr, "spatny pocet argumentu\n");
konci(1);
}
/* webalizerovy link */
ret= strlen("webalizer") + strlen( argv[1]);
webal= malloc( ret+1);
strcpy( webal, argv[1]);
strcat( webal, "webalizer" );
DEBUG( stderr, "%s\n", webal);
/* vnejsi cyklus, ktery se stara o soubory */
do {
/* nacti cas */
cas(&caslogu);
/* vytvor link pro webalizer */
linkwebal( &caslogu, webal, argv[1]);
/* vytvor jmeno */
pojmenuj(&caslogu, argv[1]);
/* otevri log soubor */
otevri( jmeno, argv[1]);
/* vnitrni cyklus ktery se stara o cteni a preruseni jen na \n */
do {
/* smazany log? */
ret=stat(jmeno, &staty);
if (0!=ret) {
DEBUG(stderr, "nekdo smazal soubor? chyba stat(): %s\n",
strerror(errno));
if (newlinereset()) break;
}
/* kontrola casu */
cas(&casted);
/* zmena dne je zmena logu (na konci radky) */
if (caslogu.tm_yday!= casted.tm_yday) {
DEBUG(stderr, "zmena casu. bude se menit soubor\n");
if (newlinereset()) break;
}
if ( strlen(buff)) {
if (strlen(buff)!= write(log, buff, strlen(buff))) {
konci(3);
} else {
last= buff[strlen(buff)-1];
}
}
} while ( !feof(stdin) && !ferror(stdin) &&
fgets(buff, sizeof(buff), stdin)!=NULL);
} while ( !feof(stdin) && !ferror(stdin));
/* nikdy */
close( log);
free(jmeno);
return(0);
}
/* korektne skonci */
void konci(int kod)
{
if (log>=0) close(log);
if (!firstmem && NULL!=jmeno) free(jmeno);
exit(kod);
}
/* nacti cas */
void cas(struct tm *plogtime)
{
time_t ted= time(NULL);
localtime_r( &ted, plogtime);
}
/* vytvor link pro webalizer */
void linkwebal(struct tm *caslogu, char *webal, char *prefix)
{
int ret;
struct tm oldlogtime;
struct stat wstaty;
time_t vcera= time(NULL) - 86400;
/* zkontroluj existenci "webalizer" hardlinku */
ret=stat( webal, &wstaty);
if ( ret && !ENOENT== errno) perror("webalizer link - stat");
/* pokud existuje, tak jej odstran */
if (( 0== ret) || (! ENOENT== errno)) {
if ( (0== ret) || (-1 == ret && (! ENOENT== errno))) {
ret= unlink( webal);
if (ret) perror("webalizer link - unlink");
}
}
/* pokud je to po stratu, tak vyrob jmeno z minuleho dne */
if ( NULL== jmeno) {
localtime_r( &vcera, &oldlogtime);
pojmenuj( &oldlogtime, prefix);
}
/* pokud existuje, tak proved hardlink */
ret= stat( jmeno, &wstaty);
if (! ret) {
ret= link( jmeno, webal);
if ( ret) perror("webalizer link - link");
}
}
/* vytvor jmeno z casu */
void pojmenuj(struct tm *caslogu, char *prefix)
{
int delka, cela;
char strcas[10];
if (8!=strftime( strcas, 9, "%Y%m%d", caslogu)) {
DEBUG(stderr, "spatna delka prevedeneho casu\n");
konci(2);
}
strcas[8]='\0';
/* vyrob jmeno logu */
delka= strlen(prefix);
cela= delka+ strlen(strcas)+ 1;
if (firstmem) {
jmeno= (char *)malloc( cela);
firstmem=0;
if ( NULL==jmeno) {
DEBUG(stderr, "nepodarilo se alokovat pamet\n");
konci(3);
}
}
/* pospojuj argument a cas do jmena */
strncpy(jmeno, prefix, delka);
jmeno[delka]='\0';
strncpy( (char *)&(((char *)jmeno)[delka]), strcas, cela-delka);
jmeno[cela]='\0';
}
/* otevri soubor */
void otevri( char *jmeno, char *param)
{
log= open( jmeno, O_WRONLY | O_CREAT | O_APPEND, 384);
if (-1==log) {
DEBUG(stderr, "nepodarilo se otevrit soubor\n");
konci(4);
}
unlink(param );
link( jmeno, param);
}
/* kontroluj jestli je posledni zapsany \n */
int newlinereset(void)
{
fdatasync( fileno(stdin));
/* byl napsan naposled konec radku */
if (last=='\n') {
DEBUG(stderr, "newlinereset(): last \\n\n");
findnewline=0;
close( log);
return(1);
/* je potreba najit konec radku */
} else if (last!='\n') {
DEBUG(stderr, "newlinereset(): last neni \\n\n");
findnewline=1;
return(0);
}
}
v konfiguraku pak
CustomLog "|/usr/local/bin/jmeno /usr/local/apache2/logs/access_log" combined
no a zalohovani resim z cronu jen pro stary logy nejak takhle:
#!/bin/bash
store="/mnt/backup/log/apache";
log="${store}/default.tar";
if test -f ${log} && test -r ${log}; then
if test `date +%u` = 2; then
newlog=`date +%Y%m%d_%s`;
mv ${log} ${store}/default_do_${newlog}.tar;
bzip2 -9z ${store}/default_do_${newlog}.tar;
fi
fi
for F in `cat /usr/local/var/logs/default.log`; do
dir=`dirname ${F}`;
find ${dir} -mtime +3 | grep -e "${F}[0-9]\+" | xargs --no-run-if-empty tar vuf ${log} --exclude ${F} --exclude ${F}`date +%Y%m%d` --remove-files
done
kdyz tak na to letmo koukam, tak uz ani presne nevim co ten skript dela... ale par jich necha ne puvodnim miste a zbytek hodi do tar archivu kterej po case zrotuje a zbali bzipem. ale funguje a to je hlavni.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.