这是一个学习过程中笔记,如果有大部分雷同,则说明你和我肯定是同学,只是不认识吧了!
cacti概述
cacti使用php语言来实现的一款软件,主要功能是用snmp服务获取相关数据,然后用rrdtool存储和更新数据在有需要时用rrdtool生成图标供用户查看相关数据,因此,snmp和rrdtool是cacti的关键。snmp关系着数据的收集,rrtdool关系着数据的存储和图表的生成。
mysql搭配php程序用来存储一些变量数据并对变量数据进行调用,如主机名,主机ip,snmp团体名,端口号,模版信息等变量。而snmp抓取到数据后不是存储在mysql中,而是存储在rrtdool生成的RRD文件中(在Cacti根目录的rra文件夹下)。rrdtool对数据的更新和存储就是对RRD文件的处理。RRD文件是大小固定的档案文件(Round Robin Archive),能够存储的数据量在创建时已经定义。
下图是cacti的架构:
下图是cacti的工作模式:
cacti的部署:
因为cacti依赖于lnmp环境,因此在部署cacti之前首先需要搭建lnmp环境。
首先我们借助yum安装的方式安装mysql,apache,php及相关插件:
yum install -y httpd php php-mysql mysql mysql-server mysql-devel php-gd libjpeg libjpeg-devel libpng libpng-devel
启动http,如下:
[[email protected] ~]# /etc/init.d/httpd start Starting httpd: httpd: Could not reliably determine the server‘s fully qualified domain name, using 211.98.71.195 for ServerName [ OK ] /启动http的时候会出现如上问题,解决办法如下: [[email protected] ~]# vim /etc/httpd/conf/httpd.conf /打开这个文件,把其中的#ServerName 修改为 ServerName localhost:80 然后在重启一下: [[email protected] ~]# /etc/init.d/httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
启动mysql:如下
[[email protected] ~]# /etc/init.d/mysqld start Initializing MySQL database: Installing MySQL system tables... 160508 3:37:29 [Warning] ‘--skip-locking‘ is deprecated and will be removed in a future release. Please use ‘--skip-external-locking‘ instead. OK Filling help tables... 160508 3:37:29 [Warning] ‘--skip-locking‘ is deprecated and will be removed in a future release. Please use ‘--skip-external-locking‘ instead. OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /usr/bin/mysqladmin -u root password ‘new-password‘ /usr/bin/mysqladmin -u root -h lvs password ‘new-password‘ Alternatively you can run: /usr/bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /usr ; /usr/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/mysql-test ; perl mysql-test-run.pl Please report any problems with the /usr/bin/mysqlbug script! [ OK ] Starting mysqld: [ OK ]
安装cacti及其所需要的插件:(我们在这里部署的是主监控机,暂时并没有客户端)
[[email protected] ~]# yum install -y cacti net-snmp net-snmp-utils rrdtool net-snmp-devel net-snmp-libs lm-sensors php-xml zlib libpng freetype cairo-devel pango-devel gd [[email protected] ~]# /etc/init.d/snmpd start /启动snmp服务 Starting snmpd: [ OK ] [[email protected] ~]# mysql -u root /mysql数据库 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.1.73-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> create database cacti; /创建cacti数据库! mysql> grant all on cacti.* to ‘cactiuser‘@‘localhost‘ identified by ‘cactiuser‘; /创建用户,并且给用户授权。 Query OK, 0 rows affected (0.00 sec) mysql> use cacti; /选择cacti数据库 Database changed mysql> source /usr/share/doc/cacti-0.8.8b/cacti.sql; /导入sql文件 Query OK, 0 rows affected (0.01 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Query OK, 0 rows affected (0.04 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.01 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) ········· mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit; Bye
编辑cacti配置文件:
[[email protected] ~]# vim /usr/share/cacti/include/config.php ····· $database_type = "mysql"; $database_default = "cacti"; $database_hostname = "localhost"; $database_username = "cactiuser"; /在数据库中授权时的名字及密码 $database_password = "cactiuser"; $database_port = "3306"; $database_ssl = false; ····· [[email protected] ~]# vim /etc/httpd/conf.d/cacti.conf Deny from all 修改为 Allow from all [[email protected] ~]# /etc/init.d/httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
然后就是web访问,在浏览器中输入http://ip/cacti!在浏览器访问的时候,总会出现如下问题
FATAL: Cannot connect to MySQL server on ‘localhost‘. Please make sure you have specified a valid MySQL database name in ‘include/config.php‘
当初一直觉得是数据库授权的问题,然后各种检测也没有错误,后来一条命令解决了如下:
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
正常之后,在浏览器输入http://ip/cacti会出现如下界面:
这一步直接next,然后会出现如下界面:
因为我们是第一次安装,所以选择新安装,然后next:出现如下界面:
这一步我们看见各种文件的位置,若是全部显示Found,则可以直接点击finish,若不是可以自己找到合适的位置,一般都是默认即可!下一步:
输入帐号和密码,都为admin,默认的,然后下一步会强制修改密码:
点击save,然后进入下一步:
点击左边的settings----path打开控制台,如下图:
点击graps进入监控系统界面,发现并没有图形如下图:
这时候我们运行如下命令生成图形:
[[email protected] ~]# /usr/bin/php /usr/share/cacti/poller.php PHP Warning: strtotime(): It is not safe to rely on the system‘s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Chongqing‘ for ‘CST/8.0/no DST‘ instead in /usr/share/cacti/include/global_constants.php on line 165 PHP Warning: date(): It is not safe to rely on the system‘s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Chongqing‘ for ‘CST/8.0/no DST‘ instead in /usr/share/cacti/include/global_constants.php on line 165 tifier. We selected ‘Asia/Chongqing‘ for ‘CST/8.0/no DST‘ instead in /usr/share/cacti/include/global_constants.php on line 166 ·······/过程很长,结束时我们可以查看一下命令返回值! [[email protected] ~]# echo $? 0 /返回数字“0”说明,命令运行结果正确
这时候在查看一下监控图像会发现如下图:
会发现已经出现了坐标,但是没有图像;我们刚才运行的命令,写入计划任务:
[[email protected] ~]# crontab -e no crontab for root - using an empty one crontab: installing new crontab [[email protected] ~]# crontab -l */5 * * * * /usr/bin/php /usr/share/cacti/poller.php > /dev/null 2>&1
过一段时间之后会发现已经出现图形如下:因为自己的模拟机负载不是很高,这一段时间会挺长的!
至此我们也就搭建完成了cacti的监控!对于cacti的应用及cacti怎么添加监控客户端,会在后续的博文中继续更新的!