分类: LINUX
2010-04-26 10:33:55
|
1、Nagios简介
Nagios是一款遵循GPLv2的开源网络监控软件,可用来监控指定的多种系统的主机、服务,并可在它们的工作状态发生变化时通知管理员。它的特性包括:
(1)监控网络服务(HTTP、POP3、SMTP、PING、MySQL等)
(2)监控主机资源(磁盘空间利用率、内存利用率、CPU负载等)
(3)简洁的插件设计接口,使得用户可以轻松开发所需的检测脚本
(4)并行服务模式
(5)轻松描述网络结构,并且能够区辨“宕机”和“主机不可达”
(6)通过邮件或用户自定义的方式将主机或服务的工作状态变化情况通知给管理员
(7)自动日志滚动
(8)支持以冗余方式进行主机监控
(9)可以通过web方式直观的查看当前网络状态、通知和问题历史、日志文件等等,此组件为可选
Nagios通常由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPE、NSCA、NSClient++和NDOUtils)组成。Nagios的监控工作都是通过插件实现的,因此,Nagios和Nagios-plugins是服务器端工作所必须的组件。而四个ADDON中,NRPE用来在监控的远程Linux/Unix主机上执行脚本插件以实现对这些主机资源的监控;NSCA用来让被监控的远程Linux/Unix主机主动将监控信息发送给Nagios服务器(这在冗余监控模式中特别要用到);NSClient++是用来监控Windows主机时安装在Windows主机上的组件;而NDOUtils则用来将Nagios的配置信息和各event产生的数据存入数据库,以实现这些数据的快速检索和处理。这四个ADDON(附件)中,NRPE和NSClient++工作于客户端,NDOUtils工作于服务器端,而NSCA则需要同时安装在服务器端和客户端。
目前,Nagios只能安装在Linux系统主机上,其编译需要用到gcc。同时,如果打算使用web界面的管理工具的话,还需要有apache服务器和GD图形库的支持。
2、安装前的准备工作
Nagios基本组件的运行依赖于httpd、gcc和gd。不过nagios 3xx以后的版本需要php模块的支持,不然的话,web界面显示乱码的。
我把需要的软件放在了/soft下面,这个可随意。
LAMP架构的准备。
安装Mysql数据库
# groupadd mysql
# useradd –g mysql –s /bin/false –M mysql
# tar zxf mysql-5.0.75.tar.gz
# cd mysql-5.0.75
#./configure --prefix=/usr/local/mysql --enable-thread-safe-client --enable-local-infile --with-charset=gbk --with-extra=all --with-low-memory
# make && maek install
# cp support-files/my-medium.cnf /etc/my.cnf
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 !$
# cd /usr/local/mysql/
# chown -R mysql .
# chgrp -R mysql .
# bin/mysql_install_db --user=mysql
# chown -R root .
# chown -R mysql var/
# bin/mysqld_safe --user=mysql &
# chkconfig --add mysqld
# ln -s /usr/local/mysql/lib/mysql/lib/* /usr/lib/
# ln -s /usr/local/mysql/include/mysql/* /usr/include/
# echo "/usr/local/mysql/lib">>/etc/ld.so.conf
# ldconfig –v
# export PATH=$PATH:/usr/local/mysql/bin/
# mysqladmin ping
# mysqladmin -uroot -p password "123456"
安装apache
# tar zxf httpd-2.2.11.tar.gz
# cd httpd-2.2.11
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd/conf --enable-cgi --enable-so --enable-ssl --enable-track-vars --enable-rewrite --enable-mods-shared=most –with-mpm=worker –with-zlib
#make && make install
# cd /usr/local/apache/bin/
# ./apachectl start
# echo "/usr/local/apache/bin/apachectl start">>/etc/rc.local
安装PHP
# tar jxf php-5.2.10.tar.bz2
# cd php-5.2.10
# mkdir /usr/local/php
#./configure --prefix=/usr/local/php/ --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql/ --with-mysqli=/usr/local/mysql/bin/mysql_config --with-xml --with-png --with-jpeg --with-zlib --with-freetype --with-gd --enable-track-vars --enable-mbstring=all
#make && maek install
# cp php.ini-dist /usr/local/php/lib/php.ini
编辑apache 配置文件以支持php
#vi /etc/httpd/conf/httpd.conf
添加
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
找到DirectoryIndex index.html
添加 index.php
安装ZendOptimizer
按照提示输入路径
安装eaccelerator-0.9.6加速软件
下载地址
首先运行下/usr/local/php/bin/phpize
tar jxf eaccelerator-0.9.6-rc2.tar.bz2
cd eaccelerator-0.9.6-rc2
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
Make && make install
[root@LEE eaccelerator-0.9.6-rc2]# make install
Installing shared extensions: /usr/local/php//lib/php/extensions/no-debug-zts-20060613/
修改php.ini,在[zend]之前加入以下内容(注:必须放在[zend]之前):
[eaccelerator]
extension="/usr/local/php//lib/php/extensions/no-debug-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/var/cache/php"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
建立缓存目录
Mkdir /var/cache/php
授权
Chmod 77 /var/cache/php
重启apache
编辑测试文件。
#vi /usr/local/apache/htdocs/index.php
Phpinfo();
?>
保存退出。
请确定红色部分出现则说明加速安装成功。
LAMP架构准备完了。下面开始Nagios了
添加nagios运行所需要的用户和组
# useradd nagios
# groupadd nagcmd
# usermod -G nagcmd nagios
# usermod -G nagcmd daemon
# tar zxf nagios-3.2.0.tar.tar
# cd nagios-3.2.0
# ./configure --prefix=/usr/local/nagios --with-command=nagcmd
# make all
# make install
# make install-init
# make install-config
# make install-commandmode
# /usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
输入密码
#vi /etc/httpd/conf/httpd.conf
添加
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
Alias /nagios "/usr/local/nagios/share"
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
# vi /usr/local/nagios/etc/objects/contacts.cfg
email nagios@localhost #这个是默认设置
Email huayun1314@126.com
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 测试一下配置文件是否正确
#/usr/local/apache/bin/apache1 restart
#/server nagios restart
在IE中输入 如出现如下界面说明你配置正确
安装nagios 插件
# tar zxf nagios-plugins-1.4.9.tar.tar
# cd nagios-plugins-1.4.9
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make && make install
ls /usr/local/nagios/libexec/ 看看是不是多出很多文件
安装NRPE
# tar zxf nrpe-2.2.tar.gz
#cd nrpe-2.2
# ./configure
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
监控windows主机
vi /usr/local/nagios/etc/nagios.cfg
#cfg_file=/usr/local/nagios/etc/objects/windows.cfg 去掉这句话的注释
在windows主机中安装 NSClient++
解压到C盘
cd到刚才解压的目录
在命令行界面执行 nsclient++ /install
编辑NES.ini
在 [modules] 选项里
去掉所有的注释符号除了
CheckWMI.dll和RemoteConfiguration.dll
[Settings] 选项里
修改allowd_host=IP(nagios服务器的ip)
如果这一步要修改passwd,那么nagios服务器里面commands.cfg也要修改!
步骤: 在commands.cfg 找到check_nt 定义命令 中的 Command_line 在其中添加-s 你的密码
[NSClient] 里面,去掉port=12489的注释!
他靠端口12489侦听,所以防火墙要打开这个端口!
然后启动nsclient
nsclient++ /start
接下来我们开始配置nagios服务器里面的内容
vi /usr/local/nagios/etc/objects/windows.cfg
=================================
define host{
use windows-server
host_name IBMX3850
address 192.168.1.250
}
修改hostname和address,
======================
然后重新启动nagios
看看你的web图形是不是变化了!它会自动连接!
如果这个时候出错!尝试去telnet win服务器的ip 12489端口!!
自定义服务
define service{
use generic-service
host_name HMIT_ISCM_89
service_description vnc
check_command check_tcp!5800
}
define service{
use generic-service
host_name HMIT_ISCM_89
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
}
service{
use generic-service
host_name HMIT_ISCM_89
service_description D:\Drive Space
check_command check_nt!USEDDISKSPACE!-l d -w 80 -c 90
}
define command{
command_name check_webpage
command_line $USER1$/check_http $ARG1$
}
#check_mysql
define command{
command_name check_mysql
command_line $USER1$/check_mysql -H $ARG1$ -P $ARG2$ -u $ARG3$ -p $ARG4$
}
使用fetion报警
vi commands.cfg
#####################fetion##############################################
define command {
command_name notify-host-by-fetion
command_line /usr/local/fetion/fetion --mobile=15026***** --pwd=lihua --to=$CONTACTPAGER$ --msg-utf8="Host $HOSTSTATE$ alert for $HOSTNAME$! on '$LONGDATETIME$'" $CONTACTPAGER$
}
define command {
command_name notify-service-by-fetion
command_line /usr/local/fetion/fetion --mobile=150265**** --pwd=lihua --to=$CONTACTPAGER$ --msg-utf8="$HOSTADDRESS$ $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ on $LONGDATETIME$" $CONTACTPAGER$
添加联系人
Vi contatc.cfg
#define contact{
contact_name hmit_test
use generic-contact
alias nagios admin
host_notifications_enabled 1
service_notifications_enabled 1
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c
host_notification_options d,u,r
service_notification_commands notify-service-by-fetion
host_notification_commands notify-host-by-fetion
# email hua.li@hmit.com.cn
pager 150265*****
}
安装PNP
安装rrdtool,rrdtool不能直接通过yum安装,可以加入Dag RPM Repository以便让yum能找到rrdtool
#wget
#rpm -Uvh rpmforge-release-0.3.6-1.el5.rf.i386.rpm
#yum install rrdtool
tar -xvzf pnp4nagios-HEAD.tar.gz
cd pnp4nagios
./configure
*** Configuration summary for pnp4nagios-0.6.rc3 10-11-2009 ***
General Options:
------------------------- -------------------
Nagios user/group: nagios nagios
Install directory: /usr/local/pnp4nagios
HTML Dir: /usr/local/pnp4nagios/share
Config Dir: /usr/local/pnp4nagios/etc
Location of rrdtool binary: /usr/bin/rrdtool Version 1.2.12
RRDs Perl Modules: FOUND (Version 1.2012)
RRD Files stored in: /usr/local/pnp4nagios/var/perfdata
process_perfdata.pl Logfile: /usr/local/pnp4nagios/var/perfdata.log
Perfdata files (NPCD) stored in: /usr/local/pnp4nagios/var/spool
HTML URL:
Apache Config File: /etc/apache2/conf.d/pnp4nagios.conf
Review the options above for accuracy. If they look okay,
type 'make all' to compile.
make all
make install
make install-webconf
make install-config
make install-init
make fullinstall
[root@LEE objects]# vi templates.cfg
define host {
name host-pnp
action_url /pnp4nagios/graph?host=$HOSTNAME$&srv=_HOST_
register 0
}
define service {
name srv-pnp
action_url /pnp4nagios/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
register 0
}
修改nagios.cfg 主配置文件
1. process_performance_data=1
2. host_perfdata_command=process-host-perfdata
3. service_perfdata_command=process-service-perfdata
修改command.cfg 配置文件
define command {
command_name process-host-perfdata
command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA
}
define command {
command_name process-service-perfdata
command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl
}
重启nagios
在没个主机的配置文件中加入 host-pnp srv-pnp
NDOUtils将Nagios监控信息存入数据库
安装DBD-mysql DBI-1.6
tar xzvf DBI-1.605.tar.gz
cd DBI-1.605
perl Makefile.PL
make
make install
tar xzvf DBD-mysql-3.0008.tar.gz
cd DBD-mysql-3.0008
perl Makefile.PL
make
make install
tar zxf ndoutils-1.4b9.tar.gz
cd ndoutils-1.4b
./configure –enable-mysql
Make
Cp src/ndomod-3x.o ndo2db-3x log2ndo file2sock /usr/local/nagios/bin
Cp config/ndo* /usr/local/nagios/etc
Chown –R nagios.nagios /usr/local/nagios/etc/*
创建数据库
Mysql –uroot –plihua
Create database ‘nagios’default character set utf8 collate utf8_general_ci;
Grant all on nagios.* to nagios@localhost identified by ‘nagios’;
Flush privileds
Cd db/
./installdb –u root –p lihua –h localhost –d nagios
看看mysql nagios中师不是多了59张表
Vi /usr/local/nagios/etc/ndo2db.cfg
Db_user=nagios
Db_pass=nagios
Vi /usr/local/nagios/etc/nagios.cfg
复制下面内容粘贴到/usr/local/nagios/etc/nagios.cfg配置文件的#broker_module=...下面。
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
启动 ndo2db
/usr/local/nagios/bin/ndo2db-3x –c /usr/local/nagios/etc/nado2db.cfg
如果要停止 直接删除 ndo.sock
重启nagios
Tail /usr/local/nagios/var/nagios.log
安装squid
./configure --prefix=/usr/local/squid --disable-carp --with-aufs-threads=32 --with-pthreads --enable-storeio='ufs,aufs,null' --enable-disk-io='AIO,Blocking' --enable-removal-policies='heap,lru' --disable-wccp --enable-kill-parent-hack --disable-snmp --disable-poll --disable-select --enable-auth=basic --with-aio --disable-ident-lookup --with-filedescriptors=65536
Make && make instal