This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/mbfido/rollover.c

427 lines
10 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2001-12-23 20:17:39 +00:00
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Statistic rollover util.
*
*****************************************************************************
2004-01-25 10:57:35 +00:00
* Copyright (C) 1997-2004
2001-08-17 05:46:24 +00:00
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* MBSE BBS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MBSE BBS; see the file COPYING. If not, write to the Free
2003-08-15 20:05:34 +00:00
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
2001-08-17 05:46:24 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2001-08-17 05:46:24 +00:00
#include "../lib/libs.h"
#include "../lib/structs.h"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
2001-08-17 05:46:24 +00:00
#include "../lib/records.h"
#include "../lib/common.h"
#include "../lib/clcomm.h"
#include "rollover.h"
extern int do_quiet;
void StatAdd(statcnt *S, unsigned long V)
{
2002-07-14 12:23:39 +00:00
S->total += V;
S->tweek += V;
S->tdow[Diw] += V;
S->month[Miy] += V;
2001-08-17 05:46:24 +00:00
}
void RollWeek(statcnt *);
void RollWeek(statcnt *S)
{
2002-07-14 12:23:39 +00:00
int i;
2001-08-17 05:46:24 +00:00
2002-07-14 12:23:39 +00:00
for (i = 0; i < 7; i++) {
S->ldow[i] = S->tdow[i];
S->tdow[i] = 0L;
}
2001-08-17 05:46:24 +00:00
2002-07-14 12:23:39 +00:00
S->lweek = S->tweek;
S->tweek = 0L;
2001-08-17 05:46:24 +00:00
2002-07-14 12:23:39 +00:00
if (CFG.slow_util && do_quiet)
2004-01-25 10:57:35 +00:00
msleep(1);
2001-08-17 05:46:24 +00:00
}
FILE *OpenData(char *);
FILE *OpenData(char *Name)
{
2002-07-14 12:23:39 +00:00
char *temp;
FILE *fp;
2001-08-17 05:46:24 +00:00
2002-07-14 12:23:39 +00:00
temp = calloc(PATH_MAX, sizeof(char));
2001-08-17 05:46:24 +00:00
2002-07-14 12:23:39 +00:00
sprintf(temp, "%s/etc/%s", getenv("MBSE_ROOT"), Name);
if ((fp = fopen(temp, "r+")) == NULL) {
WriteError("$Can't open %s", temp);
2001-08-17 05:46:24 +00:00
free(temp);
2002-07-14 12:23:39 +00:00
return NULL;
}
free(temp);
return fp;
2001-08-17 05:46:24 +00:00
}
/*
* Test all files with statistic counters if a new week has started
* or a new month has started. All the record counters will be
* updated if one of these is the case.
*/
void Rollover()
{
2002-07-14 12:23:39 +00:00
time_t Now, Temp;
struct tm *t;
FILE *fp, *ft;
int do_week, do_month, Day, i;
char *temp, *temp1;
struct _history history;
Now = time(NULL);
t = localtime(&Now);
Diw = t->tm_wday;
Miy = t->tm_mon;
Day = t->tm_yday;
if ((fp = OpenData((char *)"nodes.data")) != NULL) {
fread(&nodeshdr, sizeof(nodeshdr), 1, fp);
Temp = nodeshdr.lastupd;
t = localtime(&Temp);
/*
* Test if it's sunday, and the last update wasn't today.
* If it's not sunday, and the last update was more then
* 7 days ago, we maybe missed last sunday and the update
* is still done.
*/
if (((Diw == 0) && (Day != t->tm_yday)) || ((Day - t->tm_yday) > 7))
do_week = TRUE;
else
do_week = FALSE;
/*
* If the month is different then the last update, we must
* be in a new month.
*/
if (Miy != t->tm_mon)
do_month = TRUE;
else
do_month = FALSE;
if (do_week || do_month) {
IsDoing("Date rollover");
Syslog('+', "Rollover nodes.data");
while (fread(&nodes, nodeshdr.recsize, 1, fp) == 1) {
if (do_week) {
RollWeek(&nodes.FilesSent);
RollWeek(&nodes.FilesRcvd);
RollWeek(&nodes.F_KbSent);
RollWeek(&nodes.F_KbRcvd);
RollWeek(&nodes.MailSent);
RollWeek(&nodes.MailRcvd);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
if (do_month) {
nodes.FilesSent.month[Miy] = 0;
nodes.FilesRcvd.month[Miy] = 0;
nodes.F_KbSent.month[Miy] = 0;
nodes.F_KbRcvd.month[Miy] = 0;
nodes.MailSent.month[Miy] = 0;
nodes.MailRcvd.month[Miy] = 0;
if (CFG.slow_util && do_quiet)
2004-01-25 10:57:35 +00:00
msleep(1);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
fseek(fp, - nodeshdr.recsize, SEEK_CUR);
fwrite(&nodes, nodeshdr.recsize, 1, fp);
fseek(fp, nodeshdr.filegrp + nodeshdr.mailgrp, SEEK_CUR);
}
fseek(fp, 0, SEEK_SET);
nodeshdr.lastupd = time(NULL);
fwrite(&nodeshdr, nodeshdr.hdrsize, 1, fp);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
fclose(fp);
}
if ((fp = OpenData((char *)"mareas.data")) != NULL) {
fread(&msgshdr, sizeof(msgshdr), 1, fp);
Temp = msgshdr.lastupd;
t = localtime(&Temp);
if (((Diw == 0) && (Day != t->tm_yday)) || ((Day - t->tm_yday) > 7))
do_week = TRUE;
else
do_week = FALSE;
if (Miy != t->tm_mon)
do_month = TRUE;
else
do_month = FALSE;
if (do_week || do_month) {
Syslog('+', "Rollover mareas.data");
while (fread(&msgs, msgshdr.recsize, 1, fp) == 1) {
if (do_week) {
RollWeek(&msgs.Received);
RollWeek(&msgs.Posted);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
if (do_month) {
msgs.Received.month[Miy] = 0;
msgs.Posted.month[Miy] = 0;
if (CFG.slow_util && do_quiet)
2004-01-25 10:57:35 +00:00
msleep(1);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
fseek(fp, - msgshdr.recsize, SEEK_CUR);
fwrite(&msgs, msgshdr.recsize, 1, fp);
fseek(fp, msgshdr.syssize, SEEK_CUR);
}
msgshdr.lastupd = time(NULL);
fseek(fp, 0, SEEK_SET);
fwrite(&msgshdr, msgshdr.hdrsize, 1, fp);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
fclose(fp);
}
if ((fp = OpenData((char *)"mgroups.data")) != NULL) {
fread(&mgrouphdr, sizeof(mgrouphdr), 1, fp);
Temp = mgrouphdr.lastupd;
t = localtime(&Temp);
if (((Diw == 0) && (Day != t->tm_yday)) || ((Day - t->tm_yday) > 7))
do_week = TRUE;
else
do_week = FALSE;
if (Miy != t->tm_mon)
do_month = TRUE;
else
do_month = FALSE;
if (do_week || do_month) {
Syslog('+', "Rollover mgroups.data");
while (fread(&mgroup, mgrouphdr.recsize, 1, fp) == 1) {
if (do_week) {
RollWeek(&mgroup.MsgsRcvd);
RollWeek(&mgroup.MsgsSent);
}
if (do_month) {
mgroup.MsgsRcvd.month[Miy] = 0;
mgroup.MsgsSent.month[Miy] = 0;
if (CFG.slow_util && do_quiet)
2004-01-25 10:57:35 +00:00
msleep(1);
2002-07-14 12:23:39 +00:00
}
fseek(fp, - mgrouphdr.recsize, SEEK_CUR);
fwrite(&mgroup, mgrouphdr.recsize, 1, fp);
}
2001-08-17 05:46:24 +00:00
2002-07-14 12:23:39 +00:00
mgrouphdr.lastupd = time(NULL);
fseek(fp, 0, SEEK_SET);
fwrite(&mgrouphdr, mgrouphdr.hdrsize, 1, fp);
}
fclose(fp);
}
if ((fp = OpenData((char *)"tic.data")) != NULL) {
fread(&tichdr, sizeof(tichdr), 1, fp);
Temp = tichdr.lastupd;
t = localtime(&Temp);
if (((Diw == 0) && (Day != t->tm_yday)) || ((Day - t->tm_yday) > 7))
do_week = TRUE;
else
do_week = FALSE;
if (Miy != t->tm_mon)
do_month = TRUE;
else
do_month = FALSE;
if (do_week || do_month) {
Syslog('+', "Rollover tic.data");
while (fread(&tic, tichdr.recsize, 1, fp) == 1) {
if (do_week) {
RollWeek(&tic.Files);
RollWeek(&tic.KBytes);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
if (do_month) {
tic.Files.month[Miy] = 0;
tic.KBytes.month[Miy] = 0;
if (CFG.slow_util && do_quiet)
2004-01-25 10:57:35 +00:00
msleep(1);
2002-07-14 12:23:39 +00:00
}
fseek(fp, - tichdr.recsize, SEEK_CUR);
fwrite(&tic, tichdr.recsize, 1, fp);
fseek(fp, tichdr.syssize, SEEK_CUR);
}
tichdr.lastupd = time(NULL);
fseek(fp, 0, SEEK_SET);
fwrite(&tichdr, tichdr.hdrsize, 1, fp);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
fclose(fp);
}
if ((fp = OpenData((char *)"fgroups.data")) != NULL) {
fread(&fgrouphdr, sizeof(fgrouphdr), 1, fp);
Temp = fgrouphdr.lastupd;
t = localtime(&Temp);
if (((Diw == 0) && (Day != t->tm_yday)) || ((Day - t->tm_yday) > 7))
do_week = TRUE;
else
do_week = FALSE;
if (Miy != t->tm_mon)
do_month = TRUE;
else
do_month = FALSE;
if (do_week || do_month) {
Syslog('+', "Rollover fgroups.data");
while (fread(&fgroup, fgrouphdr.recsize, 1, fp) == 1) {
if (do_week) {
RollWeek(&fgroup.Files);
RollWeek(&fgroup.KBytes);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
if (do_month) {
fgroup.Files.month[Miy] = 0;
fgroup.KBytes.month[Miy] = 0;
if (CFG.slow_util && do_quiet)
2004-01-25 10:57:35 +00:00
msleep(1);
2002-07-14 12:23:39 +00:00
}
fseek(fp, - fgrouphdr.recsize, SEEK_CUR);
fwrite(&fgroup, fgrouphdr.recsize, 1, fp);
}
fgrouphdr.lastupd = time(NULL);
fseek(fp, 0, SEEK_SET);
fwrite(&fgrouphdr, fgrouphdr.hdrsize, 1, fp);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
fclose(fp);
}
if ((fp = OpenData((char *)"hatch.data")) != NULL) {
fread(&hatchhdr, sizeof(hatchhdr), 1, fp);
Temp = hatchhdr.lastupd;
t = localtime(&Temp);
if (((Diw == 0) && (Day != t->tm_yday)) || ((Day - t->tm_yday) > 7))
do_week = TRUE;
else
do_week = FALSE;
if (Miy != t->tm_mon)
do_month = TRUE;
else
do_month = FALSE;
if (do_week || do_month) {
Syslog('+', "Rollover hatch.data");
while (fread(&hatch, hatchhdr.recsize, 1, fp) == 1) {
if (do_week)
RollWeek(&hatch.Hatched);
if (do_month) {
hatch.Hatched.month[Miy] = 0;
if (CFG.slow_util && do_quiet)
2004-01-25 10:57:35 +00:00
msleep(1);
2002-07-14 12:23:39 +00:00
}
fseek(fp, - hatchhdr.recsize, SEEK_CUR);
fwrite(&hatch, hatchhdr.recsize, 1, fp);
}
2001-08-17 05:46:24 +00:00
2002-07-14 12:23:39 +00:00
hatchhdr.lastupd = time(NULL);
fseek(fp, 0, SEEK_SET);
fwrite(&hatchhdr, hatchhdr.hdrsize, 1, fp);
}
fclose(fp);
}
temp = calloc(PATH_MAX, sizeof(char));
temp1 = calloc(PATH_MAX, sizeof(char));
sprintf(temp, "%s/var/mailer.hist", getenv("MBSE_ROOT"));
if ((fp = fopen(temp, "r"))) {
fread(&history, sizeof(history), 1, fp);
Temp = history.online;
t = localtime(&Temp);
if (t->tm_mon != Miy) {
/*
* Calculate date/time for records to delete
*/
t = localtime(&Now);
if (t->tm_mon == 0) {
t->tm_mon = 11;
t->tm_year--;
} else {
t->tm_mon--;
}
t->tm_mday = 1;
t->tm_hour = 0;
t->tm_min = 0;
t->tm_sec = 0;
Now = mktime(t);
Syslog('+', "Packing mailer history since %s", rfcdate(Now));
sprintf(temp1, "%s/var/mailer.temp", getenv("MBSE_ROOT"));
if ((ft = fopen(temp1, "a")) == NULL) {
WriteError("$Can't create %s", temp1);
fclose(fp);
} else {
memset(&history, 0, sizeof(history));
history.online = time(NULL);
history.offline = time(NULL);
fwrite(&history, sizeof(history), 1, ft);
i = 0;
while (fread(&history, sizeof(history), 1, fp)) {
if (history.online >= Now) {
fwrite(&history, sizeof(history), 1, ft);
i++;
}
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
fclose(ft);
fclose(fp);
unlink(temp);
rename(temp1, temp);
Syslog('+', "Written %d records", i);
}
} else {
fclose(fp);
2001-08-17 05:46:24 +00:00
}
2002-07-14 12:23:39 +00:00
}
free(temp);
free(temp1);
2001-08-17 05:46:24 +00:00
}