aboutsummaryrefslogtreecommitdiff
blob: 2fa7b8b931ebcde28a202f5ca22a45f2ab53995d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/perl
#
# lxc-ps
#
# Authors:
# Daniel Lezcano <daniel.lezcano@free.fr>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

#
# This script allows to
# display processes information with related container name if available.
#
use strict;


# Some globals

our $PS_HEADERS;      # String containing headers of the ps output
our $PS_PID_INDEX;    # Index of the PID column in the ps headers
our @PS_LINES;        # Output lines of the ps command

our $LXC_DISPLAY = 0; # By default do not display container information
our %LXC_NAMES;       # Specified container names (if any)

sub get_container_names {
	my $ref_names = shift;
	my $lxcpath = '@LXCPATH@';

	open(active, "netstat -xa | grep $lxcpath |") or return;
	while(<active>) {
		chomp;
		s#.*$lxcpath/(.*)/command.*#$1#;
		push @$ref_names, $_;
	}
	close active;
}

sub get_cgroup {
	my $ref_cgroup = shift;
	my $mount_string;

	$mount_string=`mount -t cgroup |grep -E -e '^lxc '`;
	if ($mount_string) {
                # use the one 'lxc' cgroup mount if it exists
		chomp($mount_string);
		$$ref_cgroup=`echo "$mount_string" |cut -d' ' -f3`;
		chomp($$ref_cgroup);
	}
	# Otherwise (i.e. cgroup-bin) use the first cgroup mount
	$mount_string=`grep -m1 -E '^[^ \t]+[ \t]+[^ \t]+[ \t]+cgroup' /proc/self/mounts`;
	unless ($mount_string) {
		die "unable to find mounted cgroup" unless $$ref_cgroup;
	}
	chomp($mount_string);
	$$ref_cgroup=`echo "$mount_string" |cut -d' ' -f2`;
	chomp($$ref_cgroup);
	return;
}

sub get_pids_in_containers {
	my $ref_names = shift;
	my $ref_cgroup = shift;
	my $ref_pids = shift;
	my $init_cgroup = shift;
	my @pidlist;

	for (@{$ref_names}) {
		my $task_file = "$$ref_cgroup/$init_cgroup/lxc/$_/tasks";

		$LXC_NAMES{$_} = 1;
		open(tasks, "cat $task_file 2>/dev/null |") or next;
		while (<tasks>) {
			chomp $_;
			push @pidlist, $_;
		}
		close tasks;
	}
	$$ref_pids = join(',', @pidlist);
}

sub reclaim_pid_index {
    my @headers = split " ", $PS_HEADERS;
    for my $i (0 .. $#headers) {
	if ($headers[$i] eq "PID") {
	    $PS_PID_INDEX = $i;
	    return;
	}
    }
    print "Cannot find ps PID column !\n";
    exit 1;
}

sub execute_ps {
    open(ps, "ps @_ |") or die "Cannot execute ps command: $!\n";

    $PS_HEADERS = <ps>;
    reclaim_pid_index;

    while (<ps>) {
	push @PS_LINES, $_;
    }
    close ps;
}

sub get_init_cgroup {
    my $filename = "/proc/1/cgroup";
    open(LXC, "$filename");
    my @cgroup = <LXC>;
    close LXC;
    my $container = '';
    foreach ( @cgroup ) {
        chomp;
        # find the container name after :/
        s/.*:\///o;
    }
    return $container;
}

sub get_container {
    my $pid = shift;
    my $filename = "/proc/$pid/cgroup";
    open(LXC, "$filename");
    # read all lines at once
    my @cgroup = <LXC>;
    close LXC;
    my $container = '';
    foreach ( @cgroup ) {
        chomp;
        # find the container name after :/
        s/.*:\///o;
        # chop off everything up to 'lxc/'
        s/lxc\///o;
        $container = $_;
    }
    return $container;
}

sub display_headers {
    printf "%-10s %s", "CONTAINER", $PS_HEADERS;
}

sub display_usage {
    print <<EOF;
Usage: lxc-ps [--help] [--usage] [-n|--name NAME...] [--lxc] [-- ps options]
EOF
}

sub display_help {
    display_usage;
    print <<EOF;

Display processes information with related container name if available.

Options:
--help     Display this help.
--usage    Display the command usage.
--name     Display processes related to given containers.
           Containers are identified by a comma separated list of
	   their names.
--lxc      Display processes related to all lxc containers.

Other available options correspond to the ps ones, see the ps manual
or try a 'ps --help' for further details.
EOF
}

use Getopt::Long qw(:config pass_through);

my $arg_help  = '';
my $arg_usage = '';
my $arg_lxc   = '';
my @arg_name;
my $init_cgroup = '/';

GetOptions('help'   => \$arg_help,
	   'usage'  => \$arg_usage,
	   'lxc'    => \$arg_lxc,
	   'name=s' => \@arg_name);

@arg_name = split(/,/, join(',', @arg_name));

# Some help
if ($arg_help)  {display_help; exit 0;}
if ($arg_usage) {display_usage; exit 0;}

if ($ARGV[0] == '--') {
	shift @ARGV;
}

# Should we filter processes related to containers
if ($arg_lxc) {
	$LXC_DISPLAY = 1;
	get_container_names \@arg_name;
}
if (@arg_name > 0) {
	my $cgroup;
	my $pid_list;
	$LXC_DISPLAY = 2;

	$init_cgroup = get_init_cgroup();
	get_cgroup \$cgroup;
	get_pids_in_containers(\@arg_name, \$cgroup, \$pid_list, $init_cgroup);
	if ($pid_list) {
		@ARGV = ("-p $pid_list",@ARGV);
	}
}

execute_ps @ARGV;

display_headers;
for (@PS_LINES) {
    my @a = split;
    my $container = get_container $a[$PS_PID_INDEX];
    if ($LXC_DISPLAY == 2 and not $LXC_NAMES{$container}) {next;}
    if ($LXC_DISPLAY == 1 and $container eq '') {next;}
    printf "%-10s %s", $container, $_;
}

exit 0;