Centos6.2
一、安装环境LAMP
#yum -y install gcc* glibc* gd* php* mysql* http*
#yum -y install httpdmysql-server mysql-devel perl-DBI perl-DBD-MySQL php php-devel php-mysql php-snmp php-pdophp-gd lm_sensors net-snmp net-snmp-libs net-snmp-utils net-snmp-devel
# chkconfig mysqld on
# chkconfig httpd on
# chkconfig snmpd on
# service mysqld start
# service httpd start
# service snmpd start
#vim /var/www/html/index.php
<?php
phpinfo();
?>
访问http://localhost/index.php
# mysqladmin -u root password '123'
报错处理: # exportPATH=$PATH:/usr/local/mysql/bin
# mysql -u root -p
输入密码
mysql>create database cacti default character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql>grant all on cacti.* to cacti@localhost identified by '123';
Query OK, 0 rows affected (0.01 sec)
mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
二、安装cacti
安装rrdtool需要的软件包
# yum -y install cairo-devel libxml2-devel pango pango-devel
安装rrdtool
# tar zxvf rrdtool-1.4.4.tar.gz
# cd rrdtool-1.4.4
# ./configure --prefix=/usr/local/rrdtool && make && make install
# ln -s /usr/local/rrdtool/bin/* /usr/local/bin/ #此步非常重要
安装cacti-0.8.7.e中文版
#wget http://blogimg.chinaunix.net/blog/upfile2/090815172648.gz
# tar zxvf 090815172648.gz -C /var/www/html
# cd /var/www/html
# mv cacti-0.8.7e-cn-utf8/ cacti
# mysql -ucacti -p cacti < cacti/cacti.sql /导入数据库
Enter password:
创建cacti用户
# useradd -s /sbin/nologin cacti
# chown -R cacti:cacti /var/www/html/cacti/
# chmod -R 777 /var/www/html/cacti/
修改cacti配置文件,连接数据库
# vim /var/www/html/cacti/include/config.php
$database_username = "cacti";
$database_password = "123";
添加计划任务
# crontab -e
*/5 * * * * /usr/bin/php/var/www/html/cacti/poller.php >/dev/null 2>&1
# service crond restart
三、被监控端配置
安装snmp服务
#yum -y install lm_sensors net-snmp
修改配置文件如下:(这里我省略掉了#行和空行)
只改了下面三行,(下面的地址是cacti服务器地址,由于是本机测试,所以是127.0.0.1)
#vim /etc/snmp/snmpd.conf
41 com2sec notConfigUser 127.0.0.1 public
62 access notConfigGroup "" any noauth exact all none none
85 view all included .1 80 将前面#号去掉
重启服务
# service httpd restart
# service mysqld restart
# service snmpd restart
检测在服务器上,有信息返回说名正常:
#snmpwalk -c public -v 2c localhost
# snmpwalk -v 1 -c public localhostIP-MIB::ipAdEntIfIndex
浏览器输入http://192.168.163.143/cacti/install初始化cacti,最后如下:
如果打开网页,页面出现空白,那么说明数据库没有导入进去,重新导入数据库后重启httpd服务即可
RRDTOOLS版本,默认为1.0.x,改为1.3.x就行了
如果打开未看到生成图像,请手动执行下面命令
#/usr/bin/php/var/www/cacti/poller.php &>/dev/null手动生成图像
字体在图形中显示不正常的解决方法
下载并安装中文字体,我用的是微米黑字体
wget http://sourceforge.net/projects/wqy/files/wqy-microhei/0.2.0-beta/wqy-microhei-0.2.0-beta.tar.gz
#tar zxvf wqy-microhei-0.2.0-beta.tar.gz
# cd wqy-microhei
# cp wqy-microhei.ttc /usr/share/fonts/wqy-microhei.ttc
安装完成后注意在"设置"中更改下面两个必改项.
常规->RRDTool应用程序版本改为1.3.x,默认为1.0.x.不改可能图像不能正常显示出来.
路径->RRDTool默认字体路径改为上面安装的文件路径,如/usr/share/fonts/wqy-microhei.ttc
安装插件cacti-plugin
#wget http://blogimg.chinaunix.net/blog/upfile2/090818213852.gz
# mv 090818213852.gz cacti-plugin-0.8.7d-PA-v2.4-cn-utf8.diff.gz
# gunzip cacti-plugin-0.8.7d-PA-v2.4-cn-utf8.diff.gz
# mv cacti-plugin-0.8.7d-PA-v2.4-cn-utf8.diff /var/www/html/cacti/
# cd /var/www/html/cacti/
# patch -p1 -N<cacti-plugin-0.8.7d-PA-v2.4-cn-utf8.diff
# mysql -ucacti -p123 cacti <pa.sql
修改配置文件,如下:
# vim /var/www/html/cacti/include/config.php
$config['url_path'] = "/cacti/"; 在3306下一行添加,记住一定不要写成/cacti
页面设置
控制面板―用户管理―admin
插件管理打钩―保存
看到控制面板―配置,多个了插件管理
安装monitor扩展插件如下:wget http://cactiusers.org/downloads/monitor.tar.gz
#
# wget http://cactiusers.org/downloads/thold.tar.gz
# wget http://cactiusers.org/downloads/settings.tar.gz
# tar zxvf monitor.tar.gz -C /var/www/Cacti/plugins
# tar zxvf thold.tar.gz -C /var/www/Cacti/plugins
# tar zxvf settings.tar.gz -C /var/www/Cacti/plugins
# cd /var/www/html/cacti/plugins
# mysql -ucacti -p123 cacti <monitor/monitor.sql
# mysql -ucacti -p123 cacti < thold/thold.sql
# vim/var/www/html/cacti/include/config.php
$plugins[] = "monitor"; //添加此行
$plugins[] = "thold";
$plugins[] = "settings";
刷新网页即可
如果出现空白页,请查看apache日志
/var/log/httpd/error_log
四、安装nagios
建立用户
# useradd -s /sbin/nologin nagios
# mkdir -p /usr/local/nagios
# chown -R nagios.nagios /usr/local/nagios/
# chmod -R 755 /usr/local/nagios/
编译安装nagios
# tar xvf nagios-3.2.0.tar.gz
# cd nagios-3.2.0
# ./configure --prefix=/usr/local/nagios/
# make all
# make install
# make install-init
# make install-commandmode
# make install-config
# make install-webconf
# chkconfig --add nagios
# chkconfig nagios on
# service nagios start
安装插件
# tar xvf nagios-plugins-1.4.14.tar.gz
# cd nagios-plugins-1.4.14
# ./configure --prefix=/usr/local/nagios/ && make&& make install
安装Nagios的汉化包
# tar xvf nagios-cn-3.2.0.tar.bz2
# cd nagios-cn-3.2.0
# ./configure ; make all ; make install
安装sendmail
# yum install sendmail
#service sendmail start
修改apache的配置文件,最后添加如下内容,设置成需要输入密码才能登陆
# vim /etc/httpd/conf/httpd.conf
DirectoryIndex index.php index.html index.html.var
ScriptAlias /nagios/cgi-bin/ "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagions Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
建立登陆的用户名密码
# htpasswd -c /usr/local/nagios/etc/htpasswd nagiosadmin 首次创建用-c,密码随便写
# htpasswd /usr/local/nagios/etc/htpasswd benet //此用户访问nagios页面无权限
# service httpd restart
配置nagios添加主机,服务信息
在/usr/local/nagios/etc/objects/下建立2个文件:这2个文件默认没有,自己创建
编辑辑hosts.cfg配置文件编
# vim /usr/local/nagios/etc/objects/hosts.cfg定义主机的文件
define host{
use linux-server //引用模块templates.cfg
host_name web //定义主机名
alias benet-web //定义主机别名
address 192.168.1.1 //被监控端主机iP地址,此处是nagios服务器自己
}
define host{
use linux-server //引用模块templates.cfg
host_name mysql //定义主机名
alias benet-mysql //定义主机别名
address 192.168.1.2 //被监控端主机ip地址
}
define hostgroup{ //定义主机组
hostgroup_name sa-servers
alias sa servers
members web,mysql
}
编辑services.cfg配置文件
# vim /usr/local/nagios/etc/objects/services.cfg定义主机服务的文件
################- benet web-##########################
define service{ //定义被监控端主机上有哪些服务需要nagios监控,此处以本机自己为例
use local-service //引用local-service的属性信息,在template.cfg中定义host_namehost_name web //监控那个主机,host_name是在hosts.cfg中定义的
service_description ssh //对监控内容的描述
check_command check_ssh //指定监控的命令,check_ping在commands.cfg中定义
}
define service{
use local-service
host_name web
service_description Ping
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service
host_name web
service_description tcp
check_command check_tcp
#####################- MYSQL-##########################
define service{ //定义被监控端主机上有哪些服务需要nagios监控,即nagios客户端
use local-service
host_name mysql
service_description Ping
check_command check_ping
}
define service{
use local-service
host_name mysql
service_description ssh
check_command check_ssh
}
define service{
use local-service
host_name mysql
service_description ftp
check_command check_ftp
修改nagios.cfg文件,添加文件路径信息:
# vim/usr/local/nagios/etc/objects/nagios.cfg //nagios主配置文件
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg //主机配置文件
cfg_file=/usr/local/nagios/etc/objects/services.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/timeperiods.cfg //时间配置文件
cfg_file=/usr/local/nagios/etc/objects/templates.cfg //模板文件
修改contacts.cfg文件:
# vim/usr/local/nagios/etc/objects/conacts.cfg //服务器或主机出现问题时通知的联系人或联系组
35 email root@localhost ;
修改cgi.cfg文件,通过网页访问权限设置:
# vim/usr/local/nagios/etc/objects/cgi.cfg //赋予通过网页访问的用户执行权限,上面我们创建的nagiosadmin在此目录下默认已经赋予权限,现在我们将benet赋予权限
107 default_user_name=benet
119 authorized_for_system_information=nagiosadmin,benet
131 authorized_for_configuration_information=nagiosadmin,benet
144 authorized_for_system_commands=benet
157 authorized_for_all_services=nagiosadmin,benet
158 authorized_for_all_hosts=nagiosadmin,benet
171 authorized_for_all_service_commands=nagiosadmin,benet
172 authorized_for_all_host_commands=nagiosadmin,benet
Nagios的运行和维护:
测试nagios主文件是否配置正确
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
启动与停止nagios
# service nagios start
# service nagios stop
# service nagios status
# service nagios restart
四、Nagios性能分析图表(pnp)
安装pnp支持rrdtool工具
# tar zxf rrdtool-1.4.5.tar.gz
# cd rrdtool-1.4.5
# ./configure --prefix=/usr/local/rrdtool; make ; make install
安装rrdtool时候的报错:configure:error:Pleasefix thelibrary issues listed above and try again
解决办法:
yum -y install cairo-devel libxml2-devel pangopango-devel
如果上面执行后无果,请执行下面操作:
yum install cgiliblibart_lgpl-develpango-devel* cairo-devel* libxml2-devel
如果上面执行后无果,请执行下面操作:
yum -y install zlib libpng freetypelibart_lgpllibart_lgpl-devel libxml* pango*
通常编辑安装文件时,出现make无法安装,这其实是configure编译时出现了问题。
问题分析思路:先不要make,步骤分开进行,先configure编译,这样容易发现是哪一步出现的错误,并解决
安装pnp
# tar zxf pnp-0.4.13.tar.gz
# cd pnp-0.4.13
# ./configure--with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool--with-perfdata-dir=/usr/local/nagios/share/perfdata ; make all ; make install; make install-config ; make install-init
配置pnp文件
# cd /usr/local/nagios/etc/pnp/
# cp process_perfdata.cfg-sampleprocess_perfdata.cfg
# cp npcd.cfg-sample npcd.cfg
# cp rra.cfg-sample rra.cfg
# chown -R nagios:nagios/usr/local/nagios/etc/pnp/
# chmod -R 755 /usr/local/nagios/etc/pnp/
设置rw读取数据的权限,非常重要
#chown -R nagios.nagios /usr/local/nagios/var/rw
#chmod -R 777 /usr/local/nagios/var/rw
如果上面2部没有操作,nagios无法产生图像,原因,没有抓取到数据,无权限
修改process_perfdata.cfg文件
# vi/usr/local/nagios/etc/pnp/process_perfdata.cfg
44 LOG_LEVEL = 2
修改nginx主配置文件,增加小太阳图标
# cd /usr/local/nagios/etc/objetcs
# vi templates.cfg
define host{
name hosts-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$
process_perf_data 1
}
define service{
name services-pnp
register 0
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
process_perf_data 1
}
修改ngins主配置文件,去掉如***释行
# vim /usr/local/nagios/etc/nagios.cfg
833 process_performance_data=1
845 host_perfdata_command=process-host-perfdata
846service_perfdata_command=process-service-perfdata
修改commands.cfg主命令配置文件如下行
# vim/usr/local/nagios/etc/objects/commands.cfg
227 # 'process-host-perfdata' commanddefinition
228 define command{
229 command_name process-host-perfdata
230 command_line /usr/local/nagios/libexec/process_perfdata.pl
231 }
234 # 'process-service-perfdata'command definition
235 define command{
236 command_name process-service-perfdata
237 command_line /usr/local/nagios/libexec/process_perfdata.pl
238 }
修改hosts文件,增加定义pnp模块
# vim /usr/local/nagios/etc/objects/hosts.cfg定义主机的文件
define host{
use linux-server,hosts-pnp //引用模块templates.cfg
host_name web //定义主机名
alias benet-web //定义主机别名
address 192.168.1.1 //主机iP地址
}
define host{
use linux-server,hosts-pnp //引用模块templates.cfg
host_name mysql //定义主机名
alias benet-mysql //定义主机别名
address 192.168.1.2 //主机iP地址
}
define hostgroup{ //定义主机组
hostgroup_name sa-servers
alias sa servers
members web,mysql
}
编辑services.cfg配置文件
# vim /usr/local/nagios/etc/objects/services.cfg定义主机服务的文件
################- benet web-##########################
define service{
use local-service,services-pnp//引用local-service的属性信息,在template.cfg中定义
host_name host_name web //监控那个主机,host_name是在hosts.cfg中定义的
service_description ssh //对监控内容的描述
check_command check_ssh //指定监控的命令,check_ping在commands.cfg中定义
}
define service{
use local-service,services-pnp
host_name web
service_description Ping
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,services-pnp
host_name web
service_description tcp
check_command check_tcp
#####################- MYSQL-##########################
define service{
use local-service,services-pnp
host_name mysql
service_description Ping
check_command check_ping
}
define service{
use local-service,services-pnp
host_name mysql
service_description ssh
check_command check_ssh
}
define service{
use local-service,services-pnp
host_name mysql
service_description ftp
check_command check_ftp
}
刷新网页,出现小太阳图标
访问http://localhost/nagios/pnp
点击小太阳图标,出现对主机或服务器的监控页面
五、Nagios客户端配置
安装Nagios插件
# useradd -s /sbin/nologin nagios
# tar zxf nagios-plugins-1.4.14.tar.gz
# cd nagios-plugins-1.4.14
# ./configure ; make ;make install
# chown nagios:nagios/usr/local/nagios/
# chown -R nagios.nagios/usr/local/nagios/libexec/
安装nrpc插件
# tar zxf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure ; make all ; makeinstall-plugin ; make install-daemon ; make install-daemon-config
修改nrpe配置文件
# vi /usr/local/nagios/etc/nrpe.cfg
79 allowed_hosts=192.168.1.1#nagios服务器地址
44 server_address=192.168.1.2 #客户机本机地址
启动nrpe
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg-d
# netstat -antl |grep 5666
测试nrpe是否正常,回复版本信心号为正常
# /usr/local/nagios/libexec/check_nrpe-H 127.0.0.1
NRPE v2.12 //出现版本号,正常
定义nagios可以监测到本机的系统服务,我在末尾都添加了1作为区分,可以不修改
# vi /usr/local/nagios/etc/nrpe.cfg
199 command[check_users_1]=/usr/local/nagios/libexec/check_users-w 5 -c 10
200 command[check_load_1]=/usr/local/nagios/libexec/check_load-w 15,10,5 - c 30,25,20
201 command[check_sda1_1]=/usr/local/nagios/libexec/check_disk-w 20% -c 10 % -p /dev/sda1
202 command[check_zombie_procs_1]=/usr/local/nagios/libexec/check_procs-w 5 -c 10 -s Z
203 command[check_total_procs_1]=/usr/local/nagios/libexec/check_procs-w 1 50 -c 200
重启nrpe服务
# killall -9 nrpe
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg-d
# ps -elf |grep nrpe
六、Nagios服务端配置
安装nrpe插件
# tar zxf nrpe-2.12.tar.gz
# cd nrpe-2.12
# ./configure ; make ; makeinstall-plugin
测试插件能否与客户端192.168.233.204取得联系
# /usr/local/nagios/libexec/check_nrpe-H 192.168.6.200 //出现版本号为正常
定义check_nrpc监控命令
# vi/usr/local/nagios/etc/objects/commands.cfg
define command{
command_namecheck_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c$ARG1$
}
添加远程主机监控项,在services.cfg末尾添加
# vi /usr/local/nagios/etc/objects/services.cfg
define service{
use local-service
host_name mysql
service_description users
check_command check_nrpe!check_users_1
}
define service{
use local-service
host_name mysql
service_description load
check_command check_nrpe!check_load_1
}
define service{
use local-service
host_name mysql
service_description disk
check_command check_nrpe!check_sda1_1
}
define servicegroup{
servicegroup_name servergroup
alias server-group
members web,PING,web,SSH,web,SSHD,web,http,mysql,users,mysql,
load,mysql,disk
重启nagios服务器
# service nagios restart
六、整合cacti+nagios
整合cacti和nagios是利用了cacti的一个插件nagios for cacti,它的原理是将nagios的数据通过ndo2db导入到mysql数据库(cacti的库中),然后cacti读取数据库信息将nagios
的结果展示出来。
安装php-json扩展,如果已安装请略过此步。
# tar xf php-json-ext-1.2.1.tar.gz
# cd php-json-ext-1.2.1
# phpize
# sh configure && make&& make install
或直接yum安装
yum -y install php-devel
新建文件,添加信息如下:
# vim /etc/php.d/json.ini
extension=json.so
安装完成后如果先验证的话,可以写个index.php的页面看下,有json信息即可,如下:
[root@localhost~]# vim /var/www/html/index.php
<?php
phpinfo ();
?>
访问http://localhost/index.php
安装ndoutils,需要安装mysql-devel前面已经装过这里就不用再装了
# yum -y install mysql-devel
或源码安装
#wget http://nchc.dl.sourceforge.net/project/nagios/ndoutils-1.x/ndoutils-1.4b9/ndoutils-1.4b9.tar.gz
# tar xvf ndoutils-1.4b9.tar.gz
# cd ndoutils-1.4b9
#./configure --prefix=/usr/local/nagios/ --with-mysql-inc=/usr/include/mysql --with-mysql-lib=/usr/lib64/mysql --enable-mysql --disable-pgsql --with-ndo2db-user=nagios --with-ndo2db-group=nagios
# make(不用make install)
复制文件
# cp src/{ndomod-3x.o,ndo2db-3x,log2ndo,file2sock} /usr/local/nagios/bin/
# cp config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
# cp config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
# chmod 644 /usr/local/nagios/etc/ndo*
# chown -R nagios.nagios /usr/local/nagios/*
修复配置文件信息,连接数据库
# vim /usr/local/nagios/etc/ndo2db.cfg //红色数字为行号
34 socket_type=tcp //将此行#号去掉,#socket_type=unix前面加上#号
102 db_name=cacti //创建的数据库的名字
111 db_prefix=npc_
120 db_user=cacti //cacti数据库登陆用户名
121 db_pass=123 //cacti数据库登陆密码
# vim /usr/local/nagios/etc/ndomod.cfg
25 output_type=tcpsocket //将此行#号去掉,#output_type=unixsocket前面加上#号
39 output=127.0.0.1 //将此行#去掉,#output=/usr/local/nagios//var/ndo.sock前面加上#号
启动ndo2db
# /usr/local/nagios/bin/ndo2db-3x -c/usr/local/nagios/etc/ndo2db.cfg
# ps �Celf |grep ndo2db //查看进程是否启动
修改nagios添加信息
# vim /usr/local/nagios/etc/nagios.cfg //在最后添加如下行
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
安装npc
#wget http://www.constructaegis.com/downloads/npc-2.0.4.tar.gz
# tar xvf npc-2.0.4.tar.gz -C /var/www/html/cacti/plugins
# chown -R apache.apache /var/www/html/cacti/plugins/npc/
修改nagios添加信息:
# vim /usr/local/nagios/etc/nagios.cfg
broker_module=/usr/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
启动nagios
# service nagios restart
如果在网页没有出现npc插件,请添加如下行
# vim /var/www/html/cacti/include/config.php
$plugins[]='npc';
启动之后在cacti中添加npc,如下:
当访问到npc这个页面的时候已经擦不多了
但是看npc里发现空白的,没有数据,是因为php没有PDO_MYSQL支持,所以安装PDO_MYSQL,如果已安装请直接修改数据库。
查看index.php网页,如果有则不用安装,否则安装
# wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
# tar zxvf PDO_MYSQL-1.0.2.tgz
# cd PDO_MYSQL-1.0.2
# /usr/local/php/bin/phpize
#./configure --with-php-config=/usr/bin/php-config
#make
#make install
修改数据库结构:
# mysql -u root �Cp
mysql> use cacti; //复制粘贴如下内容即可
alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output;
alter table npc_hostchecks add long_output TEXT NOT NULL default '' after output;
alter table npc_hoststatus add long_output TEXT NOT NULL default '' after output;
alter table npc_notifications add long_output TEXT NOT NULL default '' after output;
alter table npc_servicechecks add long_output TEXT NOT NULL default '' after output;
alter table npc_servicestatus add long_output TEXT NOT NULL default '' after output;
alter table npc_statehistory add long_output TEXT NOT NULL default '' after output;
alter table npc_systemcommands add long_output TEXT NOT NULL default '' after output;
重新启动服务:
# service httpd restart
# service mysqld restart
# service nagios restart
之后再访问就可看到有数据了:
当查看/var/log/message文件时,出现如下错误,也请添加数据库
Nov 14 14:47:30 localhost ndo2db-3x: Error: mysql_query() failed for 'INSERT INTO npc_servicechecks SET instance_id='0', service_object_id='0', check_type='0',