I wanted to be able to control the time delta for CPU usagePercent and the network usage.
Get a bunch of other usage information of the Jetson system.
Output all this to one JSON line so I could create my own stats!
I thought I would save allot of time for people trying to track down where to get all this system information from.
#!/bin/bash
# Copyright 2022 David Lee Burrows III, meeki007@gmail.com
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Paramiter one used to set time in sec between reads
sleepDurationSeconds=$1
if [ "$1" -lt 1 ]; then
sleepDurationSeconds=1
fi
##########################################################
########## Stuff Mesured between sleep duration ##########
##########################################################
# previous
readarray -t previousStats < <( awk '/^cpu /{flag=1}/^intr/{flag=0}flag' /proc/stat )
previousrx_bytes=$(head -n1 /sys/class/net/eth0/statistics/rx_bytes)
previoustx_bytes=$(head -n1 /sys/class/net/eth0/statistics/tx_bytes)
sleep $sleepDurationSeconds
# current
readarray -t currentStats < <( awk '/^cpu /{flag=1}/^intr/{flag=0}flag' /proc/stat )
currentrx_bytes=$(head -n1 /sys/class/net/eth0/statistics/rx_bytes)
currenttx_bytes=$(head -n1 /sys/class/net/eth0/statistics/tx_bytes)
#########################
########## CPU ##########
#########################
#Start JSON line
json='{"cpu":{"usagePercent":{'
# loop through the arrays
for i in "${!previousStats[@]}"; do
# Break up arrays 1 line sting into an array element for each item in string
previousStat_elemant_array=(${previousStats[i]})
currentStat_elemant_array=(${currentStats[i]})
# Get all columns
previousStat_colums="${previousStat_elemant_array[@]:1:7}"
currentStat_colums="${currentStat_elemant_array[@]:1:7}"
# Replace the column seperator (space) with +
previous_cpu_sum=$((${previousStat_colums// /+}))
current_cpu_sum=$((${currentStat_colums// /+}))
# Get the delta between two reads
cpu_delta=$((current_cpu_sum - previous_cpu_sum))
# Get the idle time Delta
cpu_idle=$((currentStat_elemant_array[4]- previousStat_elemant_array[4]))
# Calc time spent working
cpu_used=$((cpu_delta - cpu_idle))
# Calc cpu percentage used
cpu_usage=$(echo 'scale = 2; x=100 * '$cpu_used'/'$cpu_delta'; if(x>0 && x<1){"0"}; x' | bc)
# Get cpu used for calc cpu percentage used
cpu_used_for_calc="${currentStat_elemant_array[0]}"
# save outputs to json string
if [[ "$cpu_used_for_calc" == "cpu" ]]; then
json+='"total":'$cpu_usage','
else
json+='"'$cpu_used_for_calc'":'$cpu_usage','
fi
done
#remove last ','
json=${json::-1}
# end usagePercent in json line
json+='},'
########## CPU freq ##########
# start freqGHz line in json
json+='"freqGHz":{'
# Put CPUs freq into an array
readarray -t cpuFreqsArray < <( cat /sys/devices/system/cpu/cpu*/cpufreq/cpuinfo_cur_freq )
#non hardware way use this line for no sudo (not always up to date)
#readarray -t cpuFreqsArray < <( cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq )
#check if cpuFreqsArray is empty
if (( ${#cpuFreqsArray[@]} )); then
# loop through the array
for i in "${!cpuFreqsArray[@]}"; do
# calc cpuFreq from kHz to GHz and save outputs to json string
json+='"cpu'$i'":'$(echo 'scale = 2; x='${cpuFreqsArray[i]}'/'1000000 '; if(x>0 && x<1){"0"}; x' | bc)','
done
#remove last ','
json=${json::-1}
fi
# end cpuFreq in json line
json+='},'
########## CPU (voltage in mV) (current in mA) (power in mW) ##########
# start electricity line in json
json+='"electricity":{'
#cpu voltage_mV
cpu_voltage_mV=$(head -n1 /sys/bus/i2c/devices/6-0040/iio\:device0/in_voltage2_input)
if [ -n "${cpu_voltage_mV}" ]; then
json+='"voltage_mV":'$cpu_voltage_mV','
else
json+='"voltage_mV":null,'
fi
#cpu current_mA
cpu_current_mA=$(head -n1 /sys/bus/i2c/devices/6-0040/iio\:device0/in_current2_input)
if [ -n "${cpu_current_mA}" ]; then
json+='"current_mA":'$cpu_current_mA','
else
json+='"current_mA":null,'
fi
#cpu power_mW
cpu_power_mW=$(head -n1 /sys/bus/i2c/devices/6-0040/iio\:device0/in_power2_input)
if [ -n "${cpu_current_mA}" ]; then
json+='"power_mW":'$cpu_power_mW
else
json+='"power_mW":null'
fi
# end cpuElectricity in json line
json+='},'
########## CPU Loadavg ##########
# start cpuLoadavg line in json
json+='"loadavg":{'
# Get first 3 entries to array
cpuLoadavgArray=($(head -n1 /proc/loadavg))
cpuLoadavgArrayformatted=(${cpuLoadavgArray[@]:0:3})
# loop through the array
for i in ${!cpuLoadavgArrayformatted[@]}; do
# calc the minutes 1,5,15 for the load averages
loadAverageMinutes=$(echo 'scale = 0; 3*'$i'^2+1*'$i'+1' | bc)
#cpuLoadavg 1min
json+='"'$loadAverageMinutes'min":'${cpuLoadavgArrayformatted[i]}','
#echo "cpuLoadavgArray: " ${cpuLoadavgArray[i]}
#echo "cpuLoadavgArrayformatted: " ${cpuLoadavgArrayformatted[i]}
done
#remove last ','
json=${json::-1}
# end cpuLoadavg in json line
json+='},'
########## CPUs temp in Celcius ##########
#NOTE cat /sys/devices/virtual/thermal/thermal_zone*/type
#NOTE cat /sys/devices/virtual/thermal/thermal_zone*/temp
#cpuTempCraw=$(head -n1 /sys/devices/virtual/thermal/thermal_zone1/temp)
# format to celcius dec
json+='"tempC":'$(echo 'scale = 2; x='$(head -n1 /sys/devices/virtual/thermal/thermal_zone1/temp)'/'1000 '; if(x>0 && x<1){"0"}; x' | bc)','
########## CPUs present (number if CPUs) ##########
cpusPresentRAW=$(head -n1 /sys/devices/system/cpu/present)
cpusPresent=$((${cpusPresentRAW##*-} + 1))
# cpusPresent line in json
json+='"coresPresent":'$cpusPresent
# end of cpu section
json+='},'
##############################
########## GPU ###############
##############################
# start gpu line in json
json+='"gpu":{'
# tempC - format to celcius dec
json+='"tempC":'$(echo 'scale = 2; x='$(head -n1 /sys/devices/virtual/thermal/thermal_zone2/temp)'/'1000 '; if(x>0 && x<1){"0"}; x' | bc)','
# freqGHz
json+='"freqGHz":'$(echo 'scale = 2; x='$(head -n1 /sys/devices/57000000.gpu/devfreq/57000000.gpu/cur_freq)'/'100000000 '; if(x>0 && x<1){"0"}; x' | bc)','
# usagePercent
json+='"usagePercent":'$(echo 'scale = 2; x='$(head -n1 /sys/devices/gpu.0/load)'/'10 '; if(x>0 && x<1){"0"}; x' | bc)','
########## gpu (voltage in mV) (current in mA) (power in mW) ##########
# start electricity line in json
json+='"electricity":{'
#gpu voltage_mV
gpu_voltage_mV=$(head -n1 /sys/bus/i2c/devices/6-0040/iio\:device0/in_voltage1_input)
if [ -n "${gpu_voltage_mV}" ]; then
json+='"voltage_mV":'$gpu_voltage_mV','
else
json+='"voltage_mV":null,'
fi
#gpu current_mA
gpu_current_mA=$(head -n1 /sys/bus/i2c/devices/6-0040/iio\:device0/in_current1_input)
if [ -n "${gpu_current_mA}" ]; then
json+='"current_mA":'$gpu_current_mA','
else
json+='"current_mA":null,'
fi
#gpu power_mW
gpu_power_mW=$(head -n1 /sys/bus/i2c/devices/6-0040/iio\:device0/in_power1_input)
if [ -n "${gpu_current_mA}" ]; then
json+='"power_mW":'$gpu_power_mW
else
json+='"power_mW":null'
fi
# end gpuElectricity in json line
json+='}'
# end of gpu section
json+='},'
##############################
########## Network ###########
##############################
# start network line in json
json+='"network":{'
# start eth0 line in json
json+='"eth0":{'
# start rx_bytes line in json
json+='"rx_bytes":'$(($currentrx_bytes-$previousrx_bytes))','
# start tx_bytes line in json
json+='"tx_bytes":'$(($currenttx_bytes-$previoustx_bytes))
# end of eth0 section
json+='}'
# end of network section
json+='},'
##############################
########## RAM/SWAP ##########
##############################
# start Ram line in json
json+='"memory":{'
# MemTotalGigabytes line in json
MemTotalGigabytes=$(echo 'scale = 2; '$(awk '/MemTotal/ { print $2 }' /proc/meminfo)'/'1048576 | bc)
json+='"ramTotalGigabytes":'$MemTotalGigabytes','
# MemAvailableGigabytes line in json
MemAvailableGigabytes=$(echo 'scale = 2; '$(awk '/MemAvailable/ { print $2 }' /proc/meminfo)'/'1048576 | bc)
json+='"ramAvailableGigabytes":'$MemAvailableGigabytes','
# MemUsedGigabytes line in json
json+='"ramUsedGigabytes":'$(echo 'scale = 2; x='$MemTotalGigabytes'-'$MemAvailableGigabytes '; if(x>0 && x<1){"0"}; x' | bc)','
# SwapTotalGigabytes line in json
SwapTotalGigabytes=$(echo 'scale = 2; '$(awk '/SwapTotal/ { print $2 }' /proc/meminfo)'/'1048576 | bc)
json+='"swapTotalGigabytes":'$SwapTotalGigabytes','
# SwapFreeGigabytes line in json
SwapFreeGigabytes=$(echo 'scale = 2; '$(awk '/SwapFree/ { print $2 }' /proc/meminfo)'/'1048576 | bc)
json+='"swapFreeGigabytes":'$SwapFreeGigabytes','
# SwapUsedGigabytes line in json
json+='"swapUsedGigabytes":'$(echo 'scale = 2; x='$SwapTotalGigabytes'-'$SwapFreeGigabytes '; if(x>0 && x<1){"0"}; x' | bc)
#1 GB = 1048576 kB
# end of RAM/SWAP section
json+='},'
##############################
########## Storage ###########
##############################
# start Storage line in json
json+='"storage":{'
# size in Megabytes
json+='"sizeMegabytes":'$(df -BM / | awk 'NR==2{print substr($2, 1, length($2)-1)}')','
# used in Megabytes
json+='"usedMegabytes":'$(df -BM / | awk 'NR==2{print substr($3, 1, length($3)-1)}')','
# available in GB
json+='"availableMegabytes":'$(df -BM / | awk 'NR==2{print substr($4, 1, length($4)-1)}')','
# percentage used
json+='"percentageDiskUsed":'$(df / | awk 'NR==2{print substr($5, 1, length($5)-1)}')
# end of Storage section
json+='},'
#################################
########## Uptime ###############
#################################
uptimeRAW=$(head -n1 /proc/uptime)
json+='"uptime_in_days":'$(echo 'scale = 4; x='$(echo "${uptimeRAW%%.*}")'/'86400 '; if(x>0 && x<1){"0"}; x' | bc)','
#################################
########## Time Frame ###########
#################################
# start Storage line in json
json+='"measurementInterval_in_seconds":'$1
### end/complete JSON String ###
json+='}'
# handle no return option for parameter 2 of nr
if [[ "$2" == "nr" ]]; then
echo -ne $json
else
echo $json
fi
Just save make execu and run it.
example: run it for 1 seconds
$ sudo jetsonSystemStatsJSON 1
example: run it for 10 seconds and dont print the char return
$ sudo jetsonSystemStatsJSON 10 nr
hope this helps someone