Commit 6e2faa37 by Jäkel, Guido Committed by Stéphane Graber

A new option '--host' for lxc-ps

Allow for an additional --host parameter to lxc-ps hiding all processes running in containers. Signed-off-by: 's avatarGuido Jäkel <G.Jaekel@dnb.de> Signed-off-by: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent 85296585
...@@ -51,6 +51,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ...@@ -51,6 +51,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
<command>lxc-ps</command> <command>lxc-ps</command>
<arg choice="opt">--name <replaceable>name</replaceable></arg> <arg choice="opt">--name <replaceable>name</replaceable></arg>
<arg choice="opt">--lxc</arg> <arg choice="opt">--lxc</arg>
<arg choice="opt">--host</arg>
<arg choice="opt">-- ps option</arg> <arg choice="opt">-- ps option</arg>
</cmdsynopsis> </cmdsynopsis>
</refsynopsisdiv> </refsynopsisdiv>
...@@ -106,6 +107,18 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ...@@ -106,6 +107,18 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
<varlistentry> <varlistentry>
<term> <term>
<option>--host</option>
</term>
<listitem>
<para>
limit the output to the processes belonging
to the host.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option><optional>ps options</optional></option> <option><optional>ps options</optional></option>
</term> </term>
<listitem> <listitem>
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
usage() usage()
{ {
echo "usage: $(basename $0) [--lxc | --name NAME] [--] [PS_OPTIONS...]" >&2 echo "usage: $(basename $0) [--lxc | --host | --name NAME] [--] [PS_OPTIONS...]" >&2
} }
help() { help() {
...@@ -28,6 +28,7 @@ help() { ...@@ -28,6 +28,7 @@ help() {
echo "List current processes with container names." >&2 echo "List current processes with container names." >&2
echo >&2 echo >&2
echo " --lxc show processes in all containers" >&2 echo " --lxc show processes in all containers" >&2
echo " --host show processes not related to any container, i.e. to the host" >&2
echo " --name NAME show processes in the specified container" >&2 echo " --name NAME show processes in the specified container" >&2
echo " (multiple containers can be separated by commas)" >&2 echo " (multiple containers can be separated by commas)" >&2
echo " PS_OPTIONS ps command options (see \`ps --help')" >&2 echo " PS_OPTIONS ps command options (see \`ps --help')" >&2
...@@ -80,6 +81,8 @@ while true; do ...@@ -80,6 +81,8 @@ while true; do
containers=$2; list_container_processes=1; shift 2;; containers=$2; list_container_processes=1; shift 2;;
--lxc) --lxc)
list_container_processes=1; shift;; list_container_processes=1; shift;;
--host)
list_container_processes=-1; shift;;
--) --)
shift; break;; shift; break;;
*) *)
...@@ -118,6 +121,7 @@ ps "$@" | awk -v container_field_width="$container_field_width" \ ...@@ -118,6 +121,7 @@ ps "$@" | awk -v container_field_width="$container_field_width" \
-v list_container_processes="$list_container_processes" ' -v list_container_processes="$list_container_processes" '
# first line is PS header # first line is PS header
NR == 1 { NR == 1 {
header = $0
# find pid field index # find pid field index
for (i = 1; i<=NF; i++) for (i = 1; i<=NF; i++)
if ($i == "PID") { if ($i == "PID") {
...@@ -126,9 +130,9 @@ NR == 1 { ...@@ -126,9 +130,9 @@ NR == 1 {
} }
if (pididx == "") { if (pididx == "") {
print("No PID field found") > "/dev/stderr" print("No PID field found") > "/dev/stderr"
header = "" # to signal error condition to the END rule
exit 1 exit 1
} }
header = $0
next next
} }
...@@ -152,10 +156,11 @@ FNR == 1 { ...@@ -152,10 +156,11 @@ FNR == 1 {
} }
END { END {
if (!header) exit 1 # quit due to internal error
printf("%-" container_field_width "s %s\n", "CONTAINER", header) printf("%-" container_field_width "s %s\n", "CONTAINER", header)
for (i in ps_line) { for (i in ps_line) {
container = container_of_pid[pid_of_line[i]] container = container_of_pid[pid_of_line[i]]
if (list_container_processes == 0 || container != "") if (list_container_processes == 0 || (container != "") == (list_container_processes > 0) )
printf("%-" container_field_width "s %s\n", container, ps_line[i]) printf("%-" container_field_width "s %s\n", container, ps_line[i])
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment