qstream-check-bb 2.96 KB
Newer Older
oak's avatar
oak committed
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/perl
#
# process qstream log files  and report to Xymon

use XML::Simple;
use Data::Dumper;
# use Socket;
# use IO::Select;
# use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
# use DateTime;
Olav Kvittem's avatar
Olav Kvittem committed
11 12
use Getopt::Long;
@opts=( 'bb=s' => \$opt_bb, 'debug' => \$opt_debug, 'dump' => \$opt_dump, 'v' => \$opt_v, 'h' => \$opt_h, 'help' => \$opt_help);
oak's avatar
oak committed
13 14 15 16 17
my $usage="Usage $0 [-debug] [-v] [-h]
-bb <hostname> - XYMON/BB host to receive reports

";

Olav Kvittem's avatar
Olav Kvittem committed
18
my $rc=GetOptions(@opts);
oak's avatar
oak committed
19 20 21
die $usage if not $rc or $opt_h or $opt_help;

my $bb_host = $opt_bb   || `hostname -f`; 
22 23 24
chomp($bb_host);
my $report_host = `hostname -s`;
chomp($report_host);
oak's avatar
oak committed
25 26 27

$span_limit=[0,1,9]; # sec
$bw_limit=[0,2000000, 30000]; # bps
Olav Kvittem's avatar
Olav Kvittem committed
28
$jitter_limit=[0, 2, 5 ]; # jitter_factor
oak's avatar
oak committed
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
$loss_limit=[0, 0.1, 1 ]; # %

$bb_color=["red", "yellow", "green"];
@r_color= reverse(@$bb_color);
$rev_color= \@r_color;
$call_limit=[0,70,90]; # call succes in percent

# our $tz=DateTime::TimeZone->new( name => 'local' );

foreach $flow_file(@ARGV){
    process_flow($flow_file);
}
    


exit(0);


#--------------------------------------------------------------------------------

sub process_flow {
    my $file=shift;
    my $xml = XMLin($file, ForceArray => 0 );
    
    if ( $xml){ # successful parsing
	foreach $flow ( $xml->{flow}){
Olav Kvittem's avatar
Olav Kvittem committed
55 56 57 58
	    my $jratio=0;
	    if ($flow->{network_stats}->{gap_avg} > 0 ){
		my $jratio= $flow->{pcr_jitter}->{pcr_jitter_avg} / $flow->{network_stats}->{gap_avg};
	    }
59
	    &bb_report("Response", $flow->{flow_name}, bb_color($span_limit, $bb_color, $flow->{span}), (sprintf "%.1f",$flow->{span}).'sec' );
Olav Kvittem's avatar
Olav Kvittem committed
60
	    &bb_report("Jitter", $flow->{flow_name}, bb_color($jitter_limit, $rev_color,$jratio), 
61
		        (sprintf "%.1f",$flow->{pcr_jitter}->{pcr_jitter_avg}).'ms' );
oak's avatar
oak committed
62 63 64 65
	    my $loss_rate=0;
	    if ( $flow->{transport}->{mpeg_packets} > 0){
	        $loss_rate=100 * $flow->{transport}->{mpeg_lost_packets} / $flow->{transport}->{mpeg_packets};
	    }
66
	    &bb_report("Loss", $flow->{flow_name}, bb_color($loss_limit, $rev_color, $loss_rate ), $loss_rate."%" );
oak's avatar
oak committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	}
    }
}

#--------------------------------------------------------------------------------

sub bb_color {
    my ($limit, $color, $value)=@_;

    for ($i=$#$limit;$i>=$[;$i--){
	if ( $value >= @$limit[$i]){ 
	    return @$color[$i];
	}
	
    }

}

sub bb_report {
    
    return 0 if $opt_f && ! $at_end_of_file;
    my ($bbtest, $machine, $color, $msg)=@_;
#    my $cmd="/usr/lib/hobbit/client/bin/bb $bb_host 'status $machine.$bbtest $color $msg\n'>>$ENV{HOME}/cmd.log 2>&1";
90
    my $cmd="/usr/lib/hobbit/client/bin/bb $bb_host 'status+35/group:$report_host $machine.$bbtest $color $msg\n'";
oak's avatar
oak committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#    system($cmd)|| warn "Command failed : $cmd : $!";
    my @cmd=("/usr/lib/hobbit/client/bin/bb", "$bb_host", "status $machine.$bbtest $color $msg\n");
    system(@cmd) if $opt_bb; # || warn "Command failed : @cmd : $!" ;
    print $cmd, "\n" if $opt_v;

}


# hh:mm:ss to seconds

sub to_sec {

    my ($hh, $mm, $ss,$ms)=split(/:/, shift);
    return (3600*$hh+60*$mm+$ss+$ms/1000);


}