Skip to content

Commit

Permalink
added cpu usage metrics to output
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheww95 committed Nov 15, 2024
1 parent 534f099 commit c405019
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - Added CPU usage

### Added

- Added CPU usage fields to output

### Fixed

- Fixed warnings from shellcheck

## [0.0.1] - Initial Release

### Added
Expand Down
55 changes: 40 additions & 15 deletions slm
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ memoryinuse(){
allocmem="AllocMem"
realmem="RealMemory"
freemem="FreeMem"
cpu_alloc="CPUAlloc"
cpu_total="CPUTot"
na_field="N/A"
declare -Ar mem_values=([$allocmem]=1 [$realmem]=1)
declare -Ar cpu_values=([$cpu_alloc]=1 [$cpu_total]=1)
{
IFS=$'\n'
echo "$nodename_field ${allocmem}(GB) $realmem(GB) $freemem(GB) PercentAllocated"
echo "$nodename_field ${allocmem}(GB) $realmem(GB) $freemem(GB) PercentMemAllocated ${cpu_alloc} ${cpu_total} PercentCPUAllocated"
for i in $(scontrol -o show nodes)
do
declare -A sctrl_stats
Expand All @@ -20,30 +23,52 @@ memoryinuse(){
do
key=${f/=*/}
value=${f/*=/}
if [[ mem_values["$key"] -ne 1 ]]

if [[ $((mem_values["$key"])) -eq 1 ]]
then
sctrl_stats["$key"]="$value"
if [[ "$value" =~ ^-?[0-9]+$ ]]
then
sctrl_stats["$key"]=$(( value / 1024))
else
sctrl_stats["$key"]=$na_field
fi
continue
fi

if [[ "$value" =~ ^-?[0-9]+$ ]]
if [[ $((cpu_values["$key"])) -eq 1 ]]
then
sctrl_stats["$key"]=$(( $value / 1024))
else
sctrl_stats["$key"]=$na_field
if [[ "$value" =~ ^-?[0-9]+$ ]]
then
sctrl_stats["$key"]=$value
else
sctrl_stats["$key"]=$na_field
fi
continue
fi

sctrl_stats["$key"]=$value

done
name=${sctrl_stats[$nodename_field]}
allocmem_gb=${sctrl_stats[$allocmem]}
realmem_gb=${sctrl_stats[$realmem]}
name=${sctrl_stats["$nodename_field"]}
allocmem_gb=${sctrl_stats["$allocmem"]}
realmem_gb=${sctrl_stats["$realmem"]}
freemem_gb=$na_field
percent_utilized=$na_field
mem_percent_utilized=$na_field
cpu_percent_utilized=$na_field
total_cpu=${sctrl_stats["$cpu_total"]}
alloc_cpu=${sctrl_stats["$cpu_alloc"]}
if [[ "$allocmem_gb" != "$na_field" ]] && [[ "$realmem_gb" != "$na_field" ]]
then
freemem_gb=$(( $realmem_gb - $allocmem_gb ))
percent_utilized=$( bc -l <<< "scale=1; $allocmem_gb / $realmem_gb * 100" )
freemem_gb=$(( realmem_gb - allocmem_gb ))
mem_percent_utilized=$( bc -l <<< "scale=2; $allocmem_gb / $realmem_gb * 100" )
fi
echo "$name ${allocmem_gb} ${realmem_gb} ${freemem_gb} ${percent_utilized}%"

if [[ "$alloc_cpu" != "$na_field" ]] && [[ "$total_cpu" != "$na_field" ]]
then
cpu_percent_utilized=$( bc -l <<< "scale=2; $alloc_cpu / $total_cpu * 100" )
fi

echo "$name ${allocmem_gb} ${realmem_gb} ${freemem_gb} ${mem_percent_utilized}% ${alloc_cpu} ${total_cpu} ${cpu_percent_utilized}%"
IFS=$'\n'
done } | column -t

Expand Down

0 comments on commit c405019

Please sign in to comment.