analy2.in 2.98 KB
Newer Older
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl

use Data::Dumper;
use POSIX 'strftime','mktime';

use strict;
use warnings;
no warnings 'uninitialized';

10 11 12 13
our (@to_plot);
require "@prefix@/etc/node_usage.conf";

chdir "@prefix@/data/node_usage";
14 15 16

my $TOLERANCE_INTERVAL = 1/3;

17 18
my @idxs = (0 .. (@to_plot * 2 - 1));

19 20 21 22 23 24 25 26
my %res;

sub tally ($$@) {
    my ($str, $what, @d) = @_;
    if ($d[0] != $d[0]) { # ie NaN
	$res{$what}{data}{$str}{invalid}++;
    } else {
	$res{$what}{data}{$str}{count}++;
27
	foreach my $i (@idxs) {
28 29 30 31 32 33 34 35 36
	    $res{$what}{data}{$str}{data}[$i] += $d[$i];
	}
    }
}

sub tally_mod ($$@) {
    my ($bin, $what, @d) = @_;
    return if $d[0] != $d[0]; # ie NaN
    $res{$what}{data}[$bin]{count}++;
37
    foreach my $i (@idxs) {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	$res{$what}{data}[$bin]{data}[$i] += $d[$i];
    }
}

open F, "node_usage-hourly.dat" or die;

$res{hourly_last2weeks} = {type=>'normal'};
$res{daily_last2months} = {type=>'normal'};
$res{daily} = {type=>'normal'};
$res{weekly} = {type=>'normal'};
$res{monthly} = {type=>'normal'};
$res{yearly} = {type=>'normal'};
$res{by_hour} = {type=>'mod'};
$res{by_dayofweek} = {type=>'mod'};
$res{by_hourofweek} = {type=>'mod',div=>24};
$res{by_month} = {type=>'mod'};

my @now = localtime();
my $hourly_start = mktime(0, 0, 0, $now[3]-14, $now[4], $now[5]);
my $daily_start = mktime(0, 0, 0, $now[3], $now[4]-2, $now[5]);

while (<F>) {
    chop;
    my @d = split / /;
    my $time = shift @d;
    my @time = localtime($time);
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = @time;
    #next unless $year + 1900 == 2009;
    my $wday_m = ($wday - 1) % 7;
    tally(strftime("%Y-%m-%d_%R", @time), 'hourly_last2weeks', @d)
	if ($time >= $hourly_start);
    tally(strftime("%Y-%m-%d", @time), 'daily_last2months', @d)
	if ($time >= $daily_start);
    tally(strftime("%Y-%m-%d", @time), 'daily', @d);
    tally(strftime("%Y-%m-%d", $sec,$min,$hour,$mday-$wday_m,$mon,$year), 'weekly', @d);
    tally(strftime("%Y-%m", @time), 'monthly', @d);
    tally(strftime("%Y", @time), 'yearly', @d);
    tally_mod($hour,'by_hour', @d);
    tally_mod($wday_m,'by_dayofweek', @d);
    tally_mod($wday_m*24+$hour,'by_hourofweek', @d);
    tally_mod($mon, 'by_month', @d);
}

foreach my $k (keys %res) {

    open F, ">node_usage-$k.dat";

    if ($res{$k}{type} eq 'normal') {
	foreach my $i (sort keys %{$res{$k}{data}}) {
	    my @r;
	    my $d = $res{$k}{data}{$i};
	    my $invalid = $d->{invalid};
	    my $count = $d->{count};
	    if ($invalid / ($invalid + $count) > $TOLERANCE_INTERVAL) {
92
		@r = map {'NaN'} @idxs;
93
	    } else {
94
		foreach my $j (@idxs) {
95 96 97
		    $r[$j] = $d->{data}[$j]/$count;
		}
	    }
98
	    print F join(' ', "$i ", map {sprintf("%5.1f", $_)} @r),"\n" 
99 100 101 102 103 104 105 106
	}
    } else {
	my $div = $res{$k}{div};
	$div = 1 unless defined $res{$k}{div};
	foreach my $i (0 .. $#{$res{$k}{data}}) {
	    my @r;
	    my $d = $res{$k}{data}[$i];
	    my $count = $d->{count};
107
	    foreach my $j (@idxs) {
108 109
		$r[$j] = $d->{data}[$j]/$count;
	    }
110
	    print F join(' ', sprintf("%6.3f ", $i/$div), map {sprintf("%5.1f", $_)} @r),"\n" 
111 112 113
	}
    }
}