Nagios 監控實例部署

Nagios是一款企業級開源軟件,專注於監控服務器上服務是否正常,不生成圖形,提供報警機制,郵件或者短信發送監控狀態,它通過各種插件實現不同的功能。


Nagios 監控平臺主程序

Nagios-plugins 必選插件

NRPE 監控遠程服務器的主機資源

NSClient++ 用於監控Windows主機

NDOUtils 將數據寫入數據庫


一、安裝RHEL7.2

  最小化安裝,配置IP,時間同步,本地yum源,安裝vim(個人習慣)、bash-completion(命令補齊)

# hostnamectl set-hostname nagios_cacti

# yum install vim

# yum install bash-completion

# yum install chrony

# systemctl enable chronyd

# systemctl start chronyd

# vim /etc/chrony.conf

server 10.100.2.5 iburst //增加一行時間源

# yum install ntpdate

# ntpdate 10.100.2.5 //手動同步時間

配置CentOS 163 yum源

# yum install wget

# wget http://mirrors.163.com/centos/7.2.1511/os/x86_64/Packages/yum-3.4.3-132.el7.centos.0.1.noarch.rpm

# wget http://mirrors.163.com/centos/7.2.1511/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm

# wget http://mirrors.163.com/centos/7.2.1511/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-34.el7.noarch.rpm

# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

# rpm -qa|grep yum //檢查redhat是否安裝了yum,及有哪些Yum包

# rpm -qa|grep yum|xargs rpm -e --nodeps //刪除redhat自帶的yum包

# rpm -ivh yum-3.4.3-132.el7.centos.0.1.noarch.rpm yum-metadata-parser-1.1.4-10.el7.x86_64.rpm yum-plugin-fastestmirror-1.1.31-34.el7.noarch.rpm

# mv CentOS7-Base-163.repo /etc/yum.repos.d/

# vim /etc/yum.repos.d/CentOS7-Base-163.repo //通過":1,$s/$releasever/7/gc"和":1,$s/$basearch/x86_64/gc"查找和替換文件內容

yum clean all //清除yum緩存

yum makecache //重建緩存,以提高搜索軟件包速度

# yum update //更新系統(省略)


實例應用:

1 監控快速部署

監控需要安裝http php nagios nagios-plugins NRPE軟件包

yum install -y gd gd-devel openssl openssl-devel httpd php gcc glibc glib-common make wget

net-snmp

setenforce 0

iptables -F


安裝nagios 源碼包下載安裝

wget http://sourceforge.net/projects/nagios/files/nagios-3.x/nagios-3.5.0/nagios-3.5.0.tar.gz/download

groupadd nagios

useradd -g nagios nagios

tar -zxf nagios-3.5.0.tar.gz -C /usr/src/

cd /usr/src/nagios

./configure --with-nagios-user=nagios --with-nagios-group=nagios

make all

make install

make install-init #安裝啓動腳本

make install-commandmode #安裝與配置目錄權限

make install-config #安裝配置文件模板

make install-webconf #web監控界面配置


安裝nagios-plugins和nrpe

wget http://nchc.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/nagios-plugins-1.4.16.tar.gz

tar -zxf nagios-plugins-1.4.16.tar.gz -C /usr/src/

cd /usr/src/nagios-plugins-1.4.16

./configure --prefix=/usr/local/nagios/

make && make install

wget wget http://nchc.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz

tar -zxf nrpe-2.14.tar.gz -C /usr/src/

cd /usr/src/nrpe-2.14

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config


chown -R nagions.nagions /usr/local/nagios


創建賬戶信息


htpasswd -c /usr/local/nagions/etc/htpasswd.users tomcat


iptables -I INPUT -p tcp --dport 80 -j ACCEPT


service iptables save


啓動服務


service httpd start

/etc/init.d/nagios start

chkconfig httpd on

chkconfig --add nagios

chkconfig nagios on


2 修改配置文件

nagios的配置文件較多,主要位於/usr/local/nagios/etc 下

nagios.conf 主配置文件

nrpe.cfg 遠程監控配置文件

cgi.conf CGI配置文件

commands.cfg 命令定義文件

contacts.cfg 定義聯繫人文件

timepreriods.cfg 時間週期定義文件

tempaltes.cfg 對象定義參考模板

localhost.cfg 監控本機配置模板

printer.cfg 監控打印機模板

switch.cfg 監控交換模板

windows.cfg 監控Windows配置模板


很多配置文件無需修改可以直接使用

修改主配置文件nagios.cfg,主要是用cfg_file配置加載其他配置文件。


vim /usr/local/nagios/etc/nagios.cfg


cfg_file=/usr/local/nagios/etc/objects/commands.cfg

cfg_file=/usr/local/nagios/etc/objects/contacts.cfg

cfg_file=/usr/local/nagios/etc/objects/templates.cfg

cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg

cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

cfg_file=/usr/local/nagios/etc/web1.cfg

cfg_file=/usr/local/nagios/etc/web2.cfg


修改CGI配置文件cgi.cfg,添加tomcat賬戶進來


vim /usr/local/nagios/etc/cgi.cfg


default_user_name=tomcat

authorized_for_system_information=nagiosadmin,tomcat

authorized_for_configuration_information=nagiosadmin,tomcat

authorized_for_system_commands=nagiosadmin,tomcat

authorized_for_all_services=nagiosadmin,tomcat

authorized_for_all_hosts=nagiosadmin,tomcat

authorized_for_all_service_commands=nagiosadmin,tomcat

authorized_for_all_host_commands=nagiosadmin,tomcat


修改命令配置文件command.cfg,定義命令實現的方式,如郵件報警,使用工具,內容格式等。


vim /usr/local/nagios/etc/objects/commands.cfg


define command{

command_name check_nrpe

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -t 30 -c $ARG1$

}

define command{

command_name check_nrpe_args

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -t 30 -c $ARG1$ -a $ARG2$

}


修改聯繫人配置文件contacts.cfg 報警的聯繫人及聯繫方式

define contact{

contact_name nagiosadmin

use generic-contact

alias Nagios Admin

email [email protected]

}


修改報警時間週期timeperiods.cfg


vim /usr/local/nagios/etc/objects/timeperiods.cfg


define timeperiods{

timeperiod_name 24x7 #監控所有時間段(7*24小時)

alias 24 Hours A Day, 7 Days A Week

sunday 00:00-24:00

monday 00:00-24:00

tuesday 00:00-24:00

wednesday 00:00-24:00

thursday 00:00-24:00

friday 00:00-24:00

saturday 00:00-24:00

}


修改本機的配置localhost.cfg

define host{

use linux-server

host_name duangr-1

alias duangr-1

address 192.168.56.10

}


define service{

use local-service

host_name duangr-1

service_description Host Alive

check_command check-host-alive

}

define service{

use local-service

host_name duangr-1

service_description Users

check_command check_local_users!20!50

}

define service{

use local-service

host_name duangr-1

service_description CPU

check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0

}

define service{

use local-service

host_name duangr-1

service_description Disk Root

check_command check_local_disk!20%!10%!/

}

define service{

use local-service

host_name duangr-1

service_description Disk Home

check_command check_local_disk!20%!10%!/export/home

}

define service{

use local-service

host_name duangr-1

service_description Zombie Procs

check_command check_local_procs!5!10!Z

}

define service{

use local-service

host_name duangr-1

service_description Total Procs

check_command check_local_procs!250!400!RSZDT

}

define service{

use local-service

host_name duangr-1

service_description Swap Usage

check_command check_local_swap!20!10

}


修改模板文件templates.cfg

 

vi /usr/local/nagios/etc/objects/templates.cfg


#聯繫人模板generic-contact


define contact{

name generic-contact

service_notification_period 24x7

host_notification_period 24x7

service_notification_options w,u,c,r,f,s

host_notification_options d,u,r,f,s

service_notification_commands notify-service-by-email

host_notification_commands notify-host-by-email

register 0

}


#定義generic-host主機模板


define host{

name generic-host

notifications_enabled 1

event_handler_enabled 1

flap_detection_enabled 1

failure_prediction_enabled 1

process_perf_data 1

retain_status_information 1

retain_nonstatus_information 1

notification_period 24x7

register 0

}


#定義Linux主機模板


define host{

name linux-server

use generic-host

check_period 24x7

check_interval 5

retry_interval 1

max_check_attempts 10

check_command check-host-alive

notification_period workhours

notification_interval 120

notification_options d,u,r

contact_groups admins

register 0

}


創建遠程監控web1.cfg

vim /usr/local/nagios/etc/web1.cfg

define host{

use linux-server

host_name duangr-2

alias duangr-2

address 192.168.56.11

}


define service{

use local-service

host_name duangr-2

service_description Host Alive

check_command check-host-alive

}

define service{

use local-service

host_name duangr-2

service_description Users

check_command check_nrpe_args!check_users!5 10

}

define service{

use local-service

host_name duangr-2

service_description CPU

check_command check_nrpe_args!check_load!15,10,5 30,25,20

}

define service{

use local-service

host_name duangr-2

service_description Disk Root

check_command check_nrpe_args!check_disk!20% 10% /

}

define service{

use local-service

host_name duangr-2

service_description Disk /export/home

check_command check_nrpe_args!check_disk!20% 10% /export/home

}

define service{

use local-service

host_name duangr-2

service_description Procs Zombie

check_command check_nrpe_args!check_procs!5 10 Z

}

define service{

use local-service

host_name duangr-2

service_description Procs Total

check_command check_nrpe_args!check_procs_args!"-w400 -c600" }

define service{

use local-service

host_name duangr-2

service_description Swap Usage

check_command check_nrpe_args!check_swap!20% 10%

}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 下面是一些常用進程的監控,主要是雲平臺相關進程

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; 監控crond進程

define service{

use local-service

host_name duangr-2

service_description PS: crond

check_command check_nrpe_args!check_procs_args!"-c1:1 -Ccrond" }

;; 監控zookeeper進程

define service{

use local-service

host_name duangr-2

service_description PS: QuorumPeerMain

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.quorum.QuorumPeerMain" }

;;監控storm的從節點進程

define service{

use local-service

host_name duangr-2

service_description PS: supervisor

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -adaemon.supervisor" }

;; 監控storm的主節點進程

define service{

use local-service

host_name duangr-2

service_description PS: nimbus

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -adaemon.nimbus" }

;; 監控MetaQ進程

define service{

use local-service

host_name duangr-2

service_description PS: MetaQ

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -ametamorphosis-server-w" }

;; 監控Redis進程

define service{

use local-service

host_name duangr-2

service_description PS: redis-server

check_command check_nrpe_args!check_procs_args!"-c1:1 -Credis-server" }

;; 監控hadoop主節點NameNode進程

define service{

use local-service

host_name duangr-2

service_description PS: NameNode 

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.namenode.NameNode" }

;; 監控hadoop主節點SecondaryNameNode進程

define service{

use local-service

host_name duangr-2

service_description PS: SecondaryNameNode

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.namenode.SecondaryNameNode" }

;; 監控hadoop主節點ResourceManager進程

define service{

use local-service

host_name duangr-2

service_description PS: ResourceManager

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.resourcemanager.ResourceManager" }

;; 監控hadoop從節點DataNode進程

define service{

use local-service

host_name duangr-2

service_description PS: DataNode

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.datanode.DataNode" }

;;監控hadoop從節點NodeManager進程

define service{

use local-service

host_name duangr-2

service_description PS: NodeManager

check_command check_nrpe_args!check_procs_args!"-c1:1 -Cjava -aserver.nodemanager.NodeManager" }


由於duangr-2是遠程主機,因此使用check_nrpe_args命令來監控.


/etc/init.d/nagios restart


快速定位配置文件問題所在命令


/usr/local/nagios/bin/nagios -V /usr/local/nagios/etc/nagios.cfg


被監控機安裝軟件 nagios-plugin nrpe


yum install -y openssl openssl-devel


groupadd nagios

useradd -g nagios -s /sbin/nologin nagios

tar -zxf nagios-plugins-2.1.6.tar.gz -C /usr/src/

cd /usr/src/nagios-plugins-2.1.6

./configure --prefix=/usr/local/nagios/ --with-nagios-user=nagios --with-nagios-group=nagios

make && make install

 

tar -zxf nrpe-2.14.tar.gz -C /usr/src/

cd /usr/src/nrpe-2.14

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config


修改客戶端的NRPE配置文件

command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

command[check_sda2]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2

command[check_swap]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/shm

command[check_home]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/mapper/VolGroup00-LogVol00

command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z

command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 200 -c 300

command[check_ping81]=/usr/local/nagios/libexec/check_ping -H 10.155.0.1 -w 100.0,20% -c 500.0,60%#

command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1


/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local

netstat -lnupt |grep 5666

iptables -I INPUT -p tcp --dport 5666 -j ACCEPT

service iptables save


 


檢查監控命令配置是否ok

/usr/local/nagios/libexec/check_nrpe -H localhost -c check_users -a 5 10

/usr/local/nagios/libexec/check_nrpe -H localhost -c check_load -a 15,10,5 30,25,20

/usr/local/nagios/libexec/check_nrpe -H localhost -c check_disk -a 20% 10% /

/usr/local/nagios/libexec/check_nrpe -H localhost -c check_procs -a 200 400 RSZDT

/usr/local/nagios/libexec/check_nrpe -H localhost -c check_swap -a 20% 10%


沒有問題就可以用瀏覽器訪問nagios了


二、安裝Nagios


1、下載軟件包並安裝Nagios

Nagios-4.2.1:

http://nchc.dl.sourceforge.net/project/nagios/nagios-4.x/nagios-4.2.1/nagios-4.2.1.tar.gz

Nagios-plugins-2.1.3:

https://nagios-plugins.org/download/nagios-plugins-2.1.3.tar.gz

NRPE-3.0.1:

https://codeload.github.com/NagiosEnterprises/nrpe/tar.gz/3.0.1

官方安裝文檔:Nagios QuickstartInstallation Guides

https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/quickstart.html

# yum install httpd php gcc glibc glibc-common gd gd-devel

# yum install unzip //編譯所需,否則會報錯。

# useradd -M -s /sbin/nologin nagios

# usermod -aG nagios apache

# tar zxvf nagios-4.2.1.tar.gz

# cd nagios-4.2.1/

# make all

# make install

# make install-init

# make install-config

# make install-commandmode

# make install-webconf

# vim /usr/local/nagios/etc/objects/contacts.cfg

email [email protected] //修改nagios警告信息的郵件地址

# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios //配置登錄賬號和密碼


2、安裝nagios-plugins插件

# tar zxvfnagios-plugins-2.1.3.tar.gz

# cd nagios-plugins-2.1.3/

# ./configure --with-nagios-user=nagios --with-nagios-group=nagios

# make

# make install

# chown -R nagios.nagios/usr/local/nagios/

# systemctl enable httpd

# systemctl start httpd

# systemctl enable nagios

# systemctl start nagios

# /etc/init.d/nagios checkconfig //檢查nagios配置文件是否有錯誤,或使用以下命令檢查:

# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

# firewall-cmd --zone=public --add-service=http –permanent

# firewall-cmd –reload

# systemctl restart firewalld

使用http://10.100.2.158/nagios登錄控制檯,輸入配置的賬號密碼即可登錄。

wKiom1fo3t6jkVoQAAHA1QTH65w052.jpg

注:如果web管理員不是使用默認的nagiosadmin,需要修改cgi.cfg

# vim /usr/local/nagios/etc/cgi.cfg

//把所有的nagiosadmin改爲自定義的用戶名,否則查看Services時會提示權限不夠。

wKiom1fo4TqjdDWYAACuiYSp7tE237.jpg

默認HTTP會有告警信息,解決辦法:在/var/www/html目錄新建一個空白index.html文件即可。

# touch /var/www/html/index.html

重啓nagios和httpd服務,等待幾分鐘即恢復正常。


3、安裝NRPE插件

# tar zxvf nrpe-3.0.1.tar.gz

# cd nrpe-3.0.1/

# yum install openssl-devel //解決checking for SSL headers... configure: error: Cannotfind ssl headers錯誤問題

# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl

# make all //編譯和安裝nrpe

# make install-plugin

# make install-daemon

# make install-config //注:nrpe3.0以下請使用# make install-daemon-config

# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d //啓動nrpe服務

# yum install net-tools

# netstat –tnpl //可以看到5666端口已處於監聽狀態,說明nrpe服務已啓動

# echo 「/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg –d」 >> /etc/rc.local

# chmod +x /etc/rc.d/rc.local //設置開機自啓動,手動重啓方法如下:

# pkill nrpe && /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

vim /usr/local/nagios/etc/objects/commands.cfg //末尾增加以下內容

define command{

command_name check_nrpe

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

//允許check_nrpe命令定義nagios服務,-c後面帶的$ARG1$參數是傳給nrpe daemon執行的檢測命令,它必須是nrpe.cfg中所定義的命令。

//自定義的Servers下的cfg配置文件中使用check_nrpe的時候要用」!」帶上這個參數。

//可通過# /usr/local/nagios/libexec/check_nrpe –h查看插件的命令參數。

# mkdir /usr/local/nagios/etc/servers //創建servers監控配置文件集中存儲目錄

# vim /usr/local/nagios/etc/nagios.cfg //修改配置文件

cfg_dir=/usr/local/nagios/etc/servers //啓用此規則,即默認讀取處理此目錄下的配置文件


4、添加客戶端(Client被監控端)

1>、客戶端安裝NRPE和插件nagios-plugins

下載所需軟件包

nagios-plugins-2.1.3.tar.gz

nrpe-3.0.1.tar.gz

新建用戶

# useradd –M –s /sbin/nologinnagios

先安裝nagios-plugins(NRPE依賴於nagios-plugins)

# tar zxvf nagios-plugins-2.1.3.tar.gz

# cd nagios-plugins-2.1.3

# ./configure--with-nagios-user=nagios --with-nagios-group=nagios

# make all

# make install

再安裝NRPE

# yum install openssl-devel

# tar zxvf nrpe-3.0.1.tar.gz

# cd nrpe-3.0.1

# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl

# make all

# make install-plugin

# make install-daemon

# make install-config

# ls /usr/local/nagios/libexec/ //查看安裝成功的NRPE插件,有check_nrpe說明安裝成功

# vim /usr/local/nagios/etc/nrpe.cfg //配置nrpe

allowed_hosts=127.0.0.1,10.100.2.158 //添加服務端IP

dont_blame_nrpe=1 //把0改爲1,允許命令參數


#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d //啓動nrpe服務


爲了便於NRPE服務的啓動,可以定義一個/etc/init.d/nrpe腳本

# vim /etc/init.d/nrpe //輸入以下內容:

#!/bin/bash

# chkconfig: 2345 88 12

# description: NRPE DAEMON

NRPE=/usr/local/nagios/bin/nrpe

NRPECONF=/usr/local/nagios/etc/nrpe.cfg

case "$1" in

start)

echo -n "Starting NRPE daemon..."

$NRPE -c $NRPECONF -d

echo " done."

;;

stop)

echo -n "Stopping NRPE daemon..."

pkill -u nagios nrpe

echo " done."

;;

restart)

$0 stop

sleep 2

$0 start

;;

*)

echo "Usage: $0start|stop|restart"

;;

esac

exit 0

# chmod a+x /etc/init.d/nrpe //賦予腳本執行權限,即可以通過systemctl或service執行啓動,停止了。

# service nrpe start //啓動nrpe

#echo 「/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d」 >> /etc/rc.local

或# chkconfig nrpe on //設置爲開機自啓動

# netstat –tnlp //查看5666端口是否成功啓動


測試監控主機和被監控設備之間的連通性(Server上):


#/usr/local/nagios/libexec/check_nrpe -H 10.100.2.200


NRPE v3.0.1 //通信成功


2>、Server監控端創建Client被監控端配置文件


vim /usr/local/nagios/etc/servers/test.cfg //監控主機上新建Client端配置文件

define host{

use linux-server

host_name commission

alias commission

address 10.100.2.200

max_check_attempts 5

check_period 24x7

notification_interval 30

notification_period 24x7

}

define service{

use generic-service

host_name commission

service_description PING

check_command check_ping!100.0,20%!500.0,60%

}

define service{

use generic-service

host_name commission

service_description SSH

check_command check_ssh

notifications_enabled 0 ;disable notification

}

define service{

use generic-service

host_name commission

service_description CPU

check_command check_nrpe!check_cpu

notifications_enabled 1

}

define service{

use generic-service

host_name commission

service_description Physical Memory

check_command check_nrpe!check_mem

notifications_enabled 1

}

//可以以templates.cfg模板進行修改

關於check_cpu和check_mem自定義插件的使用方法(插件見附件):


2.1從官網下載需要的插件,注意修改+x執行權限和屬性


2.2修改Client端配置:修改nrpe.cfg,增加以下內容


command[check_mem]=/usr/local/nagios/libexec/check_mem -w 10 -c 5

command[check_cpu]=/usr/local/nagios/libexec/check_cpu -w 80 -c 90


2.3重啓nrpe服務


pkill nrpe&&/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d


2.4修改Server端配置:修改test.cfg,在define service中定義check_command

check_command check_nrpe!check_mem

check_command check_nrpe!check_cpu


3>、利用NSClicent++監控遠程Windows系統

下載插件包NSCP-0.4.4.19-x64.msi

在Windows客戶端安裝插件包:

wKiom1fuASiw0zYTAAHibsedLsU348.png

查看服務是否啓動,勾選登錄中的允許服務與桌面交互

wKiom1fuAYGwrzdoAANNKghjEpM605.png

安裝完成查看啓動的端口,5666nrpe12489NSClient++

wKioL1fuAZiBKLcoAACLVIDWWhk865.jpg

在監控主機的commands.cfg配置文件中修改以下部分,添加-s 密碼:

vim /usr/local/nagios/etc/objects/commands.cfg

# 'check_nt' command definition

define command{

command_name check_nt

command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -s 123456 -v $ARG1$ $ARG2$

}

添加監控客戶端:

vim /usr/local/nagios/etc/nagios.cfg

cfg_file=/usr/local/nagios/etc/objects/windows.cfg //啓用windows監控,如果有添加啓用cfg_dir=/usr/local/nagios/etc/servers目錄則需要註釋掉windows.cfg,否則會有衝突

以windows.cfg爲模板,添加新的windows服務器

# cp /usr/local/nagios/etc/objects/windows.cfg /usr/local/nagios/etc/servers/wintest.cfg

//修改配置中的host_name,IP地址等。

# /usr/local/nagios/libexec/check_nt -H 10.100.2.189 -p 12489 -s 123456 -v UPTIME


//測試客戶端連通性(注意有特殊符號需要單引號),以下信息表示連接正常。


System Uptime - 20 day(s) 4 hour(s)11 minute(s) |uptime=29051

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //測試配置

systemctl restart nagios //重啓nagios服務


默認check_nt!MEMUSE!-w 80 –c 90監控的是物理內存和虛擬內存的總和,單獨監控物理內存方法:

1) 修改Client的nsclient.ini文件三個選項:

[/settings/NRPE/server]下的insecure = true、verify mode = none、allow arguments = true

修改完成後,通過# /usr/local/nagios/libexec/check_nrpe -H 10.100.2.189測試連通性,

I (0.4.4.19 2015-12-08) seem to bedoing fine...表示連接正常,如果提示

CHECK_NRPE: Error - Couldnot complete SSL handshake.則表示未修改正確。

查看監控顯示結果:

# /usr/local/nagios/libexec/check_nrpe -H 10.100.2.189 -p 5666 -c CheckMEM -a MaxWarn=80% MaxCrit=90% type=physicalShowAll

2) 修改Server的commands.cfg文件,定義物理內存監控服務

define command{

command_name check_winmem

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -p 5666-c CheckMEM -a MaxWarn=$ARG1$% MaxCrit=$ARG2$% ShowAll=long type=physical

}

3) 修改Server的客戶端配置文件xenapp.cfg,定義監控內容

define service{

use generic-service

host_name xenapp

service_description PhysicalMemory

check_command check_winmem!80!90

}

4) 檢測配置文件是否有錯誤:# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

5) 重啓nagios服務:# systemctl restart nagios


三、安裝Cacti

1、下載軟件包並安裝

Cacti-0.8.8h:

http://www.cacti.net/downloads/cacti-0.8.8h.tar.gz

Cacti-spine-0.8.8h:

http://www.cacti.net/downloads/spine/cacti-spine-0.8.8h.tar.gz

官方安裝手冊:

http://docs.cacti.net/manual:088:1_installation.1_install_unix

配置安裝環境:

#yum install httpd php php-mysql php-snmp php-xml mariadb mariadb-server

2、安裝RRDtool工具

# yum install rrdtool

# rrdtool -h

3、安裝SNMP服務

# yum install net-snmp net-snmp-utils

# systemctl enable snmpd

# systemctl start snmpd

4、安裝cacti-spine(高效採集器)

# yum install net-snmp-devel mariadb-devel openssl-devel

# yum install autoconf automake binutils dos2unix gcc cpplibtool glibc-devel glibc-headers kernel-headers

# yum install wget patch

# tar zxvf cacti-spine-0.8.8h.tar.gz

# cd cacti-spine-0.8.8h/

# aclocal

# libtoolize –force

# autoheader

# autoconf

# automake

# ./configure

# make

# make install

# cp /usr/local/spine/bin/spine /usr/bin/spine

# cp /usr/local/spine/etc/spine.conf.dist /etc/spine.conf

# chown nagios.nagios /etc/spine.conf

# vim /etc/spine.conf

DB_Host localhost

DB_Database cacti

DB_User cactiuser

DB_Pass 123456

DB_Port 3306

# /usr/bin/spine //執行檢查是否有錯,安裝完cacti後再執行

5、創建cacti數據庫

啓動數據庫:

# systemctl enable mariadb

# systemctl start mariadb

# mysqladmin -uroot password 'rootpasswd' //Mariadb默認密碼爲空,先設置密碼

# mysql -uroot -p //使用root權限賬號登錄

create database cacti; //創建數據庫

grant all on cacti.* to [email protected]'localhost' identified by '123456'; //授於本地登錄權限

6、安裝cacti程序

# tar zxvf cacti-0.8.8h.tar.gz

# mv cacti-0.8.8h /var/www/html/cacti

# mysql -u cactiuser -p cacti</var/www/html/cacti/cacti.sql //導入數據庫

# chmod -R 777 /var/www/html/cacti/rra //授於rra和log文件夾777權限

# chmod -R 777 /var/www/html/cacti/log

# /usr/bin/spine //顯示以下內容表示連接正常

SPINE:Using spine config file [/etc/spine.conf]

SPINE:Version 0.8.8h starting

SPINE:Time: 0.0455 s, Threads: 5, Hosts: 2

7、修改cacti全局配置文件

# vim /var/www/html/cacti/include/config.php

修改默認數據庫名及連接數據庫的用戶名和密碼

# mv cacti-0.8.8h /var/www/html/cacti

# mysql -u cactiuser -p cacti</var/www/html/cacti/cacti.sql //導入數據庫

# chmod -R 777 /var/www/html/cacti/rra //授於rra和log文件夾777權限

# chmod -R 777 /var/www/html/cacti/log

# /usr/bin/spine //顯示以下內容表示連接正常

SPINE:Using spine config file [/etc/spine.conf]

SPINE:Version 0.8.8h starting

SPINE:Time: 0.0455 s, Threads: 5, Hosts: 2

7、修改cacti全局配置文件

# vim /var/www/html/cacti/include/config.php

修改默認數據庫名及連接數據庫的用戶名和密碼

$database_default= "cacti";

$database_username= "cactiuser";

$database_password= "123456";

修改cacti系統時區,否則php會有告警日誌信息

# vim /var/www/html/cacti/include/global.php //增加一行

date_default_timezone_set('Asia/Shanghai');

8、添加RRDtool抓圖任務計劃

# crontab -e

輸入以下任務計劃:

*/5 * * ** /usr/bin/php /var/www/html/cacti/poller.php >> /tmp/cacti_rrdtool.log2>&1

9、配置SELinux

測試php模塊是否正常,http://10.100.2.158/phpinfo.php

#vim phpinfo.php //在html首頁目錄下

<?php phpinfo();?> //測試完成後刪除文件

-------------------------------------------------------------------------------------------

測試Mysql數據庫的連接是否正常,http://10.100.2.158/mysqltest.php

#vim mysqltest.php //html首頁目錄下,名稱隨意起

<?php $link=mysql_connect(‘127.0.0.1’,’cactiuser’,’123456’);

If($link) echo 「connect success!」;

else echo 「connect fail!」;?>

測試數據庫連接性前,需要修改sebool值:在SELinux啓用情況下,php連接mysql測試會失敗


#getsebool -a |grep httpd_can_network_connect //查看httpd進程連接模式,默認爲off


#setsebool -P httpd_can_network_connect=1 //啓用連接後即可測試正常

------------------------------------------------------------------------------------------

配置SELinux上下文,否則訪問cacti時會提示禁止訪問:

配置SELinux上下文,否則訪問cacti時會提示禁止訪問:

yum install policycoreutils-python //安裝semanage工具,默認未安裝

# ls -Zd cacti/ //查看當前cacti目錄的上下文,爲admin_home_t

# semanage fcontext -a -t httpd_sys_content_t '/var/www/html/cacti(/.*)?' //定義cacti目錄的上下文規則

# restorecon -RFvv cacti/ //更改cacti目錄的上下文

修改完成後重啓httpd


vim /usr/local/nagios/etc/objects/commands.cfg

# 'check_nt' command definition

define command{

command_name check_nt

command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -s 123456 -v $ARG1$ $ARG2$

}

添加監控客戶端:

vim /usr/local/nagios/etc/nagios.cfg

cfg_file=/usr/local/nagios/etc/objects/windows.cfg //啓用windows監控,如果有添加啓用cfg_dir=/usr/local/nagios/etc/servers目錄則需要註釋掉windows.cfg,否則會有衝突

以windows.cfg爲模板,添加新的windows服務器

# cp /usr/local/nagios/etc/objects/windows.cfg /usr/local/nagios/etc/servers/wintest.cfg

//修改配置中的host_name,IP地址等。

# /usr/local/nagios/libexec/check_nt -H 10.100.2.189 -p 12489 -s 123456 -v UPTIME

//測試客戶端連通性(注意有特殊符號需要單引號),以下信息表示連接正常。

System Uptime - 20 day(s) 4 hour(s)11 minute(s) |uptime=29051

# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //測試配置

# systemctl restart nagios //重啓nagios服務


默認check_nt!MEMUSE!-w 80 –c 90監控的是物理內存和虛擬內存的總和,單獨監控物理內存方法:

1) 修改Client的nsclient.ini文件三個選項:

[/settings/NRPE/server]下的insecure = true、verify mode = none、allow arguments = true

修改完成後,通過# /usr/local/nagios/libexec/check_nrpe -H 10.100.2.189測試連通性,

I (0.4.4.19 2015-12-08) seem to bedoing fine...表示連接正常,如果提示

CHECK_NRPE: Error - Couldnot complete SSL handshake.則表示未修改正確。

查看監控顯示結果:

# /usr/local/nagios/libexec/check_nrpe -, sans-serif;font-size:16px;color:rgb(51,51,51);">command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -s 123456 -v $ARG1$ $ARG2$

}

添加監控客戶端:

vim /usr/local/nagios/etc/nagios.cfg

相關文章
相關標籤/搜索