Skip to content

Instantly share code, notes, and snippets.

@mgrantham18
Last active April 11, 2022 17:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgrantham18/91dc07ce81f19667c58bb917db64a084 to your computer and use it in GitHub Desktop.
Save mgrantham18/91dc07ce81f19667c58bb917db64a084 to your computer and use it in GitHub Desktop.
System Usage monitor for Nvidia Jetson Nano Developer Kit. Runs in terminal so works over SSH.
import os
import time
import curses
def main():
try:
os.system("tegrastats --stop")
os.system("tegrastats --interval 1000 --logfile tegrastats.log &")
stdscr = curses.initscr()
curses.start_color()
file = "tegrastats.log"
print("starting...")
time.sleep(2.1)
while True:
with open(file, "rb") as f:
first = f.readline() # Read the first line.
f.seek(-2, os.SEEK_END) # Jump to the second last byte.
while f.read(1) != b"\n": # Until EOL is found...
f.seek(-2, os.SEEK_CUR) # ...jump back the read byte plus one more.
last = f.readline() # Read last line.
ram = "RAM: " + (str(last).split("RAM "))[1].split("/")[0] + "/3963MB "
try:
# This will throw an error if there is no swap
swap = "SWAP: " + (str(last).split("SWAP"))[1].split("/")[0] + "/" + (str(last).split("/"))[2].split("MB")[0] + "MB "
noswap = 0
except Exception:
noswap = 1
cpus = (str(last).split("CPU ["))[1].split("] EMC_FREQ")[0].split(",")
gpu = "GPU:" + (str(last).split("GR3D_FREQ "))[1].split("%")[0] + "% "
cpu_temp = "CPU TEMP: " + (str(last).split("CPU@"))[1].split("C PMIC@")[0] + "C "
gpu_temp = "GPU TEMP: " + (str(last).split("GPU@"))[1].split("C AO@")[0] + "C "
thermal = "THERMAL: " + (str(last).split("thermal@"))[1].split("C POM_5V")[0] + "C "
cpu_txt = []
cpu_val = []
for cpu in cpus:
cpu_txt.append(cpu.split("@")[0] + " at " + cpu.split("@")[1] + "MHz")
cpu_val.append(float(cpu.split("%")[0]))
stdscr.refresh()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE) #red
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK) #green
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE) #title
stdscr.addstr(0,0, " NVIDIA JETSON NANO STATS ", curses.color_pair(3))
if int((str(last).split("RAM "))[1].split("/")[0]) > 3000:
stdscr.addstr(1, 0, ram, curses.color_pair(1))
else:
stdscr.addstr(1, 0, ram + " ", curses.color_pair(2))
if noswap == 0:
if int((str(last).split("SWAP"))[1].split("/")[0]) > 3000:
stdscr.addstr(2, 0, swap, curses.color_pair(1))
else:
stdscr.addstr(2, 0, swap + " ", curses.color_pair(2))
if cpu_val[0] > 70:
stdscr.addstr(3, 0, "CPU 1: " + cpu_txt[0] + "", curses.color_pair(1))
else:
stdscr.addstr(3, 0, "CPU 1: " + cpu_txt[0] + " ", curses.color_pair(2))
if cpu_val[1] > 70:
stdscr.addstr(4, 0, "CPU 2: " + cpu_txt[1] + "", curses.color_pair(1))
else:
stdscr.addstr(4, 0, "CPU 2: " + cpu_txt[1] + " ", curses.color_pair(2))
if cpu_val[2] > 70:
stdscr.addstr(5, 0, "CPU 3: " + cpu_txt[2] + "", curses.color_pair(1))
else:
stdscr.addstr(5, 0, "CPU 3: " + cpu_txt[2] + " ", curses.color_pair(2))
if cpu_val[3] > 70:
stdscr.addstr(6, 0, "CPU 4: " + cpu_txt[3] + "", curses.color_pair(1))
else:
stdscr.addstr(6, 0, "CPU 4: " + cpu_txt[3] + " ", curses.color_pair(2))
if float((str(last).split("GR3D_FREQ "))[1].split("%")[0]) > 70:
stdscr.addstr(7, 0, gpu, curses.color_pair(1))
else:
stdscr.addstr(7, 0, gpu, curses.color_pair(2))
if float((str(last).split("CPU@"))[1].split("C PMIC@")[0]) > 50:
stdscr.addstr(8, 0, cpu_temp, curses.color_pair(1))
else:
stdscr.addstr(8, 0, cpu_temp, curses.color_pair(2))
if float((str(last).split("GPU@"))[1].split("C AO@")[0]) > 50:
stdscr.addstr(9, 0, gpu_temp, curses.color_pair(1))
else:
stdscr.addstr(9, 0, gpu_temp, curses.color_pair(2))
if float((str(last).split("thermal@"))[1].split("C POM_5V")[0]) > 50:
stdscr.addstr(10, 0, thermal, curses.color_pair(1))
else:
stdscr.addstr(10, 0, thermal, curses.color_pair(2))
stdscr.move(12,0)
stdscr.refresh()
time.sleep(1.1)
finally:
curses.endwin()
os.system("rm tegrastats.log")
if __name__ == '__main__':
main()
@KeithMyers
Copy link

The script won't run with no swap file.

python3 jetson_stats.py starting... Traceback (most recent call last): File "jetson_stats.py", line 101, in <module> main() File "jetson_stats.py", line 22, in main swap = "SWAP: " + (str(last).split("SWAP"))[1].split("/")[0] + "/0MB " # ADD IN WHAT YOUR SWAP IS IndexError: list index out of range

@mgrantham18
Copy link
Author

The script won't run with no swap file.

Remove line 22, 48, 49, 50, 51 then it should run fine.

@KeithMyers
Copy link

Thanks. Easy fix. Why didn't setting the swap file to 0MB work? The comment told me to change my swap file size. And I did. Maybe you need to check for an existing swap file and if not present skip over detecting it.

@mgrantham18
Copy link
Author

Thanks. Easy fix. Why didn't setting the swap file to 0MB work? The comment told me to change my swap file size. And I did. Maybe you need to check for an existing swap file and if not present skip over detecting it.

Fixed it so it won't throw errors if there is no swap, it should work, but let me know if it doesn't for some reason.

@KeithMyers
Copy link

Thanks, works fine as is now.

@4ndre
Copy link

4ndre commented Nov 6, 2019

Somehow quite bugged:

Traceback (most recent call last):
File "jetson_stats.py", line 109, in
main()
File "jetson_stats.py", line 39, in main
cpu_txt.append(cpu.split("@")[0] + " at " + cpu.split("@")[1] + "MHz")
IndexError: list index out of range

starting...
Traceback (most recent call last):
File "jetson_stats.py", line 109, in
main()
File "jetson_stats.py", line 40, in main
cpu_val.append(float(cpu.split("%")[0]))
ValueError: could not convert string to float: 'off'

starting...
Traceback (most recent call last):
File "jetson_stats.py", line 109, in
main()
File "jetson_stats.py", line 61, in main
if cpu_val[0] > 70:
TypeError: '>' not supported between instances of 'str' and 'int'

...and so on. Gave up fixing it manually, idk what's the problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment