Skip to content

Commit

Permalink
Change rate statistics mode switch to -s
Browse files Browse the repository at this point in the history
Now -s is all it takes to enter rps stats mode. -t is still used
to set the display interval, but it has a sane default. There
are situations when it doesn't make sense to have to enter a
display interval, plus this makes it easier to start seeing data.
  • Loading branch information
jbittel committed Dec 2, 2011
1 parent fa4ffd6 commit 1185447
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
4 changes: 4 additions & 0 deletions config.h
Expand Up @@ -33,6 +33,10 @@
*** Can be overridden with -l */
#define DEFAULT_RATE_THRESHOLD 1

/* Default display interval for rate statistics
*** Can be overridden with -t */
#define DEFAULT_RATE_INTERVAL 5

/* Default location to store the PID file when running in daemon mode
*** Can be overridden with -P */
#define PID_FILENAME "/var/run/httpry.pid"
Expand Down
1 change: 1 addition & 0 deletions doc/ChangeLog
Expand Up @@ -10,6 +10,7 @@ version 0.1.7
* changed IPv6 parsing to follow extension headers if present
* changed rate statistics code to use a hash table data structure
* added a rps threshold option (-l) when in rate statistics mode
* changed rps display mode to -s, -t now just sets the display interval

version 0.1.6
* added IPv6 support
Expand Down
14 changes: 9 additions & 5 deletions doc/README
Expand Up @@ -74,7 +74,7 @@ device and output to the console with some sane defaults. The -h switch will
print out an abbreviated description of the available options to change the
defaults. This section describes these options in greater detail.

httpry [ -dFhpq ] [ -b file ] [ -f format ] [ -i device ] [-l threshold ]
httpry [ -dFhpqs ] [ -b file ] [ -f format ] [ -i device ] [-l threshold ]
[-m methods ] [ -n count ] [ -o file ] [ -P file ] [ -r file ]
[-t seconds] [ -u user ] [ 'expression' ]

Expand Down Expand Up @@ -106,8 +106,8 @@ first one found.

-l threshold
Specify a requests per second rate threshold value when running in rate
statistics mode. Only hosts with a rps value greater than this number will
be displayed. Defaults to 1.
statistics mode (-s). Only hosts with a rps value greater than this number
will be displayed. Defaults to 1.

-m methods
Provide a comma-delimited string that specifies the request methods to parse.
Expand Down Expand Up @@ -135,9 +135,13 @@ Suppress non-critical output (startup banner, statistics, etc.).
Provide an input capture file to read from instead of performing
a live capture. This option does not require root privileges.

-t seconds
-s
Run httpry in an HTTP request per second display mode. This periodically
displays the rate per active host and total rate at the specified interval.
displays the rate per active host and total rate at a specified interval.

-t seconds
Specify the host statistics display interval in seconds when running in
rate statistics mode (-s). Defaults to 5 seconds.

-u user
Specify an alternate user to take ownership of the process and any output
Expand Down
11 changes: 7 additions & 4 deletions httpry.1
Expand Up @@ -36,8 +36,8 @@ the program will poll the system for a list of interfaces and select the
first one found.
.IP "-l \fIthreshold\fP"
Specify a requests per second rate threshold value when running in rate
statistics mode. Only hosts with a rps value greater than this number will
be displayed. Defaults to 1.
statistics mode (-s). Only hosts with a rps value greater than this number
will be displayed. Defaults to 1.
.IP "-m \fImethods\fP"
Provide a comma-delimited string that specifies the request methods to parse.
The program defaults to parsing all of the standard RFC2616 method strings if
Expand All @@ -57,9 +57,12 @@ Suppress non-critical output (startup banner, statistics, etc.).
.IP "-r \fIfile\fP"
Provide an input capture file to read from instead of performing
a live capture. This option does not require root privileges.
.IP "-t \fIseconds\fP"
.IP "-s"
Run httpry in an HTTP request per second display mode. This periodically
displays the rate per active host and total rate at the specified interval.
displays the rate per active host and total rate at a specified interval.
.IP "-t \fIseconds\fP"
Specify the host statistics display interval in seconds when running in
rate statistics mode (-s). Defaults to 5 seconds.
.IP "-u \fIuser\fP"
Specify an alternate user to take ownership of the process and any output
files. You will need root privileges to do this; it will switch to the new
Expand Down
17 changes: 10 additions & 7 deletions httpry.c
Expand Up @@ -61,6 +61,7 @@ static char *format_str = NULL;
static char *methods_str = NULL;
static char *use_dumpfile = NULL;
static int rate_stats = 0;
static int rate_interval = DEFAULT_RATE_INTERVAL;
static int rate_threshold = DEFAULT_RATE_THRESHOLD;
static int force_flush = 0;
int quiet_mode = 0; /* Defined as extern in error.h */
Expand Down Expand Up @@ -527,7 +528,7 @@ void handle_signal(int sig) {
cleanup_rate_stats();
open_outfiles();
if (rate_stats)
init_rate_stats(rate_stats, use_infile, rate_threshold);
init_rate_stats(rate_interval, use_infile, rate_threshold);
return;
case SIGINT:
LOG_PRINT("Caught SIGINT, shutting down...");
Expand Down Expand Up @@ -610,7 +611,7 @@ void display_banner() {
void display_usage() {
display_banner();

printf("Usage: %s [ -dFhpq ] [-b file ] [ -f format ] [ -i device ] [ -l threshold ]\n"
printf("Usage: %s [ -dFhpqs ] [-b file ] [ -f format ] [ -i device ] [ -l threshold ]\n"
" [ -m methods ] [ -n count ] [ -o file ] [ -P file ] [ -r file ]\n"
" [ -t seconds] [ -u user ] [ 'expression' ]\n\n", PROG_NAME);

Expand All @@ -628,7 +629,8 @@ void display_usage() {
" -P file use custom PID filename when running in daemon mode \n"
" -q suppress non-critical output\n"
" -r file read packets from input file\n"
" -t seconds run in HTTP requests per second mode\n"
" -s run in HTTP requests per second mode\n"
" -t seconds specify the display interval for rate statistics\n"
" -u user set process owner\n"
" expression specify a bpf-style capture filter\n\n");

Expand All @@ -648,7 +650,7 @@ int main(int argc, char **argv) {
signal(SIGINT, &handle_signal);

/* Process command line arguments */
while ((opt = getopt(argc, argv, "b:df:Fhpqi:l:m:n:o:P:r:t:u:")) != -1) {
while ((opt = getopt(argc, argv, "b:df:Fhpqi:l:m:n:o:P:r:st:u:")) != -1) {
switch (opt) {
case 'b': use_dumpfile = optarg; break;
case 'd': daemon_mode = 1; use_syslog = 1; break;
Expand All @@ -664,7 +666,8 @@ int main(int argc, char **argv) {
case 'P': pid_filename = optarg; break;
case 'q': quiet_mode = 1; break;
case 'r': use_infile = optarg; break;
case 't': rate_stats = atoi(optarg); break;
case 's': rate_stats = 1; break;
case 't': rate_interval = atoi(optarg); break;
case 'u': new_user = optarg; break;
default: display_usage();
}
Expand All @@ -678,7 +681,7 @@ int main(int argc, char **argv) {
if (parse_count < 0)
LOG_DIE("Invalid -n value, must be 0 or greater");

if ((rate_stats != 0) && (rate_stats < 1))
if (rate_interval < 1)
LOG_DIE("Invalid -t value, must be 1 or greater");

if (rate_threshold < 0)
Expand Down Expand Up @@ -715,7 +718,7 @@ int main(int argc, char **argv) {
LOG_DIE("Cannot allocate memory for packet data buffer");

if (rate_stats)
init_rate_stats(rate_stats, use_infile, rate_threshold);
init_rate_stats(rate_interval, use_infile, rate_threshold);

start_time = time(0);
loop_status = pcap_loop(pcap_hnd, -1, &parse_http_packet, NULL);
Expand Down
14 changes: 7 additions & 7 deletions rate.c
Expand Up @@ -35,11 +35,11 @@ struct host_stats {

struct thread_args {
char *use_infile;
unsigned int display_interval;
unsigned int rate_interval;
int rate_threshold;
};

void create_rate_stats_thread(int display_interval, char *use_infile, int rate_threshold);
void create_rate_stats_thread(int rate_interval, char *use_infile, int rate_threshold);
void exit_rate_stats_thread();
void *run_stats(void *args);
struct host_stats *remove_node(struct host_stats *node, struct host_stats *prev);
Expand All @@ -57,7 +57,7 @@ static struct thread_args thread_args;

/* Initialize rate stats counters and structures, and
start up the stats thread if necessary */
void init_rate_stats(int display_interval, char *use_infile, int rate_threshold) {
void init_rate_stats(int rate_interval, char *use_infile, int rate_threshold) {
/* Initialize host totals */
totals.count = 0;
totals.first_packet = 0;
Expand All @@ -68,19 +68,19 @@ void init_rate_stats(int display_interval, char *use_infile, int rate_threshold)
LOG_DIE("Cannot allocate memory for host stats");

if (!use_infile)
create_rate_stats_thread(display_interval, use_infile, rate_threshold);
create_rate_stats_thread(rate_interval, use_infile, rate_threshold);

return;
}

/* Spawn a thread for updating and printing rate statistics */
void create_rate_stats_thread(int display_interval, char *use_infile, int rate_threshold) {
void create_rate_stats_thread(int rate_interval, char *use_infile, int rate_threshold) {
int s;

if (thread_created) return;

thread_args.use_infile = use_infile;
thread_args.display_interval = display_interval;
thread_args.rate_interval = rate_interval;
thread_args.rate_threshold = rate_threshold;

s = pthread_mutex_init(&stats_lock, NULL);
Expand Down Expand Up @@ -154,7 +154,7 @@ void *run_stats (void *args) {
struct thread_args *thread_args = (struct thread_args *) args;

while (1) {
sleep(thread_args->display_interval);
sleep(thread_args->rate_interval);
display_rate_stats(thread_args->use_infile, thread_args->rate_threshold);
}

Expand Down

0 comments on commit 1185447

Please sign in to comment.