实验环境如下
Nagios监控服务器IP:192.168.11.5
××× 监控服务器IP:192.168.11.6
Nagios 配置如下:
一 、安装Apache+php
1 编译安装apache服务器。(在之前的博客中已经写过,这里就偷懒了。)
2 编译安装php
- yum install libxml2-devel
- Cd /usr/local/php-5.4.3
- ./configure--prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs
- make && make install
3 修改apache的配置文件
- vi /usr/local/apache2/conf/httpd.conf
- <IfModule dir_module>
- DirectoryIndex index.html index.php # 添加 index.php
- IfModule>
- #LoadModule rewrite_module modules/mod_rewrite.so
- LoadModule php5_module modules/libphp5.so #添加
- AddType application/x-compress .Z
- AddType application/x-gzip .gz .tgz
- AddType application/x-httpd-php .php # 添加
4 测试php是否安装成功
- cd /usr/local/apache2/htdocs/
- vi test.php
- php
- phpinfo();
- ?>
5 使用游览器访问
php到此安装成功
二 、安装Nagios监控服务器
安装nagios
1 安装nagios
- useradd -s /sbin/nologin nagios # 建立nagios用户
- cd /usr/local/nagios-3.2.3/
- ./configure --prefix&#61;/usr/local/nagios
- make all
- make install
- make install-init #在/etc/rc.d/init.d/目录下创建启动脚本
- make install-commandmode #配置目录权限
- make install-config #安装nagios示例配置文件&#xff0c;这里安装路径为/usr/local/nagios/etc
2 安装nagios 插件
- cd /usr/local/nagios-plugins-1.4.15/
- ./configure --prefix&#61;/usr/local/nagios #这里插件安装的位置与nagios相同
- make && make install
&#xff13; 配置apache &#xff0c;使用apache支持nagios
- vi /usr/local/apache2/conf/httpd.conf
- 修改为
- User nagios
- Group nagios
- 最后添加
- #nagios
- ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
- <Directory "/usr/local/nagios/sbin">
- AuthType Basic
- Options ExecCGI
- AllowOverride None
- Order allow,deny
- Allow from all
- AuthName "Nagios Access"
- AuthUserFile /usr/local/nagios/etc/htpasswd
- Require valid-user
- Directory>
- Alias /nagios "/usr/local/nagios/share"
- <Directory "/usr/local/nagios/share">
- AuthType Basic
- Options None
- AllowOverride None
- Order allow,deny
- Allow from all
- AuthName "Nagios Access"
- AuthUserFile /usr/local/nagios/etc/htpasswd
- Require valid-user
- Directory>
4 创建nagios访问的用户 用户名称为nagios
- /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd naigos
- New password:
- Re-type new password:
- Adding password for user naigos
5 启动apache
- /usr/local/apache2/bin/apachectl restart
6 修改naigos的权限
- chown nagios.nagios /usr/local/nagios
- chown nagios.nagios -R /usr/local/nagios/libexec/
7 使用游览器访问nagios服务器
显示乱码&#xff0c;解决方法&#xff1a;
主要是apache没有开启cgi脚本的缘故
进入apache的主配置文件httpd.conf
- vi /usr/local/apache2/conf/httpd.conf
- #LoadModule cgid_module modules/mod_cgid.so
- #LoadModule actions_module modules/mod_actions.so
- 将上面2行的#去掉&#xff0c;重启apache就OK了
8 配置nagios服务器
- [root&#64;localhost local]# cd /usr/local/nagios-3.2.3/
- [root&#64;localhost nagios-3.2.3]# cd /usr/local/nagios
- [root&#64;localhost nagios]# ls
- bin etc include libexec sbin share var
- [root&#64;ac96548d etc]# vi /usr/local/nagios/etc/cgi.cfg
- main_config_file&#61;/usr/local/nagios/etc/nagios.cfg
- physical_html_path&#61;/usr/local/nagios/share
- url_html_path&#61;/nagios show_context_help&#61;0
- use_pending_states&#61;1
- use_authentication&#61;1
- use_ssl_authentication&#61;0
- default_user_name&#61;nagios
- authorized_for_system_information&#61;nagiosadmin,nagios
- #添加刚才使用htpasswd建立的用户naigos &#xff08;使用nagios于nagiosadmin用户才能看到监控的主机&#xff09;
- authorized_for_configuration_information&#61;nagiosadmin,nagios
- authorized_for_system_commands&#61;nagios
- authorized_for_all_services&#61;nagiosadmin,nagios
- authorized_for_all_hosts&#61;nagiosadmin,nagios
- authorized_for_all_service_commands&#61;nagiosadmin,nagios
- authorized_for_all_host_commands&#61;nagiosadmin,nagios
- default_statusmap_layout&#61;5 default_statuswrl_layout&#61;4
- ping_syntax&#61;/bin/ping -n -U -c 5 $HOSTADDRESS$
- refresh_rate&#61;90 escape_html_tags&#61;1
- action_url_target&#61;_blank notes_url_target&#61;_blank lock_author_names&#61;1
- /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 检测nagios的配置文件是否有语法错误
- /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg 启动nagios监控服务器
9 通过游览器可以看到nagios监控服务器本机的状态
至此 nagios监控服务器安装成功
三、配置被监控的linux 主机 &#xff08;***主机&#xff09;
1 安装nagios-plugins和nrpe插件
- Yum –y install gcc
- useradd -s /sbin/nologin nagios
- 安装nagios-plugins
- tar xzvf nagios-plugins-1.4.15.tar.gz
- cd nagios-plugins-1.4.15
- ./configure –prefix&#61;/usr/local/nagios
- make
- make install
- 安装 nagios_nrpe_2.9
- wget http://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.9/nrpe-2.9.tar.gz
- tar xzvf nrpe-2.9.tar.gz
- cd nrpe-2.9
- ./configure --with-command-group&#61;nagios --prefix&#61;/usr/local/nagios
- (如果报缺少ssl&#xff0c;请安装 yum install openssl-devel)
- make all
- make install-plugin
- make install-daemon
- make install-daemon-config
2 修改nrpe的配置文件
- vi /usr/local/nagios/etc/nrpe.cfg
- 找到 allowed_hosts&#61;127.0.0.1
- 后面加nagios服务器的IP, 用“,”隔开&#xff0c;加了之后如下&#xff1a;
- allowed_hosts&#61;127.0.0.1,192.168.11.5
3 配置nrpe的监控对象
|
注释 在/usr/local/nagios/libexec 这个目录下&#xff0c;都是监控脚本(当我们需要监控特定服务时&#xff0c;需要在此目录下创建监控脚本&#xff0c;然后
在nrpe.cfg的配置文件中添加监控的命令)
- [root&#64;localhost libexec]# ls
- check_apt check_dummy check_ircd check_ntp check_rpc check_wave
- check_breeze check_file_age check_load check_ntp_peer check_sensors negate
- check_by_ssh check_flexlm check_log check_ntp_time check_smtp urlize
- check_clamd check_ftp check_mailq check_nwstat check_ssh utils.pm
- check_cluster check_http check_mrtg check_oracle check_swap utils.sh
- check_dhcp check_icmp check_mrtgtraf check_overcr check_tcp
- check_dig check_ide_smart check_nagios check_ping check_time
- check_disk check_ifoperstatus check_nntp check_pop check_udp
- check_disk_smb check_ifstatus check_nrpe check_procs check_ups
- check_dns check_imap check_nt check_real check_user
4 启动nrpe
- 启动nrpe&#xff0c;并测试
- /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d 启动nrpe
- echo &#39;/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d &> /dev/null&#39;
- >> /etc/rc.local 设置开机自启动
- netstat -atulnp | grep &#39;nrpe&#39;
- tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 3308/nrpe
- /usr/local/nagios/libexec/check_nrpe -H localhost
- NRPE v2.12
四、配置监控服务器 &#xff08;nagios服务器&#xff09;
1 添加需要监控的linux客户端
- vi /usr/local/nagios/etc/nagios.cfg
- cfg_file&#61;/usr/local/nagios/etc/objects/localhost.cfg
- cfg_file&#61;/usr/local/nagios/etc/objects/***.cfg # 添加此行
2、在commands.cfg中添加nrpe插件
- vi /usr/local/nagios/etc/objects/commands.cfg
- #check nrpe
- define command{
- command_name check_nrpe
- command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
- }
3、建立被监控服务器的配置文件&#xff08;***.cfg&#xff09;
- vi /usr/local/nagios/etc/objects/***.cfg
- define host{
- use linux-server ; Name of host template to use
- host_name ***
- alias ***
- address 192.168.11.6
- }
- define hostgroup{
- hostgroup_name *** ; The name of the hostgroup
- alias Linux Servers ; Long name of the group
- members *** Server
- }
- define service{
- use local-service ; Name of service template to use
- host_name ***
- service_description PING
- check_command check_ping!100.0,20%!500.0,60%
- }
- define service{
- use local-service ; Name of service template to use
- host_name ***
- service_description disk
- check_command check_nrpe!check_sda5
# 通过nrpe插件监控***服务器的硬盘
- }
- define service{
- use local-service ; Name of service template to use
- host_name ***
- service_description SSH
- check_command check_ssh
- notifications_enabled 0
- }
- define service{
- use local-service ; Name of service template to use
- host_name ***
- service_description ×××
- check_command check_tcp!1723 #监控tcp的1723端口
- notifications_enabled 0
- }
当我们使用相同的参数监控多台服务器时候&#xff0c;我们可以建立组的方式&#xff0c;这样不必建立多个配置文件&#xff0c;例如监控多台tomcat 服务器
- vi /usr/local/nagios/etc/nagios.cfg
- cfg_file&#61;/usr/local/nagios/etc/objects/localhost.cfg
- cfg_file&#61;/usr/local/nagios/etc/objects/***.cfg # 添加此行
- cfg_file&#61;/usr/local/nagios/etc/objects/tomcat.cfg # 添加此行
当有多台服务器时候使用组的优点
- vi /usr/local/nagios/etc/objects/tomcat.cfg
- define host{
- use linux-server ; Name of host template to use
- ; This host definition will inherit all variables that are defined
- ; in (or inherited by) the linux-server host template definition.
- host_name Tomcat1
- alias web1
- address 192.168.11.7
- }
- define host{
- use linux-server ; Name of host template to use
- ; This host definition will inherit all variables that are defined
- ; in (or inherited by) the linux-server host template definition.
- host_name Tomcat2
- alias web2
- address 192.168.11.8
- }
- define host{
- use linux-server ; Name of host template to use
- ; This host definition will inherit all variables that are defined
- ; in (or inherited by) the linux-server host template definition.
- host_name Tomcat3
- alias web3
- address 192.168.11.9
- }
- define host{
- use linux-server ; Name of host template to use
- ; This host definition will inherit all variables that are defined
- ; in (or inherited by) the linux-server host template definition.
- host_name Tomcat4
- alias web4
- address 192.168.11.10
- }
- define host{
- use linux-server ; Name of host template to use
- ; This host definition will inherit all variables that are defined
- ; in (or inherited by) the linux-server host template definition.
- host_name Tomcat5
- alias web5
- address 192.168.11.11
- }
- define host{
- use linux-server ; Name of host template to use
- ; This host definition will inherit all variables that are defined
- ; in (or inherited by) the linux-server host template definition.
- host_name Tomcat6
- alias web6
- address 192.168.11.12
- }
- define hostgroup{
- hostgroup_name Tomcat ; The name of the hostgroup
- alias Linux Servers ; Long name of the group
- members Tomcat1,Tomcat2,Tomcat3,Tomcat4,Tomcat5,Tomcat6 ; Comma separated list of hosts that belong to this group
- } #在组中添加hostname
- define service{
- use local-service ; Name of service template to use
- hostgroup_name Tomcat #由 host_name 改成hostgroup—name
- service_description PING
- check_command check_ping!100.0,20%!500.0,60%
- }
- define service{
- use local-service ; Name of service template to use
- hostgroup_name Tomcat
- service_description SSH
- check_command check_ssh
- notifications_enabled 0
- }
- define service{
- use local-service ; Name of service template to use
- hostgroup_name Tomcat
- service_description HTTP
- check_command check_http
- notifications_enabled 0
- }
4、通过游览器访问nagios服务器