How Nagios Works
Nagios functions as a monitoring service and host. But its own well does not include this part of the function. All the monitoring and testing functions are done by various plugins. After the Nagios is started, it periodically automatically calls the hammer to detect the server state, while Nagios maintains a queue, all the status information returned by the plug-in is queued, and Nagios reads the information from the first team, processes it, and then displays the status results over the web. This is the so-called passive mode, often used to monitor the host system resources, such as system load, disk utilization, memory utilization, network status, number of system processes and so on. The other is active mode, mainly Nagios server actively to obtain data, often used to detect URL monitoring and service status monitoring. Compared to the active mode of the server actively to be monitored by the monitoring machine to obtain monitoring data, a great advantage is that the other work to remove data processing is placed on the monitor (including the transmission of data), to avoid the large number of monitors, a polling time is too long and lead to monitoring reaction delay, This is also the key to the passive mode to assume a greater amount of monitoring. Nagios provides a number of plug-ins that allow you to easily monitor many service statuses. After the installation is complete, there are all the plugins available in Nagios's/libexec in the Nagios Sunday record, such as Check_disk is the plug-in that checks the disk space, Check_load is the plug-in that checks the CPU load, and so on. Each plugin can be used to view its usage and functionality by running the./check_xxx-h command.
Nagios Four monitoring states
Nagios can identify four status return messages: 0 (OK) indicates a normal/green state. 1 (WARNING) indicates that a warning appears/xxx,2 (critcal) indicates a very serious error/red, 3 (UNKNOWN) indicates an unknown error/deep xxx. Nagios determines the state of the monitored object based on the value returned by the plug-in, and displays it through the Web for administrators to detect faults in a timely manner.
Nagios manages services remotely via the Nrpe plugin
- Nagios executes the Check_nrpe plugin installed inside it and tells Check_nrpe to detect which services.
- Connect the Nrpe daemon on the remote machine via the Ssl,check_nrpe.
- Nrpe runs a variety of local plugins to detect local service and status (Check_disk,.. etc).
- Nrpe the results of the test to the host side of the Check_nrpe, Check_nrpe and then send the results to the Nagios state queue.
- Nagios reads the information in the queue, then displays the results.
Case Environment
监控主机:192.168.10.128被控主机:192.168.10.142
Case implementation Build Nagios Surveillance System 1, turn off firewalls and SELinux
systemctl stop firewalld.servicesetenforce 0
2. Install related software packages
yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel
3. Create Nagios users and user groups
useradd -s /sbin/nologin nagios
4, create the working directory, set the genus belong to the main group
mkdir /usr/local/nagioschown -R nagios.nagios /usr/local/nagiosll -d /usr/local/nagios/ //查看权限
5. Compiling and installing Nagios
tar -zxvf nagios-4.0.1.tar.gz -C /optcd /opt/nagios-4.0.1./configure --prefix=/usr/local/nagios //指定安装目录make allmake installmake install-initmake install-commandmodemake install-configchkconfig --add /etc/rc.d/init.d/nagioschkconfig --level 35 nagios onchkconfig --list nagioscd /usr/local/nagios //检查是否存在etc、bin、sbin、share、var 这五个目录
6. Install Nagios Plugin
The various monitoring functions provided by Nagios are basically done via plugins.
tar xvzf nagios-plugins-1.5.tar.gz -C /optcd /opt/nagios-plugins-1.5./configure --prefix=/usr/local/nagios //指定安装目录make && make install
7. Install lamp
Recommended to manually compile and install lamp, here I save time with yum install
yum install -y httpd* mysql* php
8. Modify the httpd configuration file
Vim/etc/httpd/conf/httpd.conf found: User Apache group Apache modified to user Nagios group Nagios and then found <ifmodule dir_module> DirectoryIndex index.html </IfModule> modified to <ifmodule dir_module> directoryindex index.html index.php </If Module> then adds the following: AddType application/x-httpd-php. php for security reasons, it is common for nagios web monitoring pages to be authorized for access, which requires an additional authentication configuration, That is, the following information is added at the end of the httpd.conf file: #setting for Nagios scriptalias/nagios/cgi-bin "/usr/local/nagios/sbin" <directory "/usr/ Local/nagios/sbin "> AuthType Basic Options execcgi allowoverride None Order allow,deny allow F Rom All authname "Nagios Access" authuserfile/usr/local/nagios/etc/htpasswd Require valid-use R </Directory> Alias/nagios "/usr/local/nagios/share" <directory "/usr/local/nagios/share" > AuthType Bas IC Options None allowoverride none Order Allow,deny allow from all AuthName "Nagios Access" authuserfile/usr/local/nagios/etc/htpasswd Require Valid-user </Directory>
9. Create Access authentication files
htpasswd -c /usr/local/nagios/etc/htpasswd jack#用户名:jack 密码:123123 (自行定义)cat /usr/local/nagios/etc/htpasswd //查看文件信息,密码为加密信息
10. Start httpd Service
systemctl start httpd.service
Configuring the Nagios Monitoring System
After Nagios is installed, the default configuration file is in the/usr/local/nagios/etc/directory
Configuration file Introduction and modification
cd /usr/local/nagios/etc/
1.objects/templates.cfg
主要用于监控主机资源以及服务,在nagios配置中称为对象,为了不必重复定义一些监控对象,Nagios引入了一个模板配置文件,将一些共性的属性定义成模板,以便于多次引用。vim objects/templates.cfgcontact_groups 联系组属性改成 ts,ts将在后面的contacts.cfg文件中定义
2.resource.cfg file
resource.cfg是nagios的变量定义文件 一般无需更改$USER1$=/usr/local/nagios/libexec
3.objects/commands.cfg file
此文件默认是存在的,无需修改即可使用,当然如果有新的命令需要加入时,在此文件进行添加即可
4. objects/hosts.cfg file
此文件默认不存在,需要手动创建,hosts.cfg主要用来指定被监控的主机地址以及相关属性信息(不能有任何空格)vim objects/hosts.cfgdefine host{ use linux-server #引用主机linux-server的属性信息,linux-server主机在templates.cfg文件中进行了定义。 host_name Nagios-Linux #主机名 alias Nagios-Linux #主机别名 address 192.168.10.142 #被监控的主机地址,这个地址可以是ip,也可以是域名。 } #定义一个主机组 define hostgroup{ hostgroup_name bsmart-servers #主机组名称,可以随意指定。 alias bsmart servers #主机组别名 members Nagios-Linux #主机组成员,其中“Nagios-Linux”就是上面定义的主机。 }
5.objects/localhost.cfg file
Used to monitor the machine generally without changes
6.objects/windows.cfg file
Used to monitor windows here without using
7.objects/services.cfg file
This file does not exist by default and needs to be created manually, primarily for defining monitored services and host resources
define service{ use local-service #引用local-service服务的属性值,local-service在templates.cfg文件中进行了定义。 host_name Nagios-Linux #指定要监控哪个主机上的服务,“Nagios-Server”在hosts.cfg文件中进行了定义。 service_description check-host-alive #对监控服务内容的描述,以供维护人员参考。 check_command check-host-alive #指定检查的命令。 }
8.objects/contacts.cfg file
Contacts.cfg is a configuration file that defines contacts and contact groups (no spaces)
define contact{ contact_name jack #联系人的名称,这个地方不要有空格 use generic-contact #引用generic-contact的属性信息,其中“generic-contact”在templates.cfg文件中进行定义 alias Nagios Admin email [email protected] }define contactgroup{ contactgroup_name ts #联系人组的名称,同样不能空格 alias Technical Support #联系人组描述 members jack #联系人组成员,其中“jack”就是上面定义的联系人,如果有多个联系人则以逗号相隔 }
9.cgi.cfg file
This file is used to control the associated CGI script, because the Nagios Web monitoring interface verifies that the user is Jack, so simply add the user's execute permission in the Cgi.cfg file, and in the end add
default_user_name=jackauthorized_for_system_information=nagiosadmin,jack authorized_for_configuration_information=nagiosadmin,jack authorized_for_system_commands=jackauthorized_for_all_services=nagiosadmin,jack authorized_for_all_hosts=nagiosadmin,jackauthorized_for_all_service_commands=nagiosadmin,jack authorized_for_all_host_commands=nagiosadmin,jack
. nagios.cfg file
Nagios master configuration file, referencing the object profile in the Nagios.cfg file
cfg_file=/usr/local/nagios/etc/objects/hosts.cfgcfg_file=/usr/local/nagios/etc/objects/services.cfg
command_check_interval=10s //该变量用于设置nagios对外部命令检测的时间间隔
11. Check the configuration file
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //验证配置文件
Log in to Nagios
systemctl restart httpd.service浏览器输入http://192.168.10.128/nagios/
Log in using the Jack User and 123123 password that you started to set up.
Log in to the page
View monitored hosts, native and Nagios-linux
View the services on the monitored host
View specific status information for monitored hosts
Nagios Monitoring System