system_stats 772 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/bin/sh
  2. UPDATE_PERIOD=2 #seconds
  3. notif_dirty=0
  4. perform=0
  5. # USR1 callback
  6. function toggle_perform()
  7. {
  8. let perform=1-${perform}
  9. if [ ${perform} -eq 0 ]; then
  10. notif_clear
  11. notif_dirty=1
  12. fi
  13. }
  14. trap toggle_perform SIGUSR1
  15. while true; do
  16. if [ ${perform} -eq 1 ]; then
  17. # Compute stats
  18. cpu=$(printf "%.1f\n" $(mpstat -P ALL $UPDATE_PERIOD 1 | tail -1 | awk '{print 100-$12}'))
  19. ram_mem=$(printf "%.1f\n" $(free | grep Mem | awk '{print $3/$2 * 100.0}'))
  20. ram_swap=$(printf "%.1f\n" $(free | grep Swap | awk '{print $3/$2 * 100.0}'))
  21. # Notif
  22. if [ ${notif_dirty} -eq 1 ]; then
  23. notif_clear
  24. notif_dirty=0
  25. else
  26. notif_set 0 "CPU:${cpu}%% RAM:${ram_mem}%% SWAP:${ram_swap}%%"
  27. fi
  28. else
  29. sleep ${UPDATE_PERIOD}
  30. fi
  31. done
  32. exit 0