view ovz_statshow.pl @ 1:068d0e1deb08

ovz_statshow: almost works now :)
author Peter Gervai <grin@grin.hu>
date Mon, 23 Mar 2009 14:08:23 +0100
parents efb595e3b0a2
children 4080b53fd4a2
line wrap: on
line source

#!/usr/bin/perl
#$Id$
#(C)Peter Gervai, 2009
#

use strict;
use warnings;

use IO::File;


my $f_stat = "/proc/vz/vestat";
my $stat_ver = "2.2";
# jiffie per second ( zgrep ^CONFIG_HZ /proc/config.gz )
my $jiffies = 100;


my @fields = (1,2,3,4,5,7,8);
my (%ve, %ve1);

my $cpu_mhz = qx( egrep '^cpu MHz' /proc/cpuinfo | head -n1 | awk '{print \$4}' );
chomp $cpu_mhz;
print "Running on $cpu_mhz MHz CPU(s)\n";


while(1) {
    my $f = new IO::File "< $f_stat" or die $!;
    my $l = <$f>;
    if( $l !~ /Version: $stat_ver/ ) {
        die "Unknown file version $l, required $stat_ver";
    }
    $l = <$f>; # headers
    
    while( <$f> ) {
        chomp;
        my @d = split;
        # 0veid, 1user, 2nice, 3systm, 4uptime, 5idle, (6strv)
        # 7uptime, 8used, (9maxlat, 10totlat, 11numched)
        $ve{$d[0]} = [ @d[0..11] ];
    }
    
    for my $veid ( sort {$a<=>$b} keys %ve ) {
        if( defined( $ve1{$veid} ) ) {
            print "$veid: ";
            for my $field ( @fields ) {
                #print "[$field,", $ve{$veid}, ",", $ve1{$veid} ,",", $ve{$veid}->[$field],",", $ve1{$veid}->[$field], "] ";
                my $val = $ve{$veid}->[$field] - $ve1{$veid}->[$field];
                print "field=";
                if( $field <6 ) {
                    print &jiffie2ms( $val );
                } else {
                    print &clock2ms( $val );
                }
                print "; ";
                if( $field == 7 ) {
                    print "<", &clock2ms( $val - ($ve{$veid}->[5] - $ve1{$veid}->[5]) ), "> ";
                }
            }
            print "\n";
        }
        $ve1{$veid} = $ve{$veid};
    }
    print "\n";
    sleep(1);
}


sub clock2ms ($) {
    return int( (shift) / ( $cpu_mhz * $jiffies * $jiffies ) * 1000 );
}


sub jiffie2ms ($) {
    return int( (shift) / $jiffies * 1000 );
}