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](https://i-blog.csdnimg.cn/blog_migrate/430904ab7f5c26bc8ada7932fcd0a9f5.png)
![9469008-3fdc870f3a803236.png](https://i-blog.csdnimg.cn/blog_migrate/45bfb1def56e190f5ce989c6d042637a.png)
![9469008-36e2768c96de9125.png](https://i-blog.csdnimg.cn/blog_migrate/725983bf0d56856040dedf714940e9a8.png)
![9469008-4e814c29424d41b8.png](https://i-blog.csdnimg.cn/blog_migrate/6f37e44e8b2e53631ababe977c1a45d5.png)
![9469008-3655a937a3cf5a80.png](https://i-blog.csdnimg.cn/blog_migrate/2ae5fe846518d8a51d7f405dbddc478b.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
-
连接模板
7.检查取值状态
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](https://i-blog.csdnimg.cn/blog_migrate/2e0d5c43b0c1f960a13cbf1ac98f186f.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](https://i-blog.csdnimg.cn/blog_migrate/6de30696d615fe7c6c09040d03ea12af.png)
6.连接模板检查数据
![9469008-da79f7355abd2287.png](https://i-blog.csdnimg.cn/blog_migrate/23586aa23ffe4cd2a675e6e32f8c6e59.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](https://i-blog.csdnimg.cn/blog_migrate/8accd1f82b3797e8e28bf294fad32db6.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](https://i-blog.csdnimg.cn/blog_migrate/9a44fe1a822d2479ee04b6c829bcf041.png)
redis 模板
1.监控页面默认开启的,不用开了
2.上传模板
![9469008-d228d53d98f6fc98.png](https://i-blog.csdnimg.cn/blog_migrate/dc324cb90f6c24cf000e08c170d99e3a.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](https://i-blog.csdnimg.cn/blog_migrate/f00c1cdbc84b075750e669c810167b2e.png)
6.连接模板,检查最新数据
![9469008-7d0dc6a439033c3d.png](https://i-blog.csdnimg.cn/blog_migrate/cd3e4c25e126cef862db11d7c2e85b61.png)
percona监控mysql
下载percona
![9469008-c7659c65084661c8.png](https://i-blog.csdnimg.cn/blog_migrate/dc261a10ef813b759e0e417507a92426.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结尾的就是 模板
image.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](https://i-blog.csdnimg.cn/blog_migrate/c8b2d60cea0c61948f1f7bf6208f1bbe.png)
![9469008-d296776eb889d67a.png](https://i-blog.csdnimg.cn/blog_migrate/b3cfc1e2684013005a4697ed3a6dd397.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](https://i-blog.csdnimg.cn/blog_migrate/886d0924f4f6b94abb591301ad6c948a.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](https://i-blog.csdnimg.cn/blog_migrate/5c2b5b21b81dedee070819fa3278989a.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 会发生权限问题