检测证书过期并发送钉钉告警

时间:2021-06-30 18:14:38   收藏:0   阅读:0

#!/usr/bin/env bash
__Author__="liy"

# 发送钉钉告警
function DingDing(){
    curl ‘https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx‘     -H ‘Content-Type: application/json‘     -d ‘{"msgtype": "text",
        "text": {
        "content":" ‘$1‘ "
        },
        "at": {
        "atMobiles": [
            "1xxxxxxxxxx",
        ],
        "isAtAll": false
        }
        }‘
}

function checkCerts(){
    host=$1

    SSL_OVER_TIMESTAMP=$(date +%s -d "$(openssl s_client -servername ${host} -connect ${host}:443 2>/dev/null | openssl x509 -noout -dates|grep -Po "(?<=After=).*(?=GMT)")")
    NOW_TIMESTAMP=$(date +%s)
    OVER_TIMESTAMP=$(echo ${SSL_OVER_TIMESTAMP} - ${NOW_TIMESTAMP} | bc)
    OVER_DAYS=$(echo ${OVER_TIMESTAMP} / 86400 | bc)

    if [ $OVER_DAYS -le 30 ];then
        text="${host}的证书将于${OVER_DAYS}天后过期,注意续费和配置更新!\n报警源:192.168.171.201"
        DingDing ${text}
    fi
}

function main(){
    while read line
    do
        checkCerts $line
    done < ssl.list
}

main

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!