From 141b07aa0ef87d49439583a507a26094ee414c40 Mon Sep 17 00:00:00 2001 From: msavarit Date: Wed, 5 Feb 2020 11:22:01 +0100 Subject: [PATCH 1/5] Remove sudo for virsh, zabbix user should be in libvirt group --- README.md | 2 ++ virbix/scripts/domain_check.sh | 2 +- virbix/scripts/domain_list.sh | 2 +- virbix/scripts/net_check.sh | 2 +- virbix/scripts/net_list.sh | 2 +- virbix/scripts/pool_check.sh | 2 +- virbix/scripts/pool_list.sh | 2 +- virbix/scripts/report_domains.sh | 2 +- virbix/scripts/report_nets.sh | 2 +- virbix/scripts/report_node.sh | 2 +- virbix/scripts/report_pools.sh | 2 +- 11 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e946f38..4687896 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ LIBVIRT_URI|qemu:///system ## Zabbix +Add zabbix user in libvirt group (mandatory for running virsh command without sudo). + #~ git clone https://github.com/sergiotocalini/virbix.git #~ sudo ./virbix/deploy_zabbix.sh "" #~ sudo systemctl restart zabbix-agent diff --git a/virbix/scripts/domain_check.sh b/virbix/scripts/domain_check.sh index b68bbfd..8c72552 100755 --- a/virbix/scripts/domain_check.sh +++ b/virbix/scripts/domain_check.sh @@ -1,6 +1,6 @@ #!/usr/bin/env ksh -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" UUID="${1}" ATTR="${2}" diff --git a/virbix/scripts/domain_list.sh b/virbix/scripts/domain_list.sh index e291e5c..b82f0ac 100755 --- a/virbix/scripts/domain_list.sh +++ b/virbix/scripts/domain_list.sh @@ -1,6 +1,6 @@ #!/usr/bin/env ksh -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" for vm in `${VIRSH} list --all --uuid | sort | uniq`; do output="" ${VIRSH} dominfo ${vm} | while read line; do diff --git a/virbix/scripts/net_check.sh b/virbix/scripts/net_check.sh index bb8dd39..ed10ed1 100755 --- a/virbix/scripts/net_check.sh +++ b/virbix/scripts/net_check.sh @@ -1,7 +1,7 @@ #!/usr/bin/env ksh APP_DIR=$(dirname $0) -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" UUID="${1}" ATTR="${2}" TIMESTAMP=`date '+%s'` diff --git a/virbix/scripts/net_list.sh b/virbix/scripts/net_list.sh index 06dad6b..bf4e5e4 100755 --- a/virbix/scripts/net_list.sh +++ b/virbix/scripts/net_list.sh @@ -1,6 +1,6 @@ #!/usr/bin/env ksh -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" for net in `${VIRSH} net-list --all --uuid | sort | uniq`; do output="" ${VIRSH} net-info ${net} | while read line; do diff --git a/virbix/scripts/pool_check.sh b/virbix/scripts/pool_check.sh index 8e6c851..505a97f 100755 --- a/virbix/scripts/pool_check.sh +++ b/virbix/scripts/pool_check.sh @@ -1,7 +1,7 @@ #!/usr/bin/env ksh APP_DIR=$(dirname $0) -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" UUID="${1}" ATTR="${2}" TIMESTAMP=`date '+%s'` diff --git a/virbix/scripts/pool_list.sh b/virbix/scripts/pool_list.sh index d25ebde..c6862be 100755 --- a/virbix/scripts/pool_list.sh +++ b/virbix/scripts/pool_list.sh @@ -1,6 +1,6 @@ #!/usr/bin/env ksh -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" for vm in `${VIRSH} pool-list --all | egrep -v "(^-.*|^ Name.*|^$)" | awk '{print $1}' | sort | uniq`; do output="" ${VIRSH} pool-info ${vm} | while read line; do diff --git a/virbix/scripts/report_domains.sh b/virbix/scripts/report_domains.sh index 73d9053..ce7a2d0 100755 --- a/virbix/scripts/report_domains.sh +++ b/virbix/scripts/report_domains.sh @@ -10,7 +10,7 @@ TIMESTAMP=`date '+%s'` IFS_DEFAULT="${IFS}" -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" UUID="${1:-all}" TYPE="${2:-json}" diff --git a/virbix/scripts/report_nets.sh b/virbix/scripts/report_nets.sh index 73d9053..ce7a2d0 100755 --- a/virbix/scripts/report_nets.sh +++ b/virbix/scripts/report_nets.sh @@ -10,7 +10,7 @@ TIMESTAMP=`date '+%s'` IFS_DEFAULT="${IFS}" -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" UUID="${1:-all}" TYPE="${2:-json}" diff --git a/virbix/scripts/report_node.sh b/virbix/scripts/report_node.sh index 2caed1e..f715d7e 100755 --- a/virbix/scripts/report_node.sh +++ b/virbix/scripts/report_node.sh @@ -10,7 +10,7 @@ TIMESTAMP=`date '+%s'` IFS_DEFAULT="${IFS}" -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" UUID="${1:-all}" TYPE="${2:-json}" diff --git a/virbix/scripts/report_pools.sh b/virbix/scripts/report_pools.sh index 4e11d7e..561a674 100755 --- a/virbix/scripts/report_pools.sh +++ b/virbix/scripts/report_pools.sh @@ -10,7 +10,7 @@ TIMESTAMP=`date '+%s'` IFS_DEFAULT="${IFS}" -VIRSH="sudo `which virsh`" +VIRSH="`which virsh`" UUID="${1:-all}" TYPE="${2:-json}" From 7c82cbaa2874193d99628d3797605ede17b19455 Mon Sep 17 00:00:00 2001 From: msavarit Date: Wed, 5 Feb 2020 11:37:26 +0100 Subject: [PATCH 2/5] Change ownership of /etc/zabbix/scripts/agentd/virbix for allowing zabbix to write .xml --- deploy_zabbix.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/deploy_zabbix.sh b/deploy_zabbix.sh index 0084be4..d4177da 100755 --- a/deploy_zabbix.sh +++ b/deploy_zabbix.sh @@ -9,4 +9,5 @@ cp -r ${SOURCE_DIR}/virbix/scripts ${ZABBIX_DIR}/scripts/agentd/virbix/ cp ${SOURCE_DIR}/virbix/virbix.conf.example ${ZABBIX_DIR}/scripts/agentd/virbix/virbix.conf cp ${SOURCE_DIR}/virbix/virbix.sh ${ZABBIX_DIR}/scripts/agentd/virbix/ cp ${SOURCE_DIR}/virbix/zabbix_agentd.conf ${ZABBIX_DIR}/zabbix_agentd.d/virbix.conf +chown -R zabbix:zabbix ${ZABBIX_DIR}/scripts/agentd/virbix sed -i "s|LIBVIRT_DEFAULT_URI=.*|LIBVIRT_DEFAULT_URI=\"${LIBVIRT_URI}\"|g" ${ZABBIX_DIR}/scripts/agentd/virbix/virbix.conf From ab3193286b5a717a9153c8fca8e036698a6bba04 Mon Sep 17 00:00:00 2001 From: msavarit Date: Wed, 5 Feb 2020 14:26:54 +0100 Subject: [PATCH 3/5] --uid option do not exist for virsh net-list, remove it and adapt filter to get network list --- virbix/scripts/net_list.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virbix/scripts/net_list.sh b/virbix/scripts/net_list.sh index bf4e5e4..e4c8259 100755 --- a/virbix/scripts/net_list.sh +++ b/virbix/scripts/net_list.sh @@ -1,7 +1,7 @@ #!/usr/bin/env ksh VIRSH="`which virsh`" -for net in `${VIRSH} net-list --all --uuid | sort | uniq`; do +for net in `${VIRSH} net-list --all | sed 1,2d | sed '/^$/d' | cut -d' ' -f 2 | uniq`; do output="" ${VIRSH} net-info ${net} | while read line; do key=`echo ${line}|awk -F: '{print $1}'|awk '{$1=$1};1'` From 160acb7ceb8eb2ebc0fcc13428b748c503e996e7 Mon Sep 17 00:00:00 2001 From: msavarit Date: Wed, 5 Feb 2020 17:12:52 +0100 Subject: [PATCH 4/5] Add mapping for domain state. Storing string in zabbix is something nobody wants. --- virbix/scripts/domain_check.sh | 16 +++++++++++++++- virbix/scripts/pool_check.sh | 16 +++++++++++++++- zbx3.4_template_hv_kvm.xml | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/virbix/scripts/domain_check.sh b/virbix/scripts/domain_check.sh index 8c72552..cd25d00 100755 --- a/virbix/scripts/domain_check.sh +++ b/virbix/scripts/domain_check.sh @@ -5,7 +5,21 @@ UUID="${1}" ATTR="${2}" if [[ ${ATTR} == 'state' ]]; then - ${VIRSH} domstate ${UUID}|sed '/^\s*$/d' + state=`${VIRSH} domstate ${UUID}|sed '/^\s*$/d'` + case $state in + "running") + echo "0" + ;; + "paused") + echo "1" + ;; + "shut off") + echo "2" + ;; + *) + echo "3" + ;; + esac elif [[ ${ATTR} == 'json' ]]; then dump=`${VIRSH} dumpxml ${UUID}` memory=`echo "${xml}" | xmlstarlet sel -q -T -t -m "domain/memory" -v . -n` diff --git a/virbix/scripts/pool_check.sh b/virbix/scripts/pool_check.sh index 505a97f..32a6ab8 100755 --- a/virbix/scripts/pool_check.sh +++ b/virbix/scripts/pool_check.sh @@ -28,7 +28,21 @@ elif [[ ${ATTR} == 'size_free' ]]; then elif [[ ${ATTR} == 'size_total' ]]; then rval=`xmllint --xpath "string(//pool/capacity)" ${CACHE_FILE}` elif [[ ${ATTR} == 'state' ]]; then - rval="`${VIRSH} pool-info ${UUID}|grep '^State:'|awk -F: '{print $2}'|awk '{$1=$1};1'`" + state="`${VIRSH} pool-info ${UUID}|grep '^State:'|awk -F: '{print $2}'|awk '{$1=$1};1'`" + case $state in + "running") + rval="0" + ;; + "paused") + rval="1" + ;; + "shut off") + rval="2" + ;; + *) + rval="3" + ;; + esac fi echo ${rval:-0} diff --git a/zbx3.4_template_hv_kvm.xml b/zbx3.4_template_hv_kvm.xml index c9556e9..2791c85 100644 --- a/zbx3.4_template_hv_kvm.xml +++ b/zbx3.4_template_hv_kvm.xml @@ -87,14 +87,17 @@ Is the domain running or not 0 - running 1 - paused -2 - shut off +2 - shut off +3 - unknown 0 KVM - + + VIRSH Domain State + @@ -357,7 +360,9 @@ KVM - + + VIRSH Domain State + @@ -439,4 +444,27 @@ + + + VIRSH Domain State + + + 0 + running + + + 1 + paused + + + 2 + shut off + + + 3 + unknown + + + + From c55f04a27b38f121e586c8d220f9ada3ff7aae6e Mon Sep 17 00:00:00 2001 From: msavarit Date: Wed, 5 Feb 2020 17:24:54 +0100 Subject: [PATCH 5/5] Change type from string to int for state --- zbx3.4_template_hv_kvm.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zbx3.4_template_hv_kvm.xml b/zbx3.4_template_hv_kvm.xml index 2791c85..f3beea9 100644 --- a/zbx3.4_template_hv_kvm.xml +++ b/zbx3.4_template_hv_kvm.xml @@ -66,7 +66,7 @@ 30d 0 0 - 1 + 3 @@ -335,7 +335,7 @@ 7d 0 0 - 1 + 3