Advertisement
HostSlayer

OPENVZ ANTI-ABUSE SCRIPT

Mar 4th, 2017
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. #!/bin/bash
  2. # Filename...: vz-monitor.sh
  3. # Description: Aggregates statistics for OpenVZ+SolusVM resource abuse tracking
  4. # HostSlayer LLC
  5. #Email Alert on startup
  6. #/bin/mail -s "VZ Monitoring script has been started on Server1.HostSlayer.com (IPS/24)" Server1@hostslayer.com < /tmp/alert.txt
  7.  
  8. # Remove old files
  9. rm -f /tmp/vz-*tmp
  10. rm -f /tmp/notify.txt
  11.  
  12. #Variable declaration
  13. rlimit=1000000
  14. wlimit=1000000
  15. lavg=20
  16.  
  17. # Find out release version
  18. if [ `awk '{ print $3 }' /etc/redhat-release | cut -d . -f1` == "5" ]
  19. then
  20. release="el5"
  21. elif [ `awk '{ print $3 }' /etc/redhat-release | cut -d . -f1` == "6" ]
  22. then
  23. release="el6"
  24. else
  25. echo "Unsupported release. Is this CentOS/RHEL?"
  26. exit 0
  27. fi
  28.  
  29. # Initial message
  30. echo -e "\n Detected System: $release"
  31. echo -e " Please wait while we gather statistics..."
  32. echo -e "\n Note:\n Bandwidth stats are based on primary IP's only, sampled over 5 seconds."
  33. echo -e " HDD/IO stats are based on OpenVZ ioacct data, sampled over 5 seconds. \n"
  34.  
  35. # Obtain first set of networking statistics
  36. iptables -L SOLUSVM_TRAFFIC_IN -n -x -v > /tmp/vz-net1-rx.tmp
  37. iptables -L SOLUSVM_TRAFFIC_OUT -n -x -v > /tmp/vz-net1-tx.tmp
  38.  
  39. # Obtain vm list in a nice format
  40. vmlist=`/usr/sbin/vzlist | awk '{ print $1 }' | sed "1 d" | xargs`
  41.  
  42. # Obtain IO, Load statistics in parallel
  43. for i in $vmlist
  44. do
  45. (
  46. if [ "$release" == "el5" ]
  47. then
  48. read1=`grep vfs_reads /proc/bc/${i}/ioacct | awk '{ print $2 }'`
  49. write1=`grep vfs_writes /proc/bc/${i}/ioacct | awk '{ print $2 }'`
  50. sleep 5
  51. read2=`grep vfs_reads /proc/bc/${i}/ioacct | awk '{ print $2 }'`
  52. write2=`grep vfs_writes /proc/bc/${i}/ioacct | awk '{ print $2 }'`
  53. else
  54. read1=`grep read /proc/bc/${i}/ioacct | awk '{ print $2 }'`
  55. write1=`grep write /proc/bc/${i}/ioacct | awk '{ print $2 }'`
  56. sleep 5
  57. read2=`grep read /proc/bc/${i}/ioacct | awk '{ print $2 }'`
  58. write2=`grep write /proc/bc/${i}/ioacct | awk '{ print $2 }'`
  59. fi
  60. ct_reads=`expr $read2 - $read1`
  61. ct_writes=`expr $write2 - $write1`
  62. ct_load=`/usr/sbin/vzlist $i -o laverage | sed '1 d' | cut -d / -f 1`
  63. echo "$i $ct_load $ct_reads $ct_writes" >> /tmp/vz-monitor.tmp
  64. ) &
  65. done
  66. wait
  67.  
  68. # Obtain second set of networking statistics
  69. iptables -L SOLUSVM_TRAFFIC_IN -n -x -v > /tmp/vz-net2-rx.tmp
  70. iptables -L SOLUSVM_TRAFFIC_OUT -n -x -v > /tmp/vz-net2-tx.tmp
  71.  
  72. # Populate VM information
  73. row="1"
  74. for i in $vmlist
  75. do
  76. # Calculate VPS RX/TX
  77. ct_ip=`vzlist $i | awk 'NR==2 { print $4 }'`
  78. ct_rx1=`grep -w $ct_ip /tmp/vz-net1-rx.tmp | awk '{ print $2 }'`
  79. ct_rx2=`grep -w $ct_ip /tmp/vz-net2-rx.tmp | awk '{ print $2 }'`
  80. ct_tx1=`grep -w $ct_ip /tmp/vz-net1-tx.tmp | awk '{ print $2 }'`
  81. ct_tx2=`grep -w $ct_ip /tmp/vz-net2-tx.tmp | awk '{ print $2 }'`
  82. ct_rx=`expr $ct_rx2 - $ct_rx1`
  83. ct_tx=`expr $ct_tx2 - $ct_tx1`
  84.  
  85. # Add information to table
  86. pt_1=`cat /tmp/vz-monitor.tmp | awk NR==$row`
  87. pt_2="$pt_1 `expr $ct_tx / 1024` `expr $ct_rx / 1024`"
  88. echo $pt_2 >> /tmp/vz-monitor2.tmp
  89. ((row++))
  90. done
  91.  
  92. # Complete and sort the table
  93. sed -i '1i VMNAME LOAD HDD(r/5sec) HDD(w/5sec) TX(kB/5sec) RX(kB/5sec)' /tmp/vz-monitor2.tmp
  94. column -t /tmp/vz-monitor2.tmp > /tmp/vz-monitor.txt
  95. sed -i '1i vz-monitor.sh : https://github.com/c-elliott' /tmp/vz-monitor.txt
  96. sed -i '2i ====================================================================================' /tmp/vz-monitor.txt
  97.  
  98. # Display table
  99. clear
  100. #cat /tmp/vz-monitor.txt
  101. ###VMs has high Load Average######
  102. mv /tmp/vmlist.txt /tmp/vmlist.txt.tmp2
  103. cat /tmp/vz-monitor.txt | awk '{ if ($2 > '$lavg') print $1}' | grep -v VMNAME | grep -v vz-monitor.sh > /tmp/vmlist.txt
  104. for i in `cat /tmp/vmlist.txt.tmp2` ; do if `grep $i /tmp/vmlist.txt >/dev/null 2>&1`; then echo $i >> /tmp/notify.txt ; fi; done
  105.  
  106. ##Whitelist Check###
  107. for cid in `cat /tmp/notify.txt` ; do if ! grep -Fxq "$cid" /root/servermonitor/whitelist.txt; then /usr/sbin/vzctl stop $cid --fast ; /bin/mail -s "The below VM has been reported for high Load Average" Server1@hostslayer.com <<< $cid ; fi ; done
  108. rm -f  /tmp/notify.txt
  109.  
  110. ###VM has high I/O usage#######
  111. mv /tmp/vmiolist.txt /tmp/vmiolist.txt.tmp2
  112. cat /tmp/vz-monitor.txt |awk '{ if (($3 > '$rlimit')||($4 > '$wlimit')) print $1}' | grep -v VMNAME | grep -v vz-monitor.sh > /tmp/vmiolist.txt
  113. for i in `cat /tmp/vmiolist.txt.tmp2` ; do if `grep $i /tmp/vmiolist.txt >/dev/null 2>&1`; then echo $i >> /tmp/notify.txt; fi; done
  114.  
  115. ##Whitelist Check###
  116. for cid in `cat /tmp/notify.txt` ; do if ! grep -Fxq "$cid" /root/servermonitor/whitelist.txt; then /usr/sbin/vzctl stop $cid --fast ; /bin/mail -s "The below VM has been reported for high I/O usage" Server1@hostslayer.com <<< $cid ; fi ; done
  117. rm -f  /tmp/notify.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement