Provide a verbose list option to include cpu and memory usage from ps(1)

This commit is contained in:
Matt Churchyard
2020-01-29 14:45:26 +00:00
parent 495a62c671
commit 7fcf2c1d92
6 changed files with 57 additions and 11 deletions

View File

@@ -25,7 +25,7 @@
# POSSIBILITY OF SUCH DAMAGE.
VERSION=1.5-devel
VERSION_INT=105001
VERSION_INT=105002
VERSION_BSD=$(uname -K)
PATH=${PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin

View File

@@ -67,7 +67,7 @@ cmd::parse(){
image) cmd::parse_image "$@" ;;
get) core::get "$@" ;;
set) core::set "$@" ;;
list) core::list ;;
list) core::list "$@" ;;
create) core::create "$@" ;;
destroy) core::destroy "$@" ;;
rename) core::rename "$@" ;;
@@ -184,12 +184,13 @@ cmd::parse_image(){
cmd::parse_args(){
local _opt _count
while getopts fit _opt; do
while getopts fitv _opt; do
case ${_opt} in
f) VM_OPT_FORCE="1"
VM_OPT_FOREGROUND="1" ;;
i) VM_OPT_INTERACTIVE="1" ;;
t) VM_OPT_TMUX="1" ;;
v) VM_OPT_VERBOSE="1" ;;
esac
done

View File

@@ -29,16 +29,26 @@
#
core::list(){
local _name _loader _cpu _our_host
local _memory _run _vm _auto _num _graphics _vnc
local _memory _run _vm _auto _num _graphics _vnc _pid
local _state _pcpu _vsz _rss
local _format="%s^%s^%s^%s^%s^%s^%s^%s\n"
cmd::parse_args "$@"
shift $?
_our_host=$(hostname)
vm::running_load
[ -n "${VM_OPT_VERBOSE}" ] && _format="%s^%s^%s^%s^%s^%s^%s^%s^%s^%s^%s\n";
# pass everything below here to column(1)
{
printf "${_format}" "NAME" "DATASTORE" "LOADER" "CPU" "MEMORY" "VNC" "AUTOSTART" "STATE"
if [ -n "${VM_OPT_VERBOSE}" ]; then
printf "${_format}" "NAME" "DATASTORE" "LOADER" "CPU" "MEMORY" "VNC" "AUTOSTART" "%CPU" "VSZ" "RSS" "STATE"
else
printf "${_format}" "NAME" "DATASTORE" "LOADER" "CPU" "MEMORY" "VNC" "AUTOSTART" "STATE"
fi
for _ds in ${VM_DATASTORE_LIST}; do
datastore::get "${_ds}" || continue
@@ -55,9 +65,14 @@ core::list(){
# defaults
_vnc="-"
_pid=""
_state=""
_pcpu="-"
_vsz="-"
_rss="-"
# check if the guest is running
if vm::running_check "_run" "${_name}" || \
if vm::running_check "_run" "_pid" "${_name}" || \
[ -e "${VM_DS_PATH}/${_name}/run.lock" -a "$(head -n1 ${VM_DS_PATH}/${_name}/run.lock 2>/dev/null)" = "${_our_host}" ]; then
# if running and graphics, try to get vnc port
@@ -67,6 +82,19 @@ core::list(){
fi
fi
if [ -n "${_pid}" -a -n "${VM_OPT_VERBOSE}" ]; then
_state=$(ps -o"%cpu"= -o"vsz"= -o"rss"= -p "${_pid}")
if [ -n "${_state}" ]; then
util::get_part "_pcpu" "${_state}" 1
util::get_part "_vsz" "${_state}" 2
util::get_part "_rss" "${_state}" 3
[ -n "${_vsz}" ] && _vsz=$(info::__bytes_human "${_vsz}" 1)
[ -n "${_rss}" ] && _rss=$(info::__bytes_human "${_rss}" 1)
fi
fi
_num=1
_auto="No"
@@ -82,7 +110,11 @@ core::list(){
_run="Locked (${_run})"
fi
printf "${_format}" "${_name}" "${_ds}" "${_loader}" "${_cpu}" "${_memory}" "${_vnc}" "${_auto}" "${_run}"
if [ -n "${VM_OPT_VERBOSE}" ]; then
printf "${_format}" "${_name}" "${_ds}" "${_loader}" "${_cpu}" "${_memory}" "${_vnc}" "${_auto}" "${_pcpu}" "${_vsz}" "${_rss}" "${_run}"
else
printf "${_format}" "${_name}" "${_ds}" "${_loader}" "${_cpu}" "${_memory}" "${_vnc}" "${_auto}" "${_run}"
fi
done
done
} | column -ts^

View File

@@ -163,7 +163,7 @@ info::guest_show(){
local _conf="${VM_DS_PATH}/${_name}/${_name}.conf"
local _INDENT=" "
local _RUN="0"
local _res_mem _b_res_mem _global_run _port _opt
local _res_mem _b_res_mem _global_run _port _opt _pid
[ -z "${_name}" ] && return 1
[ ! -f "${_conf}" ] && return 1
@@ -172,7 +172,7 @@ info::guest_show(){
# check local and global runstate
[ -e "/dev/vmm/${_name}" ] && _RUN="1"
vm::running_check "_global_run" "${_name}"
vm::running_check "_global_run" "_pid" "${_name}"
_global_run=$(echo "${_global_run}" | tr '[:upper:]' '[:lower:]')
echo "------------------------"
@@ -445,8 +445,10 @@ info::__find_bridge(){
#
info::__bytes_human(){
local _val="$1" _int _ext
local _dec="$2"
local _num=1
: ${_dec:=3}
: ${_val:=0}
_int=${_val%%.*}
@@ -465,7 +467,7 @@ info::__bytes_human(){
esac
export LC_ALL="C"
printf "%.3f%s" "${_val}" "${_ext}"
printf "%.${_dec}f%s" "${_val}" "${_ext}"
}
info::__find_iscsi() {

View File

@@ -882,13 +882,15 @@ vm::running_load(){
#
vm::running_check(){
local _var="$1"
local _name="$2"
local _var2="$2"
local _name="$3"
local IFS=$'\n'
local _entry
for _entry in ${VM_RUN_BHYVE}; do
if [ "${_entry##* }" = "${_name}" ]; then
setvar "${_var}" "Running (${_entry%% *})"
setvar "${_var2}" "${_entry%% *}"
return 0
fi
done
@@ -896,6 +898,7 @@ vm::running_check(){
for _entry in ${VM_RUN_LOAD}; do
if [ "${_entry##* }" = "${_name}" ]; then
setvar "${_var}" "Bootloader (${_entry%% *})"
setvar "${_var2}" "${_entry%% *}"
return 0
fi
done

View File

@@ -388,3 +388,11 @@ util::getpid(){
[ $? -eq 0 ] || return 1
setvar "${_var}" "${_ret}"
}
util::get_part(){
local _var="$1"
local _data="$2"
local _num="$3"
setvar "${_var}" $(echo "${_data}" |cut -d" " -f${_num})
}