Verified Commit 9df95d11 authored by Florian Pritz's avatar Florian Pritz
Browse files

nginx-zabbix.pl: Extract updater code into method



Signed-off-by: Florian Pritz's avatarFlorian Pritz <bluewind@xinu.at>
parent f5ee7a08
...@@ -85,26 +85,7 @@ sub main { ...@@ -85,26 +85,7 @@ sub main {
$line = trim($line); $line = trim($line);
if ($line =~ m/(?<remote_addr>\S+) (?<host>\S+) (?<remote_user>\S+) \[(?<time_local>.*?)\]\s+"(?<request>.*?)" (?<status>\S+) (?<body_bytes_sent>\S+) "(?<http_referer>.*?)" "(?<http_user_agent>.*?)" "(?<http_x_forwarded_for>\S+)"(?: (?<request_time>[\d\.]+|-))?/) { if ($line =~ m/(?<remote_addr>\S+) (?<host>\S+) (?<remote_user>\S+) \[(?<time_local>.*?)\]\s+"(?<request>.*?)" (?<status>\S+) (?<body_bytes_sent>\S+) "(?<http_referer>.*?)" "(?<http_user_agent>.*?)" "(?<http_x_forwarded_for>\S+)"(?: (?<request_time>[\d\.]+|-))?/) {
my %val = %+; update_stats_for_line($values_per_host, $stat_per_host, $value_template, \$modified_hostlist, \%+);
my $host = $val{host};
next unless $host =~ m/$hostname_regex/;
if (not defined $values_per_host->{$host}) {
$values_per_host->{$host} = clone($value_template);
$stat_per_host->{$host} = Statistics::Descriptive::Full->new();
$modified_hostlist = 1;
}
my $stat = $stat_per_host->{$host};
my $values = $values_per_host->{$host};
$stat->add_data($val{request_time}) if $val{request_time} != 0;
$values->{request_count}++;
$values->{cached_request_count}++ if $val{request_time} == 0;
my $status_key = defined $values->{status}->{$val{status}} ? $val{status} : "other";
$values->{status}->{$status_key}++;
} }
my $now = time; my $now = time;
...@@ -137,4 +118,29 @@ sub main { ...@@ -137,4 +118,29 @@ sub main {
} }
} }
sub update_stats_for_line {
my ($values_per_host, $stat_per_host, $value_template, $modified_hostlist, $line_values) = @_;
my %val = %$line_values;
my $host = $val{host};
return unless $host =~ m/$hostname_regex/;
if (not defined $values_per_host->{$host}) {
$values_per_host->{$host} = clone($value_template);
$stat_per_host->{$host} = Statistics::Descriptive::Full->new();
$$modified_hostlist = 1;
}
my $stat = $stat_per_host->{$host};
my $values = $values_per_host->{$host};
$stat->add_data($val{request_time}) if $val{request_time} != 0;
$values->{request_count}++;
$values->{cached_request_count}++ if $val{request_time} == 0;
my $status_key = defined $values->{status}->{$val{status}} ? $val{status} : "other";
$values->{status}->{$status_key}++;
}
main(); main();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment