生產環境開源監控系統整合 Nagios+Cacti+Nconf 公司要換監控系統了,以前采用的是Nagios+Centreon這兩款軟件的整合然后做出來的,但是Centreon的畫圖是在令人感覺慘不忍睹,而且統計出來的數據,也不是非常準確和人性化……所以,在研究了之后我們決定換監控系統,而最終的方案就是使用Naigos來捕獲數據和報警,使用Cacti來畫圖,使用Nconf來配置多臺主機,方便管理。 那么ok,先說一下我們的環境平臺。 監控機采用的是R410的服務器,8G內存和16核cpu,采用CentOS 5.5 x86_64的操作系統。好的,配置好網絡之后,我們開始! 注意:這里默認關閉了Selinux(生產環境也是關的,放心吧。)以及關閉了iptables。如有開啟的需要,請根據網絡酌情添加相關內容。 準備環境: 為以后的配置搭建安裝環境 yum install -y http* yum install -y mysql* yum install -y php-* yum install -y net-snmp* yum install -y httpd gcc glibc glibc-common gd gd-devel php php-gd ntp 一:Nagios環境的搭建 1.安裝nagios wget http://sourceforge.net/projects/nagios/files/nagios-3.x/nagios-3.3.1/nagios-3.3.1.tar.gz tar zxf nagios-3.3.1.tar.gz cd nagios ./configure --prefix=/var/www/html/nagios make all useradd nagios make install && make install-init && make install-commandmode && make install-config && make install-webconf 2.增加nagios登陸認證文件,一定要用默認的nagiosadmin作為用戶,否則需要修改其他文件。 htpasswd -c /var/www/html/nagios/etc/htpasswd.users nagiosadmin 3. 安裝插件: wget http://sourceforge.net/projects/nagiosplug/files/nagiosplug/1.4.15/nagios-plugins-1.4.15.tar.gz tar -zvxf nagios-plugins-1.4.15.tar.gz cd nagios-plugins-1.4.15 ./configure --prefix=/var/www/html/nagios/ make make install 4.將運行Apache的用戶添加到nagios組里,這樣Apache才有權限讀取文件 usermod -G nagios apache 5.階段測試 chown nagios.nagios /var/www/html/nagios/ -R service httpd restart chkconfig httpd on /etc/init.d/nagios start 二:cacti環境的搭建 1.rrdtool的安裝,這里需要安裝的是:rrdtool,rrdtool-devel.rrdtool-perl,rrdtool-php,我用的rrdtools的版本是1.4.4.1_x86_64。 | 在這里沒有提供rrdtool的工具,需要下載可以去這里下載上述的3個軟件包 rrdtool、rrdtool-devel的地址:http://packages.express.org/rrdtool/ rrdtool-php的地址:http://pkgs.org/download/rrdtool-php 注意,rrdtool的組件和附加組件版本盡量一致,而由于rrdtool-php這個包非常稀有,所以,盡量請以rrdtool-php的版本來決定rrdtool以及rrdtool-devel的版本。還有一定要對應環境,別安裝成了fedora的版本了=。= | 下載完成后,本地yum安裝 yum localinstall -y --nogpgcheck rrdtool-* service mysqld start 2.配置snmp vim /etc/snmp/snmpd.conf service snmpd restart 3.安裝cacti wget http://www.cacti.net/downloads/cacti-0.8.7h.tar.gz tar zxvf cacti-0.8.7h.tar.gz mv cacti-0.8.7h /var/www/html/cacti 4.創建cacti數據庫 mysql> create database cacti; mysql> grant all on cacti.* to 'cacti'@'localhost' identified by 'cacti'; mysql> flush privileges; 5.將cacti的表內容導入創建的數據庫 cd /var/www/html/cacti mysql -ucacti -pcacti cacti < /var/www/html/cacti/cacti.sql 6.分別編輯兩個.php文件,以適應環境,修改內容相同。 vim /var/www/html/cacti/include/config.php vim /var/www/html/cacti/include/global.php | $database_default = "cacti"; //默認數據庫名 $database_hostname = "localhost"; //主機名 $database_username = "cacti"; //登陸數據庫用戶名 $database_password = "cacti"; //登陸數據庫密碼 $database_port = "3306"; | useradd –r –M cacti chown –R cacti /var/www/html/cacti/rra/ chown –R cacti /var/www/html/cacti/log/ 7.在cacti用戶下創建計劃任務以畫圖 su cacti crontab –e */5 * * * * php /var/www/html/cacti/poller.php > /dev/null 2>&1 php /var/www/html/cacti/poller.php > /dev/null 2>&1 exit 8.從web頁面啟動cacti,安裝,并查看圖形化界面 三:整合Nagios與Cacti 1.下載并安裝ndoutils wget http://sourceforge.net/projects/nagios/files/ndoutils-1.x/ndoutils-1.4b9/ndoutils-1.4b9.tar.gz/download tar zxvf ndoutils-1.4b9.tar.gz cd ndoutils-1.4b9 ./configure --prefix=/var/www/html/nagios --enable-mysql --disable-pgsql --with-mysql-inc=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql make 2.準備配置文件 cp -v src/{ndomod-3x.o,ndo2db-3x,file2sock,log2ndo} /var/www/html/nagios/bin cd db ./installdb -ucacti -pcacti -hlocalhost -d cacti cd .. cp -v config/{ndo2db.cfg-sample,ndomod.cfg-sample} /var/www/html/nagios/etc mv /var/www/html/nagios/etc/ndo2db.cfg-sample /var/www/html/nagios/etc/ndo2db.cfg mv /var/www/html/nagios/etc/ndomod.cfg-sample /var/www/html/nagios/etc/ndomod.cfg chmod 644 /var/www/html/nagios/etc/ndo* chown nagios:nagios /var/www/html/nagios/etc/* chown nagios:nagios /var/www/html/nagios/bin/* 3.修改nagios.cfg配置文件以適應當前環境 vim /var/www/html/nagios/etc/nagios.cfg broker_module=/var/www/html/nagios/bin/ndomod-3x.o config_file=/var/www/html/nagios/etc/ndomod.cfg event_broker_options=-1 4.修改ndo2db.cfg以適應當前環境 vim /var/www/html/nagios/etc/ndo2db.cfg # 確保下列內容為唯一項 socket_type=tcp db_servertype=mysql db_host=localhost db_port=3306 db_name=cacti db_prefix=nagios_ db_user=cacti db_pass=cacti 5.修改ndomod.cfg以適應當前環境 vim /var/www/html/nagios/etc/ndomod.cfg output_type=tcpsocket output=127.0.0.1 6.為ndo2db添加啟動進程 cp ./daemon-init /etc/init.d/ndo2db vim /etc/init.d/ndo2db Ndo2dbBin=/var/www/html/nagios/bin/ndo2db-3x chmod +x /etc/init.d/ndo2db 7.啟動守護進程與nagios service ndo2db start tail -20 /var/log/messages service nagios start 四:安裝ncp,以在Cacti中展現Nagios 1.先安裝cacti擴展模塊 wget http://www.cacti.net/downloads/pia/cacti-plugin-0.8.7h-PA-v3.0.tar.gz tar xvf cacti-plugin-0.8.7h-PA-v3.0.tar.gz cp -R cacti-plugin-arch/* /var/www/html/cacti/ cd /var/www/html/cacti/ mysql -ucacti -pcacti cacti < pa.sql patch -p1 -N < cacti-plugin-0.8.7h-PA-v3.0.diff vim include/config.php # 修改(或新加入)為 $url_path = "/cacti/"; 從web進入cacti,啟用cacti plugin擴展 2.安裝npc tar zxvf npc-2.0.4.tar.gz mv npc /var/www/html/cacti/plugins/ vim /var/www/html/cacti/include/config.php $plugins[] = 'npc'; 3.安裝 npc支持:json wget http://pkgs.fedoraproject.org/repo/pkgs/php-pecl-json/json-1.2.1.tgz/d8904d2f004ceec85eeacf524cd25539/json-1.2.1.tgz tar zxvf json-1.2.1.tgz cd json-1.2.1 phpize ./configure make && make install php -i | grep php.ini 4.打開php.ini文件添加對json的支持 vim /etc/php.ini # 添加 extension=json.so 5.為apache添加php支持。 vim /etc/httpd/conf/httpd.conf AddType application/x-httpd-php .php .phtml DirectoryIndex index.html index.html.var index.php usermod -G cacti apache service httpd restart php -m 6.修改配置文件以讓npc讀取到新的數據。 vim /var/www/html/nagios/etc/ndo2db.cfg db_prefix=npc_ 6.1 從Web進入Cacti,確保正確使用npc選項。 6.2 修改mysql中的表結構: ./mysql -ucacti -p cacti mysql> use cacti;mysql> alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output; mysql> alter table npc_hostchecks add long_output TEXT NOT NULL default '' after output; mysql> alter table npc_hoststatus add long_output TEXT NOT NULL default '' after output; mysql> alter table npc_notifications add long_output TEXT NOT NULL default '' after output; mysql> alter table npc_servicechecks add long_output TEXT NOT NULL default '' after output; mysql> alter table npc_servicestatus add long_output TEXT NOT NULL default '' after output; mysql> alter table npc_statehistory add long_output TEXT NOT NULL default '' after output; mysql> alter table npc_systemcommands add long_output TEXT NOT NULL default '' after output; 7.重啟ndo2db進程,重新加載配置文件重啟nagios服務。 service ndo2db restart service nagios restart 8.在Web上配置NPC以使之正常讀取工作。 在Settings->npc中 ,勾上Remote Commands Nagios Command File Path= /var/www/html/nagios/var/rw/nagios.cmd Nagios URL=你的地址(http://ocalhost/nagios/) 接下來刷新npc就可以看到數據啦: 五:Nconf的安裝與配置 1.nconf的準備工作 mysql> create database nconf; mysql> grant all privileges on nconf.* to nconf@localhost identified by 'nconf'; mysql> flush privileges; mysql> quit 2.安裝nconf wget http://sourceforge.net/projects/nconf/files/nconf/1.2.6-0/nconf-1.2.6-0.tgz/download tar -zxf nconf-1.2.6-0.tgz -C /var/www/html/ cd /var/www/html/nconf chown -R apache.apache config/ temp/ static_cfg/ output/ 3.進入Web頁面在web頁面按照安裝提示一步步安裝 4.使用剛才創建的nconf數據庫和nconf數據庫用戶/密碼 5.成功連接mysql數據庫,之后一切保持默認,直到下面的,設置nconf管理員密碼,此時默認登錄用戶為admin 6.之后,網頁安裝完成。 rm -rf INSTALL INSTALL.php UPDATE UPDATE.php ln -s /var/www/html/nagios/bin/nagios bin/nagios chmod +x /var/www/html/nconf/bin/generate_config.pl chown -R apache.apache bin chmod +x /var/www/html/nagios/bin/nagios chmod +x /var/www/html/nconf/ADD-ONS/deploy_local.sh vim /var/www/html/nconf/ADD-ONS/deploy_local.sh 修改里面的Nagios路徑為 /var/www/html/nagios/etc/ 7.重新刷新頁面,輸入admin/您設置的密碼,進入nconf頁面 8.點擊Generate Nagios config嘗試生成配置文件,以測試是否與nagios成功關聯 9.最后的設置 ①在web頁面對監控服務和主機配置完畢,點擊Generate Nagios config ②deploy_local.sh是nconf自帶配置文件導入腳本 /var/www/html/nconf/ADD-ONS/deploy_local.sh 手動執行此腳本,將在/var/www/html/nagios/etc下生成兩目錄Default_collector與global ③修改nagios主配置文件 vim /var/www/html/nagios/etc/nagios.cfg cfg_dir=/var/www/html/nagios/etc/Default_collector cfg_dir=/var/www/html/nagios/etc/global service nagios reload 六:錯誤總結: 1.啟動ndo2db發現日志報錯: | ndomod: Could not open data sink! I'll keep trying, but some output may get lost... 解決辦法: 檢查 /nagios/etc目錄的權限是否足夠的大 確保/var/www/html/nagios/etc/nagios.cfg中有如下行出現,否則,請自行添加: event_broker_options=-1 檢查ndo2db.cfg中的配置,確保使用tcp連接 socket_type=tcp 檢查ndomod.cfg中的配置,確保使用tcp套接字 output_type=tcpsocket | 2.在npc安裝過程中進行表操作時報錯 | mysql> alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output; ERROR 1146 (42S02): Table 'cacti.npc_eventhandlers' doesn't exist 解決辦法:要先在cacti頁面下正確的啟用npc插件,正確的啟用~一個向上的箭頭和一個向右的箭頭都要開啟才行~ |
? 3.在應用nconf的時候報錯 | 點 nconf --> Generate Nagios config --> 報錯了。 Error accessing or executing Nagios / Icinga binary '/var/www/html/nconf/bin/nagios'. Cannot run the mandatory syntax check. 解決辦法: 檢查你的/var/www/html/nconf/bin/nagios這個文件所鏈接的那個原始文件的權限,確保是777或者775 |
? 4....當然還沒有發現錯誤,如果有,將會繼續補充,歡迎網友在安裝部署的過程中發現錯誤將錯誤積極交流,我將一一添加…… 七:最后的樣式 nagios定義服務 vim /var/www/html/nagios/etc/nagios.cfg 添加定義的東西,cfg_file=/var/www/html/nagios/etc/objects/115.cfg cd /var/www/html/nagios/etc/objects define host{ use linux-server ; Name of host template to use host_name ser-115 alias ser-115 address 192.168.8.115 } ? ############################################################################### ############################################################################### # # HOST GROUP DEFINITION # ############################################################################### ############################################################################### #define hostgroup{ # hostgroup_name linux-servers ; The name of the hostgroup # alias Linux Servers ; Long name of the group # } ? ############################################################################### ############################################################################### # ############################################################################### ############################################################################### # Define a service to "ping" the local machine define service{ host_name ser-115 service_description PING # Define a service to check the disk space of the root partition # on the local machine. Warning if < 20% free, critical if # < 10% free space on partition. define service{ host_name ser-115 service_description Root Partition ? # Define a service to check the number of currently logged in # users on the local machine. Warning if > 20 users, critical # if > 50 users. define service{ check_command check_local_users!20!50 } # Define a service to check the number of currently running procs # on the local machine. Warning if > 250 processes, critical if # > 400 users. define service{ check_command check_local_procs!250!400!RSZDT } ? # Define a service to check the load on the local machine. define service{ host_name ser-115 service_description Current Load ? # Define a service to check the swap usage the local machine. # Critical if less than 10% of swap is free, warning if less than 20% is free define service{ host_name ser-115 service_description Swap Usage check_command check_local_swap!20!10 } ? # Define a service to check SSH on the local machine. define service{ host_name ser-115 notifications_enabled 0 } normal_check_interval 1 use generic-service host_name ser-115 service_description check_8080port check_command check_tcp!8080 } define service{ normal_check_interval 1 use generic-service host_name ser-115 service_description check_7080port check_command check_tcp!7080 } # Define a service to check HTTP on the local machine. #define service{ # host_name localhost # service_description HTTP # check_command check_http # notifications_enabled 0 # } define service{ normal_check_interval 1 use generic-service host_name ser-115 service_description check_tomcat8080 check_command check_tomcat_8080 } define service{ normal_check_interval 1 use generic-service host_name ser-115 service_description check_tomcat7080 check_command check_tomcat_7080 } vim commands.cfg define command{ command_name check_tomcat_8080 command_line /var/www/html/nagios/libexec/check_http -I $HOSTADDRESS$ -p 8080 -u /f5.jsp -e 200 } define command{ command_name check_tomcat_7080 command_line /var/www/html/nagios/libexec/check_http -I $HOSTADDRESS$ -p 7080 -u /f5.jsp -e 200 } |