Nagios是一款Linux上成熟的監視系統運行狀態和網絡信息的開原IT基礎設施監視系統,Nagios能監視所指定的本地或遠程主機及服務,例如HTTP服務、FTP服務等,同時提供異常通知、事件處理等功能,當主機或服務出現故障時,Nagios還可以通過郵件、手機短信等形式在第一時間進行通知。Nagios可運行在Linux和Unix平台上,同時提供一個可選的基於浏覽器的Web界面,方便系統管理員查看系統的運行狀態、網絡狀態、各種系統問題及日志異常等。
環境: 192.168.0.201 mysql主機 rhel6.4
192.186.0.202 nagios主機 rhel6.4
安裝相關軟件包:
yum install httpd gcc make perl-ExtUtils-Embed.x86_64 -y yum localinstall gd-devel-2.0.35-11.el6.x86_64.rpm -y
安裝nagios:
useradd nagios usermod -G nagios apache #編譯所需軟件包 #運行 nagios 服務的用戶 #使 apache 用戶對 nagios 目錄具有寫權限,不然 web 頁面操作失敗 tar jxf nagios-cn-3.2.3.tar.bz2 #nagios 軟件安裝 cd nagios-cn-3.2.3 ./configure –enable-embedded-perl make all //根據提示完成安裝 make install make install-init make install-commandmode make install-config make install-webconf //在apache下通過這條命令可以快速整合
安裝nagios-plugins:
yum install mysql-devel openssl-devel -y tar zxf nagios-plugins-1.4.15.tar.gz cd nagios-plugins-1.4.15 ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-extra-opts --enable-libtap --enable-perl-modules make make install
配置nagios:
vi /usr/local/nagios/etc/nagios.cfg
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg #注釋掉localhost.cfg文件 cfg_file=/usr/local/nagios/etc/objects/services.cfg #新建 services.cfg 文件,存放服務與服務組定義 cfg_file=/usr/local/nagios/etc/objects/hosts.cfg #新建 hosts.cfg 文件,存放主機與主機組定義
配置hosts.cfg:
vi /usr/local/nagios/etc/objects/hosts.cfg
define host{ //主機的管理 use linux-server //使用默認的主機規則 host_name vm2.example.com //主機名 alias Nagios vm2 //主機別名 address 127.0.0.1 //主機ip地址 icon_image switch.gif //圖標 statusmap_image switch.gd2 2d_coords 100,200 //2D圖像坐標 3d_coords 100,200,100 //3D圖像坐標 } define hostgroup{ //主機組的管理 hostgroup_name linux-servers //主機組名字 alias Linux Servers //別名 members * //*代表上面所有主機
配置mysql
----建立nagdb專用數據庫
mysql> create database nagdb default CHARSET=utf8;
Query OK, 1 row affected (0.01 sec)
mysql> grant select on nagdb.* to 'nagios'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> update mysql.user set 'Password' = PASSWORD('nagios') where 'User'='nagios';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Password' = PASSWORD('nagios') where 'User'='nagios'' at line 1 mysql> update mysql.user set Password = PASSWORD('nagios') where user='nagios'; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
----用以下命令來驗證一下:
[root@node2 etc]# /usr/local/nagios/libexec/check_mysql -H 192.168.1.152 -u nagios -d nagdb -p nagios
Uptime: 3374 Threads: 1 Questions: 11 Slow queries: 0 Opens: 33 Flush tables: 1 Open tables: 26 Queries per second avg: 0.003
備注:如果出現:/usr/local/nagios/libexec/check_mysql: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
請查看$MYSQL_HOME/lib
[root@node2 lib]# ln -s ./libmysqlclient.so.18.0.0 /usr/lib/libmysqlclient.so.18
配置nrpe.cfg
[root@node2 etc]# tail /usr/local/nagios/etc/nrpe.cfg
command[check_mysql]=/usr/local/nagios/libexec/check_mysql -H 192.168.1.152 -u nagios -d nagdb -p nagios //加入此行
配置nagios服務端
[root@node1 etc]# cat services.cfg //加入下面內容
define service{ use local-service host_name node2 service_description mysql check_command check_nrpe!check_mysql notifications_enabled 1 }
最後重啟nrpe和服務端的nagios。