Annotation of bitaacj/src/schedule.pl, revision 1.6

1.1       cherian     1: #!/usr/bin/perl -w
                      2: print "\nProgram Starting...... \n";
                      3: #Lets catch the errors as soon as we can
                      4: use strict;
                      5: use warnings;
                      6: use IO::Socket;
                      7: use DBI;
                      8: use Sys::Hostname;
                      9: use Switch;
                     10: use POSIX;
                     11: use threads;
                     12: require Net::Daemon;
                     13: use Net::Ping;
1.2       cherian    14: # use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep clock_gettime clock_getres clock_nanosleep clock );
1.1       cherian    15: #use Thread::Pool;
                     16: STDOUT->autoflush(1);
                     17: 
                     18: 
1.2       cherian    19: 
                     20: 
                     21: #for my $handle ( *STDIN, *STDOUT, *STDERR){
                     22: #      open ($handle, "+<" ,"/dev/null")
                     23: #      || die "can't reopen $handle to /dev/null : $!";
                     24: #}
                     25: 
                     26: 
1.1       cherian    27: #initializations
                     28: my $sql;
                     29: my $statement;
                     30: my $st_result=0;
1.4       cherian    31: my $size;
                     32: my $hashes;
                     33: my $count;
1.1       cherian    34: #mysql connection initialization
                     35: #database design will be posted on the homepage soo. jus check it out
                     36: print "\nTrying to connect to mysql database...... ";
                     37: my $db_handle = DBI->connect("dbi:mysql:database=tracker;host=10.4.1.185;user=sprt;password=sprt")
                     38:     or die "Couldn't connect to database: $DBI::errstr\n";
                     39: print "connected \n";
                     40: 
                     41: 
                     42: #delete records greater than one day
                     43: $sql= "delete from tab_ip_tim where tim < adddate(now(), interval -1 day )"; 
                     44: $statement = $db_handle->prepare($sql)
                     45:        or die "Couldn't prepare query '$sql': $DBI::errstr\n";
                     46: $statement->execute()
                     47:        or die "Couldn't execute query '$sql': $DBI::errstr\n";
                     48: 
                     49: #get ip's
                     50: $sql = "select distinct ip from tab_ip_tim;";
                     51: my $statement = $db_handle->prepare($sql) 
                     52:        or die "Couldn't prepare query '$sql': $DBI::errstr\n";
                     53: $statement->execute() 
                     54:        or die "Couldn't execute query '$sql': $DBI::errstr\n";
1.2       cherian    55:        # my $temp=hires();
1.1       cherian    56: while ( my $row = $statement->fetchrow_array ) {
                     57:        my $p = Net::Ping->new("tcp");
1.2       cherian    58:        my ($status, $latency) = $p->ping($row);
                     59:        if (defined($status)){
                     60:                print "$row is alive.\n" ;
                     61:                print "Latency of ping for ".$row ." is ". $latency."\n"; 
1.3       cherian    62:                #enter into table the ip's and latency prefetching cache from the peers
                     63:                $sql= "insert into schedule_ip_lat values('$row','$latency') on duplicate key update latency='$latency'"; 
                     64:                my $statement_schedule_ip_lat = $db_handle->prepare($sql)
                     65:                        or die "Couldn't prepare query '$sql': $DBI::errstr\n";
                     66:                $statement_schedule_ip_lat->execute()
                     67:                        or die "Couldn't execute query '$sql': $DBI::errstr\n";
1.1       cherian    68:        }
                     69:        else{
1.3       cherian    70:                print "$row Not alive \n";
                     71:                #deleting records not tcp pingable and 2 hours old
                     72:                $sql= "delete from tab_ip_tim where ip='$row' and tim < adddate( now(),inte rval -2 hour )"; 
                     73:                my $statement_delete_tab_ip_tim = $db_handle->prepare($sql)
                     74:                        or die "Couldn't prepare query '$sql': $DBI::errstr\n";
                     75:                $statement_delete_tab_ip_tim->execute()
                     76:                        or die "Couldn't execute query '$sql': $DBI::errstr\n";
1.1       cherian    77:        }
                     78:        $p->close();
                     79:        #icmp unix pinging now disabled due to performance issues
                     80:        #print "Going to do icmp ping on  ".$row ." \n";
                     81:        #my @args= ("ping -c 1 $row >> c");
                     82:        #my $c = system (@args);
                     83:        #print "the result is ...". $c ."\n";
                     84:        }
1.5       cherian    85: #code for deleting from hitcounter 
                     86: my $sql_hitcounter= "delete hitcounter,tab_ip_tim from hitcounter, tab_ip_tim where hitcounter.count<2 and hitcounter.tim< adddate(now(), interval -2 hour) and tab_ip_tim.url_hash=hitcounter.url_hash;"; 
                     87: my $statement_hitcounter = $db_handle->prepare($sql_hitcounter)
                     88:        or die "Couldn't prepare query '$sql': $DBI::errstr\n";
                     89: $statement_hitcounter->execute()
                     90:        or die "Couldn't execute query '$sql': $DBI::errstr\n";
                     91: 
1.3       cherian    92: #code for prefetching 
                     93: $sql= "select ip from schedule_ip_lat order by latency asc limit 5"; 
                     94: my $statement_select = $db_handle->prepare($sql)
                     95:        or die "Couldn't prepare query '$sql': $DBI::errstr\n";
                     96: $statement_select->execute()
                     97:        or die "Couldn't execute query '$sql': $DBI::errstr\n";
                     98: while (my $row_select = $statement_select->fetchrow_array ) {
                     99:        #the big prefetching code starts here!!!!
                    100:        print "IPs are " . $row_select ." \n";
                    101:        my $sock = new IO::Socket::INET (
1.4       cherian   102:                                  PeerAddr => $row_select,
                    103:                                  PeerPort => '27003',
1.3       cherian   104:                                   Proto => 'tcp',
                    105:                                  );
                    106:        die "Could not create socket: $!\n" unless $sock;
                    107:        $sock->send("PRE000");
                    108:        #the browser should reply back the hashes list...but since this is 2nd priority and that akhil has not coded the corresponding frame....i'll be adding  #the split functions sometime later
1.4       cherian   109:        $sock->recv($size, 3);
                    110:        $sock->recv($hashes,$size);
                    111:        my $leng = length($hashes);
                    112:        for($count=0; $count<$leng; $count+=32) {
                    113:        my $md5hash = substr($hashes, $count, 32);
1.6     ! cherian   114:        my $sql_prefetch= "insert into tab_ip_tim values ('$md5hash','$sock->peerhost()',now()) on duplicate key update tim=now()"; 
        !           115:        my $statement_prefetch = $db_handle->prepare($sql_prefetch)
1.4       cherian   116:                or die "Couldn't prepare query '$sql': $DBI::errstr\n";
1.6     ! cherian   117:        $statement_prefetch->execute()
1.4       cherian   118:                or die "Couldn't execute query '$sql': $DBI::errstr\n";
                    119:        }
                    120:        
1.1       cherian   121: 
1.3       cherian   122:        }
1.1       cherian   123: 
                    124: 
                    125: 
                    126: 
                    127: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>