我正在尝试配置我的 Nagios 设置,以便在主机发生故障(PING 服务进入 HARD CRITICAL 状态)时自动在我们的票务系统中记录票证。我有一个脚本,它可以创建票证,使用“sudo -u nagios”成功运行。我设置了一个命令和事件处理程序,当主机状态失败(或至少看起来失败)时执行。但是,与命令关联的脚本似乎没有执行,或者我添加的日志记录不起作用。有什么想法下一步该看哪里吗?调用事件处理程序时不会创建日志文件。
命令:
define command {
command_name make-ticket
command_line /etc/nagios/commands/make-ticket-wrapper "$SERVICESTATE$" "$SERVICESTATETYPE$" "$HOSTNAME" "$HOSTADDRESS$ "$HOSTSTATE" "$HOSTGROUPALIAS" "$SERVICEDESC"
}
示例主机和服务
define host {
use generic-switch
host_name test
alias test
address 192.168.100.13
}
define service {
use generic-service
host_name test
service_description PING
check_command check_ping!200.0,20%!600.0,60%
normal_check_interval 5
retry_check_interval 1
event_handler make-ticket
}
包装脚本:
#!/bin/bash
#
# Cut a ticket
#
set -x
LOGFILE="/tmp/tickets.log"
touch $LOGFILE
echo Running make-ticket-wrapper `date` >> $LOGFILE
echo Params: $* >> $LOGFILE
TICKET="/etc/nagios/commands/make-ticket"
SERVICESTATE="$1"
SERVICESTATETYPE="$2"
HOSTNAME="$3"
HOSTADDRESS="$4"
HOSTSTATE="$5"
HOSTGROUPALIAS="$6"
SERVICEDESC="$7"
echo "SERVICESTATE=$SERVICESTATE" >> $LOGFILE
echo "SERVICESTATETYPE=$SERVICESTATETYPE" >> $LOGFILE
echo "HOSTNAME=$HOSTNAME" >> $LOGFILE
echo "HOSTADDRESS=$HOSTADDRESS" >> $LOGFILE
echo "HOSTSTATE=$HOSTSTATE" >> $LOGFILE
echo "HOSTGROUPALIAS=$HOSTGROUPALIAS" >> $LOGFILE
echo "SERVICEDESC=$SERVICEDESC" >> $LOGFILE
$TICKET "$SERVICESTATE" "$SERVICESTATETYPE" "$HOSTNAME" "$HOSTADDRESS" "$HOSTSTATE" "$HOSTGROUPALIAS" "$SERVICEDESC" 2>&1 | tee -a $LOGFILE
日志:
[1422560163] HOST ALERT: test;DOWN;SOFT;1;(Host Check Timed Out)
[1422560253] HOST ALERT: test;DOWN;SOFT;2;(Host Check Timed Out)
[1422560353] HOST ALERT: test;DOWN;SOFT;3;(Host Check Timed Out)
[1422560433] SERVICE ALERT: test;PING;CRITICAL;HARD;1;PING CRITICAL - Packet loss = 100%
[1422560433] SERVICE EVENT HANDLER: test;PING;CRITICAL;HARD;1;make-ticket
答案1
command_line /etc/nagios/commands/make-ticket-wrapper "$SERVICESTATE$" "$SERVICESTATETYPE$" "$HOSTNAME" "$HOSTADDRESS$ "$HOSTSTATE" "$HOSTGROUPALIAS" "$SERVICEDESC"
$HOSTADDRESS$ 后缺少双引号,并且 HOSTNAME、HOSTSTATE、HOSTGROUPALIAS 和 SERVICEDESC 末尾缺少 $。
当您错过双引号时,它会搞乱该行的其余部分,导致 shell 脚本错过一些参数。
当您省略尾随的美元符号时,Nagios 不会替换宏,因此它会被传递到 shell 中,在那里它被解析为一个空变量。
答案2
nagios 因这些引号而窒息
改变:
command_line /etc/nagios/commands/make-ticket-wrapper "$SERVICESTATE$" "$SERVICESTATETYPE$" "$HOSTNAME" "$HOSTADDRESS$ "$HOSTSTATE" "$HOSTGROUPALIAS" "$SERVICEDESC"
到:
command_line /etc/nagios/commands/make-ticket-wrapper $SERVICESTATE$ $SERVICESTATETYPE$ $HOSTNAME$ $HOSTADDRESS$ $HOSTSTATE$ $HOSTGROUPALIAS$ $SERVICEDESC$
另外,您还漏掉了命令行中的一些 $ 符号,请查看上文。之后它将按预期工作。我刚刚尝试过:
#cat /tmp/tickets.log Running make-ticket-wrapper Mon Feb 2 17:32:05 EST 2015 Params: OK HARD test 192.168.1.1 UP $ PING SERVICESTATE=OK SERVICESTATETYPE=HARD HOSTNAME=test HOSTADDRESS=192.168.1.1 HOSTSTATE=UP HOSTGROUPALIAS=$ SERVICEDESC=PING