shell 自动添加 nagios被监控端
来源:互联网 发布:饮用水质检测标准数据 编辑:程序博客网 时间:2024/05/16 18:34
shell 自动添加 nagios被监控端
此文章为转载,原网址为
http://blog.chinaunix.net/uid-27050514-id-3756646.html
记录下来方便以后使用
写这个的初衷是满足部门nagios新人的要求,方便以后的添加。通过让他们了解脚本内容也可以了解到整个添加过程。有时间会扩充下其它客户端接入,并且模版做成可选择。目前满足snmp 客户端添加,模版部份写死了。command 也是之前已经设置好的,此脚本适用于:已安装完nagios服务端,需要批量对snmp 客户端进行监控。
听说有WEB方式可以配置,但还是觉的麻烦。下一步将从安装、服务端配置开始,将必要模版及可选模版分开选择。再对此添加脚本进行修改(做到可添加、可删除、可修改)目标是做到一健安装,一健添加。
!!以下脚本实现了主机添加、服务添加(有几默认服务,硬盘分区给出默认C D E),可选择是否添加程序监控项。已添加主机将给予处理。
#!/bin/bash #*******************************************# #自动添加主机跟服务 # #根据输入自动生成对应的配置文件 # #配置文件规则: # #主机:$hostip_win/linux_snmp/nsclient/nrpe # #wanggy for 2013/06/03 # #1.0版本完成win snmp 机器的添加判断 # #*******************************************# #update: # #2013-06-05 for wanggy # # 1:服务可添加/删除 # # 2:将对应服务添加至escalations # #2013-06-19 for wanggy # # 1:添加服务可修改 # #*******************************************# #添加逻辑:read交互方式获取要监控的服务器 # # 1:添加主机监控相关配置 # # 2:添加服务监控相关配置 # # 3:添加至nagios 启动项里 # # 4:检查并判断配置项是否异常 # # 5:重启nagios服务 # #*******************************************# #函数列表: #文件/路径设置 cfg_path=/usr/local/nagios/etc/objects command_cfg=/usr/local/nagios/etc/objects/commands.cfg nagios_cfg=/usr/local/nagios/etc/nagios.cfg nagios_bin=/usr/local/nagios/bin/nagios #----------------------------------------------------- cd $cfg_path #切换至工作目录 main_config() { read -p "请输入要监控的ip地址(ip):" ip echo "------>ip地址:$ip" ip_judge } ip_judge() { if [ "$ip" = "" ];then main_config else host_main fi } #---------------------------------------------------- host_main() { echo "开始判断hosts.cfg里是否有$ip的配置" host=`sed -n /$ip/p hosts.cfg` if [ "$host" = "" ];then host_add else host_del fi } #---------------------------------------------------- service_main() { #添加service配置 #生成对应的配置文件($hostip_win_snmp.cfg) if [ ! -f ${cfg_path}/${ip}_win_snmp.cfg ];then service_add else echo "------>检测到$ip在nagios系统已有服务配置:" service_judge fi } #*********************************************************# # 以下为程序子函数部份 # #*********************************************************# #------主机-添加函数------ host_add() { echo "正在添加主机配置" #主机配置函数---config_host hostname_input config_host service_main } hostname_input() { read -p "请输入$ip服务器对应的名称.默认名称为:$ip服务器(name):" name name_def=$ip服务器 : ${name:=$name_def} echo "------>主机名:$name" } #------主机-删除函数 host_del() { read -n1 -p "$ip 主机监控已存在是否删除(n/y)" choose echo "" case $choose in y|Y) echo "删除主机、服务及相关对应配置" sed -i /$ip/',/配置结束/d' hosts.cfg rm -rf ${cfg_path}/${ip}_win_snmp.cfg sed -i "/${ip}_win_snmp.cfg/d" $nagios_cfg sed -i /$ip/',/配置结束/d' escalations.cfg #删除后,检测并退出 nagios_check exit 0 ;; n|N) #进入服务配置模块 service_main ;; esac } #------服务-首次添加函数------ service_add() { echo "开始为$ip添加对应的配置文件" touch ${cfg_path}/${ip}_win_snmp.cfg echo "------>正在自动添加基本监控,请稍候......" #基本配置函数---config_basic config_basic sleep 2 #服务监控硬盘部份函数 service_disk read -n1 -p "硬盘监控已完成,请选择是否需要添加程序监控(y/n)" choose echo "" case $choose in y|Y) #服务监控程序部份函数 service_program ;; n|N) echo "没有程序需要监控,service 服务添加完毕." sleep 2 ;; esac echo "将对应服务添加至escalations.cfg配置文件中" escalations_partition_name=`echo $partition |awk '{for (i=1;i<=NF;i++) printf(",Driver_"$i"")}'` escalations_process_name=`echo $service |awk '{for (i=1;i<=NF;i++) printf(",Process_"$i"")}'` config_escalations $escalations_partition_name $escalations_process_name #通知配置(改变通知间隔) #添加至nagios.cfg 对应的位置---#shell auto config 下面(指定的位置,在nagios.cfg里面添加) sed -i '/#shell auto config/a cfg_file='${cfg_path}'/'${ip}'_win_snmp.cfg' $nagios_cfg echo "------>已添加完成,执行nagios -v 检测并重启nagios." nagios_check } #------服务-判断子函数------ service_judge() { #取服务列表 read -n1 -p "继续添加输入a,修改原记录请按m (a/m):" choose echo "" case $choose in m|M) sed -n '/service_description/p' ${ip}_win_snmp.cfg |awk '{for (i=1;i<NR;i++); printf(""i": "$2"\n")}' >.service_list cat .service_list line=`wc -l .service_list |awk '{print$1}'` a=`echo $line |cut -c 1-1` b=`echo $line |cut -c 2-2` read -p "请输入要删除/修改服务的编号,全部删除请按dd:" modif case $modif in dd ) echo "------>删除对应的配置,请稍等...." echo "删除服务配置文件" rm -rf ${cfg_path}/${ip}_win_snmp.cfg echo "删除nagios.cfg对应配置" sed -i "/${ip}_win_snmp.cfg/d" $nagios_cfg echo "删除escalations对应配置" sed -i /$ip/',/配置结束/d' escalations.cfg sleep 2 ;; [1-9]|$a[0-$b]) modif_service=`awk 'NR=='$modif' {print$2}' .service_list` echo "$modif对应的服务为$modif_service" read -n1 -p "删除请按d,修改请按r:" action echo "" case $action in d) echo "正在删除编码为$modif的服务${modif_service}...." #删除${ip}_win_snmp.cfg 对应的配置服务 service_mod del $modif_service #删除escalations (del 服务名称) echo "删除escalations" escalations del $modif_service ;; r) #暂时不能使用 read -p "请输入新的服务名称以替换$modif_service,输入时请注意服务名称格式." new_service echo "正在将${modif_service}替换为${new_service},请稍等....." #替换服务对应的配置跟escalations service_mod replace $modif_service $new_service escalations replace $modif_service sleep 3 exit 0 ;; esac ;; *) exit 0 ;; esac ;; a|A) service_program escalations add $add_service ;; *) exit 0 ;; esac nagios_check } #------escalations 添加删除子函数------ escalations() { #$1 add 添加,$1 del 删除 modif_service=$2 add_service=$2 #对service变量进行处理(格式转换serivce1 service2转换成Process_service1,Process_service2) escal_service=`echo $add_service |awk '{for (i=1;i<=NF;i++) printf",""Process_"$i}'` #开始更改escalations.cfg row=`wc -l escalations.cfg |awk '{print$1}'` for ((i=1;i<=row;i++));do match_content=`sed -n $i{/#$ip/p} escalations.cfg` if [ "$match_content" != "" ];then #在第一次发现$ip的下面10行--也就是escalations配置服务名称的位置(escalations函数插入) result_row=`expr $i + 10` #$n_row行后插入添加的服务名称(,service_name) case $1 in add) #添加 sed -i $result_row{s/$/$escal_service/} escalations.cfg ;; del) #删除 sed -i ${result_row}s/,$modif_service//g escalations.cfg ;; esac fi done } #------服务-程序子函数------ service_program() { read -p "请输入完整的监控程序名称--多个服务之间请用空格分隔(取任务管理器名称)" add_service echo $add_service if [ "$add_service" = "" ];then echo "---->未输入任何服务,退出" else for service_ in $add_service;do display_name=Process_${service_} command_name=${service_} echo "正在将${service_}添加至${cfg_path}/${ip}_win_snmp.cfg" #程序配置函数--config_program config_program done fi } #------服务-修改配置文件子函数------ service_mod() { select_action=$1 modif_service=$2 new_service=$3 #分为删除(del),替换(replace) row=`wc -l ${ip}_win_snmp.cfg |awk '{print$1}'` for ((i=1;i<=row;i++));do match_content=`sed -n $i{/$modif_service/p} ${ip}_win_snmp.cfg` if [ "$match_content" != "" ];then case $select_action in del) echo "删除$modif_service" #在第一次发现$modif_service的下面2行跟上面3行--也就是删除整个服务配置 beg_row=`expr $i - 3` end_row=`expr $i + 2` #删除beg_row 至end_row sed -i $beg_row,$end_row'd' ${ip}_win_snmp.cfg ;; replace) #替换对应的服务 sed -i /${modif_service}/${new_service} ${ip}_win_snmp.cfg ;; esac fi done } #------服务-硬盘子函数------ service_disk() { echo "开始添加硬盘监控配置,请根据实际情况输入(类似C D E)" partition_def="C D E F" echo -n "请输入硬盘分区!!一定要大写(默认:$partition)" read partition : ${partition:=$partition_def} echo $partition for partition_ in $partition;do display_name=Driver_$partition_ command_name=$partition_ #硬盘配置函数---config_disk config_disk done } #------配置-检测------ nagios_check() { ${nagios_bin} -v ${nagios_cfg} if [ $? = 0 ];then echo "------>检查配置正常,正在重启nagios,请稍候" sudo service nagios restart exit 0 else echo "------>配置检查未通过,请联系nagios管理员处理." exit 0 fi } #************************************************************# # 以下为写入配置部份 # #************************************************************# config_host() { cat >> $cfg_path/hosts.cfg <<EOF #$ip define host{ host_name $ip alias $name address $ip check_command check-host-alive ;查看commands.cfg 并根据实际情况修改 max_check_attempts 7 ;检测次数 normal_check_interval 20 ;检测间隔 retry_check_interval 1 ;软态检测间隔 check_period 24x7 ;检测执行时间模版,请查看timeperiods.cfg对应配置 contact_groups admins ;联系人组,请查看contacts.cfg里的配置 notification_interval 30 ;通知间隔 notification_period 24x7 ;通知执行时间,查看timeperiods.cfg notification_options d,u,r ;通知选项;d 宕机,u 不可达,r 恢复。 } #配置结束 EOF } config_basic() { cat >> ${cfg_path}/${ip}_win_snmp.cfg <<EOF #此配置文件由脚本自动生成 #sh /usr/local/nagios/etc/object/ #for wanggy define service{ use generic-service ;配置模版,查看templates.cfg host_name $ip service_description System_Memery_Usage ;检测内存命令,查看commands.cfg check_command check_snmp_stor_xh!-m "^Physical Memory$"!80!90 ;查看commands.cfg 匹配命令格式 } define service{ use generic-service host_name $ip service_description System_Total_Memory_Usage check_command check_snmp_stor_xh!-m "^Virtual Memory$"!70!90 } define service{ use generic-service host_name $ip service_description System_Cpu_Load check_command check_snmp_load_xh!80!90 } EOF } config_disk() { cat >> ${cfg_path}/${ip}_win_snmp.cfg <<EOF define service{ use generic-service host_name $ip service_description $display_name check_command check_snmp_stor_xh!-m "^$command_name"!85!95 } EOF } config_program() { cat >> ${cfg_path}/${ip}_win_snmp.cfg <<EOF define service{ use generic-service host_name $ip service_description $display_name check_command check_snmp_process_xh!$command_name!0!0 } EOF } config_escalations() { #注意$1$2的值,查看service_add 函数 #注意前后的注释--为了sed删除的时候可以指定范围。 cat >>${cfg_path}/escalations.cfg <<EOF #$ip define hostescalation{ host_name $ip first_notification 2 last_notification 0 notification_interval 480 contact_groups admins } define serviceescalation{ host_name $ip service_description System_Total_Memory_Usage,System_Memery_Usage,System_Cpu_Load$1$2 first_notification 2 last_notification 0 notification_interval 480 contact_groups admins } #配置结束 EOF } #------------------------------------------------------- main() { main_config } main exit 0
0 0
- shell 自动添加 nagios被监控端
- nagios添加被监控的机器
- nagios添加监控模块
- Nagios添加时间监控
- Nagios添加mysql监控
- Nagios被监控端的监控项目实例
- 实战Nagios NSCA方式监控Linux系统资源使用情况 -- Nagios配置篇 -- 被监控端
- nagios为监控图像添加图片
- nagios监控端的安装
- [Nagios] Linux/Win 端监控
- nagios 监控被监控服务器安装nrpe
- Nagios+pnp4nagios+rrdtool 安装配置nagios被监控端NRPE配置(二)
- centos7.2 nagios被监控端安装与配置
- nagios监控
- Nagios 监控
- Nagios监控
- Nagios监控
- nagios监控
- java学习笔记-文件流
- Tomcat shutdown 出现僵尸进程
- Android错误解决方法集锦
- Android,与北岛的诗
- 禁止用户在任务管理器中停止服务
- shell 自动添加 nagios被监控端
- rhel6.3_oracle11R2的DG搭建安装
- Swift 基础学习(闭包)
- 在mac上用Xcode写程序, run的时候总是失败, 显示clang: error: linker command failed with exit code 1
- 基于Docker快速搭建单机版Kuberntes
- input标签的获得焦点和失去焦点的样式
- 意念控制技术落地
- jmeter+ant压测执行多条参数化
- IE浏览器兼容问题