Monitoring Network Microbursts


/ Published in: Bash
Save to your folder(s)

Bytes per second isn't granular enough to get a real idea of how your network is really being used.


Copy this code and paste it in your HTML
  1. #!/bin/bash
  2.  
  3. function nets {
  4. TX_TOTAL=0
  5. RX_TOTAL=0
  6. TX_GAUGE="B" # Bytes
  7. RX_GAUGE="B" # Bytes
  8. INTERFACE="venet0"
  9. for ((i=0;i<10;i++)); do
  10. RX_BYTES=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
  11. TX_BYTES=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
  12. LAST_RX_BYTES=${RX_BYTES}
  13. LAST_TX_BYTES=${TX_BYTES}
  14. sleep .10
  15. RX_BYTES=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes)
  16. TX_BYTES=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes)
  17. TX_DIFF=$[ ${TX_BYTES} - ${LAST_TX_BYTES} ]
  18. RX_DIFF=$[ ${RX_BYTES} - ${LAST_RX_BYTES} ]
  19. TX_TOTAL=$[ ${TX_TOTAL} + ${TX_DIFF} ]
  20. RX_TOTAL=$[ ${RX_TOTAL} + ${RX_DIFF} ]
  21. if [ ${RX_DIFF} -gt 1024 ]; then
  22. RX_GAUGE="KB"
  23. RX_DIFF=$[ ${RX_DIFF} / 1024 ]
  24. if [ ${RX_DIFF} -gt 1024 ]; then
  25. RX_GAUGE="MB"
  26. RX_DIFF=$[ ${RX_DIFF} / 1024 ]
  27. fi
  28. fi
  29. if [ ${TX_DIFF} -gt 1024 ]; then
  30. TX_GAUGE="KB"
  31. TX_DIFF=$[ ${TX_DIFF} / 1024 ]
  32. if [ ${TX_DIFF} -gt 1024 ]; then
  33. TX_GAUGE="MB"
  34. TX_DIFF=$[ ${TX_DIFF} / 1024 ]
  35. fi
  36. fi
  37. if [[ ${TX_DIFF} -gt 0 || ${RX_DIFF} -gt 0 ]]; then
  38. printf "${i}00ms - TX: ${TX_DIFF}${TX_GAUGE}\tRX: ${RX_DIFF}${TX_GAUGE}\n"
  39. fi
  40. done
  41. if [ ${RX_TOTAL} -gt 1024 ]; then
  42. RX_GAUGE="KB"
  43. RX_TOTAL=$[ ${RX_TOTAL} / 1024 ]
  44. if [ ${RX_TOTAL} -gt 1024 ]; then
  45. RX_GAUGE="MB"
  46. RX_TOTAL=$[ ${RX_TOTAL} / 1024 ]
  47. fi
  48. fi
  49. if [ ${TX_TOTAL} -gt 1024 ]; then
  50. TX_GAUGE="KB"
  51. TX_TOTAL=$[ ${TX_TOTAL} / 1024 ]
  52. if [ ${TX_TOTAL} -gt 1024 ]; then
  53. TX_GAUGE="MB"
  54. TX_TOTAL=$[ ${TX_TOTAL} / 1024 ]
  55. fi
  56. fi
  57. printf "1000ms - TX: ${TX_TOTAL}${TX_GAUGE}\tRX: ${RX_TOTAL}${RX_GAUGE}\n"
  58. }
  59.  
  60. while :; do nets && date "+%S"; done

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.