简介php
在企业网络运维过程当中,管理员必须时刻关注服务器的运行状态,如CPU、内存、磁盘空间使用状况等。为了可以及时的发现问题,尽可能减小故障的发生。当网络中的设备,服务器等数量较多时,能够部署一套监控系统来实时跟踪服务器,咱们一般会借助一些软件来实现html
今天咱们就以Cacti套件为例,介绍服务器集中监测体系的构建和使用python
Cacti是一款使用PHP语言开发的性能与流量监测工具,监测的对象可使linux或windows服务器也能够是路由交换等网络设备,主要是基于SNMP(Simple Network Management Protocol 简单网络管理协议)来搜集CPU、占用、内存使用、运行进程数以及磁盘使用空间、网卡流量等各类数据。说白了就是监控服务器状态。mysql
实际上Cacti自己只是一个Web界面的管理套件,经过调用Net-SNMP工具来采集检测数据 并结合RRDtool(Round Robin Database tool 轮询数据库工具)记录数据并绘制图片。如图1所示linux
图1web
Cacti提供了优秀的整合和协调能力,充分利用LAMP技术平台、SNMP协议工具以及RRDtool数据引擎、不只配置简单、直观、并且支持插件和数据模板,使用时很是整洁,便于扩展。sql
实际上Cacti能够从逻辑上分为三个部分、第一部分:被检测的对象,也就是客户端。第二部分:检测平台,也就是服务器端。第三部分:管理平台。Cacti采用了两种结构实现功能,一种是C/S结构主要是实现服务器到客户端监控另一种就是B/S主要完成对Cacti平台的管理。从图1咱们能够看出,管理端是在Web上对Cacti进行管理。数据库
简单的整理一下Cacti的工做过程。vim
首先经过LAMP平台部署Cacti监控工具,以后经过客户端的web登陆Cacti进行多其余客户端的设置,能够从图1看到Cacti采集数据时必需要使用SNMP协议来链接客户端进而可以采集数据吗,也就是说客户端必须支持SNMP协议。监控以后经过RRDtool来生成图表,其实这个图表是静态的,通过频繁的数据采集,咱们也能够认为是动态的,也就是说Cacti是基于某段时间的监控。windows
主机 |
操做系统 |
IP地址 |
主要软件 |
centos1 |
CentOS 7 x86_64 |
192.168.92.130 |
Cacti-0.8.8b.tar.gz Rrdtool-1.4.8.tar.gz |
centos2 |
CentOS 7 x86_64 |
192.168.1.132 |
Mysql.5.5.tar.gz |
构建Cacti集中监测平台的服务器端时,应提早安装好LAMP或者是LNMP平台,以及Net-SNMP和RRDtool等软件支持,而后下载Cacti源码包
前面已经学习过了如何部署源码包的LAMP或LNMP平台,为了方便演示这里直接以RPM包的LAMP平台为基础搭建Cacti。所需安装包以下所述。若还提示缺乏一些包请在以YUM的方式进行安装便可。
# yum -y install httpd # yum -y install mariadb mariadb-server # yum -y install zlib freetype libjpeg fontconfig gd libxml2 libxml2-devel php-gd //安装PHP须要的库文件 # yum -y install php php-mysql # systemctl start httpd.service # chkconfig httpd on # systemctl start mariadb # chkconfig mariadb on # setenforce 0 # iptables -I INPUT -p tcp --dport 80 -j ACCEPT # iptables -I INPUT -p tcp --dport 3306 -j ACCEPT # mysqladmin -u root -p password '123.abc' Enter password: (此处直接回车便可)
服务启动以后咱们能够编写测试页来验证LAMP平台的协同工做
# cat /var/www/html/test.php <?php $link=mysql_connect('localhost','root','123.abc'); if ($link) echo "mysql connect is ok!!"; mysql_close(); phpinfo(); ?>
访问页面测试
通过测试LAMP平台已经可以彻底的结合在一块儿并运行。
以后在客户端进行访问
Cacti平台经过SNMP协议采集检测数据,这些工具由net-snmp-utils软件包提供,所需安装的软件包以下所示。
# yum -y install net-snmp net-snmp-utils
下载rrdtool软件到/root目录下
经过YUM确认并安装相关软件包
# yum -y install cairo-devel zlib libxml2 libxml2-devel glib2 glib2-devel libpng libpng-devel freetype freetype-devel libart_lgpl pango pango-devel perl-CPAN
若是提示须要其余软件包,在另行安装。
安装rddtool源码包
# tar -xvf rrdtool-1.4.5.tar.gz # cd rrdtool-1.4.5/ # ./configure --prefix=/usr/local && make && make install
下载到root目录下
将下载的源码包解压到root下并移动到网页文档根目录下更名为cacti
# wget http://www.cacti.net/downloads/cacti-0.8.8b.tar.gz # tar -xvf cacti-0.8.8b.tar.gz # mv cacti-0.8.8b /var/www/html/cacti
添加一个用户,用来读写检测数据的用户帐户(如cacti),并调整期目录的属主,以便正常读取以及写入数据
# useradd cacti # chown -R cacti:cacti /var/www/html/cacti/
先建立一个用于cacti检测平台的数据库,并受权一个用户cactiuser而后使用cacti源码目录下的cacti.sql脚本,导入预设的各类数据表
# mysql -uroot -p123.abc Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create database cacti default character set utf8; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all on cacti.* to cactiuser@'localhost' identified by '123.abc'; Query OK, 0 rows affected (0.11 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.16 sec)
导入
# cd /var/www/html/cacti/
# mysql -u cactiuser -p cacti < cacti.sql
Enter password: //输入密码
[root@localhost cacti]# mysql -uroot -p123.abc Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 8 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> use cacti; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [cacti]> show tables; //验证是否导入表到cacti库中
+---------------------------+ | Tables_in_cacti | +---------------------------+ | cdef | | cdef_items | | colors | | data_input | | data_input_data | | data_input_fields | | data_local | | data_template | | data_template_data | | data_template_data_rra | | data_template_rrd | | graph_local | | graph_template_input | | graph_template_input_defs | | graph_templates | | graph_templates_gprint | | graph_templates_graph | | graph_templates_item | | graph_tree | | graph_tree_items | | host | | host_graph | | host_snmp_cache | | host_snmp_query | | host_template | | host_template_graph | | host_template_snmp_query | | plugin_config | | plugin_db_changes | | plugin_hooks | | plugin_realms | | poller | | poller_command | | poller_item | | poller_output | | poller_reindex | | poller_time | | rra | | rra_cf | | settings | | settings_graphs | | settings_tree | | snmp_query | | snmp_query_graph | | snmp_query_graph_rrd | | snmp_query_graph_rrd_sv | | snmp_query_graph_sv | | user_auth | | user_auth_perms | | user_auth_realm | | user_log | | version | +---------------------------+ 52 rows in set (0.00 sec)
上诉操做中,建立Cacti库时,指定默认的编码字符集为utf8,便于中文支持,导入预设库时最好是使用cactiuser导入。最后验证一下是否导入。
cacti的配置文件位于源码目录下的include/目录下,名称为config.php,要使Cacti系统可以正常访问并使用数据库,必须修改config.php文件,却保数据库链接参数正确。
# vim include/config.php $database_type = "mysql"; //指定链接的类型 $database_default = "cacti"; //链接的库 $database_hostname = "localhost"; //登陆源 $database_username = "cactiuser"; //用户名 $database_password = "123.abc"; //用户密码 $database_port = "3306"; //链接的端口 $database_ssl = false; //不启用ssl $url_path = "/"; //指定cacti的根目录,这个目录是cacti的文件目录下,咱们后面还须要修改httpd的主配置文件。
修改httpd的主配置文件,设置好网页文档根目录、自动索引页
# vim /etc/httpd/conf/httpd.conf DocumentRoot "/var/www/html/cacti" <Directory "/var/www/html/cacti"> Options Indexes none AllowOverride None Order allow,deny Allow from all </Directory> DirectoryIndex index.php index.html index.html.var AddDefaultCharset UTF-8
重启服务
# systemctl restart httpd
在游览器中访问Cacti服务器的Web服务,如http://192.168.1.1,除此访问时会自动跳转至安装指南界面如图3所示
以上就是安装界面,内容是一些欢迎信息,直接点击下一步继续
选择新的安装,点击Next继续
以上信息必须所有都为绿色,若是出现红色,请检查相关步骤。点击Finish完成安装
只要看到了这个登陆界面,那么咱们今天的Cacti就部署完成了,默认的用户名和密码都是admin,首次登陆以后须要修改密码。我的建议先不要进行登陆,先进行一下操做,以后在进行登陆。
不管是路由器、交换机、仍是linux或windows服务器,只要正确支持SNMP协议,并容许Client服务器采集数据,就可以对其进行监测。下面仅介绍在linux服务器中启用SNMP支持并设置共同体名的简单方法
ps:共同体名,值的是一种验证机制,全部的client都必须使用同一个共同体名,就像咱们以前学习的hsrp的组同样,只有组号相同才能是一个组的,这里也是如此,只有共同体名相同的时候,Cacti才能经过这个共同体名对client进行监控。
须要安装net-snmp、lm_sensors软件包,而后经过修改其配置文件/etc/snmp/snmpd.conf,并启动snmpd服务,并创建规则,默认监听UDP的161号端口。
# yum -y install net-snmp lm_sensors # vim /etc/snmp/snmpd.conf com2sec notConfigUser 192.168.90.130 public access notConfigGroup "" any noauth exact all none none view all included .1 80
192.168.92.130 指的是Cacti的服务器IP(默认是default),表示容许其查询本机数据,public指的就是共同体名,用来识别及验证
all表示开放全部的SNMP查询权限(默认是 SystemView)
去掉注释以后,以便支持各类查询。
# systemctl start snmpd # iptables -I INPUT -p udp --dport 161 -j ACCEPT
配置好Cacti的主控端、被控端以后,就能够设置集中监测任务了。下面讲学习在Cacti系统的Web控制台的基本操做,以及如何经过添加插件来扩展集中监测功能。
在游览器中访问Cacti系统,如http://192.168.1.1,输入用户和密码,便可登陆到Web控制台界面
未完待续。。。