如何设置和记录 Jenkin 服务器关闭和启动的警报

如何设置和记录 Jenkin 服务器关闭和启动的警报

我有 Jenkins 管道作业,它会考虑我们所有的 Jenkins 服务器并检查连接性(每隔几分钟运行一次)。

.ksh 文件:

#!/bin/ksh
JENKINS_URL=$1

    curl  --connect-timeout 10 "$JENKINS_URL" >/dev/null
    status=`echo $?`
     if [ "$status" == "7" ]; then
        export SUBJECT="Connection refused or can not connect to URL $JENKINS_URL"
        echo "$SUBJECT"|/usr/sbin/sendmail -t [email protected]

    else
        echo "successfully connected $JENKINS_URL"
     fi

    exit 0

我想添加另一段代码,它将服务器关闭的所有时间(应该包括服务器的名称和时间戳)记录到一个文件中,如果服务器再次启动,则发送一封电子邮件通知关于这件事,也会记录在档案中。我不想收到额外的警报,只有当它关闭时收到一个警报(文件和邮件),当它再次启动时收到一个警报。知道如何实施吗?

答案1

#!/bin/ksh
JENKINS_URL=$1
# extract just the host and potental port number from the url
HOSTP=${JENKINS_URL#*:} ; HOSTP=${HOSTP%%/*}

# Create down directory if it doesn't exist
[ -d down ] || mkdir -p down

curl  --connect-timeout 10 "$JENKINS_URL" >/dev/null
status=$?
if [ "$status" == "7" ]; then
    [ -e "down/$HOSTP" ] && exit 0
    { echo -n "$HOSTP   down    " ; date } >> times
    touch "down/$HOSTP"
    SUBJECT="Connection refused or can not connect to URL $JENKINS_URL"
    echo "$SUBJECT"|/usr/sbin/sendmail -t [email protected]
else
    echo "successfully connected $JENKINS_URL"
    [ -e "down/$HOSTP" ] || exit 0
    rm "down/$HOSTP"
    { echo -n "$HOSTP    up     " ; date } >> times
fi

exit 0

相关内容