遠程修改Nrpe的腳本
前言:作為一個運維人員,經常修改監控是無可避免的,改少量機器還好說,如果量特別多的話,那就不容易了。如老大一句話,修改所有機器的check_disk參數,那就傻了。我今天就遇到了,開發說要修改所有業務系統的check_swap,自己寫了個腳本,可以供大家參考下。
兩個文件: www.2cto.com
1、hostlist 這個是我需要修改監控參數的主機文件;
2、editnrpe.sh 這個是我的腳本,執行腳本時,需要調用1文件;
3、scannrpe.sh 這個是我修改完之後,再掃描一下Nrpe的腳本文件;
下面的腳本實現如下功能,之前的Nrpe參數為:
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 80% -c 70%
修改後的參數為:
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 75% -c 65%
腳本如下:
[
[email protected] ~]$ cat editnrpe.sh
#!/bin/bash
#
# This script is auto edit Nrpe
# By Only.liu
swapfile="/usr/local/nagios/etc/nrpe.cfg"
for host in `cat hostlist`
do
ping -c 1 $host >> /dev/null 2>&1
if [ "$?" == 0 ]; then
echo "$host is alived!"
ssh $host "sudo sed -i -e '/check_swap/{s/\-w 80\%/\-w 75\%/;s/-c 70\%/-c 65\%/}' '$swapfile'"
if [ "$?" == 0 ]; then
echo "Swap is edited success!"
ssh $host "grep "check_swap" $swapfile"
fi
ssh $host "ps aux | grep nrpe | grep -v grep | awk '{ print \$2 }' | xargs sudo kill -HUP"
ssh $host "exit"
echo "Nrpe restart ok!"
fi
done
修改完之後,不放心,再掃描一下:
腳本如下: www.2cto.com
[
[email protected] ~]$ cat scannrpe.sh
#!/bin/bash
#
# This script is auto edit Nrpe
# By Only.liu
#hostlist=`cat hostlist`
swapfile="/usr/local/nagios/etc/nrpe.cfg"
for host in `cat hostlist`
do
ping -c 1 $host >> /dev/null 2>&1
if [ "$?" == 0 ]; then
echo "$host is alived!"
ssh $host "sudo sed -n '/check_swap/p' '$swapfile'"
fi
done