root / plugins / docker / docker_cpu @ 09b88141
Historique | Voir | Annoter | Télécharger (2,78 ko)
| 1 | 6de11456 | Samuel Cantero | #!/usr/bin/perl -w |
|---|---|---|---|
| 2 | # -*- perl -*- |
||
| 3 | |||
| 4 | =head1 NAME |
||
| 5 | |||
| 6 | docker_cpu - Munin plugin to monitor docker container CPU 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_cpu] |
| 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 | 0853d409 | Andreas Perhab | my $label = $name; |
| 65 | 6de11456 | Samuel Cantero | # manage container name containing arithmetic operators and dots. E.g, my-container. |
| 66 | $name =~ s/[-\+*\/\.]/_/g; |
||
| 67 | # truncate container name with "," character. |
||
| 68 | $name =~ s/,.*//g; |
||
| 69 | 0853d409 | Andreas Perhab | # prefix if container starts with 0-9 |
| 70 | $name =~ s/^([0-9])/c$1/; |
||
| 71 | 0a5f6adf | Samuel Cantero | if (open(my $file, '<', "/sys/fs/cgroup/cpuacct/docker/$id/cpuacct.usage")) |
| 72 | {
|
||
| 73 | my $total_cpu_ns = <$file>; |
||
| 74 | $total_cpu_ns =~ s/\s+$//; |
||
| 75 | close $file; |
||
| 76 | if (open($file, '<', "/sys/fs/cgroup/cpuacct/docker/$id/cpuacct.usage_percpu")) |
||
| 77 | {
|
||
| 78 | my @ncpu = split / /, <$file>; |
||
| 79 | close $file; |
||
| 80 | 0853d409 | Andreas Perhab | push @result, {'name'=>$name, 'label'=>$label, 'total_cpu_ns'=>$total_cpu_ns, 'ncpu'=>$#ncpu};
|
| 81 | 0a5f6adf | Samuel Cantero | } |
| 82 | } |
||
| 83 | 6de11456 | Samuel Cantero | } |
| 84 | 17f78427 | Lars Kruse | |
| 85 | 6de11456 | Samuel Cantero | if (defined $ARGV[0] and $ARGV[0] eq "config") |
| 86 | {
|
||
| 87 | my $nanoSecondsInSecond=1000000000; |
||
| 88 | 260c8c8f | Andreas Perhab | my $graphlimit = 1; |
| 89 | 6de11456 | Samuel Cantero | foreach(@result){
|
| 90 | 260c8c8f | Andreas Perhab | if ($$_{'ncpu'} || 1 > $graphlimit){
|
| 91 | 6de11456 | Samuel Cantero | $graphlimit = $$_{'ncpu'};
|
| 92 | } |
||
| 93 | 17f78427 | Lars Kruse | } |
| 94 | 6de11456 | Samuel Cantero | $graphlimit = $graphlimit * 100; |
| 95 | print "graph_title Docker container CPU usage\n"; |
||
| 96 | print "graph_args --base 1000 -r --lower-limit 0 --upper-limit $graphlimit\n"; |
||
| 97 | print "graph_vlabel %\n"; |
||
| 98 | print "graph_scale no\n"; |
||
| 99 | print "graph_period second\n"; |
||
| 100 | 6c3ce4e1 | dipohl | print "graph_category virtualization\n"; |
| 101 | 6de11456 | Samuel Cantero | print "graph_info This graph shows docker container CPU usage.\n"; |
| 102 | 17f78427 | Lars Kruse | |
| 103 | 6de11456 | Samuel Cantero | foreach(@result) |
| 104 | {
|
||
| 105 | 0853d409 | Andreas Perhab | print "$$_{'name'}.label $$_{'label'}\n";
|
| 106 | 6de11456 | Samuel Cantero | print "$$_{'name'}.draw LINE2\n";
|
| 107 | print "$$_{'name'}.min 0\n";
|
||
| 108 | print "$$_{'name'}.type DERIVE\n";
|
||
| 109 | print "$$_{'name'}.cdef $$_{'name'},$nanoSecondsInSecond,/\n";
|
||
| 110 | } |
||
| 111 | exit 0; |
||
| 112 | } |
||
| 113 | |||
| 114 | # Note: Counters/derive need to report integer values. |
||
| 115 | |||
| 116 | foreach(@result) |
||
| 117 | {
|
||
| 118 | $tcpu = ($$_{'total_cpu_ns'}*100); #to percentage
|
||
| 119 | print "$$_{'name'}.value $tcpu\n";
|
||
| 120 | } |
||
| 121 | |||
| 122 | # vim:syntax=perl |
