root / plugins / docker / docker_memory @ 3a20ae41
Historique | Voir | Annoter | Télécharger (1,96 ko)
| 1 | 6de11456 | Samuel Cantero | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | # -*- perl -*- |
||
| 3 | |||
| 4 | =head1 NAME |
||
| 5 | |||
| 6 | docker_memory - Munin plugin to monitor docker container memory usage. |
||
| 7 | |||
| 8 | =head1 APPLICABLE SYSTEMS |
||
| 9 | |||
| 10 | Should work on any Linux system that has docker support. |
||
| 11 | |||
| 12 | =head1 CONFIGURATION |
||
| 13 | |||
| 14 | Root privilege required to execute docker command. |
||
| 15 | |||
| 16 | 1. Create a new file named "docker" inside the folder /etc/munin/plugin-conf.d/ |
||
| 17 | 2. Docker file content: |
||
| 18 | |||
| 19 | 09b88141 | Lars Kruse | [docker_memory] |
| 20 | user root |
||
| 21 | 6de11456 | Samuel Cantero | |
| 22 | =head1 MAGIC MARKERS |
||
| 23 | |||
| 24 | 09b88141 | Lars Kruse | #%# family=auto |
| 25 | #%# capabilities=autoconf |
||
| 26 | 6de11456 | Samuel Cantero | |
| 27 | =head1 VERSION |
||
| 28 | |||
| 29 | 09b88141 | Lars Kruse | v.0.1 |
| 30 | 6de11456 | Samuel Cantero | |
| 31 | =head1 AUTHOR |
||
| 32 | |||
| 33 | 09b88141 | Lars Kruse | Copyright (C) 2015 Samuel Cantero <scanterog at gmail dot com> |
| 34 | 6de11456 | Samuel Cantero | |
| 35 | =head1 LICENSE |
||
| 36 | |||
| 37 | GPLv3 |
||
| 38 | |||
| 39 | =cut |
||
| 40 | |||
| 41 | ae03bc6f | Samuel Cantero | my $docker=`which docker`; |
| 42 | |||
| 43 | if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) {
|
||
| 44 | if ($docker) {
|
||
| 45 | print "yes\n"; |
||
| 46 | exit 0; |
||
| 47 | } |
||
| 48 | else{
|
||
| 49 | print "no (Docker has not been found)\n"; |
||
| 50 | exit 0; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | $docker =~ s/\s+$//; |
||
| 55 | |||
| 56 | my @containers = split "\n" , `$docker ps --no-trunc=true`; |
||
| 57 | 6de11456 | Samuel Cantero | my $result; |
| 58 | |||
| 59 | for my $i (1 .. $#containers) |
||
| 60 | {
|
||
| 61 | my @fields = split / +/, $containers[$i]; |
||
| 62 | my $id = $fields[0]; |
||
| 63 | my $name = $fields[$#fields]; |
||
| 64 | # manage container name containing arithmetic operators and dots. E.g, my-container. |
||
| 65 | $name =~ s/[-\+*\/\.]/_/g; |
||
| 66 | # truncate container name with "," character. |
||
| 67 | $name =~ s/,.*//g; |
||
| 68 | 0a5f6adf | Samuel Cantero | if (open(my $file, '<', "/sys/fs/cgroup/memory/docker/$id/memory.usage_in_bytes")) |
| 69 | {
|
||
| 70 | my $memory_bytes = <$file>; |
||
| 71 | $memory_bytes =~ s/\s+$//; |
||
| 72 | push @result, {'name'=>$name, 'memory_bytes'=>$memory_bytes};
|
||
| 73 | } |
||
| 74 | 6de11456 | Samuel Cantero | } |
| 75 | |||
| 76 | if (defined $ARGV[0] and $ARGV[0] eq "config") |
||
| 77 | {
|
||
| 78 | print "graph_title Docker container memory usage\n"; |
||
| 79 | print "graph_args --base 1024 -l 0\n"; |
||
| 80 | print "graph_vlabel Bytes\n"; |
||
| 81 | 6c3ce4e1 | dipohl | print "graph_category virtualization\n"; |
| 82 | 6de11456 | Samuel Cantero | print "graph_info This graph shows docker container memory usage.\n"; |
| 83 | 17f78427 | Lars Kruse | |
| 84 | 6de11456 | Samuel Cantero | foreach(@result) |
| 85 | {
|
||
| 86 | print "$$_{'name'}.label $$_{'name'}\n";
|
||
| 87 | print "$$_{'name'}.draw LINE2\n";
|
||
| 88 | } |
||
| 89 | exit 0; |
||
| 90 | } |
||
| 91 | |||
| 92 | foreach(@result) |
||
| 93 | {
|
||
| 94 | print "$$_{'name'}.value $$_{'memory_bytes'}\n";
|
||
| 95 | } |
||
| 96 | |||
| 97 | # vim:syntax=perl |
