Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
open_vestat.pl
#!/usr/bin/perl
use strict;
use warnings;
use lib "/usr/local/lib/perl";
# Author pavel.odintsov@gmail.com
my $cpu_scheduler_path = '/proc/vz/fairsched';
my $blkio_sheduler_path = '/proc/vz/beancounter';
my $vzlist = "/usr/sbin/vzlist";
my @containers = get_all_running_containers();
my $cpu_data_storage = {};
my $cpu_consumption = {};
my @checks = ("disk_time", "disk_sectors", "cpu");
# sort by cpu by default
my $check_type = 'cpu';
if (defined $ARGV[0]) {
if (in_array($check_type, @checks)) {
$check_type = $ARGV[0];
} else {
die "Please provide correct sorters: ". (join "/", @checks) . "\n";
}
}
for my $check (@checks) {
$cpu_data_storage->{$check} = {};
$cpu_consumption->{$check} = {};
}
while (1) {
for my $check (@checks) {
calc_resources_consumption(\@containers, $check);
}
my $number_of_top_records = 20;
my $counter = 0;
print "We sort data by $check_type\n";
my @keys_sorted_by_custom_parameter = reverse sort {
$cpu_consumption->{$check_type}->{$a} <=> $cpu_consumption->{$check_type}->{$b}
} keys %{ $cpu_consumption->{$check_type} };
for my $key (@keys_sorted_by_custom_parameter) {
if ($counter > $number_of_top_records) {
last;
}
# skip HWN
if ($key eq 'hwn') {
next;
}
print "$key:\t";
for my $check (@checks) {
unless ($cpu_consumption->{$check}->{hwn}) {
next;
}
print "$check: " . sprintf("%.1f",$cpu_consumption->{$check}->{$key}/$cpu_consumption->{$check}->{hwn}*100) . " %\t";
}
print "\n";
#print "process snapshot:\n";
#print `vzprocess $key|sort -g -k 3|tail -n 1`;
$counter++;
}
sleep(5);
system('clear');
}
sub calc_resources_consumption {
my @containers = @{ $_[0] };
my $type = $_[1];
my $main_path = '';
my $value_path = '';
if ($type eq 'cpu') {
$main_path = $cpu_scheduler_path;
$value_path = 'cpuacct.usage';
} elsif ($type eq 'disk_time') {
$main_path = $blkio_sheduler_path;
$value_path = 'blkio.time';
} elsif ($type eq 'disk_sectors') {
$main_path = $blkio_sheduler_path;
$value_path = 'blkio.sectors';
} else {
die "Unexpected type";
}
my $server_total_cpu_usage = read_file_contents("$main_path/$value_path");
if ($type =~ /^disk/) {
$server_total_cpu_usage = (split /\s+/, $server_total_cpu_usage)[1];
}
if (defined $cpu_data_storage->{$type}->{hwn}) {
$cpu_consumption->{$type}->{hwn} = $server_total_cpu_usage - $cpu_data_storage->{$type}->{hwn};
}
$cpu_data_storage->{$type}->{hwn} = $server_total_cpu_usage;
for my $ct (@containers) {
my $ct_cpu_usage = read_file_contents("$main_path/$ct/$value_path");
if ($type =~ /^disk/) {
$ct_cpu_usage = (split /\s+/, $ct_cpu_usage)[1];
}
if (defined $cpu_data_storage->{$type}->{$ct}) {
$cpu_consumption->{$type}->{$ct} = $ct_cpu_usage - $cpu_data_storage->{$type}->{$ct};
}
$cpu_data_storage->{$type}->{$ct} = $ct_cpu_usage;
}
}
sub get_all_running_containers {
my @containers = `$vzlist -H1`;
chomp @containers;
map {
s/^\s+//;
s/\s+$//;
} @containers;
return @containers;
}
sub in_array {
my ($elem, @array) = @_;
return scalar grep { $elem eq $_ } @array;
}
# Считываем файл в переменную
sub read_file_contents {
my $path = shift;
open my $fl, "<", $path or die "Can't open file";
my $data = '';
while (<$fl>) {
chomp;
$data .= $_;
}
close $fl;
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.