Projet

Général

Profil

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

root / plugins / docker / docker_memory @ 6c3ce4e1

Historique | Voir | Annoter | Télécharger (1,97 ko)

1
#!/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
[docker_memory]
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/memory/docker/$id/memory.usage_in_bytes"))
70
   {
71
      my $memory_bytes = <$file>;
72
      $memory_bytes =~ s/\s+$//;
73
      push @result, {'name'=>$name, 'memory_bytes'=>$memory_bytes};
74
   }
75
}
76

    
77
if (defined $ARGV[0] and $ARGV[0] eq "config")
78
{
79
   print "graph_title Docker container memory usage\n";
80
   print "graph_args --base 1024 -l 0\n";
81
   print "graph_vlabel Bytes\n";
82
   print "graph_category virtualization\n";
83
   print "graph_info This graph shows docker container memory usage.\n";
84
   
85
   foreach(@result)
86
   {
87
      print "$$_{'name'}.label $$_{'name'}\n";
88
      print "$$_{'name'}.draw LINE2\n";
89
   }
90
   exit 0;
91
}
92

    
93
foreach(@result)
94
{
95
   print "$$_{'name'}.value $$_{'memory_bytes'}\n";
96
}
97

    
98
# vim:syntax=perl