Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Dzen2 toolbar/statusbar execution file.
  4. # Options:
  5. # -xs = which Xinerama screen
  6. # -l = number of lines in slave (dropdown) window
  7. # -u update continually
  8. # -p <n> timed termination; w/o n seconds, persist forever.
  9. # retval: 0 = EOF; 1 = error; or exit:n where n=user-defined retval.
  10. # -e event... -e 'event1=action1:option1:...option<n>,...,action<m>;...;event<l>'
  11. # event... -e 'button1=exec:xterm:firefox;entertitle=uncollapse,unhide;button3=exit'
  12. # Supported events: (see latest README or online docs)
  13. # onstart Perform actions right after startup
  14. # onexit Perform actions just before exiting
  15. # onnewinput Perform actions if there is new input for the slave window
  16. # button1 Mouse button1 released
  17. # button2 Mouse button2 released
  18. # button3 Mouse button3 released
  19. # button4 Mouse button4 released (usually scrollwheel)
  20. # button5 Mouse button5 released (usually scrollwheel)
  21. # entertitle Mouse enters the title window
  22. # leavetitle Mouse leaves the title window
  23. # enterslave Mouse enters the slave window
  24. # leaveslave Mouse leaves the slave window
  25. # sigusr1 SIGUSR1 received
  26. # sigusr2 SIGUSR2 received
  27. # key_KEYNAME Keyboard events (*)
  28.  
  29.  
  30. # Before doing anything, kill any running dzen2:
  31. kill_counter=$((0))
  32. while [ "$(ps -C dzen2 | grep dzen2 | awk '{print $1}')" ]; do
  33. kill -9 $(ps -C dzen2 | grep dzen2 | awk '{print $1}') 2>/dev/null
  34. kill_counter=$((kill_counter+1)); sleep 1
  35. if [ $kill_counter -ge 5 ]; then
  36. echo "dzen2 error: unkillable zombies; cannot start new dzen2." 1>&2 && exit 1
  37. fi
  38. done
  39.  
  40. # Pre execution: see if 'stop' was passed as $1 and if so
  41. # don't restart dzen2; just exit instead:
  42. [ "$1" = 'stop' ] && exit 0
  43.  
  44. # Not exiting; proceed then:
  45.  
  46. # FUNCTIONS:
  47.  
  48. get_date () {
  49. # Observe/fix if `date` pads single digits as formatted.
  50. echo "^fg($TX2)$(date +'%H:%M:%S %a %x')"
  51. }
  52.  
  53. get_mem () {
  54. TOTALMEM=$(awk '/^MemTotal: /{print $2}' /proc/meminfo)
  55. FREEMEM=$(awk '/^MemFree: /{print $2}' /proc/meminfo)
  56. USEDMEM=$(($TOTALMEM - $FREEMEM))
  57.  
  58. UBARS=$(( $(echo "scale = 0; $USEDMEM * 100 / $TOTALMEM" | bc -l) ))
  59. FBARS=$(( $(echo "scale = 0; $FREEMEM * 100 / $TOTALMEM" | bc -l) ))
  60. [ $UBARS -ge 95 ] && FG="^fg($RED)" || FG="^fg($GRN)"
  61.  
  62. echo "^fg($WHT)MEM^p(2;0)${FG}^r(${UBARS}x16)^fg($BAR)^r(${FBARS}x16)"
  63. }
  64.  
  65. disk_space () {
  66. unset DISKS ALLDISKS
  67. df | {
  68. while read line; do
  69. LABEL="$(echo $line | awk '{print $1}'):"
  70. if [ "$(echo "$LABEL" | grep 'root')" ]; then
  71. DEVTOTAL=$(echo $line | awk '{print $2}')
  72. DEVFREE=$(echo $line | awk '{print $4}')
  73. DEVUSED=$(($DEVTOTAL - $DEVFREE))
  74.  
  75. UBARS=$(( $(echo "scale = 0; $DEVUSED * 100 / $DEVTOTAL" | bc -l) ))
  76. FBARS=$(( $(echo "scale = 0; $DEVFREE * 100 / $DEVTOTAL" | bc -l) ))
  77. [ $UBARS -ge 95 ] && FG="^fg($RED)" || FG="^fg($GRN)"
  78.  
  79. DISKS="^pa(;4)${DISKS}^fg($WHT)${LABEL}^pa(;4)${FG}^r(${UBARS}x16)^fg($BAR)^r(${FBARS}x16)"
  80. ALLDISKS="${ALLDISKS}${DISKS} "; unset DISKS
  81. fi
  82. done
  83. echo "$ALLDISKS"
  84. }
  85. }
  86.  
  87. interfaces () {
  88. unset OUTPUT
  89. for NIC in $NICS; do
  90. eval RXBN_${NIC}=$(cat /sys/class/net/${NIC}/statistics/rx_bytes); eval TXBN_${NIC}=$(cat /sys/class/net/${NIC}/statistics/tx_bytes)
  91. eval RXR_${NIC}=$(printf "%4d\n" $((($(eval echo \$RXBN_${NIC}) - $(eval echo \$RXB_${NIC})) / 1024/$SLEEP)) | bc )
  92. eval TXR_${NIC}=$(printf "%4d\n" $((($(eval echo \$TXBN_${NIC}) - $(eval echo \$TXB_${NIC})) / 1024/$SLEEP)) | bc )
  93.  
  94. OUTPUT="${OUTPUT}^fg($WHT)${NIC}^fg(${BAR}) "
  95. OUTPUT="${OUTPUT}$(eval echo \$TXR_${NIC})^fg($GRY)^p(2)UkB/s ^fg($BAR)$(eval echo \$RXR_${NIC})^fg($GRY)^p(2)DkB/s"
  96. OUTPUT="${OUTPUT} ^fg($BAR)$(/sbin/ip addr show label ${NIC} | awk '/inet /{gsub("/.*","",$0); print $2}') "
  97. done
  98.  
  99. echo $OUTPUT
  100.  
  101. for NIC in $NICS; do
  102. eval RXB_${NIC}=$(eval echo \$RXBN_${NIC})
  103. eval TXB_${NIC}=$(eval echo \$TXBN_${NIC})
  104. done
  105. }
  106.  
  107. # END OF FUNCTIONS
  108. # script execution begins here:
  109.  
  110. # Define colors and spacers etc..:
  111. TX1='#DBDADA' # medium grey text
  112. TX2='#F9F9F9' # light grey text
  113. GRY='#909090' # dark grey text
  114. BAR='#A6F09D' # green background of bar-graphs
  115. GRN='#65A765' # light green (normal)
  116. YEL='#FFFFBF' # light yellow (caution)
  117. RED='#FF0000' # light red/pink (warning)
  118. WHT='#FFFFFF' # white
  119. BLK='#000000' # black
  120. SEP="^p(4)^fg(#555555)^r(4x24)^p(4)" # item separator block/line
  121. SLEEP=1 # update interval (whole seconds, no decimals!)
  122. CHAR=$((20)) # pixel width of characters of font used
  123. # zero some vars for the CPU load reader:
  124. LASTTOTALCPU0=0; LASTIDLECPU0=0
  125. LASTTOTALCPU1=0; LASTIDLECPU1=0
  126.  
  127.  
  128.  
  129. # endless loop: DZEN on output xinerama-0
  130. while true; do
  131.  
  132. sleep $SLEEP
  133.  
  134. READCPU0=$(awk '/^cpu0 /{print}' /proc/stat | sed 's/cpu0 //')
  135. READCPU1=$(awk '/^cpu1 /{print}' /proc/stat | sed 's/cpu1 //')
  136. IDLECPU0=$(echo $READCPU0 | awk '{print $4}')
  137. IDLECPU1=$(echo $READCPU1 | awk '{print $4}')
  138. TOTALCPU0=0; TOTALCPU1=0
  139. for x in $READCPU0; do TOTALCPU0=$((TOTALCPU0+x)); done
  140. for x in $READCPU1; do TOTALCPU1=$((TOTALCPU1+x)); done
  141.  
  142. NICS="$(/sbin/ip addr show scope global | awk '/^[ ]*inet /{print $(NF)}')" # Interfaces
  143. for NIC in $NICS; do
  144. # this just grabs reference points of the NIC traffic prior to the sleep
  145. # so the traffic/time calculation can be made in the $(interfaces) function above:
  146. eval RXB_${NIC}=$(cat /sys/class/net/${NIC}/statistics/rx_bytes)
  147. eval TXB_${NIC}=$(cat /sys/class/net/${NIC}/statistics/tx_bytes)
  148. done
  149.  
  150.  
  151. # echo the string that gets printed on the Dzen2 bar:
  152. # orig line used for non-dock dzen2:
  153.  
  154. echo "^ib(1)^pa(0;0)^fg($BAR)^ro(1920x24)^pa(2;4)$(get_date)^pa(;4) $(get_mem)^pa(;4) $(disk_space) $(interfaces)"
  155.  
  156. # Remember the total and idle CPU times for the next check.
  157. LASTTOTALCPU0=$TOTALCPU0; LASTIDLECPU0=$IDLECPU0
  158. LASTTOTALCPU1=$TOTALCPU1; LASTIDLECPU1=$IDLECPU1
  159.  
  160. done | dzen2 -fn "-bitstream-arundina sans mono-bold-o-normal--0-0-0-0-p-0-ascii-0" -bg black -ta l -h 24 -u -p
  161.  
  162.  
  163. # Bitstream Vera Sans Mono:style=Bold Oblique
  164. # xfontsel!!!
  165. # fc-list | grep -i stream
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement