qstream-sipp-check 8.32 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 Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
11
use DateTime;
Olav Kvittem's avatar
Olav Kvittem committed
12 13
require "newgetopt.pl";

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

my $usage="Usage $0 [-debug] [-die mins] [-remote] [-f]
-bb <hostname> - XYMON/BB host to receive reports
18
-xml  - leave an xml-file for each flow in the current directory
Olav Kvittem's avatar
Olav Kvittem committed
19 20 21 22
-remote - this is remote sipp (not used)
-f - wait for results like tail -f
-die <minutes> - end after <minutes>
";
23

Olav Kvittem's avatar
Olav Kvittem committed
24 25 26 27 28 29 30 31 32
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
33 34
our $tz=DateTime::TimeZone->new( name => 'local' );

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

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

39
my $at_end_of_file=0; # is at end of TASKLOG
Olav Kvittem's avatar
Olav Kvittem committed
40 41 42 43 44 45 46 47 48 49 50
my $endooflife=0;

if ($opt_die){
    $endoflife=time+$opt_die*60;
}

my $timeout= $opt_timeout || 1; # wait n seconds for file too be updated

foreach $tasklog(@ARGV){
    
    open TASKLOG, "<$tasklog" || die "Could not open $tasklog : $!";
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#    my $flags;
#    fcntl(TASKLOG, F_GETFL, $flags) || die $!; # Get the current flags on the filehandle
#    $flags |= O_NONBLOCK; # Add non-blocking to the flags
#    fcntl(TASKLOG, F_SETFL, $flags) || die $!; # Set the flags on the filehandle
#    $rin = $win = $ein = '';
#    vec($rin,fileno(TASKLOG),1) = 1;
 #   $ein = $rin | $win;

#    while( $nfound=select($rout=$rin, $wout=$win, $eout=$ein, $timeout) ){ # read task.log
    while (1){

	if ($line=<TASKLOG>){
	    &process_line($line);
	} else {
	    last if !$opt_f;
66
	    $at_end_of_file=1 if ! $at_end_of_file; # set true at first eof
67 68
	    sleep $timeout;
	}
69

70
	exit(1) if $opt_die && (time > $endoflife); 
Olav Kvittem's avatar
Olav Kvittem committed
71 72


73

74 75 76 77 78 79
#	} elsif (!eof(TASKLOG)) { # line ready
#	} elsif ($opt_die){
#	    last if (time > $endoflife);
#        } else { # probably eof
#	    last if ! $opt_f;  
#	}
80 81 82
    }
    close TASKLOG;
}
Olav Kvittem's avatar
Olav Kvittem committed
83

84 85 86 87 88 89 90 91 92 93 94 95

exit(0);


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

sub process_line {
    my $line=shift;
    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);
    
Olav Kvittem's avatar
Olav Kvittem committed
96
#	sleep 300; #  minutes to be sure that the forked processes are dead
97 98 99 100 101 102 103 104 105 106 107 108 109 110
    # active=1 test $active -gt 0
    until (system("ps -p $pid > /dev/null")){  # wait until process finished
	sleep $timeout;
    }
    
    $src=$id; $src=~ s/.*@//;
    $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);
	$machine= gethostbyaddr($iaddr, AF_INET);
    }
    $bb_machine=~s/\./,/g;
Olav Kvittem's avatar
Olav Kvittem committed
111

112 113
#
# checking call success
114 115 116
    my $log='';
    our $stime=$time;  # default time from tasklog, but prefer from call-log csv
    my $sdate=$date; 
117 118 119 120
 #   my $xml_out="$machine-$function-${stime}_${pid}_sipp.xml" if ($opt_xml);
    my $rtp_xml="$machine-$function-${stime}_${pid}_rtp.xml";
    my $call_xml="$machine-$function-${stime}_${pid}_call.xml";

121 122 123 124 125

    if ($opt_remote){
	if ($n > 0){
	    $status=100;
	    $message="Call received successfully";
126
	} else {
127 128 129 130 131 132
	    $status=0;
	    $message="No calls received\n";
	}
	&bb_report("C-$function", $bb_machine, &bb_color($call_limit,$bb_color,$status), $message);
    } else {

133
	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|");
134 135 136
	while(<CSV>){
	    $log.=$_;
	    if (/^[\d-]+[\d:]+\s+\d+/){  # date time unixtime
137 138
		($sdate,$etime,$unixtime,$elapsed, $callsout, $success, $failed, $retrans, $response_t, $response_t_sd, 
		 $call_lth, $call_lth_sd)=split;
139
		if ($success>0){
140
		    my $csec=to_sec($call_lth);
141
		    my $dt=DateTime->from_epoch(epoch => $unixtime, time_zone => $tz);
142 143
# elapsed time is from start of sipp ..
		    $dt->subtract(seconds => $csec);  
144 145 146 147 148 149 150 151 152
		    $stime=$dt->hms;
		    $sdate=$dt->ymd;
		    
		    $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";
153
		}
154
		&bb_report("C-$function", $bb_machine, &bb_color($call_limit,$bb_color,$status), $message);
155 156 157 158 159 160 161

		open XML, ">$call_xml" || die "Could not open $call_xml";
		my $sectime=$etime;
		$sectime=~s/\:\d+$//;  # remove ms
	        print XML &call_xml("$id:$function", $machine_BB, $date, $stime,  $callsout, $success, $failed, $retrans, &to_sec($response_t), &to_sec($call_lth));
		close XML;

162 163
	    }
	}
164 165
	close CSV;
    }
166 167 168 169

#
# checking call rtp quality

170
    my $mossum=0, $n=0;
171
    my @f=`ls rtp_*_${pid}_*.pcap* 2>/dev/null`; 
172 173 174 175 176 177
    my $qstream_rep='';

    if($#f >=0){ # yess some files
	chomp(@f);
	my $rtp_files= join(' ', @f);

178
	$cmd="/usr/bin/qstream -v -name '${id}:$function' -rtp -rtt  '$rtt' -xml $rtp_xml $rtp_files";
179 180 181 182 183 184 185
	my $T=`date +%T`; chomp($T);
	$qstream_rep=sprintf "%s $pid $port $mport $service@$server   $function $machine ping",$T;
	open QSTREAM, "$cmd|";
	while(<QSTREAM>){ $qstream_rep.=$_}
	close QSTREAM;

	# system($cmd); # || die "command failed : $cmd : $!";
186
	$xml = XMLin($rtp_xml) || die "Could not analyze XML $rtp_xml : $!";
187 188 189 190 191 192 193 194 195
	print Dumper($xml) if $opt_dump;
	unlink $tmp if -f $tmp;

	
	my @flow=();
	if (ref($xml->{flow}) eq "ARRAY"){
	    @flow= @{$xml->{flow}};
	} else { # just one
	    @flow=($xml->{flow});
Olav Kvittem's avatar
Olav Kvittem committed
196
	}
197 198 199 200 201 202
	$min_mos=10;
	foreach $flow (@flow){
	    my $mos= $flow->{network_stats}->{MOS};
	    $mossum += $mos;
	    $n++;      
	    $min_mos=$mos if  ($mos < $min_mos) ;
Olav Kvittem's avatar
Olav Kvittem committed
203
	}
204 205 206 207 208
    }
    if ($n >0){
	my $mean_mos=$mossum/$n;
	&bb_report("M-$function", $machine, &bb_color($mos_limit,$bb_color,$min_mos), 
		   sprintf "$sdate $stime - %.1f \n%s\n",$min_mos,$qstream_rep);
Olav Kvittem's avatar
Olav Kvittem committed
209
	
210 211
    } else {
	&bb_report("M-$function", $machine, &bb_color($mos_limit,$bb_color,0), "No RTP files");
Olav Kvittem's avatar
Olav Kvittem committed
212
    }
213
    
214 215 216 217 218 219 220 221 222 223 224 225 226 227
    system "ls -l  $rtp_xml  $call_xml" if $opt_debug;

## do not merge because there could be calls without rtp
    if ( 0 && $opt_xml){  
	if ( -s $call_xml){
	    if (-s $rtp_xml ){
		system "sipmerge.php  $rtp_xml  $call_xml > $xml_out";
	    } else {
		rename $call_xml, $xml_out;
	    }
	}		
    }    
 #   unlink $rtp_xml if -f $rtp_xml;
 #   unlink $call_xml if -f $call_xml;
Olav Kvittem's avatar
Olav Kvittem committed
228 229 230
}


231
#  /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
232

233
#--------------------------------------------------------------------------------
Olav Kvittem's avatar
Olav Kvittem committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247

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

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

}

sub bb_report {
248
    
249
    return 0 if $opt_f && ! $at_end_of_file;
Olav Kvittem's avatar
Olav Kvittem committed
250 251 252 253 254
    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");
255
    system(@cmd) if $opt_bb; # || warn "Command failed : @cmd : $!" ;
Olav Kvittem's avatar
Olav Kvittem committed
256 257 258 259 260
    print $cmd, "\n" if $opt_v;

}


261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
# hh:mm:ss to seconds

sub to_sec {

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


}


# output xml line

sub call_xml {
    my ($id, $bb_machine, $date, $etime, $callsout, $success, $failed, $retrans, $response_t, $call_lth) = @_;

    return "<?xml version=\"1.0\"?>
<qstream version=\"1.0\">
  <flow>
    <flow_name>$id</flow_name>
    <date>$date</date>
    <time>$etime</time>
    <call_stats>
      <calls_out>$callsout</calls_out>
      <calls_ok> $success</calls_ok>
      <calls_fail>$failed</calls_fail>
      <calls_retrans>$retrans</calls_retrans>
      <response_time>$response_t</response_time>
      <call_secs>$call_lth</call_secs>
    </call_stats>
  </flow>
</qstream>
";
   
}