Коллеги, покритикуйте плиз плагин ( Он для сбора инфы с LXC контейнеров )
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
#=============================================
# Icinga plugin For monitoring LXC
# Copyright 2018 Naim Shafiev
# Released under the GPLv3 license
# v0.95
#=============================================
use Getopt::Long qw(GetOptions);
Getopt::Long::Configure qw(gnu_getopt);
use FindBin qw($Bin);
use Data::Dumper;
use Sys::Syslog qw(:DEFAULT setlogsock);
use Fcntl qw(:DEFAULT :flock);
my $linux_host_cpu_usage = '/sys/fs/cgroup/cpu/cpuacct.usage';
my $linux_host_mem_usage = '/proc/meminfo';
my $linux_host_blkio_usage = '/sys/fs/cgroup/blkio/blkio.throttle.io_serviced';
my $warning;
my $critical;
my $perf;
my $type;
my $sleep_time;
my $out_to_plugin;
GetOptions(
'warning|w=i' => \$warning,
'critical|c=i' => \$critical,
'type|t=s' => \$type,
'perf|p' => \$perf,
'sleep_time|s=i' => \$sleep_time,
) or die( "usage: $0 --perf -w warning -c critical -type (memory , io, cpu , network) " );
### lock
my ( $short_name ) = $0 =~ m|([^/]+)\.pl$|;
setlogsock('unix');
openlog( $short_name, 'pid', 'local0');
my $lockfile = "/tmp/".$short_name."_.lock";
open(my $LOCK, ">",$lockfile ) or die( "can't open lock file: $!" );
unless ( flock($LOCK, LOCK_EX|LOCK_NB) ) { die( "$0 is already running - Aborting") }
### /lock
unless ( $perf ){ $perf = 0 }
#print "$warning $critical $perf $url\n";
my @cmd_path = ( '/usr/bin', '/usr/local/bin', '.' );
my $lxc_stat;
my $cons_sum_memory;
my $cons_sum_cpu;
my $num_of_cpu;
my @out = `/usr/bin/sudo /usr/bin/lxc-ls --running -F NAME -f `;
chomp(@out);
shift @out; # Remove COLUMN Name
for (@out) { s/ //g }
$out_to_plugin = "OK VM | ";
open(my $status_file , "<", '/proc/cpuinfo');
while (<$status_file>) {
if (m/processor/) {
$num_of_cpu++;
}
}
open($status_file , "<", $linux_host_cpu_usage );
while (<$status_file>) {
chomp;
$lxc_stat->{'main_HOST'}->{cputicks} = $_ / $num_of_cpu ;
$out_to_plugin .= "main_HOST_whole_cputicks=" . $_ / $num_of_cpu .'c ';
}
open($status_file , "<", $linux_host_mem_usage );
while (<$status_file>) {
chomp;
if ( m/MemTotal:\s+(\d+)/){
$lxc_stat->{'main_HOST'}->{memory} = $1*1024;
}
}
open($status_file , "<", $linux_host_blkio_usage );
while (<$status_file>) {
chomp;
if ($_ =~ /Total (\d+)/ and $_!~/:/) {
s/Total //;
$lxc_stat->{'main_HOST'}->{blkio} = $_;
$out_to_plugin .= "main_HOST_whole_blkio=$_".'c ';
}
}
#get info from each running container
foreach my $name (@out) {
my @lxc_out = `/usr/bin/sudo /usr/bin/lxc-info -S -H -n $name `;
chomp @lxc_out;
#my $link_name_br;
for (@lxc_out) {
if ($_ =~ /Link:/) {
my $link_name = (split /:/)[1];
$link_name =~ s/ //g;
my @ip_show_out = `/sbin/ip link show`;
for (@ip_show_out) {
if (m /.+$link_name.+(br-\S+)/) {
$lxc_stat->{$name}->{'link_name'} = $1;
}
}
}
if ($_ =~ /TX bytes:/) {
my ($cut,$tx) = split /:/;
$tx =~ s/ //g;
$lxc_stat->{$name}->{tx} = $tx;
$out_to_plugin .= $name.'_'.$lxc_stat->{$name}->{'link_name'}."_tx=$tx".'c ';
}
if ($_ =~ /RX bytes:/) {
my ($cut,$rx) = split /:/;
$rx =~ s/ //g;
$lxc_stat->{$name}->{'rx'} = $rx;
$out_to_plugin .= $name.'_'.$lxc_stat->{$name}->{'link_name'}."_rx=$rx".'c ';
}
if ( $_ =~ /CPU.+?(\d+)/) {
$lxc_stat->{$name}->{cputicks} = $1 / $num_of_cpu;
$out_to_plugin .= $name."_cputicks=".$1 / $num_of_cpu .'c ';
$cons_sum_cpu += $1;
}
if ( $_ =~ /Memory.+?(\d+)/) {
$lxc_stat->{$name}->{memory} = $1;
$out_to_plugin .= $name."_memory=$1".'b ';
$cons_sum_memory += $1;
}
}
open($status_file , "<", "/sys/fs/cgroup/blkio/lxc/$name/blkio.throttle.io_serviced") or die "$@ cannot open CGROUP file for IOP/S";
while (<$status_file>) {
chomp;
if ($_ =~ /Total/ and $_!~/:/) {
s/Total //;
$lxc_stat->{$name}->{blkio} = $_;
$out_to_plugin .= $name."_blkio=$_".'c ';
}
}
}
close ($status_file);
$out_to_plugin .= "main_HOST_memory_left=". ( $lxc_stat->{'main_HOST'}->{memory} - $cons_sum_memory ) .'b ';
#$out_to_plugin .= "main_HOST_cpuTotal_available_time_per_second=". ( $num_of_cpu * 1000000000 ).' ';
print "$out_to_plugin \n" ;
exit 0;