|
|
- #!/bin/bash
- HN=`hostname`
- if [[ -z $1 || -z $2 ]]; then
- ##### DISCOVERY #####
- VMS=`ls /etc/pve/nodes/$HN/qemu-server | sed s/\.conf//`
- if [[ -n ${VMS} ]]; then
- JSON="{ \"data\":["
- for VM in ${VMS}; do
- JSON=${JSON}"{ \"{#VMNAME}\":\"${VM}\"},"
- done
- JSON=${JSON}"]}"
- echo ${JSON}
- fi
- exit 0
- else
- ##### PARAMETERS #####
- VM="$1"
- METRIC="$2"
- FILECACHE="/tmp/zabbix.vmstat.cache"
- TTLCACHE="50"
- TIMENOW=`date '+%s'`
- ##### CACHE #####
- if [ -s "${FILECACHE}" ]; then
- TIMECACHE=`stat -c"%Y" "${FILECACHE}"`
- else
- TIMECACHE=0
- fi
- if [ "$((${TIMENOW} - ${TIMECACHE}))" -gt "${TTLCACHE}" ]; then
- echo "">> ${FILECACHE} # !!!
- DATACACHE1=`ps -eo pid,pcpu,%mem,rss,command | awk '/usr\/bin\/kvm -id/ {print "VM"$7" "$1" "$2" "$3" "$4}' | sort`
- DATACACHE2=`sudo /usr/sbin/iotop -P -k -n 1 --batch | awk '/kvm -id/ {print "VM"$14" "$1" "$4" "$6}' | sort`
- echo "${DATACACHE1}" > ${FILECACHE} # !!!
- echo "${DATACACHE2}">> ${FILECACHE} # !!!
- chmod 777 ${FILECACHE}
- fi
- ##### VM statistic #####
- if [ "` cat ${FILECACHE} | grep VM${VM}`" = "" ]; then
- echo 0
- exit 0
- fi
- case "$METRIC" in
- "cpu")
- cat ${FILECACHE} | grep VM${VM} | head -n 1 | awk '{print$3}'
- exit 0;;
- "mem")
- cat ${FILECACHE} | grep VM${VM} | head -n 1 | awk '{print$5}'
- exit 0;;
- "read")
- cat ${FILECACHE} | grep VM${VM} | tail -n 1 | awk '{print$3}'
- exit 0;;
- "write")
- cat ${FILECACHE} | grep VM${VM} | tail -n 1 | awk '{print$4}'
- exit 0;;
- "pid")
- cat ${FILECACHE} | grep VM${VM} | head -n 1 | awk '{print$2}'
- exit 0;;
- esac;
- #
- fi
|