summaryrefslogtreecommitdiffhomepage
path: root/src/stats.c
blob: ea0b629c09add306bba1108feca9889dac2db38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* $Id: stats.c,v 1.13 2003-03-13 21:31:03 rjkaes Exp $
 *
 * 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
 * not allow more than one child to access the statistics at the same
 * 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"
#include "heap.h"
#include "htmlerror.h"
#include "stats.h"
#include "utils.h"

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;
};

static struct stat_s *stats;

/*
 * Initialize the statistics information to zero.
 */
void
init_stats(void)
{
	stats = malloc_shared_memory(sizeof(struct stat_s));
	if (stats == MAP_FAILED)
		return;

	memset(stats, 0, sizeof(struct stat));
}

/*
 * Display the statics of the tinyproxy server.
 */
int
showstats(struct conn_s *connptr)
{
	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"
	    "</blockquote>\r\n</body></html>\r\n";

	char *message_buffer;
	char opens[16], reqs[16], badconns[16], denied[16], refused[16];
	FILE *statfile;

	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")))) {
		message_buffer = safemalloc(MAXBUFFSIZE);
		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);

		if (send_http_message(connptr, 200, "OK", message_buffer) < 0) {
			safefree(message_buffer);
			return -1;
		}

		safefree(message_buffer);
		return 0;
	}

	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);

	return 0;
}

/*
 * Update the value of the statistics. The update_level is defined in
 * stats.h
 */
int
update_stats(status_t update_level)
{
	switch (update_level) {
	case STAT_BADCONN:
		++stats->num_badcons;
		break;
	case STAT_OPEN:
		++stats->num_open;
		++stats->num_reqs;
		break;
	case STAT_CLOSE:
		--stats->num_open;
		break;
	case STAT_REFUSE:
		++stats->num_refused;
		break;
	case STAT_DENIED:
		++stats->num_denied;
		break;
	default:
		return -1;
	}

	return 0;
}