nagios 简单安装使用

Posted on Posted in nagios

nagios

yum -y install httpd mysql-devel openssl-devel php php-devel # 需要一个lap环境,可以不需要mysql

安装

useradd nagios
groupadd nagcmd
usermod -G nagcmd nagios
usermod -G nagcmd apache

tar jxvf nagios-cn-3.2.3.tar.bz2
cd nagios-cn-3.2.3
./configure --with-command-group=nagcmd
make all
make install
make install-init # 安装生成 /etc/rc.d/init.d/nagios 启动脚本
make install-config # 安装一下配置文件
ls /usr/local/nagios/etc/ # 查看配置文件目录
cat /usr/local/nagios/etc/htpasswd.users # 查看存放的用户名和密码
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin # 修改密码
make install-webconf # 安装web配置文件
/etc/init.d/httpd restart # 重启apache,访问 http://192.168.1.166/nagios/ 输入用户和密码进入

使用

/usr/local/nagios # nagios 存放目录

├── bin # 存放可执行文件
├── etc # 存放配置文件
├── libexec # 在老版本只有安装了nagios-plugins插件才会有,目录用于存放所有监控的命令
├── sbin # cgi 解析文件
├── share # 存放 web 文件
└── var

安装nagios-plugins

tar zxvf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd
make && make install
ls /usr/local/nagios/libexec/ # 这个目录下会多出一些文件

/usr/local/nagios/etc

cgi.cfg文件

main_config_file=/usr/local/nagios/etc/nagios.cfg # 主配置
physical_html_path=/usr/local/nagios/share # web 文件位置
url_html_path=/nagios # 前端访问的根目录,url类似 http://192.168.1.166/nagios/
use_authentication=1 # 开启认证,0是不开启

nagios.cfg文件

使用nagios的流程

  1. 指定主配置文件中要加载的配置文件
  2. 定义主机,主机组(可以没有)
  3. 定义服务
  4. 定义监控的命令
  5. 检查配置文件
  6. 启动nagios
nagios -v nagios.cfg # 检查配置文件是否有错误 /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
/etc/init.d/nagios start # 启动查看一下,如果出现问题,检查log

实例

监控本机对象nfs运行状态

/usr/local/nagios/etc/objects # 配置文件目录
vim localhost.cfg #新增配置
define host{                                                                                                                       
        use                     linux-server            ; 给网络设备用的       
        host_name               localhost         #改为本机                  
        alias                   HomeSwitch                                    
        address                 127.0.0.1                                 
        icon_image              switch.gif                                                    
        statusmap_image         switch.gd2                   
        2d_coords               100,200                                                      
        3d_coords               100,200,100                                          
        }
define hostgroup{                                                                                                                  
        hostgroup_name  nagcmd ; The name of the hostgroup     
        alias           Linux Servers ; Long name of the group       
        members         localhost     ; Comma separated list of hosts that belong to this group  
        }
define service{                                                                                                                    
        use                             local-service         ; Name of service template to use  
        host_name                       localhost                                                                                  
        service_description             salt-master                                                                                
        check_command                   check_tcp!2049                                      
        }

/usr/local/nagios/etc/nagios.cfg # 查看一下配置
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

nagios python 脚本

  • 0 - ok
  • 1 - warning
  • 2 - critical
  • 3 - unkown
/usr/local/nagios/pythonshell/mytest.py # 创建目录和文件

import sys

randint = random.randint(1,10)
if randint == 1:
    sys.exit(0)
elif randint == 2:
    sys.exit(1)
elif randint == 4:
    sys.exit(2)
else:
    sys.exit(3)

chown +x mytest.py

修改文件,调用脚本

/usr/local/nagios/etc/objects/commands.cfg

define commandP{
    command_name python-test # 命令
    command_line python /usr/local/nagios/pythonshell/mytest.py # 执行结果
    }

/usr/local/nagios/etc/objects/localhost.cfg

define service{
    use                     local-service
    host_name               localhost
    service_description     our_python_test
    check_command           python-test
    }

重启nagios,查看结果

» 转载请注明来源:呢喃 » nagios 简单安装使用

Leave a Reply

Your email address will not be published. Required fields are marked *

15 + twenty =