您好,登錄后才能下訂單哦!
搭建好nagios 服務器之后,剩下的工作其實就是將欲監控的主機和服務添加到配置文件中,這過程其實有很多重復性的工作,所以我寫了個交互式腳本,可以一步到位。
腳本思路分析:
首先分析下主機和服務的配置:
主機:
define host{
use linux-server,hosts-pnp
host_name client
alias client
address 192.168.56.105
}
服務:
define service{
use generic-service,services-pnp
host_name client
service_description check_load
check_command check_nrpe!check_load
max_check_attempts 5
normal_check_interval 1
}
注:上面紅色部分為變化的部分,其余的都是固定的。也就是說需要將紅色部分設置成變量。
接下來的問題就是配置服務的時候,如何一次性讀入多個服務,實現一步到位。也就是說,我可以一次性添加多個服務,而無需重復性的運行腳本。下面我是通過數組來實現的。
腳本如下:
添加主機:
#!/bin/bash echo "Please input the infomations of the server you want to set." read -p "hostname:" HNAME read -p "alias:" ALIAS read -p "IP:" IP cat << EOF > /usr/local/nagios/etc/hosts/${HNAME}.cfg define host{ use linux-server,hosts-pnp host_name ${HNAME} alias ${ALIAS} address ${IP} } EOF service nagios reload
添加服務:
#!/bin/bash read -p "Please input your hostname :" HNAME read -p "please input the number of the services you want to set :" n echo "Please input the services' name :" read -a SHELL for ((i=0;i<=${n}-1;i++)) do echo "${SHELL[$i]}" >> /tmp/name.txt done while read line do cat << EOF >> /usrl/local/nagios/etc/services/${HNAME}.cfg define service{ use generic-service,services-pnp host_name ${HNAME} service_description check_${line} check_command check_nrpe!check_${line} max_check_attempts 5 normal_check_interval 1 } EOF echo "$line" done < /tmp/name.txt rm -rf /tmp/name.txt service nagios reload
運行結果如下:(注意:下面藍色部分為交互式輸入部分)
添加主機:
[root@localhost nagios]# ./autochost.sh
Please input the infomations of the server you want to set.
hostname:slave3
alias:slave3
IP:192.168.56.110
Running configuration check...done.
Reloading nagios configuration...done
[root@localhost nagios]# cat etc/hosts/slave3.cfg
define host{
use linux-server,hosts-pnp
host_name slave3
alias slave3
address 192.168.56.110
}
添加服務:
[root@localhost nagios]# ./autocservice.sh
Please input your hostname :slave3
please input the number of the services you want to set :4
Please input the services' name :
http ssh mysql ftp
[root@localhost nagios]# cat etc/services/slave3.cfg
define service{
use generic-service,services-pnp
host_name slave3
service_description check_http
check_command check_nrpe!check_http
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service,services-pnp
host_name slave3
service_description check_ssh
check_command check_nrpe!check_ssh
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service,services-pnp
host_name slave3
service_description check_mysql
check_command check_nrpe!check_mysql
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service,services-pnp
host_name slave3
service_description check_ftp
check_command check_nrpe!check_ftp
max_check_attempts 5
normal_check_interval 1
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。