Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 143 lines (114 sloc) 3.6 KB
#! /opt/kronometrix/perl/bin/perl
use lib './lib';
use Kronometrix;
use Kronometrix::Svcrec;
use POSIX qw(pause);
use Time::HiRes qw(time alarm setitimer ITIMER_REAL);
use Getopt::Std;
use File::Basename;
use strict;
use warnings;
use feature ':5.20';
our $VERSION = '1.2.4';
my $LASTREV = '2017-09-20';
### Configuration variables
my $cfile = 'svcrec.json'; # ping configuration file
my $kfile = 'kronometrix.json'; # general configuration file
my $recname = basename($0);
my $timeout = 30;
### Process command line args
my %opts;
$Getopt::Std::STANDARD_HELP_VERSION = 1;
getopts('ahlvVdg:s:', \%opts) or HELP_MESSAGE();
if ($opts{h}) {
HELP_MESSAGE();
}
if ($opts{V}) {
VERSION_MESSAGE();
exit 0;
}
# verbose and debug flags
my $verbose = exists $opts{v} || exists $opts{d} || 0;
my $debug = exists $opts{d} || 0;
# Time between requests
print STDERR "-s switch in invalid in async mode\n" and HELP_MESSAGE()
if exists $opts{a} && exists $opts{s};
my $nap_time = exists $opts{s} ? $opts{s} : 0.1;
# Maximum number of concurrent requests
my $num_concurrent_reqs = exists $opts{g} ? $opts{g} : 25;
$num_concurrent_reqs = 1 unless exists $opts{a};
# logging option
Kronometrix->log_to_file($kfile, $0) if $opts{l};
# process [interval [count]]
my ( $interval, $loop_max );
my $precision = 0;
if ( defined $ARGV[0] ) {
$interval = $ARGV[0];
$loop_max = defined $ARGV[1] ? $ARGV[1] : 2**32;
$precision = 3 if $interval =~ /\./;
HELP_MESSAGE() if $interval == 0;
}
else {
$interval = 1;
$loop_max = 1;
}
### Parse config file
my $svcrec = Kronometrix::Svcrec->new(
config_file => $cfile,
max_concurrent_requests => $num_concurrent_reqs,
timeout => 5,
precision => $precision,
nap_time => $nap_time,
verbose => $verbose,
debug => $debug,
);
$svcrec->warnings_to_log;
$svcrec->write_verbose(
"INFO: Interval: $interval - Iterations: $loop_max "
. "- Num of concurrent requests: $num_concurrent_reqs "
. "- Total workload requests: " . $svcrec->{total}
);
### Main loop
# Set timer between queue executions
local $SIG{ALRM} = sub {};
local $| = 1;
setitimer(ITIMER_REAL, $interval, $interval);
while ($loop_max) {
$svcrec->process;
$loop_max--;
$svcrec->write_verbose("INFO: Remaining iterations: $loop_max");
pause if $loop_max;
}
Kronometrix->write_log("INFO: Iterations finished");
### Auxiliary subroutines
# Usage - print usage and exit.
sub HELP_MESSAGE {
print STDERR <<END;
USAGE: svcrec [-dhlvV] [-s secs] [-a [-g concurrent]] | [interval [count]]
OPTIONS:
-a : perform requests asynchronously
-g : the number of concurrent requests. Default is 25.
-s : sleep delay between transactions, default 0.1
-l : log raw data to krd file, no STDOUT
-h : help information
-v : verbose information
-d : debug information
-V : release version
interval : maximum number of seconds between samples
count : number of times to repeat the request cycle. Defaults to 2**32
FIELDS:
#01 timestamp : UNIX time, number
#02 name : service name, string
#03 duration : paket return time in ms, number
#04 siteid : the site-id for request
#05 zone : location, can be ICAO code, geo lat/lon, address
#06 inventory : service inventory description, string
#07 status : service status 1(active) and 0(not active)
NOTES:
END
exit 0;
}
# Revision - print revision and exit
sub VERSION_MESSAGE {
print STDERR "$recname: $VERSION, $LASTREV\n";
}