2004-04-28 02:48:53 +08:00
|
|
|
/* $Id: stats.c,v 1.16 2004-04-27 18:48:53 rjkaes Exp $
|
2000-09-12 08:06:09 +08:00
|
|
|
*
|
|
|
|
* This module handles the statistics for tinyproxy. There are only two
|
|
|
|
* public API functions. The reason for the functions, rather than just a
|
|
|
|
* external structure is that tinyproxy is now multi-threaded and we can
|
2002-05-27 02:53:14 +08:00
|
|
|
* not allow more than one child to access the statistics at the same
|
2000-09-12 08:06:09 +08:00
|
|
|
* time. This is prevented by a mutex. If there is a need for more
|
|
|
|
* statistics in the future, just add to the structure, enum (in the header),
|
|
|
|
* and the switch statement in update_stats().
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000 Robert James Kaes (rjkaes@flarenet.com)
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tinyproxy.h"
|
|
|
|
|
|
|
|
#include "log.h"
|
2002-05-24 02:20:27 +08:00
|
|
|
#include "heap.h"
|
2003-03-14 05:31:03 +08:00
|
|
|
#include "htmlerror.h"
|
2000-09-12 08:06:09 +08:00
|
|
|
#include "stats.h"
|
2001-09-09 02:58:37 +08:00
|
|
|
#include "utils.h"
|
2000-09-12 08:06:09 +08:00
|
|
|
|
|
|
|
struct stat_s {
|
|
|
|
unsigned long int num_reqs;
|
|
|
|
unsigned long int num_badcons;
|
|
|
|
unsigned long int num_open;
|
|
|
|
unsigned long int num_refused;
|
|
|
|
unsigned long int num_denied;
|
|
|
|
};
|
|
|
|
|
2002-05-27 02:53:14 +08:00
|
|
|
static struct stat_s *stats;
|
2000-09-12 08:06:09 +08:00
|
|
|
|
|
|
|
/*
|
2002-05-24 12:45:32 +08:00
|
|
|
* Initialize the statistics information to zero.
|
2000-09-12 08:06:09 +08:00
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
void
|
|
|
|
init_stats(void)
|
2000-09-12 08:06:09 +08:00
|
|
|
{
|
2004-02-14 05:27:42 +08:00
|
|
|
stats = malloc_shared_memory(sizeof(struct stat_s));
|
2002-05-30 04:49:55 +08:00
|
|
|
if (stats == MAP_FAILED)
|
2002-05-27 02:53:14 +08:00
|
|
|
return;
|
|
|
|
|
2004-04-28 02:48:53 +08:00
|
|
|
memset(stats, 0, sizeof(struct stat_s));
|
2000-09-12 08:06:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display the statics of the tinyproxy server.
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
int
|
|
|
|
showstats(struct conn_s *connptr)
|
2000-09-12 08:06:09 +08:00
|
|
|
{
|
2001-11-22 08:31:10 +08:00
|
|
|
static char *msg =
|
|
|
|
"<html><head><title>%s (%s) stats</title></head>\r\n"
|
|
|
|
"<body>\r\n"
|
|
|
|
"<center><h2>%s (%s) run-time statistics</h2></center><hr>\r\n"
|
|
|
|
"<blockquote>\r\n"
|
|
|
|
"Number of open connections: %lu<br>\r\n"
|
|
|
|
"Number of requests: %lu<br>\r\n"
|
|
|
|
"Number of bad connections: %lu<br>\r\n"
|
|
|
|
"Number of denied connections: %lu<br>\r\n"
|
|
|
|
"Number of refused connections due to high load: %lu\r\n"
|
2000-09-12 08:06:09 +08:00
|
|
|
"</blockquote>\r\n</body></html>\r\n";
|
|
|
|
|
2001-09-16 05:27:58 +08:00
|
|
|
char *message_buffer;
|
2003-03-14 05:31:03 +08:00
|
|
|
char opens[16], reqs[16], badconns[16], denied[16], refused[16];
|
|
|
|
FILE *statfile;
|
2001-09-16 05:27:58 +08:00
|
|
|
|
2003-03-14 05:31:03 +08:00
|
|
|
snprintf(opens, sizeof(opens), "%lu", stats->num_open);
|
|
|
|
snprintf(reqs, sizeof(reqs), "%lu", stats->num_reqs);
|
|
|
|
snprintf(badconns, sizeof(badconns), "%lu", stats->num_badcons);
|
|
|
|
snprintf(denied, sizeof(denied), "%lu", stats->num_denied);
|
|
|
|
snprintf(refused, sizeof(refused), "%lu", stats->num_refused);
|
|
|
|
|
|
|
|
if (!config.statpage || (!(statfile = fopen(config.statpage, "r")))) {
|
2004-02-14 05:27:42 +08:00
|
|
|
message_buffer = safemalloc(MAXBUFFSIZE);
|
2003-03-14 05:31:03 +08:00
|
|
|
if (!message_buffer)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
snprintf(message_buffer, MAXBUFFSIZE, msg,
|
|
|
|
PACKAGE, VERSION, PACKAGE, VERSION,
|
|
|
|
stats->num_open,
|
|
|
|
stats->num_reqs,
|
|
|
|
stats->num_badcons, stats->num_denied, stats->num_refused);
|
2000-09-12 08:06:09 +08:00
|
|
|
|
2003-03-14 05:31:03 +08:00
|
|
|
if (send_http_message(connptr, 200, "OK", message_buffer) < 0) {
|
|
|
|
safefree(message_buffer);
|
|
|
|
return -1;
|
|
|
|
}
|
2000-09-12 08:06:09 +08:00
|
|
|
|
2001-09-16 05:27:58 +08:00
|
|
|
safefree(message_buffer);
|
2003-03-14 05:31:03 +08:00
|
|
|
return 0;
|
2001-09-16 05:27:58 +08:00
|
|
|
}
|
|
|
|
|
2003-03-14 05:31:03 +08:00
|
|
|
add_error_variable(connptr, "opens", opens);
|
|
|
|
add_error_variable(connptr, "reqs", reqs);
|
|
|
|
add_error_variable(connptr, "badconns", badconns);
|
|
|
|
add_error_variable(connptr, "denied", denied);
|
|
|
|
add_error_variable(connptr, "refused", refused);
|
|
|
|
add_standard_vars(connptr);
|
|
|
|
send_http_headers(connptr, 200, "Statistic requested");
|
|
|
|
send_html_file(statfile, connptr);
|
|
|
|
fclose(statfile);
|
|
|
|
|
2000-09-12 08:06:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the value of the statistics. The update_level is defined in
|
|
|
|
* stats.h
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
int
|
|
|
|
update_stats(status_t update_level)
|
2000-09-12 08:06:09 +08:00
|
|
|
{
|
2001-11-22 08:31:10 +08:00
|
|
|
switch (update_level) {
|
2000-09-12 08:06:09 +08:00
|
|
|
case STAT_BADCONN:
|
2002-05-27 02:53:14 +08:00
|
|
|
++stats->num_badcons;
|
2000-09-12 08:06:09 +08:00
|
|
|
break;
|
|
|
|
case STAT_OPEN:
|
2002-05-27 02:53:14 +08:00
|
|
|
++stats->num_open;
|
|
|
|
++stats->num_reqs;
|
2000-09-12 08:06:09 +08:00
|
|
|
break;
|
|
|
|
case STAT_CLOSE:
|
2002-05-27 02:53:14 +08:00
|
|
|
--stats->num_open;
|
2000-09-12 08:06:09 +08:00
|
|
|
break;
|
|
|
|
case STAT_REFUSE:
|
2002-05-27 02:53:14 +08:00
|
|
|
++stats->num_refused;
|
2000-09-12 08:06:09 +08:00
|
|
|
break;
|
|
|
|
case STAT_DENIED:
|
2002-05-27 02:53:14 +08:00
|
|
|
++stats->num_denied;
|
2000-09-12 08:06:09 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|