analytics.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/bash
  2. function deh {
  3. echo "$1" | awk '/[0-9]$/{print $1;next} /MB$/{printf "%u\n", $1*(1024*1024);next} /KB$/{printf "%u\n", $1*1024}'
  4. }
  5. function log {
  6. curl -s -i -XPOST 'http://graphs.eba:8086/write?db=curso' --data-binary "$1" >/dev/null
  7. }
  8. function online {
  9. cuantos=$(ssh especificosba.com.ar './cuantos_online.sh')
  10. log "curso,type=online value=$cuantos"
  11. }
  12. function drops {
  13. drops=$(ssh especificosba.com.ar "find /var/videos -type f -mmin -10 | wc -l")
  14. drops=$(echo "$drops-1" | bc)
  15. if [ $drops -lt 0 ]; then
  16. drops=0
  17. fi
  18. log "curso,type=drops value=$drops"
  19. }
  20. function bw {
  21. running=$(ps aux | grep voctocore | grep -vc grep)
  22. if [ $running -ne 1 ]; then
  23. sleep 60
  24. return
  25. fi
  26. TIME=10
  27. tmp=$(mktemp)
  28. iftop -P -b -B -n -i vmbr0 -N -t -s "$TIME" 2>/dev/null >"$tmp"
  29. python3 /root/parse_iftop.py "$tmp" |\
  30. while read -r line; do
  31. ip=$(echo $line | cut -d' ' -f 1)
  32. data=$(echo $line | cut -d' ' -f 2)
  33. log "curso,type=bw,host=${ip} value=${data}"
  34. done
  35. rm "$tmp"
  36. }
  37. count=$(ps aux | grep "$0" | grep -v grep | wc -l)
  38. if [ "$count" -gt 2 ]; then
  39. exit 0
  40. fi
  41. online &
  42. bw &
  43. drops &
  44. wait