#!/usr/bin/perl

#****************************************************************************
#*
#* Copyright (C) 2008 NetUP Inc. All rights reserved.
#*
#* This file is part of the netup iptvprobe project
#*
#* This file may be used under the terms of the GNU General Public
#* License versions 2.0 or 3.0 as published by the Free Software
#* Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
#* included in the packaging of this file.  Alternatively you may (at
#* your option) use any later version of the GNU General Public
#* License if such license has been publicly approved by NetUP Inc.
#* (or its successors, if any).
#*
#* This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
#* INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
#* A PARTICULAR PURPOSE. NetUP Inc. reserves all rights not expressly
#* granted herein.
#*
#* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
#* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#*
#***************************************************************************/

# IP id <49769> time <1215331823116174606> len <1344>
#

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

$query = new CGI;

print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<HEAD><TITLE>NetUP IPTVProbe runs</TITLE></HEAD>\n";


$host = "localhost";
$database = "iptvprobe";
$user = "iptvprobe";
$pw = "tv1000";
$connectionInfo="dbi:mysql:$database;$host";
$dbh = DBI->connect($connectionInfo,$user,$pw);

$action = $query->param('action');
if($action eq "add_run"){
    my $zond_host = $query->param('zond_host');
    my $collector_host = $query->param('collector_host');
    my $monitoring_host = $query->param('monitoring_host');
    my $description = $query->param('description');

    #parse ip ( monitoring_host )
    my @ips = split(',', $monitoring_host);
    foreach my $val (@ips) {
        $val =~ s/^\s+//;
        $val =~ s/\s+$//;
        print "ip=$val\n";
    }

    $dbh->do('INSERT INTO runs (start_timestamp, collector_host, zond_host, monitoring_host, description) VALUES(unix_timestamp(NOW()),?,?,?,?)', undef, $collector_host, $zond_host, $monitoring_host, $description);
    print "<HEAD><meta http-equiv=\"REFRESH\" content=\"0;url=/cgi-bin/iptvprobe_runs.pl\"></HEAD>";
    exit (0);
}elsif($action eq "delete"){
    my $run_id4delete = $query->param('run_id4delete');
    $dbh->do("DELETE FROM runs WHERE id=$run_id4delete");
    print "<HEAD><meta http-equiv=\"REFRESH\" content=\"0;url=/cgi-bin/iptvprobe_runs.pl\"></HEAD>";
    exit (0);
}elsif($action eq "clear"){
    my $run_id4clear = $query->param('run_id4clear');
    $dbh->do("DELETE FROM data_ip WHERE run_id=$run_id4clear");
    $dbh->do("DELETE FROM data_ts WHERE run_id=$run_id4clear");
    $dbh->do("DELETE FROM stat_bandwidth WHERE run_id=$run_id4clear");
    print "<HEAD><meta http-equiv=\"REFRESH\" content=\"0;url=/cgi-bin/iptvprobe_runs.pl\"></HEAD>";
    exit (0);
};

%pkts_count = ();
%ldate = ();

# obtain statistics
$sth = $dbh->prepare("select run_id, SUM(ip_pkts_count) as pkts_count, from_unixtime(MIN(create_date)), from_unixtime(MAX(create_date)) as last_date from stat_bandwidth GROUP BY run_id");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
    my $run_id = $ref->{'run_id'};
    my $last_date = $ref->{'last_date'};
    my $pkts_count = $ref->{'pkts_count'};
    $pkts_count{$run_id} = $pkts_count;
    $ldate{$run_id} = $last_date;
};

$sth = $dbh->prepare("select id, FROM_UNIXTIME(start_timestamp) as start_timestamp, collector_host, zond_host, monitoring_host, description FROM runs");
$sth->execute();
print "<table border=1>\n";
print "<tr><td>run_id</td><td>start_timestamp</td><td>zond_host</td><td>monitoring_host</td><td>description</td><td>Command to run on probe</td> <td>operations</td><td>last stat date</td><td>pkts count</td></tr>";
while (my $ref = $sth->fetchrow_hashref()) {
    my $run_id = $ref->{'id'};
    my $start_timestamp = $ref->{'start_timestamp'};
    my $zond_host = $ref->{'zond_host'};
    my $collector_host = $ref->{'collector_host'};
    my $monitoring_host = $ref->{'monitoring_host'};
    my $description = $ref->{'description'};



    print "<tr><td>$run_id</td><td>$start_timestamp</td><td>$zond_host</td><td>$monitoring_host</td><td>$description</td>";
    print "<td>iptvprobe ";
    my @ips = split(',', $monitoring_host);
    foreach my $val (@ips) {
        $val =~ s/^\s+//;
        $val =~ s/\s+$//;
        print "-i $val ";
    };
    print "-s $collector_host -r $run_id -p 7700</td><td>";

    #parse ip ( monitoring_host )
    my @ips = split(',', $monitoring_host);
    foreach my $val (@ips) {
        $val =~ s/^\s+//;
        $val =~ s/\s+$//;
        my $bip = unpack('N',inet_aton($val)); 
        print"IP=$val:<BR>";
        print"<a href=\"/cgi-bin/iptvprobe_stat_bw.pl?timeout=2&run_id=$run_id&ip=$bip\" target=\"_blank\">UDP timeline graphic</a> <br>";
        print"<a href=\"/cgi-bin/iptvprobe_pcr.pl?timeout=2&run_id=$run_id&ip=$bip\" target=\"_blank\">PCR Arrival Interval</a><br> ";
        print"<BR>";
    }

    print" <a href=\"/cgi-bin/iptvprobe_stat_igmp.pl?timeout=10&run_id=$run_id\" target=\"_blank\">IGMP packets</a> <br>\
        <a href=\"/cgi-bin/iptvprobe_stat_igmp_timing.pl?timeout=10&run_id=$run_id\" target=\"_blank\">IGMP timings</a> <br>\
        <br><a href=\"/cgi-bin/iptvprobe_runs.pl?action=clear&run_id4clear=$run_id\">Clear data</a><br> \
        <a href=\"/cgi-bin/iptvprobe_runs.pl?action=delete&run_id4delete=$run_id\">Delete</a><br> \
        </td><td>$ldate{$run_id}</td><td>$pkts_count{$run_id}</td></tr>\n";
};            
print "</table> <br><br>\n";

# new run form
print "<form METHOD=\"GET\" ACTION=\"/cgi-bin/iptvprobe_runs.pl\">";
print "<table>";
print "<TR><TD><b>Add iptvprobe run</b></TD><TD></TD></TR>";
print "<TR><TD>Zond host (Example: 10.0.0.1) </TD><TD><input type=\"text\" name=\"zond_host\"></TD></TR>";
print "<TR><TD>Collector host (Example: 10.1.4.242 ) </TD><TD><input type=\"text\" name=\"collector_host\"></TD></TR>";
print "<TR><TD>Multicast IP for monitoring (Example: 224.10.10.10, 224.117.117.10, 224.117.117.11)</TD><TD> <input type=\"text\" name=\"monitoring_host\"></TD></TR>";
print "<TR><TD>Description  </TD><TD> <input type=\"text\" name=\"description\"></TD></TR>";
print "<input type=\"hidden\" name=\"action\" value=\"add_run\">";
print "</table>";
print "<P><INPUT TYPE=\"SUBMIT\" VALUE=\"Submit\" NAME=\"B1\"></P> </form>";


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