系统环境 centos 5.6
系统设置时间
yum install ntp* -y
vim /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=false
ARC=false
/bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
/etc/init.d/ntpd stop
ntpdate us.pool.ntp.org
hwclock -w
/etc/init.d/ntpd restart
chkconfig ntpd on
安装环境(http php mysql net-snmp gcc)
yum install -y php php-devel php-mysql php-snmp php-gd mysql mysql-server mysql-devel net-snmp net-snmp-libs net-snmp-utils php-pdo perl-DBD-MySQL gcc gcc-c++ glibc glibc-common gd gd-devel libxml2* openssl*
rpm -ivh rrdtool-1.3.8-1.el5.pp.i386.rpm
启动http mysql等服务
/etc/init.d/httpd restart
chkconfig httpd on
/etc/init.d/mysqld restart
chkconfig mysqld on
首先安装nagios
添加nagios用户
useradd -s /sbin/nologin nagios
passwd nagios
groupadd nagcmd
usermod -G nagcmd nagios
usermod -G nagcmd apache
安装软件
tar zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --prefix=/usr/local/nagios --with-command-group=nagcmd && make all && make install && make install-init && make install-config && make install-commandmode && make install-webconf
cd ..
tar xvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group=nagios && make && make install && cd ..
tar xvf nagios-cn-3.2.3.tar.bz2
cd nagios-cn-3.2.3
./configure && make all && make install && cd ..
#配置一个nagiosadmin用户,用于nagios的web管理接口登录
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
#验证nagios.cfg文件是否有错,建议每次修改配置文件后都验证一下,确认无误后重启nagios服务
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#关闭selinux
vi /etc/selinux/config
将'SELINUX=enforcing'改成'SELINUX=disabled'
setenforce 0
#启动apache服务,并设置为开机启动
service httpd start
chkconfig httpd on
#启动nagios服务,并设置为开机启动
service nagios start
chkconfig nagios on
#检查开启情况
ps -ef //检查进程
访问:http://10.2.0.250/nagios/,输入用户名密码,便可打开nagios的web管理接口
至此,主监控服务器上的nagios配置完毕。
#配置邮件报警功能
1、安装sendmail服务
yum install -y sendmail*
2、开启服务,并设置为开机启动
service sendmail start
chkconfig sendmail on
3、设置sendmail邮件接收和转发功能
vi /etc/mail/access
添加如下内容:
Connect:http://www.163.com RELAY
Connect:[email protected] RELAY
Connect:root@localhost RELAY
:wq
makemap -v hash /etc/mail/access.db < /etc/mail/access
4、更改mqueue权限
chmod 777 /var/spool/mqueue
5、检查所传送的电子邮件是否送出,或滞留在邮件服务器中
/usr/lib/sendmail -bp
若屏幕显示为“Mail queue is empty” 的信息,表示mail 已送出
6、发送邮件进行检查
echo testing | /usr/sbin/sendmail -v [email protected]
#设置联系人邮件通知,在系统有故障时通过邮件告知管理员
vi /usr/local/nagios/etc/objects/contacts.cfg
注释掉原来的define contact,并添加如下内容:
define contact{
contact_name yang_ang0219
use generic-contact
alias nagios admin
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
email [email protected]
}
并修改联系人组,修改成如下内容:
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members yang_ang0219
}
#安装pnp4nagios 输出图形和图表(需要php5.2以上的版本)
http://www.360doc.com/content/11/0810/11/1208969_139346861.shtml
安装pnp4nagios-0.6.7
tar zxvf pnp4nagios-0.6.7.tar.gz
cd pnp4nagios-0.6.7
./configure --with-nagios-user=nagios --with-nagios-group=nagcmd&& make all &&make install &&make install-webconf &&make install-config &&make install-init &&cd ..
4、修改配置
cd /usr/local/pnp4nagios/etc
cp misccommands.cfg-sample misccommands.cfg
cp nagios.cfg-sample nagios.cfg
cp npcd.cfg-sample npcd.cfg
cp process_perfdata.cfg-sample process_perfdata.cfg
cp rra.cfg-sample rra.cfg
cd pages
cp web_traffic.cfg-sample web_traffic.cfg
cd ../check_commands
cp check_all_local_disks.cfg-sample check_all_local_disks.cfg
cp check_nrpe.cfg-sample check_nrpe.cfg
cp check_nwstat.cfg-sample check_nwstat.cfg
5、启动npcd服务并设置为开机启动
service npcd start
chkconfig npcd on
6、修改nagios.cfg文件
cd /usr/local/nagios/etc
vi nagios.cfg
修改配置如下:
process_performance_data=1
host_perfdata_command=process-host-perfdata
service_perfdata_command=process-service-perfdata
:wq
7、修改commands.cfg文件
cd /usr/local/nagios/etc/objects
vi commands.cfg
修改process-host-perfdata和process-service-perfdata为:
#'process-host-perfdata' command definitio
define command{
command_name process-host-perfdata
command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA
}
#'process-service-perfdata' command definition
define command{
command_name process-service-perfdata
command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl
}
:wq
8、修改模板,添加小太阳镶嵌在nagios页面上
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
}
:wq
9、修改主机配置文件:
[root@localhost ~]# cat /usr/local/nagios/etc/objects/localhost.cfg |grep -v "#" |sed /^$/d
define host{
use linux-server,host-pnp ; 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 localhost
alias localhost
address 127.0.0.1
}
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members localhost ; Comma separated list of hosts that belong to this group
}
define service{
use local-service,srv-pnp ; Name of service template to use
host_name localhost
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,srv-pnp ; Name of service template to use
host_name localhost
service_description Root Partition
check_command check_local_disk!20%!10%!/
}
define service{
use local-service,srv-pnp ; Name of service template to use
host_name localhost
service_description Current Users
check_command check_local_users!20!50
}
define service{
use local-service,srv-pnp ; Name of service template to use
host_name localhost
service_description Total Processes
check_command check_local_procs!250!400!RSZDT
}
define service{
use local-service,srv-pnp ; Name of service template to use
host_name localhost
service_description Current Load
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
define service{
use local-service,srv-pnp ; Name of service template to use
host_name localhost
service_description Swap Usage
check_command check_local_swap!20!10
}
define service{
use local-service,srv-pnp ; Name of service template to use
host_name localhost
service_description SSH
check_command check_ssh
notifications_enabled 0
}
define service{
use local-service,srv-pnp ; Name of service template to use
host_name localhost
service_description HTTP
check_command check_http
notifications_enabled 0
}
10、删除instal.php
cd /usr/local/pnp4nagios/share
rm -f install.php
11、验证配置文件
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
12、无误后重启nagios和httpd 服务
service nagios restart
service httpd restart
最后登录nagios控制台,选择host--主机名旁边的小太阳,即可看到图形界面!大功告成!
好的至此 nagios监控端安装软件完成
下面安装cacti
cacti 是基于snmp的
所以配置安装snmp(net-snmp)
vim /etc/snmp/snmpd.conf 修改如下:
重启snmp服务器
/etc/init.d/snmpd restart
chkconfig snmpd on
tar zxvf cacti-0.8.7e.tar.gz
mv cacti-0.8.7e /var/www/html/cacti
修改cacti连接数据库的配置文件
vim /var/www/html/cacti/include/config.php
好的 我们需要创建一个cacti数据库设置密码 给予权限
>create database cacti;
>grant all privileges on cacti.* to cacti@localhost identified by 'cacti' with grant option;
>grant all privileges on cacti.* to [email protected] identified by 'cacti' with grant option;
>use cacti;
>source /var/www/html/cacti/cacti.sql;
>exit
然后登陆网页 http://IP/cacti 请安装提示操作
添加计划任务
*/5 * * * * /usr/bin/php /var/www/html/cacti/poller.php > /dev/null 2>&1
如果想立即cacti绘图的话 请直接执行:
/usr/bin/php /var/www/html/cacti/poller.php > /dev/null 2>&1
然后刷新页面 稍等会 就看到绘图了
整合cacti+nagios
整合cacti和nagios是利用了cacti的一个插件nagios for cacti,它的原理是将nagios的数据通过ndo2db导入到mysql数据库(cacti的库中),然后cacti读取数据库信息将nagios的结果展示出来。
1、安装ndoutils
tar xvf ndoutils-1.4b7.tar.gz
cd ndoutils-1.4b7
./configure --with-mysql-lib=/usr/lib/mysql/ --with-mysql-inc=/usr/include/mysql/
make
cd src/
cp ndomod-3x.o /usr/local/nagios/bin/.
cd ndo2db-3x /usr/local/nagios/bin/ndo2db
cd ../config
cp ndo2db.cfg /usr/local/nagios/etc/
cp ndomod.cfg /usr/local/nagios/etc/
cd /usr/local/nagios/etc/
chown nagios:nagios ndo2db.cfg ndomod.cfg
cd ../bin/
chown nagios:nagios ndo2db ndomod-3x.o
然后修改ndo2db.cfg ndomod.cfg的配置文件
cat /usr/local/nagios/etc/ndo2db.cfg |grep -v '^#'|sed /^$/d
ndo2db_user=nagios
ndo2db_group=nagios
socket_type=tcp
socket_name=/usr/local/nagios/var/ndo.sock
tcp_port=5668
db_servertype=mysql
db_host=127.0.0.1
db_port=3306
db_name=cacti
db_prefix=npc_
db_user=cactiuer
db_pass=cactiuser
max_timedevents_age=1440
max_systemcommands_age=10080
max_servicechecks_age=10080
max_hostchecks_age=10080
max_eventhandlers_age=44640
debug_level=1
debug_verbosity=1
debug_file=/usr/local/nagios/var/ndo2db.debug
max_debug_file_size=1000000
cat /usr/local/nagios/etc/ndomod.cfg |grep -v '^#'|sed /^$/d
instance_name=default
output_type=tcpsocket
output=127.0.0.1
tcp_port=5668
output_buffer_items=5000
buffer_file=/usr/local/nagios/var/ndomod.tmp
file_rotation_interval=14400
file_rotation_timeout=60
reconnect_interval=15
reconnect_warning_interval=15
data_processing_options=-1
config_output_options=2
然后再编辑nagios的主配置文件 nagios.cfg 在最后添加如下:
check_external_commands=1
command_check_interval=-1
event_broker_options=-1
process_performance_data=1
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
保存退出
启动ndo
/usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg
echo "/usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg" >>/etc/rc.local
service nagios restart
service httpd restart
2.安装php-json
tar xvf php-json-ext-1.2.0.tar.bz2
cd php-json-ext-1.2.0
首先初始化php(如果没有这个命令 请 yum install php-devel)
phpize
./configure
make
make install
查看是否安装成功
find / -name *json.so
vim /etc/php.ini
查找到include 在旗下添加如下语句:
include ('/etc/php.d/json.ini')
vim /etc/php.d/json.ini
内容如下:
extension=json.so
重启web服务
/etc/init.d/httpd restart
安装cacti的插件管理器 (cacti插件使用的前提 需要和cacti主程序版本一致)
unzip cacti-plugin-0.8.7e-PA-v2.5.zip -d cacti-plugin
cp -R cacti-plugin/* /var/www/html/cacti/
cd /var/www/html/cacti/
mysql -uroot cacti < pa.sql (这里因为我没有设置mysql数据库的密码 所以没有-p选项 设置mysql密码 : mysqladmin -uroot -p password mysql)
patch -p1 -N < cacti-plugin-0.8.7e-PA-v2.5.diff
vim include/global.php (修改如下
):
这是连接数据库方面的设置
重启httpd服务
cacti的插件 安装完成 。
3.npc插件的安装
tar zxvf npc-2.0.4.tar.gz
mv npc /var/www/html/cacti/plugins/.
vim /var/www/html/cacti/include/global.php 修改如下:(这是为了让npc插件在管理里面看到)
保存退出
重启httpd服务
重启nagios服务
然后呢 只需要去cacti的web页面上 设置下即可:如图
好的 至此 cacti和nagios的整合成功
不足之处 请大牛指出来 并给以相关的资料 谢谢