view ovz_statshow.pl @ 3:4cd907a693d1

ovz_statshow: some comment for people using it from the outside world
author Peter Gervai <grin@grin.hu>
date Mon, 23 Mar 2009 17:06:10 +0100
parents 4080b53fd4a2
children
line wrap: on
line source

#!/usr/bin/perl
#$Id$
#(C)Peter Gervai, 2009
# email: grin.grin.hu
#
# Released under GPL v2 or later and CC-BY-SA-3.0
#
# Lists openvz VE usage statistics.
# 

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 @fieldname = qw( user nice sys up idle up_c used_c );
my (%ve, %ve1);

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


while(1) {
    my $f = new IO::File "< $f_stat" or die $!;
    
    my $fdata;
    # have to read one chunk at once :-/ length is arbitrary
    sysread $f, $fdata, 65535;
    $f->close;
    
    my @vestat = split /\n/, $fdata;
    
    if( $vestat[0] !~ /Version: $stat_ver/ ) {
        die "Unknown file version $vestat[0], required $stat_ver";
    }
    my $debug_proc = "";
    for my $i ( 2 .. $#vestat ) {
        my $l = $vestat[$i];
        #chomp $l;
        $l =~ s/^\s*(.*?)\s*$/$1/;
        #print "##$l##\n";
        $debug_proc .= "$l\n";
        my @d = split /\s+/, $l;
        # 0veid, 1user, 2nice, 3systm, 4uptime,                                 <- jiffy
        # 5idle, (6strv), 7uptime, 8used, (9maxlat, 10totlat, 11numched)        <- cycles
        $ve{$d[0]} = [ @d[0..11] ];
    }
    
    for my $veid ( sort {$a<=>$b} keys %ve ) {
        if( defined( $ve1{$veid} ) ) {
            printf "%03d:", $veid;
            for my $i ( 0..$#fields ) {
                my $field = $fields[$i];
                #print "[$field,", $ve{$veid}, ",", $ve1{$veid} ,",", $ve{$veid}->[$field],",", $ve1{$veid}->[$field], "] ";
                my $val = $ve{$veid}->[$field] - $ve1{$veid}->[$field];
                printf "%6s(%3d)=", $fieldname[$i], $field;
                
                if( $val < 0 ) {
                    # debug
                    print "NEGATIVE VALUE!!\n";
                    print "\n#", $ve{$veid}->[$field], ' ', $ve1{$veid}->[$field], "#$_#\n";
                    print "$debug_proc\n";
                }
                
                if( $field < 5 ) {
                    printf "%4d", &jiffie2ms( $val );
                } else {
                    printf "%4d", &cycle2ms( $val );
                }
                print "; ";
                if( $field == 7 ) {
                    printf "<%4d> ", &cycle2ms( $val - ($ve{$veid}->[5] - $ve1{$veid}->[5]) );
                }
            }
            print "\n";
        }
        $ve1{$veid} = $ve{$veid};
    }
    print "\n";
    sleep(1);
}


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


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