Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils/cms_monPerf : simplify and improve #1011

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 36 additions & 15 deletions utils/cms_monPerf
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@
INTERVAL="${1:-1}" # take as parameter the interval between runs, default to 1; NB! the loop take more than a few seconds to run
NCPU=$(awk '/processor/ {nr++} END{print nr}' /proc/cpuinfo)

# replace sleep subprocess
snore() {
local IFS
[[ -n "${_snore_fd:-}" ]] || exec {_snore_fd}<> <(:)
read ${1:+-t "$1"} -u $_snore_fd || :
}

# return highest load (integer percentage) of either RX or TX
IFACE_LOAD () {
local iface="${1}"
SPEED=$(< "${iface}"/speed)
[[ "$?" -ne 0 || ! "${SPEED}" -gt 0 ]] && return 1 # bail out if speed is not valid
[[ "$?" -ne 0 || ! "${SPEED}" -gt 0 ]] && { echo 0; return; } # just consider the interface load to be 0

RX1=$(< "${iface}"/statistics/rx_bytes)
TX1=$(< "${iface}"/statistics/tx_bytes)
sleep 1
snore 1
RX2=$(< "${iface}"/statistics/rx_bytes)
TX2=$(< "${iface}"/statistics/tx_bytes)

Expand All @@ -39,34 +46,48 @@ IFACE_LOAD () {
} # end of function

CPU_UTIL () {
# http://man7.org/linux/man-pages/man5/proc.5.html ; /proc/stat ; NB!! awk arrays start with 1
CPU_BEGIN=$(awk '/^cpu\s/ { printf "%d",$2 + $4 }' /proc/stat)
sleep 1
CPU_END=$(awk '/^cpu\s/ { printf "%d",$2 + $4 }' /proc/stat)
echo -ne $(( CPU_END - CPU_BEGIN))
# http://man7.org/linux/man-pages/man5/proc.5.html ; /proc/stat
read -a CPU_ARR_BEGIN < /proc/stat
snore 1
read -a CPU_ARR_END < /proc/stat
awk -v ncpu="${NCPU}" -v CPU_BEGIN_USR="${CPU_ARR_BEGIN[1]}" -v CPU_BEGIN_SYS="${CPU_ARR_BEGIN[3]}" -v CPU_END_USR="${CPU_ARR_END[1]}" -v CPU_END_SYS="${CPU_ARR_END[3]}" 'BEGIN {
printf "%.0f", (CPU_END_USR + CPU_END_SYS - CPU_BEGIN_USR - CPU_BEGIN_SYS)/ncpu ;
}' # ' end of awk
}

while(true); do # keep infinte loop
LOAD5=$(awk -v ncpu="${NCPU}" '{ LOAD_PERC = $2*100/ncpu; printf "%.0f",LOAD_PERC; }' /proc/loadavg) #'
[[ "${LOAD5}" -gt "100" ]] && LOAD5="100"

CPU=$(CPU_UTIL)
MEM=$(awk '/MemTotal/{ MEM_TOT=$(NF-1) } /MemAvailable/ { MEM_AVAIL=$(NF-1) } END{ MEM_PERC = (MEM_TOT - MEM_AVAIL)*100/MEM_TOT; printf "%.0f",MEM_PERC; }' /proc/meminfo) #'

# should be replaced by IOwait/NCPU ?
# legacy, let's default to 0
PGIO=0

NET_LOAD="0"
IFACE_DIR="/sys/class/net"
for iface in ${IFACE_DIR}/*; do
LIST_OF_ETH=""
for iface in ${IFACE_DIR}/*; do # create a list of valid,physical and UP network interfaces
[[ $(readlink -f "${iface}") =~ virtual ]] && continue
OPERSTATE=$(< "${iface}"/operstate)
[[ "$OPERSTATE" != "up" ]] && continue
LOAD=$(IFACE_LOAD "${iface}" || continue) # if IFACE_LOAD returns with exit=1 lets bail out
[[ "${LOAD}" -gt "${NET_LOAD}" ]] && NET_LOAD="${LOAD}" # keep NET_LOAD as maximum of all interfaces
[[ -z "${LIST_OF_ETH}" ]] && LIST_OF_ETH="${iface}" || LIST_OF_ETH="${LIST_OF_ETH} ${iface}"
done

exec {cpu_util}< <( CPU_UTIL )

NET_LOAD=0
LIST_OF_NET_FD=""
for iface in ${LIST_OF_ETH}; do
exec {iface_load}< <( IFACE_LOAD "${iface}" )
[[ -z "${LIST_OF_NET_FD}" ]] && LIST_OF_NET_FD="${iface_load}" || LIST_OF_NET_FD="${LIST_OF_NET_FD} ${iface_load}"
done

CPU=$(< /dev/fd/${cpu_util})
for NET_FD in ${LIST_OF_NET_FD}; do # select from all network interfaces the one with the biggest load
THIS_NET_LOAD=$(< /dev/fd/${NET_FD})
[[ "${THIS_NET_LOAD}" -gt "${NET_LOAD}" ]] && NET_LOAD=${THIS_NET_LOAD}
done

echo -ne "${LOAD5} ${CPU} ${MEM} ${PGIO} ${NET_LOAD}\n"
[[ "${INTERVAL}" -eq "0" ]] && break || sleep ${INTERVAL}
[[ "${INTERVAL}" -eq "0" ]] && break || snore ${INTERVAL}
done # end of while loop