You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

2 years ago
  1. #!/bin/bash
  2. HN=`hostname`
  3. if [[ -z $1 || -z $2 ]]; then
  4. ##### DISCOVERY #####
  5. VMS=`ls /etc/pve/nodes/$HN/qemu-server | sed s/\.conf//`
  6. if [[ -n ${VMS} ]]; then
  7. JSON="{ \"data\":["
  8. for VM in ${VMS}; do
  9. JSON=${JSON}"{ \"{#VMNAME}\":\"${VM}\"},"
  10. done
  11. JSON=${JSON}"]}"
  12. echo ${JSON}
  13. fi
  14. exit 0
  15. else
  16. ##### PARAMETERS #####
  17. VM="$1"
  18. METRIC="$2"
  19. FILECACHE="/tmp/zabbix.vmstat.cache"
  20. TTLCACHE="50"
  21. TIMENOW=`date '+%s'`
  22. ##### CACHE #####
  23. if [ -s "${FILECACHE}" ]; then
  24. TIMECACHE=`stat -c"%Y" "${FILECACHE}"`
  25. else
  26. TIMECACHE=0
  27. fi
  28. if [ "$((${TIMENOW} - ${TIMECACHE}))" -gt "${TTLCACHE}" ]; then
  29. echo "">> ${FILECACHE} # !!!
  30. DATACACHE1=`ps -eo pid,pcpu,%mem,rss,command | awk '/usr\/bin\/kvm -id/ {print "VM"$7" "$1" "$2" "$3" "$4}' | sort`
  31. DATACACHE2=`sudo /usr/sbin/iotop -P -k -n 1 --batch | awk '/kvm -id/ {print "VM"$14" "$1" "$4" "$6}' | sort`
  32. echo "${DATACACHE1}" > ${FILECACHE} # !!!
  33. echo "${DATACACHE2}">> ${FILECACHE} # !!!
  34. chmod 777 ${FILECACHE}
  35. fi
  36. ##### VM statistic #####
  37. if [ "` cat ${FILECACHE} | grep VM${VM}`" = "" ]; then
  38. echo 0
  39. exit 0
  40. fi
  41. case "$METRIC" in
  42. "cpu")
  43. cat ${FILECACHE} | grep VM${VM} | head -n 1 | awk '{print$3}'
  44. exit 0;;
  45. "mem")
  46. cat ${FILECACHE} | grep VM${VM} | head -n 1 | awk '{print$5}'
  47. exit 0;;
  48. "read")
  49. cat ${FILECACHE} | grep VM${VM} | tail -n 1 | awk '{print$3}'
  50. exit 0;;
  51. "write")
  52. cat ${FILECACHE} | grep VM${VM} | tail -n 1 | awk '{print$4}'
  53. exit 0;;
  54. "pid")
  55. cat ${FILECACHE} | grep VM${VM} | head -n 1 | awk '{print$2}'
  56. exit 0;;
  57. esac;
  58. #
  59. fi

Powered by TurnKey Linux.