day59-zabbix模板的使用

zabbix

模板的好处

利用模板可以快速添加监控项
模板可以分享

文章中用到的模板网盘连接
链接:https://pan.baidu.com/s/1zlOSpyAhr7F-9b99T0R0Jg
提取码:kpt5

zabbix模板的使用思路

1)开启监控页面
2)导入模板
3)上传配置文件到zabbix-agent目录下,上传脚本到指定目录
4)zabbix-get取值测试
5)链接模板

tcp的11种状态

1.命令行测试命令

[root@web01 /etc/zabbix/zabbix_agentd.d]# netstat -ant|grep -c 'LISTEN'
7

2.for循环生成配置文件

[root@web01 /etc/zabbix/zabbix_agentd.d]# cat a.txt 
LISTEN
SYN-SENT
SYN-RECEIVED
ESTABLISHED
FIN-WAIT-1
FIN-WAIT-2
CLOSE-WAIT
CLOSING
LAST-ACK
TIME-WAIT
CLOSED
[root@web01 /etc/zabbix/zabbix_agentd.d]# for n in `cat a.txt`; do echo "UserParameter=$n,netstat -antp|grep -c '$n'" >>tcp.conf; done
[root@web01 /etc/zabbix/zabbix_agentd.d]# cat tcp.conf 
UserParameter=LISTEN,netstat -antp|grep -c 'LISTEN'
UserParameter=SYN-SENT,netstat -antp|grep -c 'SYN-SENT'
UserParameter=SYN-RECEIVED,netstat -antp|grep -c 'SYN-RECEIVED'
UserParameter=ESTABLISHED,netstat -antp|grep -c 'ESTABLISHED'
UserParameter=FIN-WAIT-1,netstat -antp|grep -c 'FIN-WAIT-1'
UserParameter=FIN-WAIT-2,netstat -antp|grep -c 'FIN-WAIT-2'
UserParameter=CLOSE-WAIT,netstat -antp|grep -c 'CLOSE-WAIT'
UserParameter=CLOSING,netstat -antp|grep -c 'CLOSING'
UserParameter=LAST-ACK,netstat -antp|grep -c 'LAST-ACK'
UserParameter=TIME-WAIT,netstat -antp|grep -c 'TIME-WAIT'
UserParameter=CLOSED,netstat -antp|grep -c 'CLOSED

3.重启zabbix-agent

 systemctl restart zabbix-agent.service 

4.zabbix_get 取值测试

[root@zabbix-server ~]# zabbix_get -s 10.0.0.7 -k LISTEN
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
7

这里的故障是自定义的key中加入了-p 参数,-p只有root可以使用

去掉-p之后测试成功

[root@zabbix-server ~]# zabbix_get -s 10.0.0.7 -k LISTEN
7

5.zabbix-web界面创建模板

nginx模板

nginx模板主要用于监控nginx的状态

1.开启监控页面

[root@web01 ~]# vim /etc/nginx/conf.d/www.conf
server {
    listen       80;
    server_name  localhost;
        root   /app/www;
    location / {
        index index.php index.html index.htm;
    }
    location  /nginx_status {
    stub_status;
       allow 127.0.0.1;
       allow 10.0.0.1;
       deny all;
}       
       location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000; 
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# curl 10.0.0.7/nginx_status
Active connections: 1 
server accepts handled requests
 1 1 1 
Reading: 0 Writing: 1 Waiting: 0 

2.导入模板


9469008-5f9bdf21104dee28.png
image.png

9469008-3fdc870f3a803236.png
image.png

9469008-36e2768c96de9125.png
image.png

9469008-4e814c29424d41b8.png
image.png

9469008-3655a937a3cf5a80.png
image.png

3.上传脚本到特定目录

[root@web01 ~]# ls
anaconda-ks.cfg  nginx.zip
[root@web01 ~]# unzip nginx.zip 
Archive:  nginx.zip
  inflating: nginx/nginx.conf        
  inflating: nginx/nginx_monitor.sh  
  inflating: nginx/zbx_nginx_template.xml  
[root@web01 ~]# cd nginx/
[root@web01 ~/nginx]# ls
nginx.conf  nginx_monitor.sh  zbx_nginx_template.xml
[root@web01 ~/nginx]# mkdir -p  /server/scripts/
[root@web01 ~/nginx]# mv nginx_monitor.sh /server/scripts/
[root@web01 ~/nginx]# cd /server/scripts/
[root@web01 /server/scripts]# ls
nginx_monitor.sh
[root@web01 /server/scripts]# chmod +x nginx_monitor.sh 
[root@web01 /server/scripts]# ll
total 4
-rwxr-xr-x 1 root root 1231 Jul 10 09:38 nginx_monitor.sh

4.上传nginx key 的配置文件到指定目录

[root@web01 ~/nginx]# mv nginx.conf /etc/zabbix/zabbix_agentd.d/
[root@web01 ~/nginx]# cd /etc/zabbix/zabbix_agentd.d/
[root@web01 /etc/zabbix/zabbix_agentd.d]# ls
a.txt  cookil  nginx.conf  tcp.conf  userparameter_mysql.conf
[root@web01 /etc/zabbix/zabbix_agentd.d]# cat nginx.conf 
UserParameter=nginx_status[*],/bin/bash /server/scripts/nginx_monitor.sh $1
[root@web01 /etc/zabbix/zabbix_agentd.d]# systemctl restart zabbix-agent.service 

5.zabbix_get 测试取值

[root@zabbix-server ~]# zabbix_get -s 10.0.0.7 -k nginx_status[accepts]
<body>

发现执行key取不到值,尝试去直接执行脚本

[root@web01 /server/scripts]# sh nginx_monitor.sh accepts
<body>

通过查看脚本得知。查看状态的url不对,修改为正确后测试

[root@web01 /server/scripts]# sh  nginx_monitor.sh accepts
10

执行脚本可以取到值了,尝试去执行key

[root@zabbix-server /server/scripts]# zabbix_get -s 10.0.0.7 -k nginx_status[accepts]
rm: cannot remove ‘/tmp/nginx_status.txt’: Operation not permitted
10

这时候出现报错,原因是执行zabbix_get 取值是以zabbix身份去执行,刚才执行脚本是以root身份执行了,root执行时创建的一个文件zabbix用户删除不掉。自己删除这个文件解决

[root@web01 /server/scripts]# rm -rf /tmp/nginx_status.txt

删除后测试成功

[root@zabbix-server /server/scripts]# zabbix_get -s 10.0.0.7 -k nginx_status[accepts]
11
  1. 连接模板
    7.检查取值状态


    9469008-307f3bcb24959581.png
    image.png

php模板

1.开启php监控页面

[root@web01 ~]# vim /etc/php-fpm.d/www.conf 
;pm.status_path = /status
修改为
pm.status_path = /php_status
[root@web01 ~]# vim /etc/nginx/conf.d/www.conf
server {
    listen       80;
    server_name  localhost;
        root   /app/www;
    location / {
        index index.php index.html index.htm;
    }
    location  /nginx_status {
    stub_status;
}
    location /php_status {
         fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;

}
}
[root@web01 ~]# systemctl restart php-fpm
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# curl 10.0.0.7/php_status
pool:                 www
process manager:      dynamic
start time:           14/Jul/2019:12:45:53 +0800
start since:          258
accepted conn:        2
listen queue:         0
max listen queue:     0
listen queue len:     128
idle processes:       4
active processes:     1
total processes:      5
max active processes: 1
max children reached: 0
slow requests:        0

2.上传模板


9469008-dfd79cef399d9121.png
image.png

3.上传脚本和配置文件

[root@web01 /server/scripts]# ls
fpm.sh            nginx_monitor.sh  
[root@web01 /server/scripts]# chmod +x fpm.sh 
[root@web01 /server/scripts]# cd /etc/zabbix/zabbix_agentd.d/
[root@web01 /etc/zabbix/zabbix_agentd.d]# vim fpm.conf 
UserParameter=php-fpm[*],/server/scripts/fpm.sh "$1" "$2"
[root@web01 /etc/zabbix/zabbix_agentd.d]# systemctl restart zabbix-agent.service 

4.zabbix_get 取值测试

[root@zabbix-server /server/scripts]# zabbix_get -s 10.0.0.7 -k php-fpm["total processes",10.0.0.7/php_status]
5

5.修改模板的宏,模板中的变量


9469008-b1f5c5feac6ff73f.png
image.png

6.连接模板检查数据


9469008-da79f7355abd2287.png
image.png

Discuz 论坛开启redis 加速

1.安装redis

[root@web01 /etc/zabbix/zabbix_agentd.d]# yum install -y redis
[root@web01 /etc/zabbix/zabbix_agentd.d]# systemctl start redis
[root@web01 /etc/zabbix/zabbix_agentd.d]# systemctl enable redis
Created symlink from /etc/systemd/system/multi-user.target.wants/redis.service to /usr/lib/systemd/system/redis.service.

[root@web01 /etc/zabbix/zabbix_agentd.d]# yum list |grep php|grep redis
php-nrk-Predis.noarch                   1.0.4-1.el7                    epel     
php-pecl-redis.x86_64                   2.2.8-1.el7                    epel     
php-phpiredis.x86_64                    1.0.0-2.el7                    epel     
[root@web01 /etc/zabbix/zabbix_agentd.d]# yum install -y php-pecl-redis
systemctl restart php-fpm
9469008-ab51c9edf02373aa.png
image.png

2.开启redis加速

[root@web01 ~]# cd /app/www/
[root@web01 /app/www]# vim config/config_global.php
$_config['memory']['redis']['server'] = '127.0.0.1';

配置上redis的地址

9469008-7cf43dbe2f7a2742.png
image.png

redis 模板

1.监控页面默认开启的,不用开了
2.上传模板


9469008-d228d53d98f6fc98.png
image.png

3.上传配置文件和脚本

[root@web01 /server/scripts]# ls
fpm.sh  nginx_monitor.sh  redis.sh
[root@web01 /server/scripts]# chmod +x redis.sh 
[root@web01 /server/scripts]# ls
fpm.sh  nginx_monitor.sh  redis.sh
[root@web01 /server/scripts]# cd /etc/zabbix/zabbix_agentd.d/
[root@web01 /etc/zabbix/zabbix_agentd.d]# ls
a.txt  cookil  fpm.conf  nginx.conf  redis.conf  tcp.conf  userparameter_mysql.conf
[root@web01 /etc/zabbix/zabbix_agentd.d]# vim redis.conf 
UserParameter=redis.discovery,/servers/cripts/redis.sh localhost list_key_space_db
UserParameter=redis[*],/server/scripts/redis.sh $1 $2 $3
[root@web01 /etc/zabbix/zabbix_agentd.d]# systemctl restart zabbix-agent.service 

4.测试取值

[root@zabbix-server /server/scripts]# zabbix_get -s 10.0.0.7 -k redis[127.0.0.1,redis_git_sha1,none]
00000000

5.检查自定义宏


9469008-cf72720727a25c1b.png
image.png

6.连接模板,检查最新数据


9469008-7d0dc6a439033c3d.png
image.png

percona监控mysql

下载percona

9469008-c7659c65084661c8.png
image.png

安装percona

[root@web ~]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm 
warning: percona-zabbix-templates-1.1.8-1.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:percona-zabbix-templates-1.1.8-1 ################################# [100%]

Scripts are installed to /var/lib/zabbix/percona/scripts
Templates are installed to /var/lib/zabbix/percona/templates

安装之后会产生两个目录
/var/lib/zabbix/percona/scripts 是脚本目录
/var/lib/zabbix/percona/templates 是配置文件目录

上传模板到zabbix

[root@web /var/lib/zabbix/percona/templates]# ls
userparameter_percona_mysql.conf  zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml

目录里xml结尾的就是 模板

9469008-a20df990bb47466f.png
image.png

9469008-392b3ee1559896a2.png
image.png

发现上传不了,通过xml文件名得知原因是配置文件是2.0版本导出的,不能上传到4.0版本的zabbix上。可以通过安装2.0版本的zabbix解决: CenOS 7 zabbix-2.2版本安装
也可以下载网友修改测试好的 http://pan.baidu.com/s/1pL1wDYj

9469008-4ae82592382eb179.png
image.png

9469008-d296776eb889d67a.png
image.png

上传配置 文件

[root@web /var/lib/zabbix/percona/templates]# tail -n5 userparameter_percona_mysql.conf 
UserParameter=MySQL.Query-time-count-08,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ot
UserParameter=MySQL.Query-time-count-09,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ou
UserParameter=MySQL.Open-files,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh ij
UserParameter=MySQL.State-closing-tables,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh lq
UserParameter=MySQL.running-slave,/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh running-slave
[root@web /var/lib/zabbix/percona/templates]# mv userparameter_percona_mysql.conf  /etc/zabbix/zabbix_agentd.d/
[root@web /var/lib/zabbix/percona/templates]# cd /etc/zabbix/zabbix_agentd.d/
[root@web /etc/zabbix/zabbix_agentd.d]# ls
fpm.conf  nginx_status.conf  redis.conf  userparameter_mysql.conf  userparameter_percona_mysql.conf

重启zabbix-agent

systemctl restart zabbix-agent.service

zabbix-server 测试取值

[root@zabbix-server ~]# zabbix_get -s 10.0.0.7 -k MySQL.Query-time-count-08


发现没有取到值,手动执行下脚本

[root@web /var/lib/zabbix/percona/scripts]# sh -x get_mysql_stats_wrapper.sh 
+ ITEM=
+ HOST=localhost
++ dirname get_mysql_stats_wrapper.sh
+ DIR=.
+ CMD='/usr/bin/php -q ./ss_get_mysql_stats.php --host localhost --items gg'
+ CACHEFILE=/tmp/localhost-mysql_cacti_stats.txt
+ '[' '' = running-slave ']'
+ '[' -e /tmp/localhost-mysql_cacti_stats.txt ']'
++ stat -c %Y /tmp/localhost-mysql_cacti_stats.txt
+ TIMEFLM=1564232585
++ date +%s
+ TIMENOW=1564232780
++ expr 1564232780 - 1564232585
+ '[' 195 -gt 300 ']'
+ '[' -e /tmp/localhost-mysql_cacti_stats.txt ']'
+ awk -F: '{print $2}'
+ cat /tmp/localhost-mysql_cacti_stats.txt
+ sed 's/ /\n/g; s/-1/0/g'
+ grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

脚本中执行了这个命令/usr/bin/php -q ./ss_get_mysql_stats.php --host localhost --items gg ,我们手动执行下

[root@web /var/lib/zabbix/percona/scripts]# /usr/bin/php -q ./ss_get_mysql_stats.php --host localhost --items gg
ERROR: Access denied for user 'cactiuser'@'localhost' (using password: YES)[root@web /var/lib/zabbix/percona/scripts]# 

发现问题所在了,是数据库密码不对

9469008-2d3bc9a5e029efb0.png
image.png

他是通过php脚本连接的数据库,脚本的同级目录下还有个php脚本,修改php脚本的mysql账号密码

再尝试取值

[root@zabbix-server ~]# zabbix_get -s 10.0.0.7 -k MySQL.Query-time-count-08
0

这样就可以取到值了

9469008-d4a91ba8338608d0.png
image.png
[root@web /var/lib/zabbix/percona/scripts]# ll /tmp/localhost-mysql_cacti_stats.txt
-rw-rw-r-- 1 zabbix zabbix 1259 Jul 27 21:19 /tmp/localhost-mysql_cacti_stats.txt

注意下临时文件所以者,如果是root 会发生权限问题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值