qstream-sipp-check 6.25 KB
Newer Older
Olav Kvittem's avatar
Olav Kvittem committed
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl
#
# process sip log files from sipp-responder and sipp-test and report to Xymon
# the task.log file gives meta-info

use XML::Simple;
use Data::Dumper;
use Socket;
use IO::Select;
10
use DateTime;
Olav Kvittem's avatar
Olav Kvittem committed
11 12
require "newgetopt.pl";

13
@opts=( 'f', 'die=s', 'timeout=s', 'bb=s', 'xml', 'remote', 'debug', 'dump', 'v', 'h', 'help' );
Olav Kvittem's avatar
Olav Kvittem committed
14 15 16

my $usage="Usage $0 [-debug] [-die mins] [-remote] [-f]
-bb <hostname> - XYMON/BB host to receive reports
17
-xml  - leave an xml-file for each flow in the current directory
Olav Kvittem's avatar
Olav Kvittem committed
18 19 20 21 22 23 24 25 26 27 28 29 30
-remote - this is remote sipp (not used)
-f - wait for results like tail -f
-die <minutes> - end after <minutes>
";
my $rc=&NGetOpt(@opts);
die $usage if not $rc or $opt_h or $opt_help;

my $bb_host = $opt_bb   || `hostname -f`; 

$mos_limit=[0,3.2,3.9]; # mos values
$bb_color=["red", "yellow", "green"];
$call_limit=[0,70,90]; # call succes in percent

Olav Kvittem's avatar
Olav Kvittem committed
31 32
our $tz=DateTime::TimeZone->new( name => 'local' );

Olav Kvittem's avatar
Olav Kvittem committed
33 34 35 36 37 38
# check results from sipp-initated calls with tcpdumps in sipp-test

$tmp="/tmp/qstream-sipp-$$.xml";

my $endooflife=0;

39

Olav Kvittem's avatar
Olav Kvittem committed
40 41
if ($opt_die){
    $endoflife=time+$opt_die*60;
42 43 44 45 46
    $SIG{ALRM} = sub { 
	printf "Ended after $opt_die minutes\n";
	exit(0);
    };
    alarm($opt_die*60+3); # allow for 3 seconds to die voluntary
Olav Kvittem's avatar
Olav Kvittem committed
47 48 49
}


50

Olav Kvittem's avatar
Olav Kvittem committed
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
my $timeout= $opt_timeout || 1; # wait n seconds for file too be updated

foreach $tasklog(@ARGV){
    
    open TASKLOG, "<$tasklog" || die "Could not open $tasklog : $!";

    while( 1 ){ # read task.log

#	while(eof(TASKLOG)){  # wait for next line to file
#	    exit(1) if $opt_die && (time > $endoflife); 
#	    last if !$opt_f;
#	    sleep $timeout;
#	    next; # wait for more data
#	}
	
	exit(1) if $opt_die && (time > $endoflife); 

	if (! ($line=<TASKLOG>)){   # could not read the line
	    exit(1) if $opt_die && (time > $endoflife); 
	    last if !$opt_f;
            sleep $timeout;  
            next;	
        }
	next if $line !~ /[\d-]+ [\d:]+ \d+ \d+/; # invalid line/empty ..Format : date time port port 

	my ($date, $time, $pid, $sip_port, $rtp_port, $id, $function, $machine, $rtt)=split(/\s+/, $line);
	
#	sleep 300; #  minutes to be sure that the forked processes are dead
	# active=1 test $active -gt 0
	until (system("ps -p $pid > /dev/null")){  # wait until process finished
	    sleep $timeout;
	}
	
	$src=$id; $src=~ s/.*@//;
85 86 87 88 89
	$bb_machine=$machine;
	$bb_machine=$src if $bb_machine eq '';
	$bb_machine=~s/:\d+$//;
	if ($bb_machine=~ /^\d+\.\d+/){ # lookup hostname
	    my $iaddr=inet_aton($bb_machine);
Olav Kvittem's avatar
Olav Kvittem committed
90 91
	    $machine= gethostbyaddr($iaddr, AF_INET);
	}
92
	$bb_machine=~s/\./,/g;
Olav Kvittem's avatar
Olav Kvittem committed
93

94 95 96
#
# checking call success
	my $log='';
Olav Kvittem's avatar
Olav Kvittem committed
97
	our $stime=$time;  # default time from tasklog, but prefer from call-log csv
98 99 100 101 102 103 104 105 106 107
	my $sdate=$date; 

	if ($opt_remote){
	    if ($n > 0){
		$status=100;
		$message="Call received successfully";
	    } else {
		$status=0;
		$message="No calls received\n";
	    }
108
	    &bb_report("C-$function", $bb_machine, &bb_color($call_limit,$bb_color,$status), $message);
109 110 111 112 113
	} else {

	    open (CSV, "/usr/bin/sipp-print  -f 'CurrentTime|ElapsedTime(C)|OutgoingCall(C)|SuccessfulCall(C)|FailedCall(C)|Retransmissions(C)|ResponseTime1(C)|ResponseTime1StDev(C)|CallLength(C)|CallLengthStDev(C)'  *${pid}_.csv|");
	    while(<CSV>){
		$log.=$_;
Olav Kvittem's avatar
Olav Kvittem committed
114
		if (/^[\d-]+[\d:]+\s+\d+/){  # date time unixtime
115
		    ($sdate,$etime,$unixtime,$elapsed, $callsout, $success, $failed, $rest)=split;
116
		    if ($success>0){
117 118 119 120 121 122
			my ($hh, $mm, $ss)=split(/:/, $elapsed);
			my $esec=3600*$hh+60*$mm+$ss;
			my $dt=DateTime->from_epoch(epoch => $unixtime, time_zone => $tz);
#			$dt->subtract(seconds => $esec);
			$stime=$dt->hms;
			$sdate=$dt->ymd;
123 124 125 126 127 128 129 130
			
			$calls=$success+$failed;
			$status=100*$success/$calls; # success rate in %
			$message=sprintf "$sdate $stime - %d percent success (%d/%d)\n%s",$status,$success,$calls,$log;
		    } else {
			$status=0;
			$message="No successfull calls :\n $log";
		    }
131
		    &bb_report("C-$function", $bb_machine, &bb_color($call_limit,$bb_color,$status), $message);
132 133 134 135 136 137 138 139
		}
	    }
	    close CSV;
	}

#
# checking call rtp quality

Olav Kvittem's avatar
Olav Kvittem committed
140 141 142 143 144 145 146
	my $mossum=0, $n=0;
	my @f=`ls rtp_*${pid}_*.pcap* 2>/dev/null`; 
	my $qstream_rep='';

	if($#f >=0){ # yess some files
	    chomp(@f);
	    my $rtp_files= join(' ', @f);
147 148 149 150
	    my $xml_out=$tmp;	    
	    $xml_out="$machine-$function-${stime}_$pid.xml" if ($opt_xml);

	    $cmd="/usr/bin/qstream -v -name '${id}:$function' -rtp -rtt  '$rtt' -xml $xml_out $rtp_files";
151 152
	    my $T=`date +%T`; chomp($T);
	    $qstream_rep=sprintf "%s $pid $port $mport $service@$server   $function $machine ping",$T;
Olav Kvittem's avatar
Olav Kvittem committed
153 154 155 156 157
	    open QSTREAM, "$cmd|";
	    while(<QSTREAM>){ $qstream_rep.=$_}
	    close QSTREAM;

	    # system($cmd); # || die "command failed : $cmd : $!";
158
	    $xml = XMLin($xml_out) || die "Could not analyze XML $xml_out : $!";
Olav Kvittem's avatar
Olav Kvittem committed
159
	    print Dumper($xml) if $opt_dump;
160
	    unlink $tmp if -f $tmp;
161

Olav Kvittem's avatar
Olav Kvittem committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	    
	    my @flow=();
	    if (ref($xml->{flow}) eq "ARRAY"){
		@flow= @{$xml->{flow}};
	    } else { # just one
		@flow=($xml->{flow});
	    }
	    $min_mos=10;
	    foreach $flow (@flow){
		my $mos= $flow->{network_stats}->{MOS};
		$mossum += $mos;
		$n++;      
		$min_mos=$mos if  ($mos < $min_mos) ;
	    }
	}
	if ($n >0){
	    my $mean_mos=$mossum/$n;
	    &bb_report("M-$function", $machine, &bb_color($mos_limit,$bb_color,$min_mos), 
180
		       sprintf "$sdate $stime - %.1f \n%s\n",$min_mos,$qstream_rep);
Olav Kvittem's avatar
Olav Kvittem committed
181 182 183 184 185 186 187 188 189 190 191
	    
	} else {
	    &bb_report("M-$function", $machine, &bb_color($mos_limit,$bb_color,0), "No RTP files");
	}
	
		   
	
    }
    close TASKLOG;
}

Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
192
#  /usr/bin/sipp-print -id $src -q -f 'OutgoingCall(C)|SuccessfulCall(C)|FailedCall(C)|Retransmissions(C)|ResponseTime1(C)|ResponseTime1StDev(C)|CallLength(C)|CallLengthStDev(C)'  *${pid}_.csv
Olav Kvittem's avatar
Olav Kvittem committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210



exit(0);

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

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

}

sub bb_report {
211 212 213
    
    return 0 if ! $opt_bb;

Olav Kvittem's avatar
Olav Kvittem committed
214 215 216 217 218 219 220 221 222 223 224
    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";
    my $cmd="/usr/lib/hobbit/client/bin/bb $bb_host 'status $machine.$bbtest $color $msg\n'";
#    system($cmd)|| warn "Command failed : $cmd : $!";
    my @cmd=("/usr/lib/hobbit/client/bin/bb", "$bb_host", "status $machine.$bbtest $color $msg\n");
    system(@cmd); # || warn "Command failed : $cmd : $!";
    print $cmd, "\n" if $opt_v;

}