您好,登錄后才能下訂單哦!
Nagios是一個監視系統運行狀態和網絡信息的監視系統。Nagios能監視所指定的本地或遠程主機以及服務,同時提供異常通知功能等。
Nagios可運行在Linux/Unix平臺之上,同時提供一個可選的基于瀏覽器的WEB界面以方便系統管理人員查看網絡狀態,各種系統問題,以及日志等等。
nagios常用配置文件主要用以下6個:
cgi.cfg(控制cgi訪問的配置文件),nagios.cfg(Nagios主配置文件),resource.cfg(resource.cfg定義了一些變量,以便被其他文件引用,如$USER1$),objects(objects是一個目錄,用于定義Nagios對象),servers(servers是自己創建的目錄,Nagios可以加載該目錄下所有配置文件,常用于定義各遠程服務器監控信息,需要在nagios.cfg中配置)
其中:
objects目錄下文件::
commands.cfg #命令定義配置文件,里面定義的命令可以被其他文件引用
contacts.cfg #聯系人和聯系人組配置文件
localhost.cfg #監控本地機器的配置文件
printer.cfg #監控打印機的一個事例配置文件(默認未啟用)
switch.cfg #監控路由器的一個事例配置文件(默認未啟用)
templates.cfg #模板配置文件,在此可以定義模板,在其他文件中引用
timeperiods.cfg #定義監控時間段的配置文件
windows.cfg #監控Windows的一個事例配置文件(默認未啟用)
Nagios所需要的運行條件是機器必須可以運行Linux(或是Unix變種)并且有C語言編譯器。你必須正確地配置TCP/IP協議棧以使大多數的服務檢測可以通過網絡得以進行。你需要但并非必須正確地配置Nagios里的CGIs程序,而一旦你要使用CGI程序時,你必須要安裝以下這些軟件。
1.nagios安裝##可參考官網文檔
所需軟件包:gd-devel-2.0.35-11.el6.x86_64.rpm nagios-plugins-2.1.1.tar.gz nagios-cn-3.2.3.tar.bz2
tar jxf nagios-cn-3.2.3.tar.bz2 yum install gd-devel-2.0.35-11.el6.x86_64.rpm -y
cd nagios-cn-3.2.3 groupadd nagcmd useradd -M -d /usr/local/nagios -G nagcmd nagios usermod -G nagcmd apache
./configure --with-command-group=nagcmd make all ##可以看到后面步驟 make install make install-init make install-config make install-commandmode make install-webconf
/etc/httpd/conf.d/nagios.conf cat /usr/local/nagios/etc/htpasswd.users ##可看到用戶名和密碼 htpasswd /usr/local/nagios/etc/htpasswd.users nagiosadmin ##修改nagiosadmin的密碼
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/httpd start /etc/init.d/nagios start
http://172.25.45.1/nagios/
用戶名:nagiosadmin 密碼:修改的密碼
2.nagios-plugins安裝
tar zxf nagios-plugins-2.1.1.tar.gz cd nagios-plugins-2.1.1
./configure##可看到插件安裝的信息,可根據缺少的插件進行安裝。例如:mysql-devel,openssl-devel make make install
cd /usr/local/nagios/libexec chown nagios.nagios * -R
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
cd /usr/local/nagios/etc/ vim nagios.cfg
添加兩個cfg_file(33行下面)
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
注釋掉cfg_file=/usr/local/nagios/etc/objects/localhost.cfg(38行)
cd objects/ cp -p localhost.cfg hosts.cfg cp -p localhost.cfg services.cfg vim hosts.cfg
將內容修改為以下:
define host{
use linux-server
host_name server1.example.com
alias Manager
# parents MainSwitch
address 172.25.45.1
icon_p_w_picpath server.gif
statusmap_p_w_picpath server.gd2
2d_coords 500,200
3d_coords 500,200,100
}
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members * ; Comma separated list of hosts that belong to this group
}
vim services.cfg
將166行之前全部刪掉,再刪除第8行開始的servicegroup,只留下一個servicegroup。將此servicegroup的members最后兩個成員HomeRouter和PING刪除
:%s/田朝陽家用機/server1.example.com/g
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
cd /usr/local/nagios/libexec ./check_disk ./check_disk -w 20 -c 10 ./check_disk -w 20 -c 10 -p /
vim services.cfg
添加以下內容:
max_check_attempts 2
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
【server2】
yum install -y mysql-server /etc/init.d/mysqld start mysql_secure_installation
mysql -p mysql> create database nagdb; mysql> grant select on nagdb.* to nagios@'172.25.45.1' identified by 'westos'; mysql> quit
【server1】
cd /usr/local/nagios/libexec ./check_mysql -H 172.25.45.2 -u nagios -p westos
mysql -h 172.25.45.2 -u nagios -p mysql> show databases; mysql> quit
將serevr2的mysql服務關閉
cd /usr/local/nagios/etc/objects vim commands.cfg
在212行后添上以下內容:
# 'check_mysql' command definition
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$
}
vim services.cfg
在最后添上以下內容:
#######################check_mysql
define service{
use local-service
host_name server2.example.com
service_description MYSQL
check_command check_mysql!nagios!westos
}
vim hosts.cfg
在上面host下再添加一個host,內容如下:
define host{
use linux-server
host_name server2.example.com
alias Manager
# parents MainSwitch
address 172.25.45.2
icon_p_w_picpath server.gif
statusmap_p_w_picpath server.gd2
2d_coords 400,100
3d_coords 400,100,100
}
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
###添加連線
vim hosts.cfg
將alias的值改為server2
將parents的注釋取消,并將值改為server1.example.com
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
此時HTTP的告警狀態是因為沒有index.html頁面,在/var/www/html下建立一個index.html文件就可以解決HTTP告警問題
3.安裝nrpe
【server2】
所需軟件包:nrpe-2.15.tar.gz nagios-plugins-2.1.1.tar.gz
tar zxf nrpe-2.15.tar.gz tar zxf nagios-plugins-2.1.1.tar.gz
useradd -M -d /usr/local/nagios nagios cd nagios-plugins-2.1.1 ./configure
出現以下錯誤,是因為缺少軟件,下載gcc可解決
[root@server2 nagios-plugins-2.1.1]# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether to disable maintainer-specific portions of Makefiles... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/nagios-plugins-2.1.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.
yum install openssl-devel -y
./configure##這里不需要--with-mysql,可以不安裝
make make install
cd /usr/local/nagios/ chown nagios.nagios . -R
yum install -y xinetd
cd /root/nrpe-2.15 ./configure make all make install-plugin make install-daemon make install-daemon-config make install-xinetd
cd /etc/xinetd.d vim nrpe
將15行的only_from改為172.25.45.1
vim /etc/services
添加以下內容:
nrpe 5666/tcp
/etc/init.d/xinetd start netstat -antlpe
vim /usr/local/nagios/etc/nrpe.cfg
修改221行,改為以下內容:
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
/etc/init.d/xinetd restart
cd /usr/local/nagios/libexec/ scp check_nrpe root@172.25.45.1:/usr/local/nagios/libexec/
【server1】
cd /usr/local/nagios/libexec/ chown nagios.nagios check_nrpe ./check_nrpe -H 172.25.45.2 ./check_nrpe -H 172.25.45.2 -c check_disk
測試:
在server2將/usr/local/nagios/etc/nrpe.cfg修改,將command[check_disk] -w 改為90%,并重啟服務
./check_nrpe -H 172.25.45.2 -c check_disk
將server2的/usr/local/nagios/etc/nrpe.cfg文件恢復上一步之前,并重啟服務。
【server1】
cd /usr/local/nagios/etc/objects vim commands.cfg
在# 'check_mysql' command definition這一段下添加以下內容:
# 'check_nrpe' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
vim services.cfg
在最后添上以下內容:
#######################check_nrpe
define service{
use local-service ; Name of service template to use
host_name server2.example.com
service_description 根分區
check_command check_nrpe!check_disk
}
define service{
use local-service ; Name of service template to use
host_name server2.example.com
service_description 登錄用戶數
check_command check_nrpe!check_users
}
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
http://172.25.45.1/nagios/
4.虛擬機連網
ip addr add 172.25.254.101/24 dev eth0 ip addr show
ip route show route -n ping 172.25.254.45 route del default gw 172.25.45.250 route add default gw 172.25.254.45 route -n
vim /etc/resolv.conf
ping 192.168.122.1 ping 172.25.254.45
測試網絡是否連通
dig baidu.com ping baidu.com
已連網
在家里用無限上網,也可以用NAT的方式進行虛擬機連網,網絡配置選擇DHCP分配網絡
可以看到本地發送郵件的地址。
登錄郵箱,進行設置。在反垃圾處設置郵件地址白名單:nagios@server1.example.com
【server1】
yum install mail -y
su - nagios mail wjl19940429@qq.com
cd /usr/local/nagios/etc/objects vim contacts.cfg
修改35行email的值為自己的郵箱
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
【server2】
/etc/init.d/mysqld stop
【server1】
vim /usr/local/nagios/etc/objects/templates.cfg
修改186行max_check_attempts的值為3
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
為了盡快測試,將檢測次數減少一些
##110云告警##
所需軟件包:alert-agent-4.1.3.1-linux-x64.tar.gz
su - nagios -bash-4.1$ tar zxf alert-agent-4.1.3.1-linux-x64.tar.gz -C /usr/local/nagios/libexec/ -bash-4.1$ cd /usr/local/nagios/libexec/ -bash-4.1$ cd alert-agent/ -bash-4.1$ ll
-bash-4.1$ cd plugin/nagios-plugin/ -bash-4.1$ cp nagios /usr/local/nagios/libexec/ -bash-4.1$ cp 110monitor.cfg /usr/local/nagios/etc/objects/
在110云告警頁面選擇配置,添加應用云告警
復制下key
cd /usr/local/nagios/etc/objects vim 110monitor.cfg
將復制下的key粘貼在18行,paper后
vim contacts.cfg
在54行members后添加110monitor
cd .. vim nagios.cfg
在35行之后添加cfg_file=/usr/local/nagios/etc/objects/110monitor.cfg
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios reload
網頁進行下一步,完成配置。
選擇通知策略,進行配置
例如
【server2】
/etc/init.d/mysqld stop
【server1】
cd /usr/local/nagios/var tail -f nagios.log
/etc/init.d/nagios reload
缺云告警成功示例截圖
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。