Chinaunix首页 | 论坛 | 博客
  • 博客访问: 235381
  • 博文数量: 37
  • 博客积分: 325
  • 博客等级: 一等列兵
  • 技术积分: 1199
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-09 00:04
个人简介

坚持原创,虽然有些东西很简单,但也算是一种积累。

文章分类

全部博文(37)

文章存档

2013年(28)

2012年(9)

我的朋友

分类: 系统运维

2013-06-04 16:09:13

    写这个的初衷是满足部门nagios新人的要求,方便以后的添加。通过让他们了解脚本内容也可以了解到整个添加过程。有时间会扩充下其它客户端接入,并且模版做成可选择。目前满足snmp 客户端添加,模版部份写死了。command 也是之前已经设置好的,此脚本适用于:已安装完nagios服务端,需要批量对snmp 客户端进行监控。
    
听说有WEB方式可以配置,但还是觉的麻烦。下一步将从安装、服务端配置开始,将必要模版及可选模版分开选择。再对此添加脚本进行修改(做到可添加、可删除、可修改)目标是做到一健安装,一健添加。

!!以下脚本实现了主机添加、服务添加(有几默认服务,硬盘分区给出默认C D E),可选择是否添加程序监控项。已添加主机将给予处理。

-6月19号修正了部份BUG,增加服务修改

点击(此处)折叠或打开

  1. #!/bin/bash
  2. #*******************************************#
  3. #自动添加主机跟服务 #
  4. #根据输入自动生成对应的配置文件 #
  5. #配置文件规则: #
  6. #主机:$hostip_win/linux_snmp/nsclient/nrpe #
  7. #wanggy for 2013/06/03 #
  8. #1.0版本完成win snmp 机器的添加判断 #
  9. #*******************************************#
  10. #update: #
  11. #2013-06-05 for wanggy #
  12. # 1:服务可添加/删除 #
  13. # 2:将对应服务添加至escalations #
  14. #2013-06-19 for wanggy #
  15. # 1:添加服务可修改 #
  16. #*******************************************#
  17. #添加逻辑:read交互方式获取要监控的服务器 #
  18. # 1:添加主机监控相关配置 #
  19. # 2:添加服务监控相关配置 #
  20. # 3:添加至nagios 启动项里 #
  21. # 4:检查并判断配置项是否异常 #
  22. # 5:重启nagios服务 #
  23. #*******************************************#
  24. #函数列表:
  25. #文件/路径设置
  26. cfg_path=/usr/local/nagios/etc/objects
  27. command_cfg=/usr/local/nagios/etc/objects/commands.cfg
  28. nagios_cfg=/usr/local/nagios/etc/nagios.cfg
  29. nagios_bin=/usr/local/nagios/bin/nagios
  30. #-----------------------------------------------------
  31. cd $cfg_path #切换至工作目录
  32. main_config()
  33. {
  34. read -p "请输入要监控的ip地址(ip):" ip
  35. echo "------>ip地址:$ip"
  36. ip_judge
  37. }
  38. ip_judge()
  39. {
  40. if [ "$ip" = "" ];then
  41.     main_config
  42. else
  43.     host_main
  44. fi
  45. }
  46. #----------------------------------------------------
  47. host_main()
  48. {
  49. echo "开始判断hosts.cfg里是否有$ip的配置"
  50. host=`sed -n /$ip/p hosts.cfg`
  51. if [ "$host" = "" ];then
  52.     host_add
  53. else
  54.     host_del
  55. fi
  56. }
  57. #----------------------------------------------------
  58. service_main()
  59. {
  60. #添加service配置
  61. #生成对应的配置文件($hostip_win_snmp.cfg)
  62. if [ ! -f ${cfg_path}/${ip}_win_snmp.cfg ];then
  63.     service_add
  64. else
  65.     echo "------>检测到$ip在nagios系统已有服务配置:"
  66.     service_judge
  67. fi
  68. }
  69. #*********************************************************#
  70. # 以下为程序子函数部份 #
  71. #*********************************************************#
  72. #------主机-添加函数------
  73. host_add()
  74. {
  75.     echo "正在添加主机配置"
  76.     #主机配置函数---config_host
  77.     hostname_input
  78.     config_host
  79.     service_main
  80. }
  81. hostname_input()
  82. {
  83. read -p "请输入$ip服务器对应的名称.默认名称为:$ip服务器(name):" name
  84. name_def=$ip服务器
  85. : ${name:=$name_def}
  86. echo "------>主机名:$name"
  87. }
  88. #------主机-删除函数
  89. host_del()
  90. {
  91.     read -n1 -p "$ip 主机监控已存在是否删除(n/y)" choose
  92.     echo ""
  93.     case $choose in
  94.         y|Y)
  95.             echo "删除主机、服务及相关对应配置"
  96.             sed -i /$ip/',/配置结束/d' hosts.cfg
  97.             rm -rf ${cfg_path}/${ip}_win_snmp.cfg
  98.             sed -i "/${ip}_win_snmp.cfg/d" $nagios_cfg
  99.             sed -i /$ip/',/配置结束/d' escalations.cfg
  100.             #删除后,检测并退出
  101.             nagios_check
  102.             exit 0
  103.             ;;
  104.         n|N)
  105.             #进入服务配置模块
  106.             service_main
  107.             ;;
  108.     esac
  109. }    
  110. #------服务-首次添加函数------
  111. service_add()
  112. {
  113.     echo "开始为$ip添加对应的配置文件"
  114.     touch ${cfg_path}/${ip}_win_snmp.cfg
  115.     echo "------>正在自动添加基本监控,请稍候......"
  116.     #基本配置函数---config_basic
  117.     config_basic
  118.     sleep 2
  119.     #服务监控硬盘部份函数
  120.     service_disk
  121.     read -n1 -p "硬盘监控已完成,请选择是否需要添加程序监控(y/n)" choose
  122.     echo ""
  123.     case $choose in
  124.         y|Y)
  125.             #服务监控程序部份函数
  126.             service_program
  127.             ;;
  128.         n|N)
  129.             echo "没有程序需要监控,service 服务添加完毕."
  130.             sleep 2
  131.             ;;
  132.     esac
  133.         echo "将对应服务添加至escalations.cfg配置文件中"
  134.         escalations_partition_name=`echo $partition |awk '{for (i=1;i<=NF;i++) printf(",Driver_"$i"")}'`
  135.         escalations_process_name=`echo $service |awk '{for (i=1;i<=NF;i++) printf(",Process_"$i"")}'`
  136.         config_escalations $escalations_partition_name $escalations_process_name #通知配置(改变通知间隔)
  137.         #添加至nagios.cfg 对应的位置---#shell auto config 下面(指定的位置,在nagios.cfg里面添加)
  138.         sed -i '/#shell auto config/a cfg_file='${cfg_path}'/'${ip}'_win_snmp.cfg' $nagios_cfg
  139.         echo "------>已添加完成,执行nagios -v 检测并重启nagios."
  140.         nagios_check
  141. }
  142. #------服务-判断子函数------
  143. service_judge()
  144. {
  145. #取服务列表
  146. read -n1 -p "继续添加输入a,修改原记录请按m (a/m):" choose
  147.     echo ""
  148.     case $choose in
  149.         m|M)
  150.             sed -n '/service_description/p' ${ip}_win_snmp.cfg |awk '{for (i=1;i >.service_list
  151.             cat .service_list
  152.             line=`wc -l .service_list |awk '{print$1}'`
  153.             a=`echo $line |cut -c 1-1`
  154.             b=`echo $line |cut -c 2-2`
  155.             read -p "请输入要删除/修改服务的编号,全部删除请按dd:" modif
  156.                 case $modif in
  157.                     dd )
  158.                         echo "------>删除对应的配置,请稍等...."
  159.                         echo "删除服务配置文件"
  160.                         rm -rf ${cfg_path}/${ip}_win_snmp.cfg
  161.                         echo "删除nagios.cfg对应配置"
  162.                         sed -i "/${ip}_win_snmp.cfg/d" $nagios_cfg
  163.                         echo "删除escalations对应配置"
  164.                         sed -i /$ip/',/配置结束/d' escalations.cfg
  165.                         sleep 2
  166.                         ;;
  167.                     [1-9]|$a[0-$b])
  168.                         modif_service=`awk 'NR=='$modif' {print$2}' .service_list`
  169.                         echo "$modif对应的服务为$modif_service"
  170.                         read -n1 -p "删除请按d,修改请按r:" action
  171.                         echo ""
  172.                         case $action in
  173.                             d)        
  174.                                 echo "正在删除编码为$modif的服务${modif_service}...."
  175.                                 #删除${ip}_win_snmp.cfg 对应的配置服务
  176.                                 service_mod del $modif_service    
  177.                                 #删除escalations (del 服务名称)
  178.                                 echo "删除escalations"
  179.                                 escalations del $modif_service
  180.                                 ;;
  181.                             r)
  182.                                 #暂时不能使用
  183.                                 read -p "请输入新的服务名称以替换$modif_service,输入时请注意服务名称格式." new_service
  184.                                 echo "正在将${modif_service}替换为${new_service},请稍等....."
  185.                                 #替换服务对应的配置跟escalations
  186.                                 service_mod replace $modif_service $new_service
  187.                                 escalations replace $modif_service
  188.                                 sleep 3
  189.                                 exit 0
  190.                                 ;;
  191.                         esac
  192.                             ;;
  193.                     *)
  194.                         exit 0
  195.                         ;;
  196.                 esac
  197.                     ;;    
  198.         a|A)
  199.             service_program
  200.             escalations add $add_service
  201.             ;;
  202.         *)
  203.             exit 0
  204.             ;;
  205.     esac
  206.         nagios_check
  207. }
  208. #------escalations 添加删除子函数------
  209. escalations()
  210. {
  211. #$1 add 添加,$1 del 删除
  212. modif_service=$2
  213. add_service=$2
  214. #对service变量进行处理(格式转换serivce1 service2转换成Process_service1,Process_service2)
  215. escal_service=`echo $add_service |awk '{for (i=1;i<=NF;i++) printf",""Process_"$i}'`
  216. #开始更改escalations.cfg
  217. row=`wc -l escalations.cfg |awk '{print$1}'`
  218. for ((i=1;i<=row;i++));do
  219.     match_content=`sed -n $i{/#$ip/p} escalations.cfg`
  220.     if [ "$match_content" != "" ];then
  221.                 #在第一次发现$ip的下面10行--也就是escalations配置服务名称的位置(escalations函数插入)
  222.                 result_row=`expr $i + 10`
  223.                 #$n_row行后插入添加的服务名称(,service_name)
  224.         case $1 in
  225.             add)
  226.                 #添加
  227.                 sed -i $result_row{s/$/$escal_service/} escalations.cfg
  228.                 ;;
  229.             del)
  230.                 #删除
  231.                 sed -i ${result_row}s/,$modif_service//g escalations.cfg
  232.                 ;;
  233.                 
  234.         esac
  235.     fi
  236. done
  237. }
  238. #------服务-程序子函数------
  239. service_program()
  240. {
  241. read -p "请输入完整的监控程序名称--多个服务之间请用空格分隔(取任务管理器名称)" add_service
  242.     echo $add_service
  243.     if [ "$add_service" = "" ];then
  244.         echo "---->未输入任何服务,退出"
  245.     else
  246.         for service_ in $add_service;do
  247.             display_name=Process_${service_}
  248.             command_name=${service_}
  249.             echo "正在将${service_}添加至${cfg_path}/${ip}_win_snmp.cfg"
  250.             #程序配置函数--config_program
  251.             config_program
  252.         done
  253.     fi
  254. }
  255. #------服务-修改配置文件子函数------
  256. service_mod()
  257. {
  258. select_action=$1
  259. modif_service=$2
  260. new_service=$3
  261. #分为删除(del),替换(replace)
  262. row=`wc -l ${ip}_win_snmp.cfg |awk '{print$1}'`
  263. for ((i=1;i<=row;i++));do
  264.         match_content=`sed -n $i{/$modif_service/p} ${ip}_win_snmp.cfg`
  265.         if [ "$match_content" != "" ];then
  266.             case $select_action in
  267.                 del)
  268.                     echo "删除$modif_service"
  269.                     #在第一次发现$modif_service的下面2行跟上面3行--也就是删除整个服务配置
  270.                     beg_row=`expr $i - 3`
  271.                     end_row=`expr $i + 2`
  272.                     #删除beg_row 至end_row
  273.                     sed -i $beg_row,$end_row'd' ${ip}_win_snmp.cfg
  274.                     ;;
  275.                 replace)
  276.                     #替换对应的服务
  277.                     sed -i /${modif_service}/${new_service} ${ip}_win_snmp.cfg
  278.                     ;;
  279.             esac
  280.         fi
  281. done
  282. }
  283. #------服务-硬盘子函数------
  284. service_disk()
  285. {
  286.     echo "开始添加硬盘监控配置,请根据实际情况输入(类似C D E)"
  287.     partition_def="C D E F"
  288.     echo -n "请输入硬盘分区!!一定要大写(默认:$partition)"
  289.     read partition
  290.     : ${partition:=$partition_def}
  291.     echo $partition
  292.     for partition_ in $partition;do
  293.         display_name=Driver_$partition_
  294.         command_name=$partition_
  295.         #硬盘配置函数---config_disk
  296.         config_disk
  297.     done
  298. }
  299. #------配置-检测------
  300. nagios_check()
  301. {
  302. ${nagios_bin} -v ${nagios_cfg}
  303. if [ $? = 0 ];then
  304.     echo "------>检查配置正常,正在重启nagios,请稍候"
  305.     sudo service nagios restart
  306.     exit 0
  307. else
  308.     echo "------>配置检查未通过,请联系nagios管理员处理."
  309.     exit 0
  310. fi
  311. }
  312. #************************************************************#
  313. # 以下为写入配置部份 #
  314. #************************************************************#
  315. config_host()
  316. {
  317. cat >> $cfg_path/hosts.cfg <<EOF
  318. #$ip
  319. define host{
  320.     host_name            $ip
  321.     alias                $name
  322.     address                $ip
  323.     check_command            check-host-alive ;查看commands.cfg 并根据实际情况修改
  324.     max_check_attempts 7 ;检测次数
  325.     normal_check_interval 20 ;检测间隔
  326.     retry_check_interval 1 ;软态检测间隔
  327.     check_period 24x7 ;检测执行时间模版,请查看timeperiods.cfg对应配置
  328.     contact_groups            admins ;联系人组,请查看contacts.cfg里的配置
  329.     notification_interval 30 ;通知间隔
  330.     notification_period 24x7 ;通知执行时间,查看timeperiods.cfg
  331.     notification_options d,u,r ;通知选项;d 宕机,u 不可达,r 恢复。
  332.     }
  333. #配置结束
  334. EOF
  335. }
  336. config_basic()
  337. {
  338. cat >> ${cfg_path}/${ip}_win_snmp.cfg <<EOF
  339. #此配置文件由脚本自动生成
  340. #sh /usr/local/nagios/etc/object/
  341. #for wanggy
  342. define service{
  343.     use generic-service            ;配置模版,查看templates.cfg
  344.     host_name $ip
  345.     service_description System_Memery_Usage        ;检测内存命令,查看commands.cfg
  346.     check_command check_snmp_stor_xh!-m "^Physical Memory$"!80!90        ;查看commands.cfg 匹配命令格式
  347.     }
  348. define service{
  349.     use generic-service
  350.     host_name $ip
  351.     service_description System_Total_Memory_Usage
  352.     check_command check_snmp_stor_xh!-m "^Virtual Memory$"!70!90
  353.     }
  354. define service{
  355.     use generic-service
  356.     host_name $ip
  357.     service_description System_Cpu_Load
  358.     check_command check_snmp_load_xh!80!90
  359. }
  360. EOF
  361. }
  362. config_disk()
  363. {
  364. cat >> ${cfg_path}/${ip}_win_snmp.cfg <<EOF
  365. define service{
  366.     use                generic-service
  367.     host_name            $ip
  368.     service_description        $display_name
  369.     check_command            check_snmp_stor_xh!-m "^$command_name"!85!95
  370.     }
  371. EOF
  372. }
  373. config_program()
  374. {
  375. cat >> ${cfg_path}/${ip}_win_snmp.cfg <<EOF
  376. define service{
  377.     use                generic-service
  378.     host_name            $ip
  379.     service_description        $display_name
  380.     check_command            check_snmp_process_xh!$command_name!0!0
  381.     }
  382. EOF
  383. }
  384. config_escalations()
  385. {
  386. #注意$1$2的值,查看service_add 函数
  387. #注意前后的注释--为了sed删除的时候可以指定范围。
  388. cat >>${cfg_path}/escalations.cfg <<EOF
  389. #$ip
  390. define hostescalation{
  391. host_name $ip
  392. first_notification 2
  393. last_notification 0
  394. notification_interval 480
  395. contact_groups admins
  396. }
  397. define serviceescalation{
  398. host_name $ip
  399. service_description System_Total_Memory_Usage,System_Memery_Usage,System_Cpu_Load$1$2
  400. first_notification 2
  401. last_notification 0
  402. notification_interval 480
  403. contact_groups admins
  404. }
  405. #配置结束
  406. EOF
  407. }
  408. #-------------------------------------------------------
  409. main()
  410. {
  411. main_config
  412. }
  413. main
  414. exit 0


阅读(4654) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

左手_wanggy2013-07-04 10:18:41

fr222093:大哥你好!! 請教一下,假使想實現未知client端啟動以後,nagios server 會自動把這台node和service 加入,那前提下是client已有安裝NSClient++ 。這該如何做。請給小弟一點方向 感謝。

这个脚本不完善,我后面改了一版可以添加/删除/修改。我这个是针对snmp客户端,一般win下我很少NSclient,另外这个脚本是针对服务端已经架设好,并且已经设置了基础数据(如commands.cfg之类),主要是方便下面的人使用。如果是连服务端也没有配置,那么可以写个一健安装nagios 的脚本,加上你的业务应用。如果你要NSclient 可以改下配置部份。如果是未知IP你按网段描述,找出匹配的NSclient或其它方式客户端。这样就不用主动指定IP了。

回复 | 举报

fr2220932013-07-03 01:19:34

大哥你好!! 請教一下,假使想實現未知client端啟動以後,nagios server 會自動把這台node和service 加入,那前提下是client已有安裝NSClient++ 。這該如何做。請給小弟一點方向 感謝。