Projet

Général

Profil

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

root / plugins / docker / docker_cpu @ 17f78427

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
   if (open(my $file, '<', "/sys/fs/cgroup/cpuacct/docker/$id/cpuacct.usage"))
70
   {
71
      my $total_cpu_ns = <$file>;
72
      $total_cpu_ns =~ s/\s+$//;
73
      close $file;
74
      if (open($file, '<', "/sys/fs/cgroup/cpuacct/docker/$id/cpuacct.usage_percpu"))
75
      {
76
         my @ncpu = split / /, <$file>;
77
         close $file;
78
         push @result, {'name'=>$name, 'total_cpu_ns'=>$total_cpu_ns, 'ncpu'=>$#ncpu};
79
      }
80
   }
81
}
82

    
83
if (defined $ARGV[0] and $ARGV[0] eq "config")
84
{
85
   my $nanoSecondsInSecond=1000000000;
86
   my $graphlimit = $result[0]{'ncpu'};
87
   foreach(@result){
88
      if ($$_{'ncpu'} > $graphlimit){
89
         $graphlimit = $$_{'ncpu'};
90
      }
91
   }
92
   $graphlimit = $graphlimit * 100;
93
   print "graph_title Docker container CPU usage\n";
94
   print "graph_args --base 1000 -r --lower-limit 0 --upper-limit $graphlimit\n";
95
   print "graph_vlabel %\n";
96
   print "graph_scale no\n";
97
   print "graph_period second\n";
98
   print "graph_category virtualization\n";
99
   print "graph_info This graph shows docker container CPU usage.\n";
100

    
101
   foreach(@result)
102
   {
103
      print "$$_{'name'}.label $$_{'name'}\n";
104
      print "$$_{'name'}.draw LINE2\n";
105
      print "$$_{'name'}.min 0\n";
106
      print "$$_{'name'}.type DERIVE\n";
107
      print "$$_{'name'}.cdef $$_{'name'},$nanoSecondsInSecond,/\n";
108
   }
109
   exit 0;
110
}
111

    
112
# Note: Counters/derive need to report integer values.
113

    
114
foreach(@result)
115
{
116
   $tcpu = ($$_{'total_cpu_ns'}*100); #to percentage
117
   print "$$_{'name'}.value $tcpu\n";
118
}
119

    
120
# vim:syntax=perl