Projet

Général

Profil

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

root / plugins / docker / docker_memory @ ae03bc6f

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
   open(my $file, '<', "/sys/fs/cgroup/memory/docker/$id/memory.usage_in_bytes") or die "Unable to open file, $!";
70
   my $memory_bytes = <$file>;
71
   $memory_bytes =~ s/\s+$//;
72
   push @result, {'name'=>$name, 'memory_bytes'=>$memory_bytes}; 
73
}
74

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

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

    
96
# vim:syntax=perl