Guest User

Plotter

a guest
Aug 31st, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.52 KB | None | 0 0
  1. #!/bin/bash
  2. term=${1:-wxt}
  3. if [[ $term =~ ^- ]]; then
  4.     echo "Usage: ${0##*/} TERM"
  5.     echo "Example TERM:"
  6.     echo '"wxt": gtk'
  7.     echo '(buggy) "x11": x11 (Examine man gnuplot for custom color scheme. Problematic with constant raising.)'
  8.     echo '"dumb": plain text'
  9.     exit
  10. fi
  11.  
  12. shopt -s nullglob
  13. IFS=$'\n\t ' #make \n first for "*" expansion which uses the first character.
  14.  
  15. tput civis
  16. gpfile=/dev/shm/plotter_file_$$
  17. trap "rm $gpfile; tput cnorm; exit" INT TERM
  18.  
  19. function updatedata() {
  20.     #DOWN
  21.     totaldown=0
  22.     for network in /sys/class/net/{eth,wlan}*/statistics/rx_bytes; do
  23.         read network < $network
  24.         (( totaldown+=network ))
  25.     done
  26.     (( oldtotaldown != 0 )) && DOWN=$(( (totaldown - oldtotaldown) /1024)) || DOWN=0
  27.     oldtotaldown=$totaldown
  28.    
  29.     #UP
  30.     totalup=0
  31.     for network in /sys/class/net/{eth,wlan}*/statistics/tx_bytes; do
  32.         read network < $network
  33.         (( totalup+=network ))
  34.     done
  35.     (( oldtotalup != 0 )) && UP=$(( (totalup - oldtotalup) /1024)) || UP=0
  36.     oldtotalup=$totalup
  37.    
  38.     #CPU
  39.     read cpu a b c idle rest < /proc/stat
  40.     total=$((a+b+c+idle))
  41.     intervaltotal=$((total-prevtotal))
  42.     (( prevtotal != 0 )) && CPU=$((100* ( intervaltotal - (idle - previdle) ) / intervaltotal)) || CPU=0
  43.     prevtotal=$total
  44.     previdle=$idle
  45.    
  46.     #MEM
  47.     while read type size kb; do
  48.         [[ $type == MemTotal: ]] && total=$size && used=$total
  49.         [[ $type =~ (MemFree|Buffers|Cached): ]] && ((used-=size))
  50.     done < /proc/meminfo
  51.     MEM=$(( (100 * used) / total))
  52. }
  53.  
  54. #Configure $TITLE
  55. function updatetitle() {
  56.     DAY=$(date +%a)
  57.     TIME=$(date +"%H:%M")
  58.     DATE=$(date +"%b %d")
  59.     TITLE="($DAY) $TIME ($DATE)"
  60. }
  61.  
  62. function updatetics() {
  63.     now=$(date +"%H:%M:%S" -d "10 seconds")
  64.     minuteago=$(date +"%H:%M:%S" -d "50 seconds ago")
  65. }
  66. if [[ $term == dumb ]]; then
  67.     color=31
  68.     sedcolorizer=$(
  69.     for drawing in '*' '#' '$' '%'; do
  70.         echo "s/\\$drawing\+/\\x1b[$((color++))m&\\x1b[m/g"
  71.     done
  72.     echo "s/[|+]\+\( [0-9]*\)*$/\\x1b[33m&\\x1b[m/"
  73.     )
  74. fi
  75. counter=0
  76. minuteago=$(date +"%H:%M:%S")
  77. now=$(date +"%H:%M:%S" -d "1 minute")
  78.  
  79. sleep 0.$((1000000000 - $(date +%-N) ))s
  80. {
  81.     [[ $term != dumb ]] && echo "set term $term noraise title 'Gnuplot$$'"
  82.  
  83.     echo 'set grid'
  84.  
  85.     echo 'set logscale y'
  86. #   echo 'set ytics 0,250'
  87.     echo 'unset mytics'
  88.  
  89.     echo 'set y2tics 0,25'
  90.  
  91.  
  92.     while [[ $term == dumb ]] || [[ $counter = 0 ]] || wmctrl -l | grep -q Gnuplot$$; do
  93.         [[ $term = dumb ]] && echo "set term dumb $(($(tput cols)+7)) $(($(tput lines)+2))"
  94.         #reset axes every time, because you can zoom-in without zooming-out (X11)
  95.         echo 'set xrange[0:60]'
  96.         echo 'set yrange[1:2000]'
  97.         echo 'set y2range[0:100]'
  98.  
  99.         #update x-axis every 10 seconds
  100.         if (( ++counter == 61 )); then
  101.             updatecounter=9
  102.             while (( ++updatecounter != 61 )); do
  103.                 data[$((updatecounter-10))]=${data[$updatecounter]}
  104.                 unset "data[$updatecounter]"
  105.             done
  106.             updatetics
  107.             (( counter-=10 ))
  108.         fi
  109.         echo "set xtics ('$minuteago' 0, '' 10, '' 20, '' 30, '' 40, '' 50, '$now' 60)"
  110.  
  111.         #gather and print data to temporary file
  112.         updatedata
  113.         printf -v "data[$counter]" "%s\t" $UP $DOWN $CPU $MEM
  114.         echo "${data[*]}" > $gpfile
  115.  
  116.         updatetitle
  117.         echo "set title '$TITLE'"
  118.  
  119.         echo "plot  '$gpfile'   using 1 title 'Upload' with lines, \
  120.                 '$gpfile'   using 2 title 'Download' with lines, \
  121.                 '$gpfile'   using 3 title 'CPU' axes x1y2 with lines, \
  122.                 '$gpfile'   using 4 title 'MEM' axes x1y2 with lines"
  123.  
  124.         #sleep to about the whole second
  125.         sleep 0.$((1000000000 - $(date +%-N) ))s
  126.     done
  127. } | gnuplot | { [[ $term = dumb ]] && sed -e "$sedcolorizer" -e 's/^  //;/^$/d'; }
  128.  
  129. rm $gpfile
  130. tput cnorm
Advertisement
Add Comment
Please, Sign In to add comment