
我有一個設定係統,將新主機設定在網域 test.domain.local 中,即client44.test.domain.local
,並且我有一個 Icinga 伺服器,我想使用正規表示式自動監視所有這些主機,例如*.test.domain.local
。
所有客戶端都將獲得nagios-nrpe-server
(版本 2.13-3)包,該包還配置為允許 icinga-server 從中獲取數據,並且這已被驗證可以正常工作。
我們現在只是要監視我們知道所有節點都會有的服務/事物,例如 SSH、對 ping 的回應等。
我看過這個連結但我不太明白主機、主機群組和服務類別之間的關係?
Icinga 伺服器和所有客戶端都執行 Debian。
答案1
我一直在與 fredmu 合作解決這個問題,並在 Tim Brigham 的回答的啟發下提出了一個可行的解決方案。
使用預設generic-host
定義,可以使用基於範本自動產生主機設定檔的腳本來解決此問題。或者,它可以作為一個 cronjob 來定期產生這些文件。
產生_主機_cfg.sh
#!/usr/bin/env bash
icinga_root="/etc/icinga/objects"
subnet="10.0.0.*"
template="template_icinga.cfg"
alias_file="alias.txt"
# navigate to the Icinga configuration directory
cd $icinga_root
# create first server alias if doesn't exist
if [ ! -e $alias_file ]; then
echo "1" > $alias_file
fi
# iterate through subnet, store "hostname:ip" in associative array
declare -A address
for host in $(nmap -sP $subnet | awk -F '[ ()]' '/for [a-z]+/ {print $5 ":" $7}'); do
address[$(echo $host | cut -d: -f1)]=$(echo $host | cut -d: -f2)
done
# iterate through hosts, create files if not exist based off template
for host in ${!address[@]}; do
host_file=${host}_icinga.cfg
if [ ! -e $host_file ]; then
# fetch new server alias
alias=$(cat $alias_file)
# create the next server alias
expr $alias + 1 > $alias_file
# create hostname_icinga.cfg if doesn't exist, based off template
cp $template $host_file
# replace contents of new template; hostname, alias and ip
sed -i -r \
-e "s/tmp-hostname/$host/" \
-e "s/tmp-alias/Server$alias/" \
-e "s/tmp-address/${address[$host]}/" $host_file
fi
done
模板_icinga.cfg
define host{
use generic-host
host_name tmp-hostname
alias tmp-alias
address tmp-address
}
產生如下文件:
define host{
use generic-host
host_name monitor.company.local
alias Server1
address 10.0.0.1
}
答案2
有很多方法可以解決這個問題。
我之前已經組裝了 bash 腳本,這些腳本將作為單一 icinga 檢查運行。然後,腳本將檢查子網域中的所有主機,並根據是否發現故障或發現故障數量返回狀態。
最近,我開始喜歡木偶中的自訂事實。結合良好的 writeb 模板,您可以使用 for 迴圈輕鬆地在大量節點上新增檢查。