Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / docker / docker_cpu @ ae03bc6f

Historique | Voir | Annoter | Télécharger (2,69 ko)

1
#!/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
[docker_cpu]
20
user root
21

    
22
=head1 MAGIC MARKERS
23

    
24
  #%# family=auto
25
  #%# capabilities=autoconf
26

    
27
=head1 VERSION
28

    
29
  v.0.1
30

    
31
=head1 AUTHOR
32

    
33
Copyright (C) 2015 Samuel Cantero.
34
Email: scanterog at gmail dot com
35

    
36
=head1 LICENSE
37

    
38
GPLv3
39

    
40
=cut
41

    
42
my $docker=`which docker`;
43

    
44
if ( defined $ARGV[0] and $ARGV[0] eq "autoconf" ) {
45
   if ($docker) {
46
      print "yes\n";
47
      exit 0;
48
   }
49
   else{
50
      print "no (Docker has not been found)\n";
51
      exit 0;
52
   }
53
}
54

    
55
$docker =~ s/\s+$//;
56

    
57
my @containers = split "\n" , `$docker ps --no-trunc=true`;
58
my $result;
59

    
60
for my $i (1 .. $#containers)
61
{
62
   my @fields = split / +/, $containers[$i];
63
   my $id = $fields[0];
64
   my $name = $fields[$#fields];
65
   # 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
   open(my $file, '<', "/sys/fs/cgroup/cpuacct/docker/$id/cpuacct.usage") or die "Unable to open file, $!";
70
   my $total_cpu_ns = <$file>;
71
   $total_cpu_ns =~ s/\s+$//;
72
   close $file;
73
   open($file, '<', "/sys/fs/cgroup/cpuacct/docker/$id/cpuacct.usage_percpu") or die "Unable to open file, $!";
74
   my @ncpu = split / /, <$file>;
75
   close $file;
76
   push @result, {'name'=>$name, 'total_cpu_ns'=>$total_cpu_ns, 'ncpu'=>$#ncpu};
77
}
78
 
79
if (defined $ARGV[0] and $ARGV[0] eq "config")
80
{
81
   my $nanoSecondsInSecond=1000000000;
82
   my $graphlimit = $result[0]{'ncpu'};
83
   foreach(@result){
84
      if ($$_{'ncpu'} > $graphlimit){
85
         $graphlimit = $$_{'ncpu'};
86
      }
87
   } 
88
   $graphlimit = $graphlimit * 100;
89
   print "graph_title Docker container CPU usage\n";
90
   print "graph_args --base 1000 -r --lower-limit 0 --upper-limit $graphlimit\n";
91
   print "graph_vlabel %\n";
92
   print "graph_scale no\n";
93
   print "graph_period second\n";
94
   print "graph_category Docker\n";
95
   print "graph_info This graph shows docker container CPU usage.\n";
96
   
97
   foreach(@result)
98
   {
99
      print "$$_{'name'}.label $$_{'name'}\n";
100
      print "$$_{'name'}.draw LINE2\n";
101
      print "$$_{'name'}.min 0\n";
102
      print "$$_{'name'}.type DERIVE\n";
103
      print "$$_{'name'}.cdef $$_{'name'},$nanoSecondsInSecond,/\n";
104
   }
105
   exit 0;
106
}
107

    
108
# Note: Counters/derive need to report integer values.
109

    
110
foreach(@result)
111
{
112
   $tcpu = ($$_{'total_cpu_ns'}*100); #to percentage
113
   print "$$_{'name'}.value $tcpu\n";
114
}
115

    
116
# vim:syntax=perl