如何从命令行通过curl为任何特定的nagios主机设置一定时间的停机时间?

发布于 2024-11-26 16:44:46 字数 674 浏览 3 评论 0原文

我需要通过curl 从命令行为特定nagios 主机设置计划停机时间..我该怎么做?

这是我已经用于从命令行启用/禁用服务/主机通知的东西。

curl -d "some input here" url "user:pass" 

就像我需要为计划停机时间做的事情一样。现在的问题是停机时间选项需要更多选项,即开始时间、结束时间、评论等。

那么我如何从命令行通过curl来完成它呢?

curl -d " some key value pair(hostname,servicename" url "username:passowrd"

这将从命令行打开和关闭服务/主机通知。所以我想以这种方式使用curl 来为特定的nagios 服务器提供停机时间。

上面的脚本不起作用,因为 nagios 的停机选项需要更多参数,我尝试将这些参数注入脚本中..但它没有那样工作。我们还需要提供开始时间、结束时间和注释值。

另外,我已经尝试了curl的名为--form和--form-string的选项与该脚本..无法通过。

基本的想法是我们不想进入 Nagios Web 界面,而是从命令行完成这件事(我们已经成功实现了服务/主机服务和通知)。

希望我现在已经完全清楚了。

蒂亚·

巴斯卡

I need to set a schedule downtime for specific nagios host from the commandline by curl..how do I do that?

here is something I am already using for service/host notification enable/disable from commandline.

curl -d "some input here" url "user:pass" 

Like way I need to do the thing for schedule downtime.Now the problem is that downtime option takes more options i.e starttime,endtime,comment etc.

So how do I get it done by curl from the commandline?

curl -d " some key value pair(hostname,servicename" url "username:passowrd"

which will do the service/host notification on and off from the commandline. So I want use curl in this fashion to provide downtime for specific nagios server.

Above script is not working for it because downtime option of nagios taked more parameter and I tried to infuse those in the script..but it didn't work out that way.We need provide starttime,endtime and comment value too.

Plus I have tried curl's option called --form and --form-string with that script..not able to get through.

The besic idea is instead of going to the Nagios web interface, we want to done this thing from the command line(we have succeded for service/host service and notification).

Hope I am absolutely clear now.

TIA

Bhaskar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绅刃 2024-12-03 16:44:46

我增强了 Anders 的答案,以提供完整的脚本,并且不需要使用支持 --data-urlencode 的较新的curl。这还会自动计算发送的结束时间并检查请求是否已成功提交给 Nagios。此外,这还会安排主机和主机上所有服务的停机时间。

#!/bin/bash

function die {
  echo $1;
  exit 1;
}

echo Scheduling downtime on nagios

HOST=monitoredhost
NAGURL=https://nagios.example.com/cgi-bin/nagios3/cmd.cgi
USER=nagiosuser
PASS=nagiospassword
MINUTES=10

export MINUTES

# The following is urlencoded already
STARTDATE=`date "+%Y-%m-%d+%H%%3A%M%%3A%S"`
# This gives us the date/time X minutes from now
ENDDATE=`date "+%Y-%m-%d+%H%%3A%M%%3A%S" -d "$MINUTES min"`
curl --silent --show-error \
    --data cmd_typ=86 \
    --data cmd_mod=2 \
    --data host=$HOST \
    --data "com_data=Updating+application" \
    --data trigger=0 \
    --data "start_time=$STARTDATE" \
    --data "end_time=$ENDDATE" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data btnSubmit=Commit \
    --insecure \
    $NAGURL -u "$USER:$PASS" | grep -q "Your command request was successfully submitted to Nagios for processing." || die "Failed to contact nagios";

echo Scheduled downtime on nagios

I enhanced Anders answer to provide a complete script and to not require the use of a newer curl that supports --data-urlencode. This also automatically calculates the end time to send and checks that the request was successfully submitted to Nagios. Also, this schedules downtime for the host and all the services on the host.

#!/bin/bash

function die {
  echo $1;
  exit 1;
}

echo Scheduling downtime on nagios

HOST=monitoredhost
NAGURL=https://nagios.example.com/cgi-bin/nagios3/cmd.cgi
USER=nagiosuser
PASS=nagiospassword
MINUTES=10

export MINUTES

# The following is urlencoded already
STARTDATE=`date "+%Y-%m-%d+%H%%3A%M%%3A%S"`
# This gives us the date/time X minutes from now
ENDDATE=`date "+%Y-%m-%d+%H%%3A%M%%3A%S" -d "$MINUTES min"`
curl --silent --show-error \
    --data cmd_typ=86 \
    --data cmd_mod=2 \
    --data host=$HOST \
    --data "com_data=Updating+application" \
    --data trigger=0 \
    --data "start_time=$STARTDATE" \
    --data "end_time=$ENDDATE" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data btnSubmit=Commit \
    --insecure \
    $NAGURL -u "$USER:$PASS" | grep -q "Your command request was successfully submitted to Nagios for processing." || die "Failed to contact nagios";

echo Scheduled downtime on nagios
灵芸 2024-12-03 16:44:46

您只需添加更多 --data(-d) 参数即可使用 curl 发送多个表单字段值。这应该安排 Nagios 系统上的服务停机时间:

curl \
    --data cmd_typ=56 \
    --data cmd_mod=2 \
    --data host=$HOSTNAME \
    --data-urlencode "service=${SERVICENAME}" \
    --data-urlencode "com_data=${COMMENT}" \
    --data trigger=0 \
    --data-urlencode "start_time=2011-07-31 00:00:00" \
    --data-urlencode "end_time=2011-07-31 01:00:00" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data btnSubmit=Commit \
    $NAGIOS-URL "username:password"

You can send multiple form field values with curl simply by adding more --data(-d) arguments. This should schedule service downtime on a Nagios system:

curl \
    --data cmd_typ=56 \
    --data cmd_mod=2 \
    --data host=$HOSTNAME \
    --data-urlencode "service=${SERVICENAME}" \
    --data-urlencode "com_data=${COMMENT}" \
    --data trigger=0 \
    --data-urlencode "start_time=2011-07-31 00:00:00" \
    --data-urlencode "end_time=2011-07-31 01:00:00" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data btnSubmit=Commit \
    $NAGIOS-URL "username:password"
夜灵血窟げ 2024-12-03 16:44:46

我进一步增强了 Sarels 的回答。

  • 使它与我们的 Nagios 3.5.1 一起工作(更改了 cmd_typ,添加了子选项,更改了日期格式)。
  • HOSTUSER 设为命令行
  • 使用 $USER (当前用户)作为默认
  • 添加密码提示(无硬编码密码)
  • 参数

我的版本:

#!/bin/bash

# Bash script to schedule downtime for Host
# source: http://stackoverflow.com/a/9198181
# Author: Sarel Botha http://stackoverflow.com/users/35264/

function die {
  echo $1;
  exit 1;
}

if [ $# -lt 1 ]; then
  echo "$0 <host> [<user>]"
  exit 0;
elif [ $# -ge 2 ]; then
  USER=$2
fi

HOST=$1
NAGURL=https://nagios.example.com/nagios3/cgi-bin/cmd.cgi
MINUTES=30

echo Scheduling downtime on nagios for $HOST

export MINUTES

# read password
read -s  -p "Password for $USER:" PASS
echo ""  # newline after prompt

# The following is urlencoded already
STARTDATE=`date "+%d-%m-%Y+%H%%3A%M%%3A%S"`
# This gives us the date/time X minutes from now
ENDDATE=`date "+%d-%m-%Y+%H%%3A%M%%3A%S" -d "$MINUTES min"`
curl --silent --show-error \
    --data cmd_typ=55 \
    --data cmd_mod=2 \
    --data host=$HOST \
    --data "com_author=$USER" \
    --data "com_data=reboot+due+to+security+updates" \
    --data trigger=0 \
    --data "start_time=$STARTDATE" \
    --data "end_time=$ENDDATE" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data childoptions=0 \
    --data btnSubmit=Commit \
    --insecure \
    $NAGURL -u "$USER:$PASS" | grep -q "Your command request was successfully submitted to Nagios for processing." || die "Failed to contact nagios";

echo Scheduled downtime on nagios

I further enhanced Sarels answer.

  • made it work with our Nagios 3.5.1 (changed cmd_typ, added childoptions, changed date format).
  • made HOST and USER a command line arg
  • using $USER (current user) as default
  • added password prompt (no hardcoded password)
  • added author to nagios message

My version:

#!/bin/bash

# Bash script to schedule downtime for Host
# source: http://stackoverflow.com/a/9198181
# Author: Sarel Botha http://stackoverflow.com/users/35264/

function die {
  echo $1;
  exit 1;
}

if [ $# -lt 1 ]; then
  echo "$0 <host> [<user>]"
  exit 0;
elif [ $# -ge 2 ]; then
  USER=$2
fi

HOST=$1
NAGURL=https://nagios.example.com/nagios3/cgi-bin/cmd.cgi
MINUTES=30

echo Scheduling downtime on nagios for $HOST

export MINUTES

# read password
read -s  -p "Password for $USER:" PASS
echo ""  # newline after prompt

# The following is urlencoded already
STARTDATE=`date "+%d-%m-%Y+%H%%3A%M%%3A%S"`
# This gives us the date/time X minutes from now
ENDDATE=`date "+%d-%m-%Y+%H%%3A%M%%3A%S" -d "$MINUTES min"`
curl --silent --show-error \
    --data cmd_typ=55 \
    --data cmd_mod=2 \
    --data host=$HOST \
    --data "com_author=$USER" \
    --data "com_data=reboot+due+to+security+updates" \
    --data trigger=0 \
    --data "start_time=$STARTDATE" \
    --data "end_time=$ENDDATE" \
    --data fixed=1 \
    --data hours=2 \
    --data minutes=0 \
    --data childoptions=0 \
    --data btnSubmit=Commit \
    --insecure \
    $NAGURL -u "$USER:$PASS" | grep -q "Your command request was successfully submitted to Nagios for processing." || die "Failed to contact nagios";

echo Scheduled downtime on nagios
迷爱 2024-12-03 16:44:46

为了在我的 Nagios 上工作,我必须在“data host=$HOST”下添加一行额外的行

--data “com_author=Automatic+Downtime” \

如果没有它,我的 Nagios 将不会接受停机。

For this to work on my Nagios, I had to add an extra line under "data host=$HOST"

--data "com_author=Automatic+Downtime" \

Without that, my Nagios wouldn't accept the downtime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文