一、导语
此实验是在搭建好zabbix集群的基础上操作的,读者可查看文章二、添加 http 服务监控项
http是zabbix自带的监控模板点击 配置->主机-> server2 ->模版 ->选择 -> Template App HTTP Service再点击 添加 -> 更新
这样主机 server2的监控项中就出现了 HTTP 服务
三、监控 nginx 服务
nginx监控项zabbix没有,需要在agent端部署nginx服务 部署nginx服务,这里不再赘述[root@server2 ~]# tar zxf nginx-1.15.8.tar.gz [root@server2 ~]# yum install gcc make pcre-devel zlib-devel -y[root@server2 ~]# cd nginx-1.15.8[root@server2 nginx-1.15.8]# vim auto/cc/gcc 171 # debug172 #CFLAGS="$CFLAGS -g"##关闭debug日志[root@server2 nginx-1.15.8]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module[root@server2 nginx-1.15.8]# make && make install[root@server2 nginx-1.15.8]# vim /usr/local/nginx/conf/nginx.conf48 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 }[root@server2 nginx-1.15.8]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@server2 nginx-1.15.8]# /usr/local/nginx/sbin/nginx
测试:利用curl http://127.0.0.1/status访问nginx用来做测试
[root@server2 nginx-1.15.8]# curl http://127.0.0.1/statusActive connections: 1 server accepts handled requests2 2 1 Reading: 0 Writing: 1 Waiting: 0 过滤监控参数(访问次数,连接次数等等) 将过滤出来的参数以key—value键值对的形式存储:[root@server2 nginx-1.15.8]# curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'1[root@server2 ~]# curl -s http://127.0.0.1/status | awk NR==3 | awk '{print $1}'30
3. 创建nginx监控项(nginx.active和nginx.access)
[root@server2 nginx-1.15.8]# cd /etc/zabbix/zabbix_agentd.d/[root@server2 zabbix_agentd.d]# lsuserparameter_mysql.conf[root@server2 zabbix_agentd.d]# cp userparameter_mysql.conf userparameter_nginx.conf# 利用监控mysql服务的配置文件模版复制一份给nginx服务[root@server2 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}' # 获取请求次数##配置完重启服务[root@server2 ~]# systemctl restart zabbix-agent
zabbix-server端测试是否能拿到数据
[root@server1 4.0]# yum install -y zabbix-get-4.0.5-1.el7.x86_64.rpm##可以拿到[root@server1 4.0]# zabbix_get -s 172.25.19.2 -p 10050 -k "nginx.active"1[root@server1 include]# zabbix_get -s 172.25.19.2 -p 10050 -k "nginx.access"33
配置—>主机—>点击server2主机—>点击监控项—>创建监控项
添加监控项,写好之后记得更新再创建一个监控项,写好更新
然后添加图形,将刚才的两个监控项导入
获得监控图形
但是我们发现这里的字体是乱码,所以要进行修改下载我们喜欢的字体到/usr/share/zabbix/fonts目录下,并替换defines.inc.php 的字体
[root@server1 ~ ]# cd /usr/share/zabbix/fonts[root@server1 fonts# lssimkai.ttf[root@server1 include]# pwd/usr/share/zabbix/include[root@server1 include]# vim defines.inc.php :%s/graphfont/simkai/g
再次刷新页面,显示的字体就不是乱码了,等待一会,就会有监控数据
三、监控 zabbix server 上的 mysql 服务
首先我们可以给Zabbix server主机,添加自带的监控模板但是自带的模板监控项太少了,我们可以继续添加模板增加监控项
向zabbix监控中导入模版监控mysql的模版perconapercona:开源的mysql分析工具配置mysql
[root@[root@server1 zabbix_agentd.d]# mkdir /var/lib/zabbix[root@server1 zabbix_agentd.d]# cd /var/lib/zabbix/[root@server1 zabbix]# vim .f[mysql]host = localhostuser = rootpassword = westossocket = /var/lib/mysql/mysql.sock[mysqladmin]host = localhostuser = rootpassword = westossocket = /var/lib/mysql/mysql.sock[root@server1 zabbix]# systemctl restart zabbix-agent
安装percona-zabbix模版
[root@server1 ~]# lspercona-zabbix-templates-1.1.8-1.noarch.rpm [root@server1 ~]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm [root@server1 ~]# cd /var/lib/zabbix/[root@server1 zabbix]# lspercona[root@server1 zabbix]# cd percona/[root@server1 percona]# cd templates/[root@server1 templates]# lsuserparameter_percona_mysql.conf zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml[root@server1 templates]# cp userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/拷贝userparameter_percona_mysql.conf 文件到被监控的mysql服务器上(此时也是server1主机)[root@server1 templates]# cd ..[root@server1 percona]# cd scripts/[root@server1 scripts]# lsget_mysql_stats_wrapper.sh ss_get_mysql_stats.php由于percona的监控脚本是使用php写的,所以需要准备好php运行环境, 将zabbix的数据库和密码写入php脚本中 [root@server1 scripts]# vim f<?php$mysql_user = 'root';$mysql_pass = 'westos';[root@server1 scripts]# systemctl restart zabbix-agent
测试percona的脚本,过滤监控项
[root@server1 scripts]# cd /etc/zabbix/zabbix_agentd.d/[root@server1 zabbix_agentd.d]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg # 测试脚本[root@server1 zabbix_agentd.d]# cd /tmp[root@server1 tmp]# cat localhost-mysql_cacti_stats.txt ##查看监控项[root@server1 tmp]# rm -f localhost-mysql_cacti_stats.txt
导入模板