zabbix-搭建、后端agent添加方法、应用监控(apache、nginx、mysql)、percona

zabbix-搭建、后端agent添加方法、应用监控(apache、nginx、mysql)、percona,第1张

zabbix-搭建、后端agent添加方法、应用监控(apache、nginx、mysql)、percona

zabbix中文文档

1、zabbix监控搭建
安装zabbix-server:
[root@server1 ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm  安装官方rpm包
[root@server1 ~]# cd /etc/yum.repos.d/
[root@server1 yum.repos.d]# ls
dvd.repo  redhat.repo  zabbix.repo   安装rpm包后会生成相应的yum源
[root@server1 yum.repos.d]# yum install zabbix-server-mysql zabbix-agent -y  安装
[root@server1 zabbix]# yum install mariadb-server -y 安装数据库
[root@server1 zabbix]# systemctl start mariadb.service  启动数据库
[root@server1 zabbix]# systemctl enable mariadb.service  设置开机子启
[root@server1 zabbix]# mysql
MariaDB [(none)]>  create database zabbix character set utf8 collate utf8_bin;  创建数据库
MariaDB [(none)]> create user zabbix@localhost identified by 'westos'; 创建用户
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost;  授权用户所有权限
[root@server1 zabbix]# cd /usr/share/doc/
[root@server1 doc]# cd zabbix-server-mysql-5.0.19/
[root@server1 zabbix-server-mysql-5.0.19]# ls
AUTHORS  ChangeLog  COPYING  create.sql.gz  double.sql  NEWS  README
[root@server1 zabbix-server-mysql-5.0.19]# zcat create.sql.gz | mysql -uzabbix -pwestos zabbix  导入初始架构和数据
[root@server1 zabbix-server-mysql-5.0.19]# mysql
MariaDB [(none)]> use zabbix  进入zabbix数据库
MariaDB [zabbix]> show tables;  查看表,表已经导入
+----------------------------+
| Tables_in_zabbix           |
+----------------------------+
| acknowledges               |
| actions                    |
| alerts                     |
| application_discovery      |
| application_prototype      |
| application_template       |
| applications               |
| auditlog                   |
[root@server1 zabbix-server-mysql-5.0.19]# cd /etc/zabbix/
[root@server1 zabbix]# vim zabbix_server.conf    

[root@server1 zabbix]# systemctl start zabbix-server.service   启动
[root@server1 zabbix]# systemctl enable --now zabbix-agent.service   设置zabbix-agent开机子启

安装zabbix前台:
清华镜像站zabbix

[root@server1 ~]# cd /etc/yum.repos.d/  进入目录
[root@server1 yum.repos.d]# vim zabbix.repo  编辑配置文件

curl -o /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-7.repo  安装CentOS7安装源
[root@server1 yum.repos.d]# ls
CentOS-base.repo  dvd.repo  redhat.repo  zabbix.repo
[root@server1 yum.repos.d]# vim CentOS-base.repo  编辑仓库软件源

[root@server1 yum.repos.d]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y  安装

更改正确的时区:
[root@server1 ~]# cd /etc/opt/rh/rh-php72/php-fpm.d/
[root@server1 php-fpm.d]# ls
www.conf  zabbix.conf
[root@server1 php-fpm.d]# vim zabbix.conf  

[root@server1 php-fpm.d]# systemctl enable --now  httpd rh-php72-php-fpm  设置开机自启

访问172.25.50.1/zabbix





设置server2为agent:
[root@server1 ~]# cd /etc/yum.repos.d/
[root@server1 yum.repos.d]# ls
CentOS-base.repo  dvd.repo  redhat.repo  zabbix.repo
[root@server1 yum.repos.d]# scp zabbix.repo server2:/etc/yum.repos.d/  将server1上的zabbix.repo拷贝到server2上
[root@server2 yum.repos.d]# yum install  -y zabbix-agent  在server2上安装zabbix-agent 

[root@server2 ~]# cd /etc/zabbix/
[root@server2 zabbix]# vim zabbix_agentd.conf 
Server=172.25.50.1   表示指向zabbix-server
ServerActive=172.25.50.1  表示agent端主动把数据传给server端172.25.50.1,默认是server端传给agent端
Hostname=server2  表示当agent连接server端时,是以主机名的方式识别的,主机名一定要有解析
[root@server2 zabbix]# systemctl enable  --now zabbix-agent.service  重启服务
 [root@server2 zabbix]# cat /var/log/zabbix/zabbix_agentd.log   查看日志,可以发现server2找不到,需要手动添加server2


问题:点击图形预览发现字体是乱码,如何解决??

[root@foundation50 zabbix]# scp simkai.ttf  [email protected]:/usr/share/zabbix/assets/fonts/  将下载好的字体拷贝到server1上
[root@server1 ~]# cd /usr/share/zabbix/
[root@server1 zabbix]# cd assets/
[root@server1 assets]# ls
fonts  img  styles
[root@server1 assets]# cd fonts/
[root@server1 fonts]# ls
graphfont.ttf  simkai.ttf  下载的字体
[root@server1 fonts]# ll
lrwxrwxrwx 1 root root      33 Jan 11 22:04 graphfont.ttf -> /etc/alternatives/zabbix-web-font   
-rw-r--r-- 1 root root 4135804 Jan 12 01:13 simkai.ttf
[root@server1 fonts]# rm -fr graphfont.ttf  将原先的字体文件删除
[root@server1 fonts]# ln -s simkai.ttf graphfont.ttf  将graphfont.ttf链接到我们下载的字体上
[root@server1 fonts]# ll
lrwxrwxrwx 1 root root      10 Jan 12 01:19 graphfont.ttf -> simkai.ttf   
-rw-r--r-- 1 root root 4135804 Jan 12 01:13 simkai.ttf

2、手动添加agent(server2)







3、自动发现agent-适合批量化(server3)
[root@server2 yum.repos.d]# scp zabbix.repo server3:/etc/yum.repos.d/  将server2上的zabbix仓库拷贝到server3上
[root@server3 ~]# yum install -y  zabbix-agent
[root@server3 ~]# cd /etc/zabbix/
[root@server3 zabbix]# vim zabbix_agentd.conf 
Server=172.25.50.1
ServerActive=172.25.50.1
Hostname=server3
[root@server3 zabbix]# systemctl enable --now zabbix-agent.service   开启






注意:在配置时,首先在配置里面的动作进行激活,然后再到配置的自动发现进行激活


4、自动注册

自动注册前先停掉自动发现

停掉动作

删除server3









[root@server3 zabbix]# systemctl restart zabbix-agent.service   重启server3

5、API
执行请求:
[root@foundation50 Desktop]# curl -XPOST http://172.25.50.1/zabbix/api_jsonrpc.php  -H 'Content-Type: application/json-rpc' -d '   
POST 表示发送请求往server发, -H后面跟类型  -d 后面跟需要做的事情,此命令可以在远程,可以在sever上
{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
    },
    "id": 1,
    "auth": null
}' | python3 -m json.tool   表示用此参数是以 json方式展示结果
{    
    "jsonrpc": "2.0",
    "result": "51bb1fad6b9e770b642496d274880d55",   得到返回直
    "id": 1
}

检索主机:
在server上执行命令
[root@server1 ~]# vim zabbix_api.sh  创建运行脚本
curl -s -XPOST http://172.25.50.1/zabbix/api_jsonrpc.php  -H 'Content-Type: application/json-rpc' -d '
{
   "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "hostid",
            "host"
        ],
        "selectInterfaces": [
            "interfaceid",
            "ip"
        ]
    },
    "id": 2,
    "auth": "51bb1fad6b9e770b642496d274880d55"   将之前得到返回直填在此处
}' | python -m json.tool

[root@server1 ~]# sh zabbix_api.sh  运行脚本,所有server主机检索成功,总共三台
{
    "id": 2,
    "jsonrpc": "2.0",
    "result": [
        {
            "host": "Zabbix server",
            "hostid": "10084",
            "interfaces": [
                {
                    "interfaceid": "1",
                    "ip": "127.0.0.1"
                }
            ]
        },
        {
            "host": "server2",
            "hostid": "10434",
            "interfaces": [
                {
                    "interfaceid": "2",
                    "ip": "172.25.50.2"
                }
            ]
        },
        {
            "host": "server3",
            "hostid": "10437",
            "interfaces": [
                {
                    "interfaceid": "5",
                    "ip": "172.25.50.3"
                }
            ]
        }
    ]
}

删除主机:
[root@server1 ~]# vim zabbix_api.sh  编辑运行脚本
curl -s -XPOST http://172.25.50.1/zabbix/api_jsonrpc.php  -H 'Content-Type: application/json-rpc' -d '
{
   "jsonrpc": "2.0",
    "method": "host.delete",   方法:删除
    "params": [
        "10437"   sever3主机id,查找主机id如下图
    ],
    "id": 2,
    "auth": "51bb1fad6b9e770b642496d274880d55"
}' | python -m json.tool


[root@server1 ~]# sh zabbix_api.sh 运行脚本
{
“id”: 2,
“jsonrpc”: “2.0”,
“result”: {
“hostids”: [
“10437”
]
}
}

创建主机:
[root@server1 ~]# vim zabbix_api.sh
curl -s -XPOST http://172.25.50.1/zabbix/api_jsonrpc.php  -H 'Content-Type: application/json-rpc' -d '
{
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "server3",   
        "interfaces": [
            {
                "type": 1,
                "main": 1,
                "useip": 1,
                "ip": "172.25.50.3",    ## server3 ip地址
                "dns": "",
                "port": "10050"
            }
        ],
        "groups": [
            {
                "groupid": "2"   
            }
        ],
        "tags": [
            {
                "tag": "Host name",
                "value": "server3"
            }
        ],
        "templates": [
            {
                "templateid": "10001"
            }
        ]
    },
    "id": 2,
    "auth": "51bb1fad6b9e770b642496d274880d55"
}' | python -m json.tool                                               

获取主id方法:


获取模板id方法:


[root@server1 ~]# sh zabbix_api.sh   运行脚本
{
    "id": 2,
    "jsonrpc": "2.0",
    "result": {
        "hostids": [
            "10439"
        ]
    }
}

6、 zabbix监控应用-apache
[root@server2 yum.repos.d]# yum install httpd -y 安装apache
[root@server2 yum.repos.d]# systemctl start httpd.service   启动
[root@server2 ~]# echo www.westos.org > /var/www/html/index.html  建立测试页
[root@server2 ~]# curl localhost  访问成功
www.westos.org




7、zabbix监控应用-nginx
nginx安装:
[root@foundation50 lamp]# scp nginx-1.18.0.tar.gz server3:/mnt  将下载好的nginx拷贝到server1上
[root@server3 mnt]# tar zxf nginx-1.18.0.tar.gz   解压
[root@server3 mnt]# yum install -y gcc pcre-devel openssl-devel  安装依赖性
[root@server3 nginx-1.18.0]# vim auto/cc/gcc  关闭debug

[root@server3 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module  执行
[root@server3 nginx-1.18.0]# make && make install  编译及安装
[root@server3 ~]# vim .bash_profile 添加nginx环境变量

[root@server3 ~]# source .bash_profile   生效
[root@server3 ~]# nginx  启动

由于监控nginx应用系统没有直带模板,该如何监控?

[root@server3 conf]# nginx -s reload  重载服务
[root@server3 conf]# curl http://127.0.0.1/status  访问
Active connections: 1 
server accepts handled requests
 2 2 2 
Reading: 0 Writing: 1 Waiting: 0 
[root@server3 conf]# curl -s http://127.0.0.1/status | grep Active | awk '{print }'
1   取出链接数,为1
[root@server3 conf]# cd /etc/zabbix/
[root@server3 zabbix]# cd zabbix_agentd.d/  创建自定义监控项
UserParameter=nginx_active,curl -s http://127.0.0.1/status | grep Active | awk '{print }'    nginx_active表示监控项自定义名字,后面的脚本表示如何取key的直
[root@server3 zabbix_agentd.d]# systemctl restart zabbix-agent.service   重启agent
[root@server1 mnt]# yum install -y zabbix-get  在server1上安装软件包
[root@server1 mnt]# zabbix_get  --help  或取帮助
[root@server1 mnt]# zabbix_get  -s 172.25.50.3 -p 10050 -k 'nginx_active'    -s 后指定agent地址, -k 表示指定key
1   直取到了






拓展:
[root@server3 conf]# curl http://127.0.0.1/status
Active connections: 1 
server accepts handled requests
 98 99 100       98表示允许进来的请求,99表示建立了多少次三次握手,100表示处理了多少个请求
如何将这三个直取出:
[root@server3 ~]# cd /etc/zabbix/zabbix_agentd.d 编辑文件
UserParameter=nginx_active,curl -s http://127.0.0.1/status | grep Active | awk '{print }'
UserParameter=nginx_accept,curl -s http://127.0.0.1/status | awk 'NR==3 {print }'   添加
UserParameter=nginx_handle,curl -s http://127.0.0.1/status | awk 'NR==3 {print }'    添加
UserParameter=nginx_request,curl -s http://127.0.0.1/status | awk 'NR==3 {print }'   添加
[root@server3 zabbix_agentd.d]# systemctl restart zabbix-agent.service  重启服务
[root@server1 mnt]# zabbix_get  -s 172.25.50.3 -p 10050 -k 'nginx_accept'   取直
122
[root@server1 mnt]# zabbix_get  -s 172.25.50.3 -p 10050 -k 'nginx_handle' 取直
124
[root@server1 mnt]# zabbix_get  -s 172.25.50.3 -p 10050 -k 'nginx_request'  取直
126







8、zabbix监控应用-mysql

server1上有数据库,直接监控server1上的
直接给zabbix-server添加自带的mysql监控项,如下图:


由于zabbix4版本稳定,方便后续实验,将版本从zabbix5变成zabbix4

zabbix4 搭建:
[root@foundation50 ~]# cd /mnt/pub/docs/zabbix/  
[root@foundation50 zabbix]# cp -r 4.0/ /var/www/html/    将下载好的zabbix4.0rpm包拷贝到html目录里
[root@server1 ~]# cd /etc/yum.repos.d/
[root@server1 yum.repos.d]# vim zabbix.repo  编辑软件仓库
[zabbix]
name=zabbix
baseurl=http://172.25.254.50/4.0
gpgcheck=0
[root@server1 yum.repos.d]# yum install php-*  zabbix-server-mysql  zabbix-agent  zabbix-web-mysql -y  安装
[root@server1 yum.repos.d]# yum install mariadb-server -y  安装数据库
[root@server1 yum.repos.d]# systemctl start mariadb.service  启动数据库
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;  建立数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> create user zabbix@localhost identified by 'westos';  创建用户
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost;   授权
Query OK, 0 rows affected (0.00 sec)
[root@server1 ~]# cd /usr/share/doc/zabbix-server-mysql-4.0.5/  
[root@server1 zabbix-server-mysql-4.0.5]# ls
AUTHORS  ChangeLog  COPYING  create.sql.gz  NEWS  README
[root@server1 zabbix-server-mysql-4.0.5]# zcat create.sql.gz  | mysql -uzabbix -pwestos zabbix  
[root@server1 zabbix-server-mysql-4.0.5]# cd /etc/zabbix/
[root@server1 zabbix-server-mysql-4.0.5]# cd /etc/zabbix/
[root@server1 zabbix]# vim zabbix_server.conf 

[root@server1 zabbix]# systemctl enable --now zabbix-server zabbix-agent  设置开机自启

zabbix前端配置
[root@server1 zabbix]# cd /etc/httpd/
[root@server1 httpd]# cd conf.d/
[root@server1 conf.d]# vim zabbix.conf 指定正确的时区

[root@server1 conf.d]# systemctl enable --now httpd   启动apache
9 、zabbix----percona

可以看到监控项及其触发器图形都是很少的,这在企业中是远远不够的需要导入自己的mysql服务的监控模版
Percona Server为 MySQL 数据库服务器进行了改进,在功能和性能上较 MySQL 有着很显著的提升。该版本提升了在高负载情况下的 InnoDB 的性能、为 DBA 提供一些非常有用的性能诊断工具;另外有更多的参数和命令来控制服务器行为

[root@server1 zabbix_agentd.d]# mysqladmin password westos  用管理员更改数据库密码
[root@server1 zabbix_agentd.d]# mysql -pwestos  登陆数据库

[root@server1 zabbix_agentd.d]# mkdir /var/lib/zabbix  建立目录
[root@server1 zabbix_agentd.d]# cd /var/lib/zabbix/  进入这个目录去加载数据库访问的用户名
[root@server1 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
[root@server1 zabbix]# systemctl restart zabbix-agent.service  重启
测试:
[root@server1 zabbix]# zabbix_get -s 127.0.0.1 -p 10050 -k 'mysql.version'   取直成功
mysql  Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1

[root@foundation50 Desktop]# cd /mnt/pub/docs/zabbix/
[root@foundation50 zabbix]# evince Percona-Monitoring-Plugins-1.1.8.pdf   可以查看下载的官方手册 
[root@foundation50 zabbix]# scp percona-zabbix-templates-1.1.8-1.noarch.rpm server1:/mnt  将下载好的模板拷贝到server1上
[root@server1 mnt]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm   安装
[root@server1 scripts]# cd /var/lib/zabbix/percona/templates    进入模板目录
[root@server1 templates]# ls
userparameter_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/  将模板放到/etc/zabbix/zabbix_agentd.d/目录里
[root@server1 zabbix_agentd.d]# systemctl restart zabbix-agent.service  重启
[root@server1 ~]# cd /var/lib/zabbix/percona/scripts
[root@server1 scripts]# vim ss_get_mysql_stats.php   更改数据库用户名和密码

[root@server1 scripts]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg  手工调用执行脚本
1
[root@server1 scripts]# /var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gq
8191
也可以用zabbix_get 调用,但是需要知道具体的key名,如何查找如下:
[root@server1 scripts]# vim /etc/zabbix/zabbix_agentd.d/userparameter_percona_mysql.conf 

[root@server1 scripts]# zabbix_get -s 127.0.0.1 -p 10050 -k 'MySQL.pool-size'   表示server端去连接agent看能不能取到直
rm: cannot remove ‘/tmp/localhost-mysql_cacti_stats.txt’: Operation not permitted
8191   可以取到具体的直

如何把监控项导入到zabbix平台上?

[root@server1 templates]# ls 进入模板目录
userparameter_percona_mysql.conf  zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml(这就是生成监控项模板,但是由于版本问题不能用,用老师修改后的监控先模板)








删除临时文件:
[root@server1 templates]# cd /tmp/
[root@server1 tmp]# ls
localhost-mysql_cacti_stats.txt  此时文件的权限是root权限,而我们的用户是zabbix,所以无法写入
systemd-private-837089c1e40c4ac099e27ca20661b268-httpd.service-pj9uZN
systemd-private-837089c1e40c4ac099e27ca20661b268-mariadb.service-sJrRfX
[root@server1 tmp]# rm -fr localhost-mysql_cacti_stats.txt  将临时文件删除
[root@server1 tmp]# ll
total 4
-rw-rw-r-- 1 zabbix zabbix 1356 Jan 17 11:11 localhost-mysql_cacti_stats.txt  删除后zabbix用户自己生成临时文件
drwx------ 3 root   root     17 Jan 16 23:28 systemd-private-837089c1e40c4ac099e27ca20661b268-httpd.service-pj9uZN
drwx------ 3 root   root     17 Jan 17 10:07 systemd-private-837089c1e40c4ac099e27ca20661b268-mariadb.service-sJrRfX

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5704118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存