Centos6.2
一、安装环境LAMP
#yum -y install gcc* glibc* gd* php* mysql* http*
#yum -y install httpd mysql-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 rootpassword '123'
报错处理: # export PATH=$PATH:/usr/local/mysql/bin
# mysql -u root �Cp
:输入密码
mysql>createdatabase cacti default character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql>grantall on cacti.* to cacti@localhost identified by '123';
Query OK, 0 rows affected (0.01 sec)
mysql>flushprivileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
二、安装cacti
1、安装rrdtool需要的软件包
# yum -y install cairo-devellibxml2-devel pango pango-devel
2、安装rrdtool
# tar xf 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/#此步非常重要
3、安装cacti-0.8.7.e中文版
#wget http://blogimg.chinaunix.net/blog/upfile2/090815172648.gz
# tar xvf090815172648.gz -C /var/www/html
# cd /var/www/html
# mv cacti-0.8.7e-cn-utf8/cacti
# mysql -ucacit �Cp cacti <cacti/cacti.sql /导入数据库
Enter password:
4、创建cacti用户
# useradd -s /sbin/nologin cacti
# chown -R cacti:cacti /var/www/html/cacti/
# chmod -R 777 /var/www/html/cacti/
5、修改cacti配置文件连接数据库
# vim /var/www/html/cacti/include/config.php
$database_username = "cacti";
$database_password = "123";
6、添加计划任务
# crontab �Ce
*/5 * * * * /usr/bin/php /var/www/html/cacti/poller.php >/dev/null2>&1
# service crond restart
三、被监控端配置
1、安装snmp服务
#yum install lm_sensorsnet-snmp
2、修改配置文件如下这里我省略掉了#行和空行
只改了下面三行下面的地址是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
3、重启服务
# service httpd restart
# service mysqld restart
# service snmpd restart
#到此就差不多了我们可以做些检测在服务器上有信息返回说名正常
#snmpwalk -cpublic -v 2c localhost
# snmpwalk -v 1 -c public localhost IP-MIB::ipAdEntIfIndex
4、浏览器输入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手动生成图像
5、字体在图形中显示不正常的解决方法
下载并安装中文字体我用的是微米黑字体
wgethttp://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
6、安装插件cacti-plugin
#wget http://blogimg.chinaunix.net/blog/upfile2/090818213852.gz
# mv 090818213852.gzcacti-plugin-0.8.7d-PA-v2.4-cn-utf8.diff.gz
# gunzipcacti-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
7、修改配置文件如下
# vim /var/www/html/cacti/include/config.php
$config['url_path'] = "/cacti/";; 在3306下一行添加记住一定不要写成/cacti
9、页面设置
控制面板―用户管理―admin
插件管理打钩―保存
看到控制面板―配置多个了插件管理
9、安装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 xvf monitor.tar.gz -C/var/www/Cacti/plugins
# tar xvf thold.tar.gz -C/var/www/Cacti/plugins
# tar xvf 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
1、建立用户
# useradd -s /sbin/nologins nagios
# mkdir -p /usr/local/nagios
# chown -R nagios.nagios/usr/local/nagios/
# chmod �CR 755 /usr/local/nagios/
2、编译安装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
3、安装插件
# 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
4、修改apache的配置文件
# vim /etc/httpd/conf/httpd.conf
3.最后添加如下内容设置成需要输入密码才能登陆
DirectoryIndex index.php index.htmlindex.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>
5、建立登陆的用户名密码
# htpasswd -c /usr/local/nagios/etc/htpasswd nagiosadmin 首次创建用-c
# htpasswd /usr/local/nagios/etc/htpasswd benet //此用户访问nagios页面无权限
# service httpd restart
6、配置nagios添加主机服务信息
在/usr/local/nagios/etc/objects/下建立2个文件这2个文件默认没有自己创建
# vim /usr/local/nagios/etc/objects/hosts.cfg定义主机的文件
define host{
use linux-server //引用模块templates.cfg
host_name 192.168.163.143 //定义主机名
alias 192.168.163.143 //定义主机别名
address 192.168.163.143 //主机iP地址
}
# vim /usr/local/nagios/etc/objects/services.cfg 定义主机服务的文件
define service{
use local-service //引用local-service的属性信息在template.cfg中定义
host_name 192.168.163.143 //监控那个主机host-142在hosts.cfg中定义
service_description ssh //对监控内容的描述
check_command check_ssh //指定监控的命令check_ping在commands.cfg中定义
}
define service{
use local-service
host_name 192.168.163.143
service_description Ping
check_command check_ping
}
7、修改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 //模板文件
8、修改contacts.cfg文件
# vim /usr/local/nagios/etc/objects/conacts.cfg //服务器或主机出现问题时通知的联系人或联系组
35 email root@localhost ;
9、修改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
10、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
四、整合cacti+nagios
整合cacti和nagios是利用了cacti的一个插件nagiosfor cacti它的原理是将nagios的数据通过ndo2db导入到mysql数据库cacti的库中然后cacti读取数据库信息将nagios
的结果展示出来。
1、安装php-json扩展因为NPC使用了ExtJS如过没有json的扩展NPC界面无法出来,如果已安装请略过此步。
# yum -y install php-devel
或源码安装
# tar xf php-json-ext-1.2.1.tar.gz
# cd php-json-ext-1.2.1
# phpize
# sh configure && make&& make install
2、新建文件添加信息如下
# 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
3、安装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)
4、复制文件
# 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/*
5、修复配置文件信息,连接数据库
# 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前面加上#号
6、启动ndo2db
# /usr/local/nagios/bin/ndo2db-3x -c/usr/local/nagios/etc/ndo2db.cfg
# ps �Celf |grep ndo2db
7、安装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/
8、修改nagios添加信息:
# vim /usr/local/nagios/etc/nagios.cfg //在最后添加如下行
broker_module=/usr/local/nagios/bin/ndomod-3x.oconfig_file=/usr/local/nagios/etc/ndomod.cfg
831 process_performance_data=1 //找到此行去掉前面#号
如果打开cacti无npc插件,请修改如下行
# vim /var/www/html/cacti/include/config.php
$plugins[]='npc';
# service nagios restart
启动之后在cacti中添加npc如下
当访问到npc这个页面的时候已经擦不多了
但是看npc里发现空白的,没有数据,是因为php没有PDO_MYSQL支持,所以安装PDO_MYSQL,如果已安装请直接修改数据库。
查看index.php网页,看是否安装过pdo-mysql,如果没安装,执行下面操作安装
# wgethttp://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> usr cacti; //复制粘贴如下内容即可
alter tablenpc_eventhandlers add long_output TEXT NOT NULL default '' after output;
alter tablenpc_hostchecks add long_output TEXT NOT NULL default '' after output;
alter tablenpc_hoststatus add long_output TEXT NOT NULL default '' after output;
alter tablenpc_notifications add long_output TEXT NOT NULL default '' after output;
alter tablenpc_servicechecks add long_output TEXT NOT NULL default '' after output;
alter tablenpc_servicestatus add long_output TEXT NOT NULL default '' after output;
alter tablenpc_statehistory add long_output TEXT NOT NULL default '' after output;
alter tablenpc_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',