Advertisement
kisslo

Average_Calculate

Jul 15th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.04 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. #==================================================================================
  4. # Auteur / Author   : kisslo
  5. # Date (little endian)  : 25/05/2011
  6. # Fichier / file name   : average_db_data.pl
  7. # Description       : Calculate average latency per IP (incoming and outgoing)
  8. #==================================================================================
  9.  
  10. use strict;
  11. use DBI;
  12. use Term::UI;
  13. use Term::ReadLine;
  14.  
  15. my ($sth0,$sth00,$sth1,$sth2,$sth3);        # Used to launch the mysql request (contains the statement)
  16. my ($ip_in,@ip_in,$latency_in,@latency_in); # Scalar and array contening IP / latency
  17. my ($time_in,$average_time_in);         # $time_in -> the latency / $average_time_in -> average of all latency
  18. my (%average_ip_in,%average_ip_out);        # Hash list contains average latency and ip
  19. my ($know_ip,@know_ip);             # Contains the existant IP in average latency table
  20. my $i;                      # Used to count the number of MySQL request during test
  21. my $j;                      # Used to defined data position in an array
  22. my $k = 1;                  # Used to count the number of loop;
  23. my $stat;                   # Boolean, identical ip = 1 . Comparison between ip in average table and ip of an other table
  24. my $fetch_data;                 # Temporary variable to storage after execution handle
  25. my ($connected_ip,@connected_ip);       # Liste of ip connected
  26. my ($valid_ip,@valid_ip);           # Ip with "time to live" valid
  27. my ($date_more,$date_less);         # The intervale to keep the  
  28.  
  29.  
  30. # ==================== Data for connection to the database ==================== #
  31. my $dbname = 'vision_db';   # Name of the database
  32. my $host = 'localhost';     # Host name / IP
  33. my $login = 'root';     # Login used for connection at the database
  34. my $pass = 'yourpass';      # Password for the database
  35.  
  36. # ==== Connection sequence ==== #
  37. my $dbh = DBI->connect("dbi:mysql:dbname=$dbname;host=$host;",$login,$pass)
  38. or die "Fail to connect at the database $dbname ! ".DBI::errstr;;
  39. # ============================================================================= #
  40.  
  41. # ======================= That value(s) can be modified ======================= #
  42. my $numb_timer = 10;    # Number of latency used for latency average
  43. # ============================================================================= #
  44.  
  45.  
  46. while (1)
  47. {
  48.     sleep 10;
  49.     print "\nLoop number : $k\n";       # The number of code looping
  50.     $k++;                   # Increment the numb of loop
  51.  
  52.     @connected_ip = `/home/vision/alex/connected_ip.pl`;    # Recover the connected ip
  53.     chop @connected_ip;
  54.  
  55.     ##################
  56.     ## PREPARATION  ##
  57.     ##################
  58.  
  59.     # ================================================== #
  60.     # ========== You can change this value(s) ========== #
  61.     # ================================================== #
  62.         my $numb_time = 10; # The numb of max values to create an average
  63.         my $date_gap = 20;  # Indicate the time duration of conservation of the ip
  64.     # ================================================== #
  65.  
  66.     $date_more = `date +%s` + $date_gap/2;
  67.     $date_less = `date +%s` - $date_gap/2;
  68.  
  69.     # === Preparation of the average table : delete all values except the values which are less old than indicate in "$date_gap" === #
  70.     # ============================================================================================================================== #
  71.     $sth0 = $dbh->prepare('DELETE FROM `IpLatencyAverage` WHERE id NOT IN (SELECT * FROM (SELECT `id` FROM `IpLatencyAverage` WHERE unix_time < '.$date_more.' AND unix_time > '.$date_less.') AS tmp0)')
  72.         or die 'Impossible to prepare the request::'.$dbh->errstr();
  73.     $sth0->execute()
  74.         or die 'Error during the execution :: '.$dbh->errstr();
  75.  
  76.     # === Check ip remaining in the table... === #
  77.     # ========================================== #
  78.     $sth00 = $dbh->prepare('SELECT DISTINCT ip_in FROM IpLatencyAverage')
  79.         or die 'Impossible to prepare the request::'.$dbh->errstr();
  80.     $sth00->execute()
  81.         or die 'Error during the execution :: '.$dbh->errstr();
  82.  
  83.     # === ...and put them in an array === #
  84.     # =================================== #
  85.     undef @valid_ip;
  86.     while ($valid_ip = $sth00->fetchrow_array())
  87.     {
  88.         push (@valid_ip,$valid_ip);
  89.     }
  90.  
  91.     #############################
  92.     ## EXECUTION : outgoing ip ##
  93.     #############################
  94.  
  95.     # === Take all different ip and make an average latency with the last X times indicate in "$numb_time"  === #
  96.     # ========================================================================================================= #
  97.     $sth1 = $dbh->prepare('SELECT DISTINCT IpIn FROM IpVisionOut ORDER BY time DESC LIMIT '.$numb_time)
  98.         or die 'Impossible to prepare the request :: '.$dbh->errstr();
  99.     $sth1->execute()
  100.         or die 'Error during the execution :: '.$dbh->errstr();
  101.  
  102.     while (@ip_in = $sth1->fetchrow_array())
  103.     {
  104.         $time_in = 0;
  105.         foreach $ip_in (@ip_in)
  106.         {
  107.             $sth2 = $dbh->prepare('SELECT TempsLatenceIp FROM IpVisionOut WHERE IpIn="'.$ip_in.'" ORDER BY id DESC LIMIT '.$numb_timer)
  108.                 or die 'Impossible to prepare the request::'.$dbh->errstr();
  109.             $sth2->execute()
  110.                 or die 'Error during the execution :: '.$dbh->errstr();
  111.  
  112.             while (@latency_in = $sth2->fetchrow_array())
  113.             {
  114.                 foreach $latency_in (@latency_in)
  115.                 {
  116.                     $time_in += $latency_in;
  117.                 }
  118.             }
  119.             $average_time_in = $time_in/$numb_timer;
  120.             $average_ip_in{$ip_in} = $average_time_in;
  121.         }
  122.     }
  123.  
  124.     # === Write the data into the database === #
  125.     # ======================================== #
  126.     foreach $ip_in (sort(keys(%average_ip_in)))
  127.     {
  128.         if ($average_ip_in{$ip_in} != 0)
  129.         {
  130.             if (grep { $_ eq $ip_in } @connected_ip )
  131.             {
  132.                 if ( $average_ip_in{$ip_in} >= 2.000000000 )
  133.                 {
  134.                     $sth3 = $dbh->prepare('INSERT INTO IpHighAverage (ip_in,latency_average) VALUES ("'.$ip_in.'","'.$average_ip_in{$ip_in}.'")')
  135.                         or die 'Impossible to prepare the request::'.$dbh->errstr();
  136.                     $sth3->execute()
  137.                         or die 'Error during the execution :: '.$dbh->errstr();
  138.                     print "INSERT HIGH -- IP in = $ip_in \t latency = $average_ip_in{$ip_in}\n";    #
  139.                         $sth3->finish();
  140.                 }
  141.                 if (grep { $_ eq $ip_in } @valid_ip )
  142.                 {
  143.                     $sth3 = $dbh->prepare('UPDATE IpLatencyAverage SET latency_average = "'.$average_ip_in{$ip_in}.'",unix_time = "'.`date +%s`.'" WHERE ip_in = "'.$ip_in.'"')
  144.                         or die 'Impossible to prepare the request::'.$dbh->errstr();
  145.                     $sth3->execute()
  146.                         or die 'Error during the execution :: '.$dbh->errstr();
  147.                     `\/home/vision/alex/analysis.pl '$ip_in' '$average_ip_in{$ip_in}'`;
  148.                     print "UPDATE -- IP in = $ip_in \t\t latency = $average_ip_in{$ip_in}\n";   #
  149.                         $sth3->finish();
  150.                 }
  151.                 else
  152.                 {
  153.                     $sth3 = $dbh->prepare('INSERT INTO IpLatencyAverage (ip_in,latency_average,unix_time) VALUES ("'.$ip_in.'","'.$average_ip_in{$ip_in}.'","'.`date +%s`.'")')
  154.                         or die 'Impossible to prepare the request::'.$dbh->errstr();
  155.                     $sth3->execute()
  156.                         or die 'Error during the execution :: '.$dbh->errstr();
  157.                     `\/home/vision/alex/analysis.pl '$ip_in' '$average_ip_in{$ip_in}'`;
  158.                     print "INSERT -- IP in = $ip_in \t\t latency = $average_ip_in{$ip_in}\n";   #
  159.                         $sth3->finish();
  160.                 }
  161.             }
  162.         }
  163.     }
  164.  
  165.     $sth0->finish();
  166.     $sth00->finish();
  167.     $sth1->finish();
  168.     $sth2->finish();
  169. }
  170. $dbh->disconnect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement