From 90166ba51f5d45f0150ba02e106938965ca1eef3 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 15 Jul 2020 05:40:49 +0200 Subject: [PATCH] Working first version. --- .gitignore | 1 + generate.rb | 12 ++ rc.zabbix_agentd | 48 +++++++ scripts/disk.sh | 52 +++++++ scripts/disks.discovery.sh | 16 +++ scripts/qemu.discovery.sh | 11 ++ scripts/qemu.sh | 24 ++++ zabbix_agent.plg | 287 +++++++++++++++++++++++++++++++++++++ zabbix_agent.template.plg | 108 ++++++++++++++ zabbix_agentd.conf | 28 ++++ 10 files changed, 587 insertions(+) create mode 100644 .gitignore create mode 100755 generate.rb create mode 100644 rc.zabbix_agentd create mode 100755 scripts/disk.sh create mode 100755 scripts/disks.discovery.sh create mode 100755 scripts/qemu.discovery.sh create mode 100755 scripts/qemu.sh create mode 100644 zabbix_agent.plg create mode 100644 zabbix_agent.template.plg create mode 100644 zabbix_agentd.conf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c595a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +temp diff --git a/generate.rb b/generate.rb new file mode 100755 index 0000000..a81259b --- /dev/null +++ b/generate.rb @@ -0,0 +1,12 @@ +#!/usr/bin/env ruby + +puts "Reading zabbix_agent.template.plg..." +input = File.read("./zabbix_agent.template.plg") + +output = input.gsub(/{{{ (.+) }}}/) do + puts "Including #{$1}..." + File.read($1) +end +puts "Writing zabbix_agent.plg..." +File.write("./zabbix_agent.plg", output) +puts "Done." diff --git a/rc.zabbix_agentd b/rc.zabbix_agentd new file mode 100644 index 0000000..d8a00c6 --- /dev/null +++ b/rc.zabbix_agentd @@ -0,0 +1,48 @@ +#!/bin/sh +# +# rc.zabbix_agentd This shell script takes care of starting and stopping +# the Zabbix agent + +OPTIONS="-c /boot/config/plugins/zabbix_agent/zabbix_agentd.conf" + +start() { + if [ -x /usr/sbin/zabbix_agentd ]; then + echo -n "Starting zabbix_agentd: " + /usr/sbin/zabbix_agentd $OPTIONS + echo " /usr/sbin/zabbix_agentd $OPTIONS" + fi +} + +stop() { + # Stop daemons. + COUNT=0 + echo -n "Shutting down zabbix_agentd: " + while `killall zabbix_agentd 2>/dev/null`; do + echo -n "." + sleep 1 + COUNT=$((COUNT+1)) + if [ $COUNT -ge 30 ]; then + killall -9 zabbix_agentd + sleep 1 + break + fi + done + echo " DONE" +} + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart|reload) + stop + start + ;; + *) + echo $"Usage: $0 {start|stop|restart}" + ;; +esac diff --git a/scripts/disk.sh b/scripts/disk.sh new file mode 100755 index 0000000..8d5148c --- /dev/null +++ b/scripts/disk.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +id="$1" +found=0 +device="" + +while read line; do + if [[ $line == "["* ]]; then + if [ $found = 1 ]; then + echo -n '{"device":"'$device'", "name":"'$name'", "status":"'$status'", "temp":'$temp', "size":'$size', "num_reads":'$num_reads', ' + echo -n '"num_writes":'$num_writes', "num_errors":'$num_errors', "type":"'$type'", "fs_size":'$fsSize', "fs_free":'$fsFree', ' + break + else + device='' + name='' + status='' + temp=-1 + size=-1 + num_reads=-1 + num_writes=-1 + num_errors=-1 + type="" + fsSize=-1 + fsFree=-1 + fi + else + while IFS="=" read key value; do + value="${value%\"}" + value="${value#\"}" + [ $key = "id" ] && [ "$value" = "$id" ] && found=1 + [ $key = "device" ] && device="$value" + [ $key = "name" ] && name="$value" + [ $key = "status" ] && status="$value" + [ $key = "temp" ] && [ "$value" != "*" ] && temp="$value" + [ $key = "size" ] && size=$(( $value * 1024 )) + [ $key = "numReads" ] && num_reads="$value" + [ $key = "numWrites" ] && num_writes="$value" + [ $key = "numErrors" ] && num_errors="$value" + [ $key = "type" ] && type="$value" + [ $key = "fsSize" ] && fsSize=$(( $value * 1024 )) + [ $key = "fsFree" ] && fsFree=$(( $value * 1024 )) + done <<< "$line" + fi +done < <(cat /var/local/emhttp/disks.ini ; echo "[empty]") + +if [ $found = 0 ]; then + echo "UNSUPPORTED" + exit 1 +fi + +read bytes_read bytes_written _ < <(grep $device"=" /var/local/emhttp/diskload.ini | cut -d"=" -f2) +echo '"bytes_reading":'$bytes_read', "bytes_writing":'$bytes_written'}' diff --git a/scripts/disks.discovery.sh b/scripts/disks.discovery.sh new file mode 100755 index 0000000..b3d7339 --- /dev/null +++ b/scripts/disks.discovery.sh @@ -0,0 +1,16 @@ +#!/bin/bash +id='""' +device='""' +first=1 + +echo -n '[' + +while IFS="=" read key value; do + if [ "$key" = "id" ] && [ -n "$value" ] && [ "$value" != '""' ]; then + [ $first = 0 ] && echo -n "," + first=0 + echo -n '{"{#ID}":'$value'}' + fi +done < /var/local/emhttp/disks.ini + +echo ']' diff --git a/scripts/qemu.discovery.sh b/scripts/qemu.discovery.sh new file mode 100755 index 0000000..fccfba8 --- /dev/null +++ b/scripts/qemu.discovery.sh @@ -0,0 +1,11 @@ +#!/bin/bash +first=1 + +echo -n "[" +while read name; do + [ "$name" = "" ] && continue + [ $first = 0 ] && echo -n "," + first=0 + echo -n '{"{#VM}":"'$name'"}' +done < <(virsh list --name --all) +echo "]" diff --git a/scripts/qemu.sh b/scripts/qemu.sh new file mode 100755 index 0000000..fcd02a2 --- /dev/null +++ b/scripts/qemu.sh @@ -0,0 +1,24 @@ +#!/bin/bash +VM="$1" + +echo -n '{' +state=`virsh domstate "$VM"` +echo -n '"state":"'$state'"' +if [ "$state" != "running" ]; then + echo -n '}' + exit +fi + +while IFS=":" read key value; do + [ "$key" = "CPU(s)" ] && echo -n ', "cpus":'$value + [ "$key" = "CPU time" ] && echo -n ', "cpu_time":'${value%s} + [ "$key" = "Max memory" ] && echo -n ', "memory_max":'$((${value%KiB} * 1024)) + [ "$key" = "Used memory" ] && echo -n ', "memory_used":'$((${value%KiB} * 1024)) +done < <(virsh dominfo "$VM") + +while read if key value; do + [ "$key" = "rx_bytes" ] && echo -n ', "network_bytes_in":'$value + [ "$key" = "tx_bytes" ] && echo -n ', "network_bytes_out":'$value +done < <(virsh domifstat "$VM" vnet0) + +echo "}" diff --git a/zabbix_agent.plg b/zabbix_agent.plg new file mode 100644 index 0000000..0a169e5 --- /dev/null +++ b/zabbix_agent.plg @@ -0,0 +1,287 @@ + + + + + + + + + + +]> + + + + +##&name; + +### 2020-07-14 +- Initial commit + + + +echo "Installation of zabbix_agent plugin started." +mkdir -p /boot/config/plugins/zabbix_agent/scripts +if [ -e /boot/config/plugins/zabbix_agent/zabbix_agentd-&agent_version; ]; then + echo "Agent already exists. Using local files." +else + echo "zabbix_agent version &agent_version; not found." + echo "Removing old files..." + rm /boot/config/plugins/zabbix_agent/zabbix_agentd-* + rm /boot/config/plugins/zabbix_agent/rc.zabbix_agentd + rm /boot/config/plugins/zabbix_agent/zabbix_agentd.conf + rm /boot/config/plugins/zabbix_agent/scripts/* + echo "Downloading &agentURL;..." + wget "&agentURL;" -O /boot/config/plugins/zabbix_agent/zabbix_agentd-&agent_version; +fi + + + +/dev/null`; do + echo -n "." + sleep 1 + COUNT=$((COUNT+1)) + if [ $COUNT -ge 30 ]; then + killall -9 zabbix_agentd + sleep 1 + break + fi + done + echo " DONE" +} + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart|reload) + stop + start + ;; + *) + echo $"Usage: $0 {start|stop|restart}" + ;; +esac + +]]> + + + + + + + + + + + + + + + + + + + + + + + +cp /boot/config/plugins/zabbix_agent/rc.zabbix_agentd /etc/rc.d/rc.zabbix_agentd +chmod +x /etc/rc.d/rc.zabbix_agentd + +mkdir -p /usr/local/emhttp/plugins/zabbix_agent/scripts +cp -R /boot/config/plugins/zabbix_agent/scripts/* /usr/local/emhttp/plugins/zabbix_agent/scripts/ +chmod +x /usr/local/emhttp/plugins/zabbix_agent/scripts/* + +/etc/rc.d/rc.zabbix_agentd stop + +cp /boot/config/plugins/zabbix_agent/zabbix_agentd-&agent_version; /usr/sbin/zabbix_agentd +chmod +x /usr/sbin/zabbix_agentd + +/etc/rc.d/rc.zabbix_agentd start + + + + + + +echo "Removal of zabbix_agent plugin started." +echo "Stopping zabbix_agentd..." +/etc/rc.d/rc.zabbix_agentd stop +echo "Deleting /usr/sbin/zabbix_agentd..." +rm /usr/sbin/zabbix_agentd +echo "Deleting /boot/config/plugins/zabbix_agent..." +rm -rf /boot/config/plugins/zabbix_agent +echo "Deleting /usr/local/emhttp/plugins/zabbix_agent..." +rm -rf /usr/local/emhttp/plugins/zabbix_agent +echo "Removal of zabbix_agent plugin completed." + + + diff --git a/zabbix_agent.template.plg b/zabbix_agent.template.plg new file mode 100644 index 0000000..5bbaed8 --- /dev/null +++ b/zabbix_agent.template.plg @@ -0,0 +1,108 @@ + + + + + + + + + + +]> + + + + +##&name; + +### 2020-07-14 +- Initial commit + + + +echo "Installation of zabbix_agent plugin started." +mkdir -p /boot/config/plugins/zabbix_agent/scripts +if [ -e /boot/config/plugins/zabbix_agent/zabbix_agentd-&agent_version; ]; then + echo "Agent already exists. Using local files." +else + echo "zabbix_agent version &agent_version; not found." + echo "Removing old files..." + rm /boot/config/plugins/zabbix_agent/zabbix_agentd-* + rm /boot/config/plugins/zabbix_agent/rc.zabbix_agentd + rm /boot/config/plugins/zabbix_agent/zabbix_agentd.conf + rm /boot/config/plugins/zabbix_agent/scripts/* + echo "Downloading &agentURL;..." + wget "&agentURL;" -O /boot/config/plugins/zabbix_agent/zabbix_agentd-&agent_version; +fi + + + + + + + + + + + + + + + + + + + + + + + + + + + +cp /boot/config/plugins/zabbix_agent/rc.zabbix_agentd /etc/rc.d/rc.zabbix_agentd +chmod +x /etc/rc.d/rc.zabbix_agentd + +mkdir -p /usr/local/emhttp/plugins/zabbix_agent/scripts +cp -R /boot/config/plugins/zabbix_agent/scripts/* /usr/local/emhttp/plugins/zabbix_agent/scripts/ +chmod +x /usr/local/emhttp/plugins/zabbix_agent/scripts/* + +/etc/rc.d/rc.zabbix_agentd stop + +cp /boot/config/plugins/zabbix_agent/zabbix_agentd-&agent_version; /usr/sbin/zabbix_agentd +chmod +x /usr/sbin/zabbix_agentd + +/etc/rc.d/rc.zabbix_agentd start + + + + + + +echo "Removal of zabbix_agent plugin started." +echo "Stopping zabbix_agentd..." +/etc/rc.d/rc.zabbix_agentd stop +echo "Deleting /usr/sbin/zabbix_agentd..." +rm /usr/sbin/zabbix_agentd +echo "Deleting /boot/config/plugins/zabbix_agent..." +rm -rf /boot/config/plugins/zabbix_agent +echo "Deleting /usr/local/emhttp/plugins/zabbix_agent..." +rm -rf /usr/local/emhttp/plugins/zabbix_agent +echo "Removal of zabbix_agent plugin completed." + + + diff --git a/zabbix_agentd.conf b/zabbix_agentd.conf new file mode 100644 index 0000000..b1111b7 --- /dev/null +++ b/zabbix_agentd.conf @@ -0,0 +1,28 @@ +Server=0.0.0.0/0 +LogType=system +AllowRoot=1 +StartAgents=1 + +DenyKey=system.run[*] +AllowKey=agent.* +AllowKey=net.if.* +AllowKey=net.if.*[*] +AllowKey=proc.* +AllowKey=proc.*[*] +AllowKey=sensor[*] +AllowKey=system.* +AllowKey=system.*[*] +AllowKey=vfs.dev.* +AllowKey=vfs.dev.*[*] +AllowKey=vfs.fs.* +AllowKey=vfs.fs.*[*] +AllowKey=vm.memory.size[*] +AllowKey=unraid.* +AllowKey=unraid.*[*] + +DenyKey=* + +UserParameter=unraid.disks.discovery,/usr/local/emhttp/plugins/zabbix_agent/scripts/disks.discovery.sh +UserParameter=unraid.disk[*],/usr/local/emhttp/plugins/zabbix_agent/scripts/disk.sh "$1" +UserParameter=unraid.qemu.discovery,/usr/local/emhttp/plugins/zabbix_agent/scripts/qemu.discovery.sh +UserParameter=unraid.qemu[*],/usr/local/emhttp/plugins/zabbix_agent/scripts/qemu.sh "$1"