歡迎您光臨本站 註冊首頁

linux27-nagios

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0

nagios


監控大量機器上運行的服務和負載等,帶報警功能.


[root@li ~]# ls /share/soft/soft/monitor2/
nagios-3.2.3.tar.gz --主程序包
nagios-plugins-1.4.15.tar.gz --插件包
nrpe-2.12.tar.gz --客戶端程序包

--注意插件包等和主程序包的版本號不一定要一致


1,搭建rpm版lamp
# yum install httpd* gd gd-devel

2,建立用戶

# useradd nagios
# groupadd nagiosgroup
# usermod -G nagiosgroup nagios
# usermod -G nagiosgroup apache


3,安裝nagios主程序包

# tar xf /share/soft/soft/monitor2/nagios-3.2.3.tar.gz -C /usr/src/
# cd /usr/src/nagios-3.2.3/

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

# make all
# make install
# make install-init
# make install-commandmode
# make install-config
# make install-webconf


make install
- This installs the main program, CGIs, and HTML files

make install-init
- This installs the init script in /etc/rc.d/init.d

make install-commandmode
- This installs and configures permissions on the
directory for holding the external command file

make install-config
- This installs *SAMPLE* config files in /usr/local/nagios/etc
You'll have to modify these sample files before you can
use Nagios. Read the HTML documentation for more info
on doing this. Pay particular attention to the docs on
object configuration files, as they determine what/how
things get monitored!

make install-webconf
- This installs the Apache config file for the Nagios
web interface


# ls /usr/local/nagios/


bin etc libexec sbin share var

--libexec目錄為空,需要安裝插件包才會有很多命令與腳本


4,安裝nagios插件包 --包含用於收集數據的程序,命令,腳本等
# tar xf /share/soft/soft/monitor2/nagios-plugins-1.4.15.tar.gz -C /usr/src/
# cd /usr/src/nagios-plugins-1.4.15/

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

# make ;make install


5.創建web界面可訪問的驗證用戶
/etc/httpd/conf.d/nagios.conf --在這個文件里已經配置了nagios的apache驗證,所以我們要把用戶給創建出來

# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin --文件路徑和用戶名都不要改,被規定了就是這個
New password:
Re-type new password:
Adding password for user nagiosadmin


6,nagios配置文件介紹

/usr/local/nagios/etc/nagios.cfg --主配置文件

/usr/local/nagios/etc/objects/ --子配置文件的目錄
localhost.cfg --一個示例模版,默認定義了監控本機的8個服務
templates.cfg --模版定義文件
commands.cfg --命令定義文件
contacts.cfg --定義通知方式的文件
timeperiods.cfg --監控時間段定義文件


==================================================

關於nagios配置文件之間的聯繫講解示例

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


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


define host{
use linux-server --模版
host_name localhost --主機名
alias localhost --主機別名
address 127.0.0.1 --被監控機器的IP


}


define hostgroup{
hostgroup_name linux-servers
alias Linux Servers
members localhost --linux Servers組現在只有localhost這一個成員
}


--下面是8個默認定義的服務,我以監控磁碟利用率的這一段為例
define service{
use local-service --模版,在templates.cfg 里定義的
host_name localhost --主機名,調用的是同配置文件里define host里定義的host_name
service_description Root Partition --描述,會在web界面顯示的一個標題
check_command check_local_disk!20%!10%!/ --檢測利用率的命令,free空間小於20%就報警,小於10就critcal警告
}


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


define host{
name linux-server
use generic-host --linux主機模版也使用了一個叫generic-host的模版,也在templates.cfg里
check_period 24x7 --在timeperiods.cfg 里定義的時間段
check_interval 5
retry_interval 1
max_check_attempts 10
check_command check-host-alive --在commands.cfg 里定義的命令
notification_period workhours --通知時間在timeperiods.cfg里定義的
notification_interval 120 --通知間隔
notification_options d,u,r --通知選項
contact_groups admins --通知組,在contacts.cfg 里定義
register 0 --不註冊,表示這只是一個模版,被調用,不會被nagios進程認為就是一台主機


}

# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check-host-alive
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
}

--命令都在libexec下,用--help去查
# /usr/local/nagios/libexec/check_ping --help


================================

--配置文件默認什麼都不要改,檢查配置文件正確性
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


# /etc/init.d/nagios restart
# /etc/init.d/httpd restart


訪問路徑:http://172.19.1.17/nagios
用戶名:nagioadmin
密碼: 前面htpasswd -c 創建時定義的密碼

===================================================


現在查看web界面,默認只監控了localhost,並監控了其8個服務

一些小操作:
1,如果http服務為黃色,是警告,則需要把網站家目錄里加一個主頁進去(家目錄為空,他就會警告).
但需要等它下一次check才會OK.如果要手動check,可以點http,再右邊點Re-schedule the next check of this service去強制check,就OK了

2,默認http和ssh是關閉通知的,是在localhost.cfg里這兩個服務有一句 notifications_enabled 0.
也可以手動打開,點進去,再右邊點enabled notifications for this service.


3,關閉ssh服務,刷新web界面,還是沒有critical.
點擊ssh,可以看到下一次計劃的check時間.如果不等的話,在右邊點Re-schedule the next check of this service強制check,再刷新就critical


4,修改ssh的check時間間隔
# vim /usr/local/nagios/etc/objects/localhost.cfg


define service{
use local-service --使用的這個模版,所以要去改這個模版里的時間
host_name localhost
service_description SSH
check_command check_ssh
notifications_enabled 0
}

# vim /usr/local/nagios/etc/objects/templates.cfg
define service{
name local-service
.............
normal_check_interval 1 --把這個五分鐘改為1分鐘
.............
}


# /etc/init.d/nagios reload


--再去web界面驗證,check時間為1分鐘了

========================================================

例1:在默認8個服務的基礎上,如何增加監控本機的服務 如ftp

思路步驟:
1,看libexec/下是否有檢測ftp的命令,如果沒有,網上下載,或自己開發
2,在localhost.cfg里定義這個服務
3,在command.cfg里定義命令

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

define service{
use local-service
host_name localhost
service_description FTP
check_command check_ftp!3!6
}


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

define command{
command_name check_ftp
command_line $USER1$/check_ftp -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$
}


# /etc/init.d/nagios restart

練習:
1,如果本機ftp服務為監聽2121埠,應該如何監控

# vim /etc/vsftpd/vsftpd.conf
listen_port=2121 --加上這一句


# netstat -ntlup |grep ftp
tcp 0 0 0.0.0.0:2121 0.0.0.0:* LISTEN 29883/vsftpd

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

---加下面一段
define service{
use local-service
host_name localhost
service_description FTP --標題改成FTP
check_command check_ftp_2121!3!6!2121
--命令我這裡是沒有的,在command.cfg里默認有一個check_ftp,沒有
--check_ftp_2121這個,所以要手動去加;!為參數分隔符,3是第一個參數,6是第二個參數,2121是第三個參數;它們對應於我下面定義的-w -c -p
}

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

define command{
command_name check_ftp_2121
command_line $USER1$/check_ftp -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p $ARG3$
}


--直接使用監控命令去手工check一下,OK的
# /usr/local/nagios/libexec/check_ftp -w 3 -c 6 -p 2121
FTP OK - 0.004 second response time on port 2121 [220-#############################
220-#]|time=0.003835s;3.000000;6.000000;0.000000;10.000000


# /etc/init.d/nagios reload
--reload后,再去web界面可以看到能監控本機的ftp這個服務了

2,監控本機的mysql

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

define service{
use local-service
host_name localhost
service_description MYSQL
check_command check_mysql!root!123
}


# vim /usr/local/nagios/etc/objects/commands.cfg
define command{
command_name check_mysql


command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u $ARG1$ -p $ARG2$ --第一個參數對應上面的root,第二個對應密碼123
}

--手動check一下mysql,OK
# /usr/local/nagios/libexec/check_mysql -u root -p123
Uptime: 189 Threads: 1 Questions: 5 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 6 Queries per second avg: 0.026


# /etc/init.d/nagios reload


--去nagios 的web界面刷新查看,OK

===========================================================


我們把監控的服務分為公共和私有

公共:如ssh,http,ftp,mysql等.監控本地或遠程的公共服務,都可以直接配置
私有:如load,users,disk usage等.監控本地私有服務直接配置就好,監控遠程私有服務,需要服務和被監控端安裝nrpe

例:監控遠程伺服器的普通服務(公共服務).如ssh,http,ftp,mysql等


如:我的被監控端IP為172.19.1.125
1.在nagios伺服器的主配置文件里加上125的主機配置文件

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

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


2,創建這個125.cfg
# cd /usr/local/nagios/etc/objects/
# cp localhost.cfg 125.cfg

# vim 125.cfg

define host{
use linux-server
host_name 172.19.1.125 --主機名,最好/etc/hosts里對應好IP,我這裡沒有做,就直接寫IP
alias 172.19.1.125 --顯示到web上的名字
address 172.19.1.125 --實際被監控主機IP
}


--下面是公共服務,這裡我只寫了五個,你可以自行增加
define service{
use local-service
host_name 172.19.1.125


service_description PING
check_command check_ping!100.0,20%!500.0,60%
}


define service{
use local-service
host_name 172.19.1.125
service_description SSH
check_command check_ssh
}

define service{
use local-service
host_name 172.19.1.125
service_description HTTP
check_command check_http
}


define service{
use local-service
host_name 172.19.1.125
service_description FTP
check_command check_ftp!3!6
}


define service{
use local-service
host_name 172.19.1.125
service_description MYSQL
check_command check_mysql!root!123
}

--驗證配置文件,再重啟服務
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# /etc/init.d/nagios reload

=====================================================


例:監控遠程的私有服務


1,在nagios伺服器上安裝nrpe插件

# tar xf /share/soft/soft/monitor2/nrpe-2.12.tar.gz -C /usr/src/
# ./configure && make && make install

--安裝完后,就有下面的腳本了
/usr/local/nagios/libexec/check_nrpe


2,增加check_nrpe命令到commands.conf文件里
# vim /usr/local/nagios/etc/objects/commands.cfg

define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ --c參數後接command, 也就說check_nrpe可以調用別的check命令
}

3,在nagios伺服器上對229的配置文件增加遠程私有服務

# vim 125.cfg

define service{
use local-service
host_name 172.19.1.125
service_description Root Partition
check_command check_nrpe!check_remote_root
--check_remote_root就是check_nrpe的C參數要調用的命令,此命令在nagios伺服器上的commands.cfg里是不存在,它會在後面的步驟中加到被監控端
}


define service{
use local-service
host_name 172.19.1.125
service_description Current Users
check_command check_nrpe!check_remote_users
}


define service{
use local-service
host_name 172.19.1.125
service_description Total Processes
check_command check_nrpe!check_remote_total_procs
}


define service{
use local-service
host_name 172.19.1.125
service_description Current Load
check_command check_nrpe!check_remote_load
}


define service{
use local-service
host_name 172.19.1.125
service_description Swap Usage


check_command check_nrpe!check_remote_swap
}


# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
--檢查一下配置文件正確性,OK的話則配置端配置完畢,先不reload nagios服務,等被監控端配置完后再reload

==============================================================


現在在被監控端125上安裝

1,新建用戶
# useradd nagios
# groupadd nagiosgroup
# usermod -G nagiosgroup nagios
# usermod -G nagiosgroup apache


2,安裝plugins插件,包含了數據採集命令腳本
# tar xf nagios-plugins-1.4.15.tar.gz -C /usr/src/
# cd /usr/src/nagios-plugins-1.4.15/
# ./configure --with-nagios-user=nagios --with-nagios-group=nagiosgroup
# make && make install

3,安裝nrpe
# tar xf nrpe-2.12.tar.gz -C /usr/src/
# cd /usr/src/nrpe-2.12/
# ./configure && make && make install

# make install-plugin
# make install-daemon
# make install-daemon-config
# make install-xinetd


4,修改nrpe的超級守護進程的配置文件

# vim /etc/xinetd.d/nrpe

service nrpe
{
flags = REUSE
socket_type = stream
port = 5666
wait = no
user = nagios
group = nagios
server = /usr/local/nagios/bin/nrpe
server_args = -c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure = USERID
disable = no
only_from = 127.0.0.1 172.19.1.17 --加上nagios伺服器的IP,允許它來訪問
}

# vim /etc/services --面加一行
nrpe 5666/tcp # NRPE

5,在nrpe配置文件里定義check命令,使nagios服務能調用

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


command[check_remote_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_remote_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_remote_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda2 --/dev/sda2是被監控端的根分區,也可以直接就寫一個 / 就可以了
command[check_remote_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
command[check_remote_swap]=/usr/local/nagios/libexec/check_swap -w 80%% -c 60%% --這句默認沒有的,但nagios伺服器有配置,所以加上這句
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z --這個是默認有的,但nagios伺服器那邊我沒有加,所以這個在這裡沒有用


# /etc/init.d/xinetd restart --啟動超級守護進程

# netstat -ntlup |grep 5666 --有埠被監聽了
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 22120/xinetd


6,在本地或nagios伺服器測試
--在被監控端測試成功
# /usr/local/nagios/libexec/check_users -w 5 -c 10
USERS OK - 3 users currently logged in |users=3;5;10;0


--在nagios伺服器上測試成功
# /usr/local/nagios/libexec/check_nrpe -H 172.19.1.125 -c check_remote_users
USERS OK - 3 users currently logged in |users=3;5;10;0

7,回到nagios伺服器重啟服務
# /etc/init.d/nagios restart

=================================================================


nagios郵件報警功能


報警方式:
1,聲音報警
2,郵件報警
3,簡訊報警


這裡我就用自帶的sendmail簡單實現


1,搭建郵件系統 --前面都有講,這裡省略,我直接用本地sendmail測試是否能收郵件
#yum install sendmail* m4 -y
#/etc/init.d/sendmail restart

# echo 'test' | mail root@li.cluster.com --發一個郵件給自己的root用戶測試能接收

2,修改nagios伺服器聯繫相關的子配置文件
# vim /usr/local/nagios/etc/objects/contacts.cfg

define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email root@li.cluster.com --改成收的郵件地址
}

# vim /usr/local/nagios/etc/objects/templates.cfg --保持默認


define contact{
name generic-contact
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
}


# vim /usr/local/nagios/etc/objects/commands.cfg --默認有這兩條命令的定義,我這裡保持默認就好了


define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$


}

define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}


# /etc/init.d/nagios restart


--關閉一些服務進行測試tail /var/mail/root來查看


===============================================================


使用免費139郵箱來進行簡訊通知


在https://mail.10086.cn/Register/default.aspx上註冊一個郵箱

我的為:
158xxxxxxxx@139.com


登錄進去郵箱:
點設置-->郵件到達通知-->點開啟


然後在本機發送一個測試郵件給你的郵箱,會發現手機會馬上收到此郵件
# mail -s 'test' 158xxxxxxxx@139.com

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

define contact{
contact_name nagiosadmin
use generic-contact
alias Nagios Admin
email 158xxxxxxxx@139.com --改成收的郵件地址
}


# /etc/init.d/nagios restart


--然後就把幾個服務關閉,開啟一下,等郵件通知,通知過就會發到手機

=============================================================


==========================


awstats
用於分析apache日誌,也可以分析squid和nginx日誌,日誌格式為combined格式


軟體包:
ls /share/soft/soft/awstats/awstats-6.95.tar.gz

解壓:
[root@li cronolog-1.6.2]# tar xvf /share/soft/soft/awstats/awstats-6.95.tar.gz -C /usr/local/

[root@li cronolog-1.6.2]# cd /usr/local/
[root@li local]# mv awstats-6.95/ awstats

[root@li awstats]# cd /usr/local/awstats/
[root@li awstats]# ./tools/awstats_configure.pl --使用這個工具來配置監控的網站,產生監控網頁


它會自動查找apache配置文件路徑,如果沒有查找到,可能需要你手動輸入httpd.conf的位置

修改apache的配置文件,把日誌格式Logformat 改為combined格式,這種格式可以分析出客戶的操作系統,瀏覽器等特性


-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y --輸入y,產生awstat的配置文件


-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> www.station35.cluster.com --定義要分析的網站的網站名,隨便定義


-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
> --定義網站分析配置文件的路徑,這裡直接回車代表使用默認路徑

-----> Create config file '/etc/awstats/awstats.www.station35.cluster.com.conf'
Config file /etc/awstats/awstats.www.station35.cluster.com.conf created.
--就會看這個信息,自動產生了/etc/awstats/awstats.www.station35.cluster.com.conf


配置新產生的這個配置文件

vim /etc/awstats/awstats.www.station35.cluster.com.conf


LogFile="/usr/local/apache2/logs/access.log" --定義要分析的access.log路徑


LogType=W --這個確認是W就OK,表示是web日誌類型


DirData="/var/lib/awstats" --這是awstats的資料庫存放目錄,目錄需要實際存在


[root@li ~]# mkdir /var/lib/awstats

更新網站數據:
[root@li ~]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.station35.cluster.com

訪問:
http:/10.1.1.35/awstats/awstats.pl?config=www.station35.cluster.com

定時更新加到crontab里
01 1 * * * /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.station35.cluster.com > /dev/null 2>&1


設置訪問許可權,不讓別人有許可權看到自己網站的統計信息

在apache配置文件里加上htpasswd


Options FollowSymLinks
AllowOverride all
order allow,deny
allow from all

然後去建立htpasswd的加密


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

關於監控多個網站的討論

1,如果是有虛擬主機的話

則每個虛擬主機都要配置一次,分析不同的虛擬主機的access_log


2,如果想要監控別的機器上的網站,按原理來說就是需要別的機器上的access_log,那麼就會有下面情況:
如果access_log在共享存儲上,則直接指定就好了
如果沒有的話,則寫腳本,把要監控的遠程機上的access_log給傳送過來就好了


1,先配置ssh等效性

2,腳本的寫法
被監控者: node100


vim /bin/accesslogcp.sh
#!/bin/bash
cat /etc/httpd/logs/access_log > /tmp/node100.log
echo " " > /etc/httpd/logs/access_log

監控者
vim /bin/nod100logcp.sh
#!/bin/bash
ssh 10.1.1.100 /bin/accesslogcp.sh > /dev/null 2>&1
scp 10.1.1.100:/tmp/node100.log /var/log/ >/dev/null 2>&1

3,在監控者這邊 使用awstats軟體,把要分析的accesslog指定為/var/log/node100.log就可以了

本文出自 「linuxart」 博客,請務必保留此出處http://linuxart.blog.51cto.com/686203/843990


[火星人 ] linux27-nagios已經有617次圍觀

http://coctec.com/docs/linux/show-post-45764.html