-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_lxc-info
executable file
·111 lines (82 loc) · 2.48 KB
/
check_lxc-info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/perl
use strict;
use warnings;
my $verbose = 0;
my $perfdata;
my $warning_count = 0;
my $error_count = 0;
my @add_info = (); # Additinal info
my $lxc_id = $ARGV[0];
if ( $lxc_id !~ /^\d+$/ ) {
print "Error: First parameter must be the container id (numeric).\n";
exit 3; # UNKNOWN
}
my $lxc_out = qx@ /usr/bin/lxc-info $lxc_id 2>&1 @;
if ( $lxc_out =~ /doesn't exist/ ) {
print "UNKNOWN: container $lxc_id doesn't exist.\n";
exit 3; # UNKNOWN
}
# Case when LXC is stopped
if ( $lxc_out =~ /State:\s+STOPPED/ ) {
print "CRITICAL: container $lxc_id is stopped.\n";
exit 2; # CRITICAL
}
my @lxc_out = split /\n/, $lxc_out;
print "<<LXC:\n$lxc_out\n>>\n" if $verbose;
print "<<LXC list:\n@lxc_out\n>>\n" if $verbose;
my %lxc_status = ( "running" => 0, "total" => 0 );
my %qm_status = ( "running" => 0, "total" => 0 );
# Sample output:
# Name: 510
# State: RUNNING
# PID: 20483
# IP: 192.168.5.2
# CPU use: 1433.59 seconds
# BlkIO use: 429.38 MiB
# Memory use: 195.99 MiB
# KMem use: 44.29 MiB
# Link: veth510i0
# TX bytes: 99.26 MiB
# RX bytes: 519.41 MiB
# Total bytes: 618.66 MiB
my $state;
my $pid;
for ( "State", "PID", "IP", "CPU use", "BlkIO use", "Memory use", "KMem use", "TX bytes", "RX bytes" ) {
if ( $lxc_out =~ /^\s*$_:\s+(.*)$/m ) {
my $value = $1;
$state = $value if $_ eq "State";
$pid = $value if $_ eq "PID";
# Perf data
( my $perfkey = $_ ) =~ s/ /_/g;
$value =~ s/ //g;
$perfdata .= " $perfkey=$value" if $perfkey !~ /State|IP|PID/;
}
}
if ( $state ne "RUNNING" ) {
$error_count++;
push @add_info, "[CRIT] not in running state";
}
# Get some more details
my $pct_out = qx@ /usr/sbin/pct config $lxc_id @;
print "<<PCT CONFIG:\n$pct_out\n>>\n" if $verbose;
# Example output:
# arch: amd64
# cores: 1
# hostname: ansible
# memory: 512
# net0: name=eth0,bridge=vmbr0,gw=192.168.xxx.xxx,hwaddr=XX.XX.XX.XX.XX.XX,ip=192.168.xxx.xxx/24,type=veth
# ostype: archlinux
# rootfs: vms2:510/vm-510-disk-1.raw,size=8G
# swap: 512
( my $hostname ) = $pct_out =~ /^hostname: (.*)$/m;
print "OK: " if $error_count == 0 and $warning_count == 0;
print "WARNING: " if $error_count == 0 and $warning_count != 0;
print "CRITICAL: " if $error_count != 0;
print "$hostname, \L$state, PID $pid. ";
print "|$perfdata" if $perfdata;
print "\n";
print join "\n", @add_info if @add_info;
print "\n";
exit 2 if $error_count;
exit 1 if $warning_count;
exit 0;