一、nagios簡(jiǎn)介
1、一款開(kāi)源的電腦系統(tǒng)和網(wǎng)絡(luò)監(jiān)視工具,可以有效監(jiān)控windows、Linux和Unix的主機(jī)狀態(tài)
,交換機(jī)、路由器等網(wǎng)絡(luò)設(shè)置。在系統(tǒng)或服務(wù)狀態(tài)異常時(shí)發(fā)出郵件或者短信報(bào)警,第一
時(shí)間通知支離維人員,在狀態(tài)恢復(fù)后發(fā)出正常的郵件或者短信通知。
2、主要功能
網(wǎng)絡(luò)服務(wù)監(jiān)控(SMTP、POP3、HTTP、NNTP、ICMP、SNMP、FTP、SSH)
主機(jī)資源監(jiān)控(CPU load、disk usage、system logs),也包括Windows主機(jī)(使用NSClient++ plugin)
可以指定自己編寫(xiě)的Plugin通過(guò)網(wǎng)絡(luò)收集數(shù)據(jù)來(lái)監(jiān)控任何情況
可以通過(guò)配置Nagios遠(yuǎn)程執(zhí)行插件遠(yuǎn)程執(zhí)行腳本
遠(yuǎn)程監(jiān)控支持SSH或SSL加通道方式進(jìn)行監(jiān)控
簡(jiǎn)單的plugin設(shè)計(jì)允許用戶(hù)很容易的開(kāi)發(fā)自己需要的檢查服務(wù),支持很多開(kāi)發(fā)語(yǔ)言
包含很多圖形化數(shù)據(jù)Plugins(Nagiosgraph、Nagiosgrapher、PNP4Nagios等)
可并行服務(wù)檢查
能夠定義網(wǎng)絡(luò)主機(jī)的層次,允許逐級(jí)檢查,就是從父主機(jī)開(kāi)始向下檢查
當(dāng)服務(wù)或主機(jī)出現(xiàn)問(wèn)題時(shí)發(fā)出通告,可通過(guò)email, pager, sms 或任意用戶(hù)自定義的plugin進(jìn)行通知
能夠自定義事件處理機(jī)制重新激活出問(wèn)題的服務(wù)或主機(jī)
自動(dòng)日志循環(huán)
支持冗余監(jiān)控
包括Web界面可以查看當(dāng)前網(wǎng)絡(luò)狀態(tài),通知,問(wèn)題歷史,日志文件等
二、nagios工作原理
1、Nagios的功能是監(jiān)控服務(wù)和主機(jī),但是他自身并不包括這部分功能,所有的監(jiān)控、檢測(cè)功能都是通過(guò)各種插件來(lái)完成的。
2、Nagios4種狀態(tài)返回信息
0(OK)表示狀態(tài)正常/綠色
1(WARNING)表示出現(xiàn)警告/***
2(CRITICAL)表示出現(xiàn)非常嚴(yán)重的錯(cuò)誤/紅色
3(UNKNOWN)表示未知錯(cuò)誤/深***
3、Nagios 通過(guò)NRPE 來(lái)遠(yuǎn)端管理服務(wù)
Nagios 執(zhí)行安裝在它里面的check_nrpe 插件,并告訴check_nrpe 去檢測(cè)哪些服務(wù)。
通過(guò)SSL,check_nrpe 連接遠(yuǎn)端機(jī)子上的NRPE daemon
NRPE 運(yùn)行本地的各種插件去檢測(cè)本地的服務(wù)和狀態(tài)(check_disk)
最后,NRPE 把檢測(cè)的結(jié)果傳給主機(jī)端的check_nrpe,check_nrpe 再把結(jié)果送到Nagios狀態(tài)隊(duì)列中。
Nagios 依次讀取隊(duì)列中的信息,再把結(jié)果顯示出來(lái)。
三、nagios安裝(CentOS7)
1、lamp環(huán)境搭建
(1)源碼安裝apr-1.5.2和apr-util-1.5.4
cd apr-1.5.2
./configure && make && make install
cd apr-util-1.5.4
./configure --with-apr=/usr/local/apr/ && make && make install
成都創(chuàng)新互聯(lián)公司認(rèn)為,企業(yè)網(wǎng)站是要賦予品質(zhì)、思維、人性,深入到用戶(hù)內(nèi)心的細(xì)膩情感,才能真正稱(chēng)得上企業(yè)網(wǎng)站。成都創(chuàng)新互聯(lián)公司根據(jù)每位用戶(hù)內(nèi)心最深的需求網(wǎng)站建設(shè)服務(wù),堅(jiān)實(shí)的設(shè)計(jì)執(zhí)行是品牌長(zhǎng)期視覺(jué)塑造的重要支持。
(2)安裝openssl-devel
#mod_ssl has been requested
yum -y install openssl-devel
(3)源碼安裝httpd-2.4.17
#./configure --prefix=/usr/local/apache2 --sysconfdir=/etc/httpd/ --enable-so --enable-ssl=static --with-ssl=/usr/local/ssl/ --enable-mods-shared=all --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr/ --enable-cgi --enable-suexec
./configure --prefix=/usr/local/apache2 --sysconfdir=/etc/httpd/ --enable-so --enable-ssl=static --with-ssl=/usr/local/ssl/ --enable-mods-shared=all --with-apr=/usr/local/apr/ --with-apr-util=/usr/local/apr/ --enable-cgi
make && make install
(4)源碼安裝MySQL-5.6.27-linux-glibc2.5-x86_64
#FATAL ERROR: please install the following Perl modules before executing scripts/mysql_install_db:Data::Dumper
yum -y install perl-Data-Dumper
tar zxvf mysql-5.6.27-linux-glibc2.5-x86_6.tar.gz -C /uar/local/
cd /usr/local
ln -s mysql-5.6.27-linux-glibc2.5-x86_64/ mysql
cd mysql
#添加mysql用戶(hù)和群組
groupadd mysql
useradd -r -g mysql mysql
#修改相關(guān)目錄和文件屬主和所屬群組
chown -R mysql .
chgrp -R mysql .
或者:chown -R mysql:mysql .
#將目錄屬主更改為root,避免mysql用戶(hù)將目錄刪除
chown -R root .
chown -R mysql data
#使用mysql用戶(hù)權(quán)限來(lái)初始化數(shù)據(jù)庫(kù)
scripts/mysql_install_db --user=mysql
#將mysql添加為服務(wù)
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
#將mysql的bin目錄加入到PATH環(huán)境變量
#在/etc/profile最后面添加
echo "PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
source /etc/profile
#腳本初始化已經(jīng)添加mysqld腳本,如果沒(méi)有直接cp /etc/init.d/mysqld
/etc/rc.d/init.d/mysqld
#啟動(dòng)mysql
mkdir -p /var/run/mysqld/
chown -R mysql:mysql /var/run/mysqld/
#/var/log/mariadb/mariadb.log No such file or directory
mkdir -p /var/log/mariadb/ /var/run/mariadb/
touch /var/log/mariadb/mariadb.log
chown root:mysql /var/log/mariadb/mariadb.log /var/run/mariadb/
chmod -R 774 /var/log/mariadb/mariadb.log /var/run/mariadb/
/usr/local/mysql/bin/mysqld_safe &
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
#在/etc/my.cnf 文件socket參數(shù)指定為/tmp/mysql.sock
echo "socket=/tmp/mysql.sock">>/etc/my.cnf
#修改root密碼為love
#mysqladmin -u root -p password love --socket=/var/lib/mysql/mysql.sock
mysqladmin -u root -p password love
(4)源碼安裝php-5.6.15
#apxs是在安裝php時(shí)生成動(dòng)態(tài)連接模塊工具,如果不指明路徑,apache就無(wú)法調(diào)用php
#一定要添加--with-apxs2=/usr/local/apache2/bin/apxs
#php安裝完成后,可以看到會(huì)在httpd.conf文件添加"LoadModule php5_module modules/libphp5.so"
#安裝依賴(lài)包libxml2-devel
#error: xml2-config not found. Please check your libxml2 installation
yum -y install libxml2-devel
#libzip:not found
yum -y install libzip-devel
#動(dòng)態(tài)加載php來(lái)完成解析
#./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql=/usr/local/mysql/ --with-apxs2=/usr/local/apache2/bin/apxs
./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql=/usr/local/mysql/ --with-apxs2=/usr/local/apache2/bin/apxs --enable-mbstring --with-libzip --enable-zip
make && make install
cp php.ini-development /etc/php.ini
#不需要進(jìn)行手動(dòng)更改就可以讓apache調(diào)用php
#php在編譯的時(shí)候會(huì)修改httpd.conf配置文件,添加如下一行
LoadModule php5_module modules/libphp5.so
#如果在編譯的時(shí)候沒(méi)有指定--with-apxs2=/usr/local/apache2/bin/apxs,則apache無(wú)法加載php
(5)lamp連接測(cè)試
#設(shè)置service方式啟動(dòng)apache
#將啟動(dòng)腳本復(fù)制為/etc/init.d/httpd
cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd
#可以這樣來(lái)停止、啟動(dòng)和重啟apache
service httpd stop/start/restart
/etc/init.d/httpd stop/start/restart
/usr/local/apache2/bin/apachectl stop/start/restart
#或許是因?yàn)榧嫒菪圆缓?,無(wú)法使用chkconfig設(shè)置開(kāi)機(jī)啟動(dòng)
chkconfig –add httpd
#但是可以修改/etc/rc.local設(shè)置apache開(kāi)機(jī)啟動(dòng)
#在/etc/rc.local添加
/usr/local/apache2/bin/apachectl start
#修改httpd.conf:
#如果使用yum安裝apache,配置文件存放在
/etc/httpd/conf/httpd.conf
#DocumentRoot為
/var/www/html
#如果是使用源碼方法安裝,配置文件存放在
/usr/local/apache2/conf/httpd.conf
#DocumentRoot為
/usr/local/apache2/htdocs
#在
AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps
#修改ServerName
ServerName 192.168.137.40:80
#在htdocs目錄添加文件index.php,內(nèi)容如下
#讀取mysql數(shù)據(jù)庫(kù)中的數(shù)據(jù)表user內(nèi)容
$con=mysql_connect("localhost","root","love");
$sql="select user,password from user";
$db_select=mysql_select_db("mysql",$con);
$result=mysql_query($sql,$con);
while ($row=mysql_fetch_array($result))
{
echo $row["user"]." ".$row["password"];
echo "
";
}
mysql_close($con);
?>
#從mysql數(shù)據(jù)庫(kù)中讀取數(shù)據(jù)表user并顯示
#加載cgi解析
#在"LoadModule php5_module modules/libphp5.so"后面添加
LoadModule cgi_module modules/mod_cgi.so
2、安裝nagios-4.1.1
cd nagios-4.1.1
useradd nagios -M -s /sbin/nologin
#unzip: command not found
yum -y install unzip
./configure --prefix=/usr/local/nagios --with-nagios-group=nagios
make install && make all && make install-init && make install-commandmode && make install-config
cp sample-config/httpd.conf /etc/httpd/nagios.conf
#make install-exfoliation,因找不到目錄而失敗
#cp: target /usr/local/nagios/share/stylesheetsis not a directory
mkdir -p /usr/local/nagios/share/stylesheetsis
cp -rf contrib/exfoliation/stylesheets/* /usr/local/nagios/share/stylesheetsis/
#make install-classicui
#cp: target usr/local/nagios/share/stylesheetsis not a directory
cp -rf html/stylesheets/* /usr/local/nagios/share/stylesheets
#安裝插件
cd nagios-plugins-2.1.1
./configure --with-gnutls --enable-perl-modules --enable-libtap --with-openssl --with-gnutls && make && make install
#Can't locate ExtUtils/MakeMaker.pm
yum -y install perl-CPAN
cpan -i ExtUtils::MakeMaker
#或者
yum -y install perl-ExtUtils-MakeMaker.noarch
#添加nagios管理用戶(hù)
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
#檢查配置文件
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#下面的是因?yàn)閚agios沒(méi)有啟動(dòng),啟動(dòng)nagios即可
#Error: Could not read object configuration data!
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
#打開(kāi)瀏覽器訪(fǎng)問(wèn),IP為實(shí)際IP或者域名,如果是域名一定要能夠解析
http://192.168.137.222/nagios
3、監(jiān)控windows主機(jī)
(1)安裝NSCP-0.4.3.143-Win32.msi
#啟用以下選項(xiàng)
Enable common check plugins
Enable nsclient server(check_nt)
Enable NRPE server(check_nrpe)
Secure(UserCertificates for authentication)
Enable NSCA client
Allow hosts:127.0.0.1,::1,192.168.137.111,192.168.137.8
password:123456
#配置文件存放于安裝目錄下的nsclient.ini
(2)查看nsclient++監(jiān)控端口5666
netstat -anbv | find "5666"
(3)配置nagios
#命令行檢查,檢查系統(tǒng)運(yùn)行時(shí)間
#-H:指定主機(jī),-s:指定密碼,-p:指定端口,-v:指定查詢(xún)選項(xiàng)
/usr/local/nagios/libexec/check_nt -H 192.168.137.222 -s 123456 -p 12489 -v UPTIME
#檢查CPU信息
#-w:警告比例;-c:緊急比例;-l:過(guò)去5分鐘平均值,80%為警告,90%為緊急
/usr/local/nagios/libexec/check_nt -H 192.168.137.222 -s 123456 -p 12489 -v CPULOAD -w 80 -c 90 -l 5,80,90
#檢查硬盤(pán)使用率
#-w:警告比例;-c:緊急比例;-l:指定盤(pán)符
/usr/local/nagios/libexec/check_nt -H 192.168.137.222 -s 123456 -p 12489 -v USEDDISKSPACE -w 80 -c 90 -l C
#在/usr/local/nagios/etc/command.cfg最后添加
define command{
command_name check_win
command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$
}
#復(fù)制/usr/local/nagios/etc/objects/windows.cfg一份win-222.cfg,并修改為
#繼承模板/usr/local/nagios/etc/objects/template.cfg windows-server主機(jī)監(jiān)控設(shè)置以及generic-service服務(wù)監(jiān)控設(shè)置
#監(jiān)控主機(jī)IP為192.168.137.222,被監(jiān)控的服務(wù)分別為:系統(tǒng)運(yùn)行時(shí)間、CPU負(fù)載、內(nèi)存使用情況、C盤(pán)使用情況以及explorer.exe進(jìn)程數(shù)
define host{
use windows-server ; Inherit default values from a template
host_name win-222; The name we're giving to this host
alias My Windows Server ; A longer name associated with the host
address 192.168.137.222 ; IP address of the host
}
define hostgroup{
hostgroup_name windows-servers ; The name of the hostgroup
alias Windows Servers ; Long name of the group
}
define service{
use generic-service
host_name win-222
service_description NSClient++ Version
check_command check_nt!CLIENTVERSION
}
define service{
use generic-service
host_name win-222
service_description Uptime
check_command check_nt!UPTIME
}
define service{
use generic-service
host_name win-222
service_description CPU Load
check_command check_nt!CPULOAD!-l 5,80,90
}
define service{
use generic-service
host_name win-222
service_description Memory Usage
check_command check_nt!MEMUSE!-w 80 -c 90
}
define service{
use generic-service
host_name win-222
service_description C:\ Drive Space
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}
define service{
use generic-service
host_name win-222
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -l explorer.exe
}
#效果圖示,可以看到已經(jīng)正常監(jiān)控到windows各項(xiàng)服務(wù)
4、監(jiān)控linux主機(jī)
#監(jiān)控IP為192.168.137.6的linux主機(jī),分別監(jiān)控ping、root分區(qū)使用情況、現(xiàn)存用戶(hù)情況、進(jìn)程數(shù)、實(shí)時(shí)負(fù)載、swap使用情況、ssh和httpd進(jìn)程
define host{
use linux-server ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name linux-6
alias linux-6
address 192.168.137.6
}
define hostgroup{
hostgroup_name my-linux-servers ; The name of the hostgroup
alias My Linux Servers ; Long name of the group
members linux-6 ; Comma separated list of hosts that belong to this group
}
define service{
use local-service ; Name of service template to use
host_name linux-6
service_description PING
check_command check_ping!100.0,20%!500.0,60%
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name linux-6
service_description Root Partition
check_command check_local_disk!20%!10%!/
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name linux-6
service_description Current Users
check_command check_local_users!20!50
}
define service{
use local-service ; Name of service template to use
host_name linux-6
service_description Total Processes
check_command check_local_procs!250!400!RSZDT
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name linux-6
service_description Current Load
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name linux-6
service_description Swap Usage
check_command check_local_swap!20!10
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name linux-6
service_description SSH
check_command check_ssh
notifications_enabled 1
}
define service{
use local-service ; Name of service template to use
host_name linux-6
service_description HTTP
check_command check_http
notifications_enabled 1
}
#在/usr/local/nagios/etc/nagios.cfg添加如下一行,并重新加載或者啟動(dòng)nagios
cfg_file=/usr/local/nagios/etc/objects/linux-6.cfg
/etc/init.d/nagios restart
/etc/init.d/nagios reload
systemctl restart nagios
systemctl reload nagios
5、郵件報(bào)警設(shè)置
5、郵件報(bào)警設(shè)置
#在/usr/local/nagios/etc/object/contact.cfg下修改為
#定義兩個(gè)Nagios Admin郵箱地址,并隸屬于admins組
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contacttemplate (defined above)
alias Nagios Admin ; Full name of user
email nagiosadmin@DNStest.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS******
}
define contact{
contact_name nagiosroot ; Short name of user
use generic-contact ; Inheritdefault values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email nagiosroot@dnstest.com ;<<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin,nagiosroot
}
#在/usr/local/nagios/etc/object/win-222.cfg中添加"notifications_enabled 1"啟用郵件報(bào)警,如修改為
define service{
use generic-service
host_name win-222
service_description C:\ Drive Space
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c90
notifications_enabled 1
}
define service{
use generic-service
host_name win-222
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -lexplorer.exe
notifications_enabled 1
}
#到192.168.137.222這臺(tái)windows主機(jī)將nsclient++服務(wù)停止或直接禁用網(wǎng)卡模擬故障
#查看nagiosadmin@dnstest.com和nagiosroot@dnstest.com有沒(méi)有收到郵件
#可以查看/var/log/messages日志文件看有沒(méi)有發(fā)送日志記錄
#故障后郵件報(bào)警
#故障恢復(fù)后的郵件通知
6、短信通告