centos7下搭建安装cacti

1.搭建lamp环境
配置apache
[root@cacti-server ~]# yum -y install httpd
[root@cacti-server ~]# systemctl start httpd
[root@cacti-server ~]# systemctl enable httpd
[root@cacti-server ~]# firewall-cmd --permanent --add-service=http
success
[root@cacti-server ~]# firewall-cmd --reload
success

配置mariadb
[root@cacti-server ~]# yum -y install mariadb-server mysql-devel
[root@cacti-server ~]# systemctl start mariadb
[root@cacti-server ~]# mysql_secure_installation
Set root password? [Y/n]
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
[root@cacti-server ~]# mysql -u root -p
MariaDB [(none)]> grant all privileges on . to test@localhost identified by ‘RedHat’; 创建用于测试php和mariadb连通性的用户
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
[root@cacti-server ~]# systemctl restart mariadb
[root@cacti-server ~]# systemctl enable mariadb
[root@cacti-server ~]# firewall-cmd --permanent --add-port=3306/tcp
success
[root@cacti-server ~]# firewall-cmd --reload
success

配置php
[root@cacti-server ~]# yum -y install php php-mysql php-gd php-pear
[root@cacti-server ~]# vim /etc/php.ini
date.timezone =PRC 修改时区
[root@cacti-server ~]# vim /var/www/html/index.php 编辑测试页面

<?php $conn=mysql_connect('localhost','test','redhat'); if ($conn) echo "database connect ok"; else echo "database connect failure"; ?> <?php phpinfo() ?>

[root@cacti-server ~]# systemctl restart httpd
测试
在这里插入图片描述
2.安装配置cacti
下载软件
[root@cacti-server ~]# cd /usr/local/src/
[root@cacti-server src]# wget http://www.cacti.net/downloads/cacti-0.8.8f.tar.gz
[root@cacti-server src]# tar zxvf cacti-0.8.8f.tar.gz
[root@cacti-server src]# mv cacti-0.8.8f /var/www/html/cacti
创建cacti数据库和cacti用户,赋予权限
[root@cacti-server ~]# mysql -u root -p
MariaDB [(none)]> create database cacti default character set utf8;
MariaDB [(none)]> grant all privileges on cacti.* to cacti@localhost identified by ‘redhat’;
MariaDB [(none)]> flush privileges;
把cacti.sql导入数据库
[root@cacti-server cacti]# mysql -ucacti -predhat cacti < /var/www/html/cacti/cacti.sql
编辑config.php和global.php
[root@cacti-server cacti]# vim /var/www/html/cacti/include/config.php|global.php
$database_type = “mysql”;
$database_default = “cacti”;
$database_hostname = “localhost”;
$database_username = “cacti”;
$database_password = “redhat”;
$database_port = “3306”;
$database_ssl = false;

安装rrdtool以生成图像
[root@cacti-server src]# yum -y install rrdtool rrdtool-devel rrdtool-php rrdtool-perl
[root@cacti-server src]# yum -y install gd gd-devel php-gd —rrdtool绘制图像需要的图形库

安装snmp服务
[root@cacti-server cacti]# yum -y install net-snmp net-snmp-utils php-snmp net-snmp-libs

编辑配置文件
[root@cacti-server ~]# 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
[root@cacti-server ~]# systemctl restart snmpd.service
[root@cacti-server ~]# systemctl enable snmpd.service

授权目录权限
[root@cacti-server ~]# useradd -r -M cacti
[root@cacti-server ~]# chown -R cacti /var/www/html/cacti/rra/
[root@cacti-server ~]# chown -R cacti /var/www/html/cacti/log/

配置一个抓图的计划任务
[root@cacti-server ~]# crontab -e
*/5 * * * * /usr/bin/php /var/www/html/cacti/poller.php >> /tmp/cacti_rrdtool.log

浏览器访问cacti管理页面进行安装:http://ip/cacti(其中IP为安装cacti服务器的地址)

测试
[root@cacti-server ~]# /usr/bin/php /var/www/html/cacti/poller.php
OK u:0.00 s:0.01 r:0.80
OK u:0.00 s:0.02 r:1.21
OK u:0.00 s:0.02 r:1.39
OK u:0.00 s:0.02 r:1.50
OK u:0.00 s:0.02 r:1.87

扫描二维码关注公众号,回复: 8900613 查看本文章

10/21/2016 04:02:32 PM - SYSTEM STATS: Time:1.4211 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:5 RRDsProcessed:5

3.安装Spine
注:由于cacti默认使用cmd.php来轮询数据,速度会很慢,特别是在监控节点比较多的情况下,cmd.php就更显不足了,因此我们采用Spine来轮询数据。cacti-spine是一个由C语言开发的,用于替代cmd.php的快速获取数据的引擎。

[root@cacti-server ~]# cd /usr/local/src/
[root@cacti-server src]#wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.8f.tar.gz
[root@cacti-server src]# tar zxvf cacti-spine-0.8.8f.tar.gz
[root@cacti-server cacti-spine-0.8.8f]# ./configure
[root@cacti-server cacti-spine-0.8.8f]# make
[root@cacti-server cacti-spine-0.8.8f]# make install
拷贝snmp的配置文件

[root@cacti-server ~]# cp /usr/local/spine/etc/spine.conf.dist /etc/spine.conf
注:spine默认配置文件需要放在/etc才会生效,否则测试时会报如下错误:
SPINE: Poller[0] FATAL: Unable to read configuration file! (Spine init)

编辑配置文件
[root@cacti-server ~]# vim /etc/spine.conf
DB_Host localhost
DB_Database cacti
DB_User cacti
DB_Pass RedHat
DB_Port 3306

测试
[root@cacti-server ~]# /usr/local/spine/bin/spine
SPINE: Using spine config file [/etc/spine.conf]
SPINE: Version 0.8.8f starting
SPINE: Time: 0.1278 s, Threads: 5, Hosts: 2

修改web的设置
进入Cacti页面设置spine路径
Console -> Configuration -> Settings -> Paths -> Alternate Poller Path -> Spine Poller File Path->/usr/local/spine/bin/spine
修改Cacti使用的Poller Type
Console -> Configuration -> Settings ->Poller->Poller Type->spine
查看日志
[root@cacti-server ~]# cat /var/www/html/cacti/log/cacti.log
10/22/2016 12:45:50 AM - SYSTEM STATS: Time:0.1146 Method:spine Processes:1 Threads:1 Hosts:2 HostsPerProcess:2 DataSources:0 RRDsProcessed:0

4.添加被监控主机
安装snmp服务

[root@cacti-client ~]# yum -y install net-snmp net-snmp-devel net-snmp-utils
编辑配置文件

[root@cacti-client ~]# vim /etc/snmp/snmpd.conf
41 com2sec notConfigUser 192.168.23.156 public
62 access notConfigGroup “” any noauth exact all none none
85 view all included .1 80

[root@cacti-client ~]# systemctl restart snmpd
[root@cacti-client ~]# systemctl enabled snmpd

配置防火墙
[root@cacti-client ~]# firewall-cmd --permanent --add-port=161/udp
success
[root@cacti-client ~]# firewall-cmd --reload
success

5.监控apache
客户端的配置
编辑apache的配置文件,加入server-status模块的设置
[root@cacti-client ~]# vim /etc/httpd/conf/httpd.conf
ExtendedStatus On
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from all

[root@cacti-client ~]# systemctl restart httpd
[root@cacti-client ~]# systemctl enable httpd
查看apache加载的模块
[root@cacti-client ~]# apachectl -t -D DUMP_MODULES|grep status
status_module (shared)
访问http://192.168.23.157/server-status查看模块的详细信息
在这里插入图片描述[root@cacti-client ~]# firewall-cmd --permanent --add-service=http
success
[root@cacti-client ~]# firewall-cmd --reload
success

服务端的配置
安装监控apache的php页面
[root@cacti-server ~]# cd /usr/local/src/
[root@cacti-serversrc]#wget http://forums.cacti.net/download/file.php?id=18576&sid=8d429b69af5be45179d928e1303f2077
[root@cacti-server src]# unzip ApacheStats_0.8.2.zip
[root@cacti-server src]# cd ApacheStats_0.8.2/
[root@cacti-serverApacheStats_0.8.2]#cp ss_apache_stats.php /var/www/html/cacti/scripts/

导入模板
在Cacti Web界面导入cacti_host_template_webserver_-_apache.xml模板:
点击Import/Export->Import Templates,上传模板即可
添加服务器并创建图表
登录Cacti Web界面,添加被监控apache服务器设备,并创建相应图表:
Devices->Add->WebServer–Apache模板->Create New Graphs,添加所需图表即可。等待一段时间就会出图

6.监控mariadb
[root@cacti-client ~]# mysql -uroot -predhat
MariaDB [(none)]> grant process,super,replication client on *.*to ‘mysqltest’@‘192.168.23.156’ identified by ‘redhat’; 创建用于监控主机连接mariadb进行监控的用户

MariaDB [(none)]> flush privileges;
[root@cacti-client ~]# systemctl restart mariadb
[root@cacti-client ~]# firewall-cmd --permanent --add-port=3306/tcp
success

[root@cacti-client ~]# firewall-cmd --reload
success
安装监控mariadb的php页面文件

[root@cacti-server ~]# cd /usr/local/src/
[root@cacti-serversrc]#wget https://www.percona.com/downloads/percona-monitoring-plugins/1.1.6/percona-monitoring-plugins-1.1.6.tar.gz
[root@cacti-server src]# tar zxvf percona-monitoring-plugins-1.1.6
[root@cacti-server src]# cd percona-monitoring-plugins-1.1.6/cacti/scripts/
[root@cacti-serverscripts]#cpss_get_mysql_stats.php /var/www/html/cacti/scripts/

编辑页面文件
[root@cacti-server scripts]# vim /var/www/html/cacti/scripts/ss_get_mysql_stats.php
$mysql_user = ‘mysqltest’; 用于监控主机连接mariadb的用户
$mysql_pass = ‘redhat’; 用户密码

导入模板
在Cacti Web界面导入cacti_host_template_percona_mysql_server_ht_0.8.6i-sver1.1.6.xml模板:
点击Import/Export->Import Templates,上传模板即可
添加服务器并创建图表
登录Cacti Web界面,添加被监控mariabdb服务器设备,并创建相应图表:
Devices->Add->dbServer–Mysql模板->Create New Graphs,添加所需图表即可。等待一段时间就会出图

7.邮件报警
下载插件
[root@cacti-server ~]# cd /usr/local/src/
[root@cacti-server src]# tar zxvf settings-v0.71-1.tgz
[root@cacti-server src]# mv settings /var/www/html/cacti/plugins/
[root@cacti-server src]# tar zxvf thold-v0.5.0.tgz

[root@cacti-server src]# mv thold /var/www/html/cacti/plugins/
访问cacti管理页面安装插件

测试
在这里插入图片描述告警被触发
查看邮件
[root@cacti-server ~]# cat /var/spool/mail/root


To: [email protected]
Subject: ALERT: test - Used Space - / [hdd_used] [hdd_used] went above threshold of 25 with 36.8871
From: Cacti [email protected]
Date: Mon, 24 Oct 2016 14:00:03 +0800

An alert has been issued that requires your attention.

Host: test (192.168.23.157)
URL: http://192.168.23.156/cacti//graph.php?local_graph_id=103&rra_id=1
Message: ALERT: test - Used Space - / [hdd_used] [hdd_used] went above threshold of 25 with 36.8871

Content-Type: image/jpg
Content-Disposition: inline; filename=“103.jpg”

发布了9 篇原创文章 · 获赞 1 · 访问量 2449

猜你喜欢

转载自blog.csdn.net/weixin_41915416/article/details/94624159
今日推荐