~ubuntu-branches/ubuntu/quantal/lxc/quantal-201208301614

« back to all changes in this revision

Viewing changes to src/lxc/lxc-ps.in

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2010-01-25 15:39:38 UTC
  • mfrom: (1.2.3 upstream)
  • mto: (3.1.15 sid)
  • mto: This revision was merged to the branch mainline in revision 6.
  • Revision ID: james.westby@ubuntu.com-20100125153938-m2uhavs2e6ofnhkn
Tags: upstream-0.6.5
ImportĀ upstreamĀ versionĀ 0.6.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
our $LXC_DISPLAY = 0; # By default do not display container information
36
36
our %LXC_NAMES;       # Specified container names (if any)
37
37
 
38
 
 
39
 
# Reclaim the PID index in the ps output.
40
 
# The $PS_HEADERS must be set to the first line of ps output and must
41
 
# contains the columns headers.
42
 
# This function will set the $PS_PID_INDEX global or exit with an error
43
 
# message if the PID index can't be retrieved.
 
38
sub get_container_names {
 
39
        my $ref_names = shift;
 
40
        my $lxcpath='@LXCPATH@';
 
41
 
 
42
        open(active, "netstat -xa | grep $lxcpath |") or return;
 
43
        while(<active>) {
 
44
                chomp;
 
45
                s#.*$lxcpath/(.*)/command.*#$1#;
 
46
                push @$ref_names, $_;
 
47
        }
 
48
        close active;
 
49
}
 
50
 
 
51
sub get_cgroup {
 
52
        my $ref_cgroup = shift;
 
53
        my $mount_string;
 
54
 
 
55
        $mount_string=`mount -t cgroup |grep -E -e '^lxc '`;
 
56
        unless ($mount_string) {
 
57
                $mount_string=`mount |grep -m1 'type cgroup'`;
 
58
        }
 
59
        chomp($mount_string);
 
60
        if ($mount_string) {
 
61
                $$ref_cgroup=`echo "$mount_string" |cut -d' ' -f3`;
 
62
                chomp($$ref_cgroup);
 
63
        }
 
64
        die "unable to find mounted cgroup" unless $$ref_cgroup;
 
65
}
 
66
 
 
67
sub get_pids_in_containers {
 
68
        my $ref_names = shift;
 
69
        my $ref_cgroup = shift;
 
70
        my $ref_pids = shift;
 
71
        my @pidlist;
 
72
 
 
73
        for (@{$ref_names}) {
 
74
                my $task_file = "$$ref_cgroup/$_/tasks";
 
75
 
 
76
                $LXC_NAMES{$_} = 1;
 
77
                open(tasks, "cat $task_file 2>/dev/null |") or next;
 
78
                while (<tasks>) {
 
79
                        chomp $_;
 
80
                        push @pidlist, $_;
 
81
                }
 
82
                close tasks;
 
83
        }
 
84
        $$ref_pids = join(',', @pidlist);
 
85
}
44
86
 
45
87
sub reclaim_pid_index {
46
88
    my @headers = split " ", $PS_HEADERS;
54
96
    exit 1;
55
97
}
56
98
 
57
 
 
58
 
# Execute the ps command
59
 
 
60
99
sub execute_ps {
61
100
    open(ps, "ps @_ |") or die "Cannot execute ps command: $!\n";
62
101
 
63
 
    # Reclaim the PID index in ps output
64
102
    $PS_HEADERS = <ps>;
65
103
    reclaim_pid_index;
66
104
 
67
 
    # Reclaim lines of ps output
68
105
    while (<ps>) {
69
106
        push @PS_LINES, $_;
70
107
    }
71
108
    close ps;
72
109
}
73
110
 
74
 
 
75
 
# Get the container name (if any) associated to a given PID.
76
 
# @param $pid The PID to use.
77
 
# @return The container name as a string or an empty string.
78
 
 
79
111
sub get_container {
80
112
    my $pid = shift;
81
113
    my $filename = "/proc/$pid/cgroup";
91
123
    return $container;
92
124
}
93
125
 
94
 
 
95
 
# Display headers line.
96
 
# The $PS_HEADERS global must be set.
97
 
 
98
126
sub display_headers {
99
127
    printf "%-10s %s", "CONTAINER", $PS_HEADERS;
100
128
}
101
129
 
102
 
 
103
 
# Display command usage
104
 
 
105
130
sub display_usage {
106
131
    print <<EOF;
107
132
Usage: lxc-ps [--help] [--usage] [--name NAME...] [--lxc] [ps options]
108
133
EOF
109
134
}
110
135
 
111
 
 
112
 
# Display command help
113
 
 
114
136
sub display_help {
115
137
    display_usage;
116
138
    print <<EOF;
130
152
EOF
131
153
}
132
154
 
133
 
 
134
 
# Process lxc-ps arguments and build
135
 
 
136
155
use Getopt::Long qw(:config no_auto_abbrev pass_through);
137
156
 
138
157
my $arg_help  = '';
153
172
 
154
173
# Should we filter processes related to containers
155
174
if ($arg_lxc) {
156
 
    # Display processes related to all containers
157
 
    $LXC_DISPLAY = 1;
158
 
    @ARGV = ('-e', @ARGV);
159
 
} elsif (@arg_name > 0) {
160
 
    # Display processes related to specified containers
161
 
    $LXC_DISPLAY = 2;
162
 
    foreach (@arg_name) {
163
 
        $LXC_NAMES{$_} = 1;
164
 
    }
165
 
    @ARGV = ('-e', @ARGV);
166
 
}
167
 
 
168
 
 
169
 
# Execute the ps command
 
175
        $LXC_DISPLAY = 1;
 
176
        get_container_names \@arg_name;
 
177
}
 
178
if (@arg_name > 0) {
 
179
        my $cgroup;
 
180
        my $pid_list;
 
181
        $LXC_DISPLAY = 2;
 
182
 
 
183
        get_cgroup \$cgroup;
 
184
        get_pids_in_containers(\@arg_name, \$cgroup, \$pid_list);
 
185
        if ($pid_list) {
 
186
                @ARGV = ("-p $pid_list",@ARGV);
 
187
        }
 
188
}
170
189
 
171
190
execute_ps @ARGV;
172
191
 
173
 
 
174
 
# Display result with addition of container name
175
 
 
176
192
display_headers;
177
193
for (@PS_LINES) {
178
194
    my @a = split;
179
195
    my $container = get_container $a[$PS_PID_INDEX];
 
196
    if ($LXC_DISPLAY == 2 and not $LXC_NAMES{$container}) {next;}
180
197
    if ($LXC_DISPLAY == 1 and $container eq '') {next;}
181
 
    if ($LXC_DISPLAY == 2 and not $LXC_NAMES{$container}) {next;}
182
198
    printf "%-10s %s", $container, $_;
183
199
}
184
200
 
185
 
 
186
 
# Done
187
 
 
188
201
exit 0;