Sweede
01-21-2001, 02:23 PM
i need to convert these functions/script into PHP for my control panel,
anyone care to help ??
#!/usr/bin/perl
$file = shift;
$chunksize = 5;
$monthsworth = 30 * 24 * (60 / $chunksize); # number of intervals in a month
$chunkseconds = $chunksize * 60;
$maxdeviation = .10 * $chunkseconds;
$warndeviation = .05 * $chunkseconds;
$day = 24 * 60 * 60 + $maxdeviation;
open(FILE, "<$file")
or die "Cannot open $file for reading: $!";
@logfile = <FILE>;
close(FILE);
@snapshots = &extractsnapshots(@logfile);
undef @logfile;
@snapshots = &trimsnapshots(@snapshots); # return only valid part of array
$chunklocation = int($#snapshots * .95);
$GBpM = int(($snapshots[$chunklocation] * $day * 30) / (1024 * 1024 * 1024));
print qq^95th percentile:
$snapshots[$chunklocation] bytes per second
$GBpM gigabytes per month
^;
sub extractsnapshots {
local(@logfile, @snapshots, $time, $incoming, $outgoing, $reftime) = @_;
for ($i = 0; $i < $#logfile - 1; $i++) {
($time, $incoming, $outgoing) =
$logfile[$i] =~ /^(\d+)\s+(\d+)\s+(\d+)/;
length($outgoing)
| | die "$0: corrupted logfile $ARGV[0]\n";
(($reftime) = $logfile[$i + 1] =~ /^(\d+)/) | |
die "$0: corrupted logfile $ARGV[0] (\$reftime)\n";
$time -= $reftime;
die "$0: time period more than a day\n" if $time > $day;
push(@snapshots, &splitinterval($time, $incoming + $outgoing));
last if $#snapshots >= $monthsworth * 2;
}
@snapshots;
}
sub splitinterval { # split anything up into 5-minute chunks
local(@_) = @_; # localize the arg array
if (&deviation($_[0]) >= $maxdeviation) {
warn "$0: splitinterval: dropping oddsized chunk $_[0] secs of $_[1] Bps\n";
shift(@_); shift(@_); return @_;
}
warn "$0: splitinterval: warning: oddsized chunk $_[0] secs of $_[1] Bps\n"
if &deviation($_[0]) >= $warndeviation;
return @_ if $_[0] < $chunkseconds + $maxdeviation;
$_[0] -= $chunkseconds;
return &splitinterval(@_, $chunkseconds, $_[1]);
}
sub deviation { # how far a number strays from multiple of $chunkseconds
local($seconds) = shift(@_);
$seconds < $chunkseconds && return $chunkseconds - $seconds;
$seconds %= $chunkseconds; # get modulus
$seconds > ($chunkseconds / 2) && return $chunkseconds - $seconds;
$seconds;
}
sub trimsnapshots {
local(@snapshots) = @_;
$#snapshots = $monthsworth * 2 - 1; # set size of array
# now drop trailing zeroes, in case it's a partial month
while (($snapshots > 1) && ($snapshots[$#snapshots] == 0)) {
pop(@snapshots); pop(@snapshots);
}
# now get rid of the times, leaving only the Bps averages per chunk...
local(@clean, $i);
for($i = 0; $i <= $#snapshots; $i++) {
push (@clean, $snapshots[$i]) if $i % 2; # only the odd-numbered cells
}
sort by_number @clean;
}
sub by_number { # for sorting from lowest to highest
$a <=> $b;
}
sub average { # average out a numeric array
local(@array) = @_; local($size) = $#array + 1;
local($sum);
foreach (@array) {
$sum += $_;
}
$sum / $size;
}
I could probably manage this myself, but there are a few things thaat confuse me.
particuarly the sub name { subroutines.
whats local ? the $#snapshots, @_ ,
sort by_number @clean;
i assume means sorty the array clean by number ?
This script parses standard MRTG log files and returns the 95% bandwidth measurements (stupid host provider bandwidth billing ideals)
here's an example of the log in case anyone needs it (first 50 lines, there is 2536 lines per log file)
980100901 14908198 124573322
980100901 0 0 0 0
980100602 0 0 0 0
980100600 0 0 0 0
980100300 0 1 75 160
980100000 75 160 307 459
980099700 305 457 307 459
980099400 0 0 0 0
980099100 0 0 13 12
980098800 12 11 13 12
980098500 5 64 1515 19223
980098200 1510 19158 1515 19223
980097900 17 18 18 19
980097600 0 0 0 0
980097300 0 0 0 0
980097000 0 0 0 0
980096700 0 0 0 0
980096400 0 0 0 0
980096100 0 0 13 12
980095800 12 11 13 12
980095500 0 0 0 0
980095200 0 0 0 0
980094900 0 0 0 0
980094600 0 0 0 0
980094300 0 0 0 0
980094000 0 0 0 0
980093700 0 0 0 0
980093400 0 0 12 12
980093100 11 11 12 12
980092800 0 0 0 0
980092500 0 0 0 0
980092200 0 0 0 0
980091900 0 0 0 0
980091600 0 0 0 0
980091300 0 0 0 0
980091000 3 40 516 6053
980090700 512 6012 516 6053
980090400 0 0 12 12
980090100 11 11 12 12
980089800 0 4 37 722
980089500 36 719 37 722
980089200 0 0 0 0
980088900 0 0 0 0
980088600 0 0 0 0
980088300 0 0 0 0
980088000 0 0 0 0
980087700 0 0 12 12
980087400 12 14 37 725
980087100 37 725 37 727
980086800 36 722 37 727
thanks
anyone care to help ??
#!/usr/bin/perl
$file = shift;
$chunksize = 5;
$monthsworth = 30 * 24 * (60 / $chunksize); # number of intervals in a month
$chunkseconds = $chunksize * 60;
$maxdeviation = .10 * $chunkseconds;
$warndeviation = .05 * $chunkseconds;
$day = 24 * 60 * 60 + $maxdeviation;
open(FILE, "<$file")
or die "Cannot open $file for reading: $!";
@logfile = <FILE>;
close(FILE);
@snapshots = &extractsnapshots(@logfile);
undef @logfile;
@snapshots = &trimsnapshots(@snapshots); # return only valid part of array
$chunklocation = int($#snapshots * .95);
$GBpM = int(($snapshots[$chunklocation] * $day * 30) / (1024 * 1024 * 1024));
print qq^95th percentile:
$snapshots[$chunklocation] bytes per second
$GBpM gigabytes per month
^;
sub extractsnapshots {
local(@logfile, @snapshots, $time, $incoming, $outgoing, $reftime) = @_;
for ($i = 0; $i < $#logfile - 1; $i++) {
($time, $incoming, $outgoing) =
$logfile[$i] =~ /^(\d+)\s+(\d+)\s+(\d+)/;
length($outgoing)
| | die "$0: corrupted logfile $ARGV[0]\n";
(($reftime) = $logfile[$i + 1] =~ /^(\d+)/) | |
die "$0: corrupted logfile $ARGV[0] (\$reftime)\n";
$time -= $reftime;
die "$0: time period more than a day\n" if $time > $day;
push(@snapshots, &splitinterval($time, $incoming + $outgoing));
last if $#snapshots >= $monthsworth * 2;
}
@snapshots;
}
sub splitinterval { # split anything up into 5-minute chunks
local(@_) = @_; # localize the arg array
if (&deviation($_[0]) >= $maxdeviation) {
warn "$0: splitinterval: dropping oddsized chunk $_[0] secs of $_[1] Bps\n";
shift(@_); shift(@_); return @_;
}
warn "$0: splitinterval: warning: oddsized chunk $_[0] secs of $_[1] Bps\n"
if &deviation($_[0]) >= $warndeviation;
return @_ if $_[0] < $chunkseconds + $maxdeviation;
$_[0] -= $chunkseconds;
return &splitinterval(@_, $chunkseconds, $_[1]);
}
sub deviation { # how far a number strays from multiple of $chunkseconds
local($seconds) = shift(@_);
$seconds < $chunkseconds && return $chunkseconds - $seconds;
$seconds %= $chunkseconds; # get modulus
$seconds > ($chunkseconds / 2) && return $chunkseconds - $seconds;
$seconds;
}
sub trimsnapshots {
local(@snapshots) = @_;
$#snapshots = $monthsworth * 2 - 1; # set size of array
# now drop trailing zeroes, in case it's a partial month
while (($snapshots > 1) && ($snapshots[$#snapshots] == 0)) {
pop(@snapshots); pop(@snapshots);
}
# now get rid of the times, leaving only the Bps averages per chunk...
local(@clean, $i);
for($i = 0; $i <= $#snapshots; $i++) {
push (@clean, $snapshots[$i]) if $i % 2; # only the odd-numbered cells
}
sort by_number @clean;
}
sub by_number { # for sorting from lowest to highest
$a <=> $b;
}
sub average { # average out a numeric array
local(@array) = @_; local($size) = $#array + 1;
local($sum);
foreach (@array) {
$sum += $_;
}
$sum / $size;
}
I could probably manage this myself, but there are a few things thaat confuse me.
particuarly the sub name { subroutines.
whats local ? the $#snapshots, @_ ,
sort by_number @clean;
i assume means sorty the array clean by number ?
This script parses standard MRTG log files and returns the 95% bandwidth measurements (stupid host provider bandwidth billing ideals)
here's an example of the log in case anyone needs it (first 50 lines, there is 2536 lines per log file)
980100901 14908198 124573322
980100901 0 0 0 0
980100602 0 0 0 0
980100600 0 0 0 0
980100300 0 1 75 160
980100000 75 160 307 459
980099700 305 457 307 459
980099400 0 0 0 0
980099100 0 0 13 12
980098800 12 11 13 12
980098500 5 64 1515 19223
980098200 1510 19158 1515 19223
980097900 17 18 18 19
980097600 0 0 0 0
980097300 0 0 0 0
980097000 0 0 0 0
980096700 0 0 0 0
980096400 0 0 0 0
980096100 0 0 13 12
980095800 12 11 13 12
980095500 0 0 0 0
980095200 0 0 0 0
980094900 0 0 0 0
980094600 0 0 0 0
980094300 0 0 0 0
980094000 0 0 0 0
980093700 0 0 0 0
980093400 0 0 12 12
980093100 11 11 12 12
980092800 0 0 0 0
980092500 0 0 0 0
980092200 0 0 0 0
980091900 0 0 0 0
980091600 0 0 0 0
980091300 0 0 0 0
980091000 3 40 516 6053
980090700 512 6012 516 6053
980090400 0 0 12 12
980090100 11 11 12 12
980089800 0 4 37 722
980089500 36 719 37 722
980089200 0 0 0 0
980088900 0 0 0 0
980088600 0 0 0 0
980088300 0 0 0 0
980088000 0 0 0 0
980087700 0 0 12 12
980087400 12 14 37 725
980087100 37 725 37 727
980086800 36 722 37 727
thanks