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/mbtask/taskstat.c

447 lines
9.7 KiB
C
Raw Normal View History

2001-08-17 05:46:24 +00:00
/*****************************************************************************
*
2001-12-23 21:05:40 +00:00
* $Id$
2001-08-17 05:46:24 +00:00
* Purpose ...............: Keep track of server status
*
*****************************************************************************
* Copyright (C) 1997-2001
*
* 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.
*
* MB 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 MB BBS; see the file COPYING. If not, write to the Free
* Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************************/
2002-06-30 13:20:57 +00:00
#include "../config.h"
2001-08-17 05:46:24 +00:00
#include "libs.h"
#include "../lib/structs.h"
2002-10-20 20:58:55 +00:00
#include "../lib/mberrors.h"
2001-08-17 05:46:24 +00:00
#include "taskstat.h"
#include "callstat.h"
2001-08-17 05:46:24 +00:00
#include "outstat.h"
#include "taskutil.h"
/*
* Semafores
*/
int s_scanout = FALSE;
int s_mailout = FALSE;
int s_mailin = FALSE;
int s_reqindex = FALSE;
int s_index = FALSE;
int s_msglink = FALSE;
int s_newnews = FALSE;
int s_bbsopen = FALSE;
2002-02-02 15:15:34 +00:00
int s_do_inet = FALSE;
2001-08-17 05:46:24 +00:00
extern int UPSalarm;
2001-12-23 21:05:40 +00:00
extern int ptimer;
2002-02-24 13:52:13 +00:00
extern int rescan;
2001-08-17 05:46:24 +00:00
extern struct taskrec TCFG;
extern int internet;
extern int ZMH;
typedef struct {
long tot_clt; /* Total client connects */
long peak_clt; /* Peak simultaneous tot_cltes */
long s_error; /* Syntax errors from clients */
long c_error; /* Comms errors from clients */
} cl_stat;
typedef struct {
time_t start; /* Start date/time */
time_t laststart; /* Last start date/time */
time_t daily; /* Last daily update */
long startups; /* Total starts */
long clients; /* Connected clients */
cl_stat total; /* Total statistics */
cl_stat today; /* Todays statistics */
unsigned open : 1; /* Is BBS open */
unsigned long sequence; /* Sequencer counter */
} status_r;
static char stat_fn[PATH_MAX]; /* Statusfile name */
static status_r status; /* Status data */
2002-02-04 20:37:01 +00:00
extern double Load; /* System Load */
2001-08-17 05:46:24 +00:00
extern int Processing; /* Is system running */
/************************************************************************
*
* Initialize the statusfile, create it if necesary.
*/
void status_init()
{
size_t cnt;
int stat_fd;
sprintf(stat_fn, "%s/var/status.mbsed", getenv("MBSE_ROOT"));
/*
* First check if this is the very first time we start the show.
* If so, we generate an empty status file with only the start
* date in it.
*/
stat_fd = open(stat_fn, O_RDWR);
if (stat_fd == -1) {
memset((char *)&status, 0, sizeof(status_r));
status.start = time(NULL);
status.daily = time(NULL);
status.sequence = (unsigned long)time(NULL);
stat_fd = open(stat_fn, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
cnt = write(stat_fd, &status, sizeof(status_r));
Syslog('+', "New statusfile created");
2001-08-17 05:46:24 +00:00
lseek(stat_fd, 0, SEEK_SET);
}
cnt = read(stat_fd, &status, sizeof(status_r));
if (cnt != sizeof(status_r)) {
printf("Error reading status file\n");
2002-10-20 20:58:55 +00:00
exit(MBERR_INIT_ERROR);
2001-08-17 05:46:24 +00:00
}
status.startups++;
status.laststart = time(NULL);
status.clients = 1; /* We are a client ourself */
s_bbsopen = status.open;
lseek(stat_fd, 0, SEEK_SET);
cnt = write(stat_fd, &status, sizeof(status_r));
if (cnt != sizeof(status_r)) {
Syslog('?', "$Error rewrite status file\n");
2002-10-20 20:58:55 +00:00
exit(MBERR_INIT_ERROR);
2001-08-17 05:46:24 +00:00
}
close(stat_fd);
}
/*
* Writeback the updated status record.
*/
void status_write(void);
void status_write(void)
{
int d, stat_fd;
struct tm *ttm, *ytm;
2001-08-17 05:46:24 +00:00
time_t temp;
temp = time(NULL);
ttm = localtime(&temp);
temp = status.daily; // On a Sparc, first put the time in temp, then pass it to locattime.
ytm = localtime(&temp);
2001-08-17 05:46:24 +00:00
/*
* If we passed to the next day, zero the today counters
*/
if (ttm->tm_yday != ytm->tm_yday) {
Syslog('+', "Last days statistics:");
Syslog('+', "Total clients : %lu", status.today.tot_clt);
Syslog('+', "Peak clients : %lu", status.today.peak_clt);
Syslog('+', "Syntax errors : %lu", status.today.s_error);
Syslog('+', "Comms errors : %lu", status.today.c_error);
2001-08-17 05:46:24 +00:00
memset((char *)&status.today, 0, sizeof(cl_stat));
status.daily = time(NULL);
Syslog('+', "Zeroed todays status counters");
2001-08-17 05:46:24 +00:00
}
if ((stat_fd = open(stat_fn, O_RDWR)) == -1) {
Syslog('?', "$Error open statusfile %s", stat_fn);
return;
}
if ((d = lseek(stat_fd, 0, SEEK_SET)) != 0) {
Syslog('?', "$Error seeking in statusfile");
return;
}
2001-08-17 05:46:24 +00:00
d = write(stat_fd, &status, sizeof(status_r));
if (d != sizeof(status_r))
Syslog('?', "$Error writing statusfile, only %d bytes", d);
2001-08-17 05:46:24 +00:00
/*
* CLose the statusfile
*/
if (close(stat_fd) != 0)
Syslog('?', "$Error closing statusfile");
2001-08-17 05:46:24 +00:00
}
/*************************************************************************
*
* Various actions on the statusfile.
*/
/*
* Check for Zone Mail Hour, return TRUE if it is.
*/
int get_zmh()
{
struct tm *l_date;
char sstime[6];
time_t Now;
Now = time(NULL);
l_date = gmtime(&Now);
sprintf(sstime, "%02d:%02d", l_date->tm_hour, l_date->tm_min);
if ((strncmp(sstime, TCFG.zmh_start, 5) >= 0) && (strncmp(sstime, TCFG.zmh_end, 5) < 0)) {
if (!ZMH) {
CreateSema((char *)"zmh");
Syslog('!', "Start of Zone Mail Hour");
2001-08-17 05:46:24 +00:00
ZMH = TRUE;
}
} else {
if (ZMH) {
RemoveSema((char *)"zmh");
Syslog('!', "End of Zone Mail Hour");
2001-08-17 05:46:24 +00:00
ZMH = FALSE;
}
}
return ZMH;
}
void stat_inc_clients()
{
status.clients++;
status.total.tot_clt++;
status.today.tot_clt++;
if (status.clients >= status.total.peak_clt)
status.total.peak_clt = status.clients;
if (status.clients >= status.today.peak_clt)
status.today.peak_clt = status.clients;
status_write();
}
void stat_dec_clients()
{
status.clients--;
status_write();
}
void stat_set_open(int op)
{
if (op) {
if (!s_bbsopen) {
Syslog('!', "The bbs is open");
2001-08-17 05:46:24 +00:00
sem_set((char *)"scanout", TRUE);
}
} else {
if (s_bbsopen) {
Syslog('!', "The bbs is closed");
2001-08-17 05:46:24 +00:00
}
}
s_bbsopen = status.open = op;
status_write();
}
void stat_inc_serr()
{
status.total.s_error++;
status.today.s_error++;
status_write();
}
void stat_inc_cerr()
{
status.total.c_error++;
status.today.c_error++;
status_write();
}
char *stat_status()
{
static char buf[160];
buf[0] = '\0';
2002-02-02 15:15:34 +00:00
sprintf(buf, "100:20,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%ld,%d,%d,%d,%d,%d,%2.2f,%lu;",
2001-08-17 05:46:24 +00:00
status.start, status.laststart, status.daily,
status.startups, status.clients,
status.total.tot_clt, status.total.peak_clt,
status.total.s_error, status.total.c_error,
status.today.tot_clt, status.today.peak_clt,
status.today.s_error, status.today.c_error,
2002-02-02 15:15:34 +00:00
status.open, get_zmh(), internet, s_do_inet, Processing, Load, status.sequence);
2001-08-17 05:46:24 +00:00
return buf;
}
/*
* Return open status:
* 0 = open.
* 1 = closed.
* 2 = Zone Mail Hour.
*/
int stat_bbs_stat()
{
if (!status.open)
return 1;
if (get_zmh())
return 2;
return 0;
}
/*
* Get next sequence number
*/
char *getseq(void)
{
static char buf[80];
buf[0] = '\0';
status.sequence++;
status_write();
sprintf(buf, "100:1,%lu;", status.sequence);
return buf;
}
int sem_set(char *sem, int value)
{
Syslog('s', "%s semafore \"%s\"", value?"Set":"Clear", sem);
2001-08-17 05:46:24 +00:00
if (!strcmp(sem, "scanout")) {
s_scanout = value;
2002-02-24 13:52:13 +00:00
if (value)
rescan = TRUE;
2001-08-17 05:46:24 +00:00
} else if (!strcmp(sem, "mailout")) {
s_mailout = value;
} else if (!strcmp(sem, "mailin")) {
s_mailin = value;
} else if (!strcmp(sem, "mbindex")) {
s_index = value;
} else if (!strcmp(sem, "newnews")) {
s_newnews = value;
} else if (!strcmp(sem, "msglink")) {
s_msglink = value;
} else if (!strcmp(sem, "reqindex")) {
s_reqindex = value;
2002-02-02 15:15:34 +00:00
} else if (!strcmp(sem, "do_inet")) {
s_do_inet = value;
2001-08-17 05:46:24 +00:00
} else {
2001-12-23 21:05:40 +00:00
return FALSE;
2001-08-17 05:46:24 +00:00
}
2001-12-23 21:05:40 +00:00
ptimer = PAUSETIME;
return TRUE;
2001-08-17 05:46:24 +00:00
}
char *sem_status(char *data)
{
char *cnt, *sem;
static char buf[40];
int value;
buf[0] = '\0';
sprintf(buf, "200:1,16;");
cnt = strtok(data, ",");
sem = strtok(NULL, ";");
if (!strcmp(sem, "scanout")) {
value = s_scanout;
} else if (!strcmp(sem, "mailout")) {
value = s_mailout;
} else if (!strcmp(sem, "mailin")) {
value = s_mailin;
} else if (!strcmp(sem, "mbindex")) {
value = s_index;
} else if (!strcmp(sem, "newnews")) {
value = s_newnews;
} else if (!strcmp(sem, "msglink")) {
value = s_msglink;
} else if (!strcmp(sem, "reqindex")) {
value = s_reqindex;
} else if (!strcmp(sem, "upsalarm")) {
value = UPSalarm;
2002-02-02 15:15:34 +00:00
} else if (!strcmp(sem, "do_inet")) {
value = s_do_inet;
2001-08-17 05:46:24 +00:00
} else {
Syslog('s', "sem_status(%s) buf=%s", sem, buf);
2001-08-17 05:46:24 +00:00
return buf;
}
sprintf(buf, "100:1,%s;", value ? "1":"0");
Syslog('s', "Check semafore \"%s\": %s present", sem, value?"is":"not");
2001-08-17 05:46:24 +00:00
return buf;
}
char *sem_create(char *data)
{
static char buf[40];
char *cnt, *sem;
cnt = strtok(data, ",");
sem = strtok(NULL, ";");
buf[0] = '\0';
sprintf(buf, "200:1,16;");
if (sem_set(sem, TRUE))
sprintf(buf, "100:0;");
return buf;
}
char *sem_remove(char *data)
{
static char buf[40];
char *cnt, *sem;
cnt = strtok(data, ",");
sem = strtok(NULL, ";");
buf[0] = '\0';
sprintf(buf, "200:1,16;");
if (sem_set(sem, FALSE))
sprintf(buf, "100:0;");
return buf;
}