溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

nagios批量創(chuàng)建主機(jī)腳本

發(fā)布時(shí)間:2020-07-24 18:07:40 來源:網(wǎng)絡(luò) 閱讀:673 作者:西索oO 欄目:移動(dòng)開發(fā)

目的:實(shí)現(xiàn)批量主機(jī)文件和服務(wù)的創(chuàng)建,將服務(wù)就寫到了主機(jī)文件中;

用戶change.sh linux.template(主機(jī)服務(wù)模版) list.txt (主機(jī)IP對(duì)應(yīng)列表)

catc change.sh


    usage () {
            echo -en "USAGE: $0 [host list] or $0 [template] [host list]\nFor ex                                                                                                 ample: $0 host.template host.list(Field : [IP] [HOST NAME])\n" 1>&2
            exit 1
    }

    if [ $# -gt 2 ];then
            usage
            exit 1
    fi

    case "$#" in
            2)
                    template=$1
                    host_list=$2
            ;;
            1)
                    template='host.template'
                    host_list=$1
            ;;
            0)
            #       template='host.template'
            #        host_list='host.list'
                    usage
            ;;
    esac

    if [ ! -f "${template}" ];then
            echo "template : ${template} not exist!" 1>&2
            exit 1
    fi

    if [ ! -f "${host_list}" ];then
            echo "host list : ${host_list} not exist!" 1>&2
            exit 1
    fi

    cat ${host_list}| while read ip hostname
    do
            echo "${ip}"|grep -oP '^\d{1,3}(\.\d{1,3}){3}$' >/dev/null 2>&1 || Field='not ip'
            if [ "${Field}" = 'not ip' ];then
                    echo "${ip} not ip!" 1>&2
                    exit 1
            fi
            host_cfg="${hostname}-${ip}.cfg"
            cp ${template} ${host_cfg}
            sed -i "s/HOST_NAME/${hostname}/g;s/ADDRESS/${ip}/g" ${host_cfg}
    done

cat linux.template

  define host{
            use linux-server,hosts-pnp
            host_name HOST_NAME
            alias HOST_NAME
            address ADDRESS
    }



###############################################################################
###############################################################################
#
# SERVICE DEFINITIONS
#
###############################################################################
###############################################################################


# Define a service to "ping" the local machine

define service{
        use                             local-service         ; Name of service template to use
        host_name                       HOST_NAME
        service_description             PING
    check_command            check_ping!100.0,20%!500.0,60%
        }


# Define a service to check the disk space of the root partition
# on the local machine.  Warning if < 20% free, critical if
# < 10% free space on partition.

define service{
        use                             local-service,services-pnp         ; Name of service template to use
        host_name                       HOST_NAME
        service_description             Root Partition
    check_command            check_nrpe!check_sda1
        }

cat list.txt

192.168.1.2   linuxhost

192.188.22.33 winhost

 

執(zhí)行 ./change.sh linux.template list.txt 即可

附件:http://down.51cto.com/data/2362459
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI