nagios批量创建主机脚本

发布时间:2020-07-24 18:07:40 作者:西索oO
来源:网络 阅读:673

目的:实现批量主机文件和服务的创建,将服务就写到了主机文件中;

用户change.sh linux.template(主机服务模版) list.txt (主机IP对应列表)

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

 

执行 ./change.sh linux.template list.txt 即可

附件:http://down.51cto.com/data/2362459
推荐阅读:
  1. 脚本批量生成ansible host inventory 主机清单
  2. 用户批量创建脚本

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

主机 nagios 批量添加主机

上一篇:读懂前端与HTML5技术区别,玩转互联网企业!

下一篇:view administrator页面上计算机名和DNS名

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》