nagios监控
一、 配置nagios监控
1、 配置LAMP平台
1)
安装LAMP软件
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# yum -y install httpd mariadb mariadb-server php php-mysql openssl-devel
2)
创建php测试文件
[root@centos01 ~]# vim /var/www/html/index.php
3)
客户端访问
4)
启动apache、mariadb
[root@centos01 ~]# systemctl start httpd
[root@centos01 ~]# systemctl enable httpd
[root@centos01 ~]# systemctl start mariadb
[root@centos01 ~]# systemctl enable mariadb
5)
创建管理nagios账户
[root@centos01 ~]# useradd -M -s /sbin/nologin nagios
6)
配置nagios
[root@centos01 ~]# umount /mnt/
[root@centos01 ~]# tar zxvf /mnt/nagios-4.0.1.tar.gz -C /usr/src/
[root@centos01 nagios-4.0.1]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
7)
安装nagios
[root@centos01 nagios-4.0.1]# make all && make install && make install-init
[root@centos01 nagios-4.0.1]# make install-commandmode && make install-config
8)
设置目录的所有者
[root@centos01 ~]# chown -R nagios:nagios /usr/local/nagios/
9)
添加系统访问设置开机自启
[root@centos01 ~]# chkconfig --add nagios
[root@centos01 ~]# chkconfig --level 35 nagios on
2、 安装nagios依赖组件
1)
安装nagios-plugins
[root@centos01 ~]# tar -zxvf /mnt/nagios-plugins-1.5.tar.gz -C /usr/src/
[root@centos01 ~]# cd /usr/src/nagios-plugins-1.5/
[root@centos01 nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios
[root@centos01 nagios-plugins-1.5]# make && make install
2)
安装nrpe
[root@centos01 ~]# tar zxvf /mnt/nrpe-2.15.tar.gz -C /usr/src/
[root@centos01 ~]# cd /usr/src/nrpe-2.15/
[root@centos01 nrpe-2.15]# ./configure && make all && make install-plugin
3、 配置httpd支持nagios
1)
修改apache主配置文件
[root@centos01 ~]# vim /etc/httpd/conf/httpd.conf
ScriptAlias /nagios/cgi-bin “/usr/local/nagios/sbin”
<Directory “/usr/local/nagios/sbin”>
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName “Nagios Access”
AuthType Basic
AuthUserFile “/usr/local/nagios/etc/htpasswd.users”
Require valid-user
Alias /nagios “/usr/local/nagios/share”
<Directory “/usr/local/nagios/share”>
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName “Nagios Access”
AuthType Basic
AuthUserFile “/usr/local/nagios/etc/htpasswd.users”
Require valid-user
2)
生成登陆验证账户密码
[root@centos01 ~]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
3)
重新启动apache
[root@centos01 ~]# systemctl restart httpd
4、 配置sendmail
1)
安装sendmail
[root@centos01 ~]# tar zxvf /mnt/sendEmail-v1.55.tar.gz -C /usr/src/
2)
移动sebdEnail到bin下
[root@centos01 ~]# mv /usr/src/sendEmail-v1.55/sendEmail /usr/local/bin/
3)
修改配置文件
[root@centos01 ~]# vim /usr/local/nagios/etc/nagios.cfg
36 cfg_dir=/usr/local/nagios/etc/conf
4)
创建目录
[root@centos01 ~]# mkdir /usr/local/nagios/etc/conf/
5、 定义nagios命令
[root@centos01 ~]# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name notify-host-by-sendEmail command_line /usr/bin/printf “%b” “***** Nagios ***\n\nNotification Type:
N
O
T
I
F
I
C
A
T
I
O
N
T
Y
P
E
NOTIFICATIONTYPE
NOTIFICATIONTYPE\nHost:
H
O
S
T
N
A
M
E
HOSTNAME
HOSTNAME\nState:
H
O
S
T
S
T
A
T
E
HOSTSTATE
HOSTSTATE\nAddress:
H
O
S
T
A
D
D
R
E
S
S
HOSTADDRESS
HOSTADDRESS\nInfo:
H
O
S
T
O
U
T
P
U
T
HOSTOUTPUT
HOSTOUTPUT\n\nDate/Time:
L
O
N
G
D
A
T
E
T
I
M
E
LONGDATETIME
LONGDATETIME\n" | /usr/local/bin/sendEmail -o fqdn=FQND -f 1378285730@qq.com -t
C
O
N
T
A
C
T
E
M
A
I
L
CONTACTEMAIL
CONTACTEMAIL -s smtp.qq.com -u "
N
O
T
I
F
I
C
A
T
I
O
N
T
Y
P
E
NOTIFICATIONTYPE
NOTIFICATIONTYPE Host Alert:
H
O
S
T
N
A
M
E
HOSTNAME
HOSTNAME is
H
O
S
T
S
T
A
T
E
HOSTSTATE
HOSTSTATE **” -xu 821409986 -xp hehe
}
define command{
command_name notify-service-by-sendEmail
command_line /usr/bin/printf “%b” “***** Nagios ***\n\nNotification Type:
N
O
T
I
F
I
C
A
T
I
O
N
T
Y
P
E
NOTIFICATIONTYPE
NOTIFICATIONTYPE\n\nService:
S
E
R
V
I
C
E
D
E
S
C
SERVICEDESC
SERVICEDESC\nHost:
H
O
S
T
A
L
I
A
S
HOSTALIAS
HOSTALIAS\nAddress:
H
O
S
T
A
D
D
R
E
S
S
HOSTADDRESS
HOSTADDRESS\nState:
S
E
R
V
I
C
E
S
T
A
T
E
SERVICESTATE
SERVICESTATE\n\nDate/Time:
L
O
N
G
D
A
T
E
T
I
M
E
LONGDATETIME
LONGDATETIME\n\nAdditional Info:\n\n
S
E
R
V
I
C
E
O
U
T
P
U
T
SERVICEOUTPUT
SERVICEOUTPUT\n" | /usr/local/bin/sendEmail -o fqdn=FQDN -f 821409986@qq.com -t
C
O
N
T
A
C
T
E
M
A
I
L
CONTACTEMAIL
CONTACTEMAIL -s smtp.qq.com -u "
N
O
T
I
F
I
C
A
T
I
O
N
T
Y
P
E
NOTIFICATIONTYPE
NOTIFICATIONTYPE Service Alert:
H
O
S
T
A
L
I
A
S
HOSTALIAS
HOSTALIAS/
S
E
R
V
I
C
E
D
E
S
C
SERVICEDESC
SERVICEDESC is
S
E
R
V
I
C
E
S
T
A
T
E
SERVICESTATE
SERVICESTATE **” -xu 821409986 -xp hehe
}
define command{
command_name check_nrpe
command_line
U
S
E
R
1
USER1
USER1/check_nrpe -H
H
O
S
T
A
D
D
R
E
S
S
HOSTADDRESS
HOSTADDRESS -c
A
R
G
1
ARG1
ARG1
}
6、 定义联系人
[root@centos01 ~]# vim /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name 821409986
alias 821409986
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-sendEmail
host_notification_commands notify-host-by-sendEmail
email 821409986@qq.com
}
define contactgroup{
contactgroup_name 821409986
alias Nagios admin
members 821409986
}
7、 定义监控主机
1)
配置监控主机100.20
[root@centos01 ~]# vim /usr/local/nagios/etc/conf/hostgroups.cfg
define hostgroup{
hostgroup_name web-server
alias web-server
members 192.168.100.20
}
[root@centos01 ~]# vim /usr/local/nagios/etc/conf/192.168.100.20.cfg
define host{
host_name 192.168.100.20
alias web-server
address 192.168.100.20
check_command check-host-alive
max_check_attempts 5
check_period 24x7
notification_period 24x7
notification_options d,u,r
contact_groups 821409986
}
define service{
host_name 192.168.100.20
service_description check-host-alive
check_command check-host-alive
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification web-server_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups 821409986
}
define service{
host_name 192.168.100.20
service_description check-procs
check_command check_nrpe!check_total_procs
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups 821409986
}
define service{
host_name 192.168.100.20
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups 821409986
}
2)
设置目录所有者并重启apache、nagios
[root@centos01 ~]# chown -R nagios:nagios /usr/local/nagios/
[root@centos01 ~]# systemctl restart httpd
[root@centos01 ~]# systemctl restart nagios
3)
客户端访问nagios,账户nagisoadmin,密码pwd@123
8、 部署被监控端
1)
安装软件包
[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# yum -y install openssl-devel openssl
2)
安装nagios-plugins插件
[root@centos02 ~]# useradd -s /sbin/nologin nagios
[root@centos02 ~]# umount /mnt/
[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# tar zxvf /mnt/nagios-plugins-1.5.tar.gz -C /usr/src/
[root@centos02 nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
[root@centos02 nagios-plugins-1.5]# make && make install
3)
安装nrpe
[root@centos02 ~]# tar zxvf /mnt/nrpe-2.15.tar.gz -C /usr/src/
[root@centos02 ~]# cd /usr/src/nrpe-2.15/
[root@centos02 nrpe-2.15]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
[root@centos02 nrpe-2.15]# make all && make install-plugin && make install-daemon && make install-daemon-config
4)
修改配置文件
[root@centos02 ~]# vim /usr/local/nagios/etc/nrpe.cfg
81 allowed_hosts=127.0.0.1,192.168.100.10
[root@centos02 ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
5)
客户端访问