Projet

Général

Profil

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

root / plugins / docker / docker_cpu @ 09b88141

Historique | Voir | Annoter | Télécharger (2,78 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 <scanterog at gmail dot com>
34

    
35
=head1 LICENSE
36

    
37
GPLv3
38

    
39
=cut
40

    
41
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
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
   my $label = $name;
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
   # prefix if container starts with 0-9
70
   $name =~ s/^([0-9])/c$1/;
71
   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
         push @result, {'name'=>$name, 'label'=>$label, 'total_cpu_ns'=>$total_cpu_ns, 'ncpu'=>$#ncpu};
81
      }
82
   }
83
}
84

    
85
if (defined $ARGV[0] and $ARGV[0] eq "config")
86
{
87
   my $nanoSecondsInSecond=1000000000;
88
   my $graphlimit = 1;
89
   foreach(@result){
90
      if ($$_{'ncpu'} || 1 > $graphlimit){
91
         $graphlimit = $$_{'ncpu'};
92
      }
93
   }
94
   $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
   print "graph_category virtualization\n";
101
   print "graph_info This graph shows docker container CPU usage.\n";
102

    
103
   foreach(@result)
104
   {
105
      print "$$_{'name'}.label $$_{'label'}\n";
106
      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