]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc-ps.in
6 # Daniel Lezcano <daniel.lezcano@free.fr>
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 # This script allows to
24 # display processes information with related container name if available.
31 our $PS_HEADERS; # String containing headers of the ps output
32 our $PS_PID_INDEX; # Index of the PID column in the ps headers
33 our @PS_LINES; # Output lines of the ps command
35 our $LXC_DISPLAY = 0; # By default do not display container information
36 our %LXC_NAMES; # Specified container names (if any)
38 sub get_container_names
{
39 my $ref_names = shift;
40 my $lxcpath='@LXCPATH@';
42 open(active
, "netstat -xa | grep $lxcpath |") or return;
45 s
#.*$lxcpath/(.*)/command.*#$1#;
52 my $ref_cgroup = shift;
55 $mount_string=`mount -t cgroup |grep -E -e '^lxc '`;
56 unless ($mount_string) {
57 $mount_string=`mount |grep -m1 'type cgroup'`;
61 $$ref_cgroup=`echo "$mount_string" |cut -d' ' -f3`;
64 die "unable to find mounted cgroup" unless $$ref_cgroup;
67 sub get_pids_in_containers
{
68 my $ref_names = shift;
69 my $ref_cgroup = shift;
74 my $task_file = "$$ref_cgroup/$_/tasks";
77 open(tasks
, "cat $task_file 2>/dev/null |") or next;
84 $$ref_pids = join(',', @pidlist);
87 sub reclaim_pid_index
{
88 my @headers = split " ", $PS_HEADERS;
89 for my $i (0 .. $#headers) {
90 if ($headers[$i] eq "PID") {
95 print "Cannot find ps PID column !\n";
100 open(ps
, "ps @_ |") or die "Cannot execute ps command: $!\n";
113 my $filename = "/proc/$pid/cgroup";
114 open(LXC
, "$filename");
115 my $container = <LXC
>;
118 if ($container =~ m/[:,]ns[:,]/o) {
119 $container =~ s/.*:\///o
;
126 sub display_headers
{
127 printf "%-10s %s", "CONTAINER", $PS_HEADERS;
132 Usage: lxc-ps [--help] [--usage] [--name NAME...] [--lxc] [ps options]
140 Display processes information with related container name if available.
143 --help Display this help.
144 --usage Display the command usage.
145 --name Display processes related to given containers.
146 Containers are identified by a comma separated list of
148 --lxc Display processes related to all lxc containers.
150 Other available options correspond to the ps ones, see the ps manual
151 or try a 'ps --help' for further details.
155 use Getopt
::Long
qw(:config no_auto_abbrev pass_through);
162 GetOptions
('help' => \
$arg_help,
163 'usage' => \
$arg_usage,
165 'name=s' => \
@arg_name);
167 @arg_name = split(/,/, join(',', @arg_name));
170 if ($arg_help) {display_help
; exit 0;}
171 if ($arg_usage) {display_usage
; exit 0;}
173 # Should we filter processes related to containers
176 get_container_names \
@arg_name;
184 get_pids_in_containers
(\
@arg_name, \
$cgroup, \
$pid_list);
186 @ARGV = ("-p $pid_list",@ARGV);
195 my $container = get_container
$a[$PS_PID_INDEX];
196 if ($LXC_DISPLAY == 2 and not $LXC_NAMES{$container}) {next;}
197 if ($LXC_DISPLAY == 1 and $container eq '') {next;}
198 printf "%-10s %s", $container, $_;