Chinaunix首页 | 论坛 | 博客
  • 博客访问: 402212
  • 博文数量: 403
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: -70
  • 用 户 组: 普通用户
  • 注册时间: 2016-09-05 12:45
文章分类

全部博文(403)

文章存档

2014年(3)

2013年(1)

2012年(3)

2011年(21)

2010年(13)

2009年(64)

2008年(9)

2007年(36)

2006年(253)

分类: 系统运维

2011-05-24 09:04:07

脚本添加nagios监控主机(带分析)

[背景]

  公司要监控的主机很多,估计有近1K台,而且还会增加.....所以,如果让我用手工加的话,妈呀!怕怕.......

  就想到了用脚本,也看了sery老师写的那个shell的,是交互性的,感觉不实用,就自己写了,也在这里和sery老师PK下喽(哈哈,开玩笑啦!)!(注:脚本的作用就是帮我解决重复的工作,所以嘛.......就把工作交给它了.)

  [过程]

  nagios所监控的对象不就是主机,联系用户,命令,服务这四个对象嘛,我们来分析下,联系用户应该算是工作量很小的工作,不需要重复得劳动的,定义命令和服务这两个差不多,都不是很累人的活.....这么说来,就是主机的定义了......

分析hosts.cfg
define host{
host_name bogus-router
alias  Bogus Router #1
address  192.168.1.254
parents  server-backbone
check_command check-host-alive
check_interval 5
retry_interval 1
max_check_attempts 5
check_period 24x7
process_perf_data 0
retain_nonstatus_information0
contact_groups router-admins
notification_interval 30
notification_period 24x7
notification_options d,u,r
}

  我从参考文档的Example Definition拷过来的......

  在这里,我们能看到host_name,alias,address应该与下一个定义主机不同的,其他的可以一样...哈哈.那就可以借用use这个选项了.

  哈哈,那我将我的hosts.cfg分开....

  原内容:

define host {
    host_name         Windows
    alias           Windows 3389
    address          192.168.x.x
    contact_groups       sagroup
    check_command       check-host-alive
    max_check_attempts     5
    notification_interval   10
    notification_period    24x7
    notification_options    d,u,r
}

划分后

definehosts.cfg
define host{
    name   inithost    #切记这个不是host_name.....
    contact_groups       sagroup
    check_command       check-host-alive
    max_check_attempts     5
    notification_interval   10
    notification_period    24x7
    notification_options    d,u,r
    register          0       #切记这个一定要加不然会报错的....
}
hosts.cfg
define host {
    use      inithost
    host_name         Windows
    alias           Windows 3389
    address          192.168.x.x
}
再修改vi nagios.cfg

cfg_file=/usr/local/nagios/etc/definehosts.cfg

  那么就可以写脚本了........脚本的功能就是读取IP,将其追加到hosts.cfg下.....

  如

define host {
    use      inithost
    host_name         192.168.1.2
    alias           KT_TEL_1
    address          192.168.1.2
}
define host {
    use      inithost
    host_name         192.168.1.3
    alias           KT_TEL_1
    address          192.168.1.3
}
............................

  大家应该看到变的是IP吧....我们只要收集好IP列表,写个这样的脚本应该不成问题吧!

addhost.pl
#!/usr/bin/perl
open(IP,"iplist.txt");
open(FH,">>hosts.cfg");
while(){
   $ip=$_;
   print FH "define host {n";
   print FH "tuse      inithostn";
   print FH "thost_name   $ip";    
   print FH "talias     KT_TEL_1n";
   print FH "taddress    $ip";    
   print FH "}n";           
}
close FH;
close IP;
执行后....

[root@study soft]# more hosts.cfg
define host {
use      inithost
host_name   192.168.1.2
alias     KT_TEL_1
address    192.168.1.2
}
define host {
use      inithost
host_name   192.168.1.3
alias     KT_TEL_1
address    192.168.1.3
}

  哈哈....

  还差一步,就是分组喽!把这个iplist.txt里的IP都划分到一个组里,这是我们需要的.

  那么再写个脚本吧!

addgroups.pl
#!/usr/bin/perl
open(IP,"iplist.txt");
while(){
   $ip=$_;
   chomp($ip);
   $members=$members.","."$ip";
}
close IP;
$members=~s/^,//;
$members="memberst".$members;
open(FH,">KTgroups.cfg");
  print FH "define group {n";
  print FH "thostgroup_name KT_TEL_1n";
  print FH "talias      KT_TEL_1n";
  print FH "t".$members."n";
  print FH "}n";
close FH;

  这样就可以了,看简化了很多吧!大家再把KTgroups.cfg加到nagios.cfg进去就可以了,或者自己建个目录修改cfg_dir,这样在这个目录下添加新的文件不需要再改nagios.cfg了,这点小手术,我相信大家还是有的,基本上功能完全实现了,这两个脚本也绝对受用!希望大家能在工作上能用得上!

  [总结]

  自己动手,丰衣足食!

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