Zabbix 分布式监控平台添加服务监控项(http,nginx,mysql)
此实验是在搭建好zabbix集群的基础上操作的
添加 http 服务监控项
1.http是zabbix自带的监控模板
2.点击 配置->主机-> server2 ->模版 ->选择 -> Template App HTTP Service
这样主机 server2的监控项中就出现了 HTTP 服务
二。监控nginx服务
nginx监控项zabbix没有,需要在agent端部署nginx服务
1.部署nginx服务
[[email protected] ~]# tar zxf nginx-1.15.8.tar.gz
[[email protected] ~]# yum install gcc make pcre-devel zlib-devel -y
[[email protected] ~]# cd nginx-1.15.8
[[email protected] nginx-1.15.8]# vim auto/cc/gcc
171 # debug
172 #CFLAGS="$CFLAGS -g" ##关闭debug日志
[[email protected] nginx-1.15.8]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module
[[email protected] nginx-1.15.8]# make && make install
[[email protected] nginx-1.15.8]# vim /usr/local/nginx/conf/nginx.conf
48 location /status {
49 stub_status on; # Nginx中的stub_status模块主要用于查看Nginx的一些状态信息. 本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定:./configure –with-http_stub_status_module
50 access_log off; # 关闭日志记录
51 allow 127.0.0.1; # 只允许本机访问
52 deny all;
53 }
[[email protected] nginx-1.15.8]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] nginx-1.15.8]# /usr/local/nginx/sbin/nginx
2.测试访问:
利用curl http://127.0.0.1/status访问nginx用来做测试
[[email protected] ~]# curl http://127.0.0.1/status
Active connections: 1
server accepts handled requests
3 3 2
Reading: 0 Writing: 1 Waiting: 0
过滤监控参数(访问次数,连接次数等等)
将过滤出来的参数以key—value键值对的形式存储:
[[email protected] ~]# curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'
1
[[email protected] ~]# curl -s http://127.0.0.1/status | awk NR==3 | awk '{print $1}'
7
3.创建nginx监控项(nginx.active和nginx.access)
[[email protected] nginx-1.15.8]# cd /etc/zabbix/zabbix_agentd.d/
[[email protected] zabbix_agentd.d]# ls
userparameter_mysql.conf
[[email protected] zabbix_agentd.d]# cp userparameter_mysql.conf userparameter_nginx.conf # 利用监控mysql服务的配置文件模版复制一份给nginx服务
[[email protected] zabbix_agentd.d]# vim userparameter_nginx.conf
UserParameter=nginx.active,curl -s http://127.0.0.1/status | grep Active | awk '{print $3}' # 获取连接数(nginx.active为key值,获得的连接数为value值)
UserParameter=nginx.access,curl -s http://127.0.0.1/status | awk NR==3 | awk '{print $1}' # 获取接受请求数
UserParameter=nginx.handled,curl -s http://127.0.0.1/status | awk NR==3 | awk '{print $2}' # 获取建立三次握手的次数
UserParameter=nginx.requests,curl -s http://127.0.0.1/status | awk NR==3 | awk '{print $3}' # 获取请求次数
##配置完重启服务
[[email protected] ~]# systemctl restart zabbix-agent
4.zabbix-server端测试是否能拿到数据
[[email protected] 4.0]# yum install -y zabbix-get-4.0.5-1.el7.x86_64.rpm
[[email protected] ~]# zabbix_get -s 172.25.26.2 -p 10050 -k "nginx.active"
1
[[email protected] ~]# zabbix_get -s 172.25.26.2 -p 10050 -k "nginx.access"
14
5.配置—>主机—>点击server2主机—>点击监控项—>创建监控项
添加两个监控项
然后添加图形,将刚才的两个监控项导入
获得监控图形
我们发现这里的字体是乱码,所以要进行修改
下载我们喜欢的字体到/usr/share/zabbix/fonts目录下,并替换defines.inc.php 的字体
[[email protected] ~ ]# cd /usr/share/zabbix/fonts
[[email protected] fonts# ls
simkai.ttf
[[email protected] include]# pwd
/usr/share/zabbix/include
[[email protected] include]# vim defines.inc.php
:%s/graphfont/simkai/g
再次刷新页面,显示的字体就不是乱码了
三。监控zabbix server上的mysql服务
1.首先我们可以给Zabbix server主机,添加自带的监控模板
但是自带的模板监控项太少了,我们可以继续添加模板
增加监控项
向zabbix监控中导入模版监控mysql的模版percona( percona:开源的mysql分析工具)
1.配置mysql
[[email protected][[email protected] zabbix_agentd.d]# mkdir /var/lib/zabbix
[[email protected] zabbix_agentd.d]# cd /var/lib/zabbix/
[[email protected] zabbix]# vim .my.cnf
[mysql]
host = localhost
user = root
password = westos
socket = /var/lib/mysql/mysql.sock
[mysqladmin]
host = localhost
user = root
password = westos
socket = /var/lib/mysql/mysql.sock
[[email protected] zabbix]# systemctl restart zabbix-agent
2.安装percona-zabbix模版
[[email protected] ~]# ls
percona-zabbix-templates-1.1.8-1.noarch.rpm
[[email protected] ~]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
[[email protected] ~]# cd /var/lib/zabbix/
[[email protected] zabbix]# ls
percona
[[email protected] zabbix]# cd percona/
[[email protected] percona]# cd templates/
[[email protected] templates]# ls
userparameter_percona_mysql.conf zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
[[email protected] templates]# cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
拷贝userparameter_percona_mysql.conf 文件到被监控的mysql服务器上(此时也是server1主机)
[[email protected] templates]# cd ..
[[email protected] percona]# cd scripts/
[[email protected] scripts]# ls
get_mysql_stats_wrapper.sh ss_get_mysql_stats.php
由于percona的监控脚本是使用php写的,所以需要准备好php运行环境, 将zabbix的数据库和密码写入php脚本中
[[email protected] scripts]# vim ss_get_mysql_stats.php.cnf
<?php
$mysql_user = 'root';
$mysql_pass = 'westos';
[[email protected] scripts]# systemctl restart zabbix-agent
3.测试percona的脚本,过滤监控项
[[email protected] scripts]# cd /etc/zabbix/zabbix_agentd.d/
[[email protected] zabbix_agentd.d]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg # 测试脚本
[[email protected] zabbix_agentd.d]# cd /tmp
[[email protected] tmp]# cat localhost-mysql_cacti_stats.txt ##查看监控项
[[email protected] tmp]# rm -f localhost-mysql_cacti_stats.txt
4.导入模板