zabbix tcpport discovery - lld

put script in folder /usr/lib/zabbix/externalscripts

zabbix_tcpport_discovery.sh
---------------
#!/bin/sh
echo '{' > /tmp/zabbix.tcpport
echo ' "data":[' >> /tmp/zabbix.tcpport
nmap -T4 -F ${1} | grep 'open' | while read port ; do
port=$(echo ${port} | cut -d/ -f1)
echo '  { "{#PORT}":"'${port}'" },' >> /tmp/zabbix.tcpport
done
echo ' ]'>> /tmp/zabbix.tcpport
echo '}'>> /tmp/zabbix.tcpport
tac /tmp/zabbix.tcpport | sed '1,3s/},/}/g' | tac
rm -rf /tmp/zabbix.tcpport
exit 0
---------------

another version
-------
#!/bin/sh

openCount=$(nmap -T4 -F ${1} | grep 'open' |wc -l)
Count=1

echo '{'
echo ' "data":['

nmap -T4 -F ${1} | grep 'open' | while read port ; do
port=$(echo ${port} | cut -d/ -f1)

if [ $Count -lt $openCount ]; then
   echo '  { "{#PORT}":"'${port}'" },'
else
   echo '  { "{#PORT}":"'${port}'" }'
fi
   Count=$(($Count+1))
done

echo ' ]'
echo '}'
exit 0
-------



nmap掃伺服器(參數1)的open port
掃的過程中做一個暫存檔/tmp/zabbix.tcpport是提供給zabbix的json檔案
掃完後這個檔案會tac一次,把本來在最後的一組資料的},,改成是},以符合json的格式規範
再tac一次倒回原樣

##
或參考 zabbix wiki

檢查的scrip
https://www.zabbix.org/wiki/File:Tcp-service-discovery.sh
使用template
https://www.zabbix.org/wiki/File:ZabbixTemplate_-_TCP_service_discovery.xml

# 20170310
https://github.com/ruilung/net-listen-json
pythod code to solve this issue in client side.

留言

熱門文章