munin-contrib/plugins/docker/docker_memory

121 lines
2.8 KiB
Perl
Executable File

#!/usr/bin/perl -w
# -*- perl -*-
=head1 NAME
docker_memory - Munin plugin to monitor docker container memory usage.
=head1 APPLICABLE SYSTEMS
Should work on any Linux system that has docker support.
=head1 CONFIGURATION
Root privilege required to execute docker command.
1. Create a new file named "docker" inside the folder /etc/munin/plugin-conf.d/
2. Docker file content:
[docker_memory]
user root
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf
=head1 VERSION
v.0.1
=head1 AUTHOR
Copyright (C) 2015 Samuel Cantero <scanterog at gmail dot com>
=head1 LICENSE
GPLv3
=cut
my $docker=`which docker`;
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) {
if ($docker) {
print "yes\n";
exit 0;
}
else{
print "no (Docker has not been found)\n";
exit 0;
}
}
$docker =~ s/\s+$//;
my @containers = split "\n" , `$docker ps --no-trunc=true`;
my $result;
my $total = 0;
for my $i (1 .. $#containers)
{
my @fields = split / +/, $containers[$i];
my $id = $fields[0];
my $name = $fields[$#fields];
# manage container name containing arithmetic operators and dots. E.g, my-container.
$name =~ s/[-\+*\/\.]/_/g;
# truncate container name with "," character.
$name =~ s/,.*//g;
if (open(my $file, '<', "/sys/fs/cgroup/memory/docker/$id/memory.usage_in_bytes"))
{
# https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
my $memory_bytes = <$file>;
$memory_bytes =~ s/\s+$//;
push @result, {'name'=>$name, 'memory_bytes'=>$memory_bytes};
$total = $total + $memory_bytes;
}
elsif(open($file, '<', "/sys/fs/cgroup/system.slice/docker-$id.scope/memory.current"))
{
# https://www.kernel.org/doc/Documentation/cgroup-v2.txt
# hexdump -C < /sys/fs/cgroup/system.slice/docker-f226ca5e7e61b884a87ae25912b6da1a62cc7c518add8940dfd81c6e6015a738.scope/memory.current
# 00000000 39 35 30 32 37 32 30 0a |9502720.|
# 00000008
my $memory_bytes = <$file>;
$memory_bytes =~ s/\s+$//;
push @result, {'name'=>$name, 'memory_bytes'=>$memory_bytes};
$total = $total + $memory_bytes;
}
}
if (defined $ARGV[0] and $ARGV[0] eq "config")
{
print "graph_title Docker container memory usage\n";
print "graph_args --base 1024 -l 0\n";
print "graph_vlabel Bytes\n";
print "graph_category virtualization\n";
print "graph_info This graph shows docker container memory usage.\n";
my $first = 1;
foreach(@result)
{
print "$$_{'name'}.label $$_{'name'}\n";
if ($first) {
print "$$_{'name'}.draw AREA\n";
$first = 0
} else {
print "$$_{'name'}.draw STACK\n";
}
}
print "total.label Total Memory\n";
print "total.draw LINE2\n";
exit 0;
}
foreach(@result)
{
print "$$_{'name'}.value $$_{'memory_bytes'}\n";
}
print "total.value $total\n";
# vim:syntax=perl