PalmaSolutions

CPU Usage Script

Dec 20th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #POSITIONAL=()
  3. FILE_PATH="$2"
  4. #echo ${FILE_PATH};
  5.  
  6. while true
  7. do
  8.         # Read /proc/stat file (for first datapoint)
  9.         read cpu user nice system idle iowait irq softirq steal guest< /proc/stat
  10.  
  11.         # compute active and total utilizations
  12.         cpu_active_prev=$((user+system+nice+softirq+steal))
  13.         cpu_total_prev=$((user+system+nice+softirq+steal+idle+iowait))
  14.  
  15.         sleep 5
  16.  
  17.         # Read /proc/stat file (for second datapoint)
  18.         read cpu user nice system idle iowait irq softirq steal guest< /proc/stat
  19.  
  20.         # compute active and total utilizations
  21.         cpu_active_cur=$((user+system+nice+softirq+steal))
  22.         cpu_total_cur=$((user+system+nice+softirq+steal+idle+iowait))
  23.  
  24.         # compute CPU utilization (%)
  25.         cpu_util=$((100*( cpu_active_cur-cpu_active_prev ) / (cpu_total_cur-cpu_total_prev) ))
  26.  
  27.         #printf " Current CPU Utilization : %s\n" "$cpu_util"
  28.         DATE=`date '+%Y-%m-%d %H:%M:%S'`
  29.         echo  "$DATE Current CPU Utilization : $cpu_util"  >> ${FILE_PATH}
  30. done
  31. #exit 0
Add Comment
Please, Sign In to add comment