#!/usr/bin/perl
# IP id <49769> time <1215331823116174606> len <1344>
#

use POSIX qw(ceil floor);
use CGI ':standard';
use GD::Graph::lines;
use DBI;
use CGI;
use Switch;


$query = new CGI;
$run_id = $query->param('run_id');
$timeout = $query->param('timeout');

print "Content-type: text/html\n\n";

$host = "localhost";
$database = "iptvprobe";
$user = "root";
$pw = "";
$connectionInfo="dbi:mysql:$database;$host";

$dbh = DBI->connect($connectionInfo,$user,$pw);
$dbh2 = DBI->connect($connectionInfo,$user,$pw);

$sth = $dbh->prepare("SELECT MAX(create_date) - MIN(create_date) as runtime, from_unixtime(MIN(create_date)) as create_date_min, from_unixtime(MAX(create_date)) as create_date_max from data_igmp WHERE run_id=$run_id");
$sth->execute();

$rows = 0;
while (my $ref = $sth->fetchrow_hashref()) {
    $rows++;
    $create_date_min = $ref->{'create_date_min'};
    $create_date_max = $ref->{'create_date_max'};
    $runtime = $ref->{'runtime'};
};            
$sth->finish();

if($create_date_min == "NULL" && $create_date_max == "NULL"){
    print "No data for this run id: $run_id yet. Please retry";
    exit (0);
};


if($timeout eq ""){
    $timeout = 2;
};

# obtain run info
$sth = $dbh->prepare("select id, FROM_UNIXTIME(start_timestamp) as start_timestamp, zond_host, monitoring_host, collector_host, description FROM runs WHERE id=$run_id");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
    $start_timestamp = $ref->{'start_timestamp'};
    $zond_host = $ref->{'zond_host'};
    $collector_host = $ref->{'collector_host'};
    $monitoring_host = $ref->{'monitoring_host'};
    $description = $ref->{'description'};
};
$sth->finish();

print "<HTML><HEAD><META http-equiv=\"refresh\" content=\"$timeout\"><TITLE>NetUP IPTVProbe run:$run_id</HEAD>";

print "<table border=1>";
print "<tr><td>Probe run identification</td><td>$run_id ($zond_host, $description) </td></tr>";
print "<tr><td>Collector IP</td><td>$collector_host</td></tr>";
print "<tr><td>Zond IP</td><td>$zond_host</td></tr>";
print "<tr><td>Monitoring IP</td><td>$monitoring_host</td></tr>";
print "<tr><td>Start date</td><td>$create_date_min</td></tr>";
print "<tr><td>Last date</td><td>$create_date_max</td></tr>";
print "<tr><td>Working time (sec)</td><td>$runtime</td></tr>";
print "</table>";

print "<BR> <B>IGMP timings: </B>";
print "<table border=1>";
print ("<tr><td><b>id</b></td><td><b>receive datetime</b></td><td><b>igmp type</b></td><td><b>group_ip</b></td><td><b>leave delta (ms)</b></td><td><b>join delta (ms)</b></td><td><b>last IGMP delta (ms)</b></td></tr>");
$sth = $dbh->prepare("SELECT id, igmp_type, inet_ntoa(group_ip) as group_ip, leave_ts_delta/1000000 as leave_ts_delta, join_ts_delta/1000000 as join_ts_delta, last_igmp_message_ts/1000000 as last_igmp_message_ts, from_unixtime(create_date) as date, timestamp from data_igmp_timing WHERE run_id=$run_id ORDER BY id DESC LIMIT 3000");
$sth->execute();

$rows = 0;
while (my $ref = $sth->fetchrow_hashref()) {
    print "<tr>";
    $rows++;
    $id = $ref->{'id'};
    $igmp_type = $ref->{'igmp_type'};
    $group_ip = $ref->{'group_ip'};
    $leave_ts_delta = $ref->{'leave_ts_delta'};
    $join_ts_delta = $ref->{'join_ts_delta'};
    $last_igmp_message_ts = $ref->{'last_igmp_message_ts'};
    $date = $ref->{'date'};
    $timestamp = $ref->{'timestamp'};

    my $mts = 0;
    my $delta = 0;
    $type = "unknown";
    switch ( $igmp_type )
    {
        case 0x11 { $type = "<font color=\"blue\">IGMPv3: Membership Query</font>"};
        case 0x22 { $type = "IGMPv3: Membership Report"};
        case 0x12 { $type = "IGMPv1: Membership Report"};
        case 0x16 { $type = "IGMPv2: Membership Report"};
        case 0x17 { 
            $type = "<font color=\"red\">IGMPv2: Leave Group</font>";
        };
    }

    #print ("<td>$id</td><td>$type ($igmp_type)</td><td>$group_ip</td><td>$src_ip</td><td>$dst_ip</td><td>$date</td><td>$delta (mts=$mts timestamp=$timestamp)</td>");
    print ("<td>$id</td><td>$date</td><td>$type ($igmp_type)</td><td>$group_ip</td><td>$leave_ts_delta</td><td>$join_ts_delta</td><td>$last_igmp_message_ts</td>");
    print "</tr>";
};            
$sth->finish();
print "</table>";

print "</HTML>";
$dbh->disconnect();
$dbh2->disconnect();

#There are two IGMP message types of concern to the IGMPv3 protocol
#described in this document:
#Type Number (hex)   Message Name
#-----------------   ------------
#0x11          Membership Query
#0x22          Version 3 Membership Report
#An implementation of IGMPv3 MUST also support the following three
#message types, for interoperation with previous versions of IGMP (see
#section 7):
#0x12          Version 1 Membership Report    [RFC-1112]
#0x16          Version 2 Membership Report    [RFC-2236]
#0x17          Version 2 Leave Group          [RFC-2236]
