~ubuntu-branches/ubuntu/oneiric/apachetop/oneiric

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#ifndef _APACHETOP_H_
#define _APACHETOP_H_

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>

#if HAVE_STRINGS_H
# include <strings.h>
#endif

#include <fcntl.h>

#include <signal.h>

#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#if HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif

#if HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif

#if HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif

#if HAVE_NETDB_H
# include <netdb.h>
#endif

#if HAVE_PCRE_H
# include <pcre.h>
# include "pcre_cpp_wrapper.h"
#endif

#include <curses.h>

#include <readline/readline.h>
#include <readline/history.h>

/* Use kqueue in preference to anything else.
 *  If we don't have that, try FAM
 *   If we don't have FAM, fall back to stat()
*/
#define USING_KQUEUE 1
#define USING_FAM    2
#define USING_STAT   3
#if HAVE_KQUEUE
# include <sys/event.h>
# define POLLING_METHOD USING_KQUEUE
#elif HAVE_FAM_H
# include <fam.h>
# define POLLING_METHOD USING_FAM
#endif

/* stat() fallback */
#ifndef POLLING_METHOD
# define POLLING_METHOD USING_STAT
#endif

#if HAVE_ADNS_H
# include <adns.h>
#endif


#define getMIN(a,b) (a < b ? a : b)
#define getMAX(a,b) (a > b ? a : b)

#ifndef MAXPATHLEN
# define MAXPATHLEN 128
#endif

/* upon startup, each input file is put into an element of this array,
 * starting at 0. The struct under this maps the current fd into this array
 * so we can find it without iterating.
*/
struct input {
	char *filename;
	int fd;
	ino_t inode;
	short type; /* types defined in log.h */
	time_t lastreq;

	/* if open == false, then ApacheTop will periodically try to re-open
	 * this input file. */
	bool open; /* is the file open or not? */

#if (POLLING_METHOD == USING_FAM)
	FAMRequest famreq;
#endif
};

#include "filters.h"

struct config {
	#define SORT_REQCOUNT 1
	#define SORT_REQPERSEC 2
	#define SORT_BYTECOUNT 3
	#define SORT_BYTESPERSEC 4
	#define SORT_2XX 12 /* see display.cc around line 911 (shellsort)  */
	#define SORT_3XX 13 /* for the consequences of changing these.     */
	#define SORT_4XX 14 /* Logic there relies on them being            */
	#define SORT_5XX 15 /* (retcode/100)+SORTTYPE_OFFSET_HACK          */
	#define SORTTYPE_OFFSET_HACK 10
	short sort, retcodes_sort;
	short refresh_delay;

	short do_resolving;

	short selected_item_screen; /* screen position our marker is at */
	unsigned int selected_item_pos; /* which url/ip/refpos it relates to */
	short selected_item_mode; /* is it url/ip/ref pos? */
	unsigned int selected_item_hash;

	short current_display_size; /* how many lines we're displaying */

	short input_count;

	#define TIMED_CIRCLE 'T'
	#define HITS_CIRCLE  'H'
	int circle_mode;
	int circle_size;

	/* these defines are used in cf.filter too */
	#define DISPLAY_URLS   1
	#define DISPLAY_HOSTS  2
	#define DISPLAY_REFS   3
	#define DISPLAY_FILES  4
	#define DISPLAY_DETAIL 9
	short display_mode;

	#define NUMBERS_HITS_BYTES  1
	#define NUMBERS_RETCODES    2
	short numbers_mode;

	/* when in DISPLAY_DETAIL mode, display_mode is copied in here */
	short display_mode_detail;
	/* what would we like to display in DISPLAY_DETAIL modes? */
	bool detail_display_hosts, detail_display_refs, detail_display_urls;

	bool display_paused;
	bool do_immed_display; /* signals a screen update is req'd */

	/* filters */
	Filter *urlfilter, *hostfilter, *reffilter;

	/* url munging */
	bool keep_querystring;
	bool lowercase_urls;
	unsigned short keep_segments;
	bool preserve_ref_protocol;

	bool debug;

	bool exit; /* true when we want to finish */

	/* keypress submenu */
	#define SUBMENU_NONE          0

	#define SUBMENU_SORT_HB       1
	#define SUBMENU_SORT_RC       2

	#define SUBMENU_DISP          4

	#define SUBMENU_FILT          5
	#define SUBMENU_FILT_ADD      6
	#define SUBMENU_FILT_SHOW     7

	#define SUBMENU_HELP          9
	unsigned short in_submenu;
	bool in_submenu_stay; /* stay in submenu till keypress? */
	time_t in_submenu_time;

};

struct hitinfo {
	double bytecount;
	double reqcount;
	time_t first, last;

	/* for stats worked out in display.cc */
	float rps, bps;
};

struct gstat {
	time_t start; /* when did we start */

	/* space for 1xx-5xx return codes */
	struct hitinfo r_codes[6];

	/* space for counting global hits and bytes per sec */
	struct hitinfo alltime;
};

//#include "opt.h"
#include "ohtbl.h"

#if HAVE_ADNS_H
# include "resolver.h"
#endif

#include "map.h"
#include "circle.h"
#include "hits_circle.h"
#include "timed_circle.h"
#include "display.h"
#include "log.h"
#include "queue.h"


#define JAN 281
#define FEB 269
#define MAR 288
#define APR 291
#define MAY 295
#define JUN 301
#define JUL 299
#define AUG 285
#define SEP 296
#define OCT 294
#define NOV 307
#define DEC 268

#define DEBUG_OUTPUT "/tmp/atop.debug"

/* this can be overridden from config.h via ./configure --with-logfile .. */
#ifndef DEFAULT_LOGFILE
# define DEFAULT_LOGFILE "/var/httpd/apache_log"
#endif
#define DEFAULT_CIRCLE_SIZE 30
#define DEFAULT_CIRCLE_MODE TIMED_CIRCLE
#define DEFAULT_SORT SORT_REQCOUNT
#define DEFAULT_RETCODES_SORT SORT_2XX
#define DEFAULT_REFRESH_DELAY 5
#define DEFAULT_DISPLAY_MODE DISPLAY_URLS
#define DEFAULT_NUMBERS_MODE NUMBERS_HITS_BYTES

/* if the layout of the display changes, these need updating */
#define COLS_RESERVED 25
#define LINES_RESERVED 7

#define MAX_INPUT_FILES 50

int recordstats(struct logbits l);

int read_key(int ch);

#define SEEK_TO_END true
#define NO_SEEK_TO_END false
int new_file(char *filename, bool do_seek_to_end);

void usage(void);
int dprintf(const char *fmt, ...);

static void catchsig(int s);
static void catchwinch(int s);

#endif