zabbix实战

实验一:安装zabbix5.0

范例: 在ubuntu20.04 安装 Zabbix Server 5.0

#官网下载
root@ubuntu2004:~# wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb
--2022-06-22 21:33:23--  https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb
Resolving repo.zabbix.com (repo.zabbix.com)... 178.128.6.101, 2604:a880:2:d0::2062:d001
Connecting to repo.zabbix.com (repo.zabbix.com)|178.128.6.101|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4244 (4.1K) [application/octet-stream]
Saving to: ‘zabbix-release_5.0-1+focal_all.deb’

zabbix-release_5.0-1+focal_all 100%[====================================================>]   4.14K  --.-KB/s    in 0s      

2022-06-22 21:33:24 (367 MB/s) - ‘zabbix-release_5.0-1+focal_all.deb’ saved [4244/4244]

root@ubuntu2004:~# dpkg -i zabbix-release_5.0-1+focal_all.deb
Selecting previously unselected package zabbix-release.
(Reading database ... 114056 files and directories currently installed.)
Preparing to unpack zabbix-release_5.0-1+focal_all.deb ...
Unpacking zabbix-release (1:5.0-1+focal) ...
Setting up zabbix-release (1:5.0-1+focal) ...

root@ubuntu2004:~# dpkg -L zabbix-release
/.
/etc
/etc/apt
/etc/apt/sources.list.d
/etc/apt/sources.list.d/zabbix.list
/etc/apt/trusted.gpg.d
/etc/apt/trusted.gpg.d/zabbix-official-repo.gpg
/usr
/usr/share
/usr/share/doc
/usr/share/doc/zabbix-release
/usr/share/doc/zabbix-release/README.Debian
/usr/share/doc/zabbix-release/changelog.Debian
/usr/share/doc/zabbix-release/copyright

#镜像加速
root@ubuntu2004:/etc/apt/sources.list.d# sed -i.bak 's#http://repo.zabbix.com#https://mirror.tuna.tsinghua.edu.cn/zabbix#' /etc/apt/sources.list.d/zabbix.list

oot@ubuntu2004:~# cat /etc/apt/sources.list.d/zabbix.list
deb https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/ubuntu focal main
deb-src https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/ubuntu focal main

root@ubuntu2004:~# apt update

#安装包
root@ubuntu2004:~#apt -y  install zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-agent2 zabbix-get

#安装数据库MySQL
root@ubuntu2004:~# apt -y install mysql-server

#如果MySQL和ZabbixServer在同一台主机,此项可不改
[root@zabbix-server ~]#vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address     = 0.0.0.0
[root@zabbix-server ~]#systemctl restart mysql.service

mysql> create database zbbix character set utf8 collate utf8_bin;
Query OK, 1 row affected, 2 warnings (0.00 sec)

mysql> create user zabbix@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on zabbix.* to zabbix@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| zabbix           | 10.0.0.%  |
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)

mysql> exit
Bye

#初绐化数据库的表
[root@zabbix-server ~]#zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123456 zabbix

#修改Zabbix Server 连接MySQL
[root@zabbix-server ~]#vim /etc/zabbix/zabbix_server.conf
DBPassword=123456

root@ubuntu2004:~# cat /etc/zabbix/nginx.conf
server {
        listen 80;
        server_name     zabbix.ehuo.org; #修改主机名
#        listen          80;
#        server_name     example.com;

        root    /usr/share/zabbix;

        index   index.php;

        location = /favicon.ico {
                log_not_found   off;
        }

        location / {
                try_files       $uri $uri/ =404;
        }

        location /assets {
                access_log      off;
                expires         10d;
        }

        location ~ /\.ht {
                deny            all;
        }

        location ~ /(api\/|conf[^\.]|include|locale|vendor) {
                deny            all;
                return          404;
        }

        location ~ [^/]\.php(/|$) {
                fastcgi_pass    unix:/var/run/php/zabbix.sock;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_index   index.php;

                fastcgi_param   DOCUMENT_ROOT   /usr/share/zabbix;
                fastcgi_param   SCRIPT_FILENAME /usr/share/zabbix$fastcgi_script_name;
                fastcgi_param   PATH_TRANSLATED /usr/share/zabbix$fastcgi_script_name;

                include fastcgi_params;
                fastcgi_param   QUERY_STRING    $query_string;
                fastcgi_param   REQUEST_METHOD  $request_method;
                fastcgi_param   CONTENT_TYPE    $content_type;
                fastcgi_param   CONTENT_LENGTH  $content_length;

                fastcgi_intercept_errors        on;
                fastcgi_ignore_client_abort     off;
                fastcgi_connect_timeout         60;
                fastcgi_send_timeout            180;
                fastcgi_read_timeout            180;
                fastcgi_buffer_size             128k;
                fastcgi_buffers                 4 256k;
                fastcgi_busy_buffers_size       256k;
                fastcgi_temp_file_write_size    256k;
        }
}
root@ubuntu2004:~# 

root@ubuntu2004:~# sed -i.bak '/date.timezone/c php_value[date.timezone] = Asia/Shanghai' /etc/zabbix/php-fpm.conf
root@ubuntu2004:~# ^C
root@ubuntu2004:~# cat /etc/zabbix/php-fpm.conf
[zabbix]
user = www-data
group = www-data

listen = /var/run/php/zabbix.sock
listen.owner = www-data
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/sessions/

php_value[max_execution_time] = 300
php_value[memory_limit] = 128M
php_value[post_max_size] = 16M
php_value[upload_max_filesize] = 2M
php_value[max_input_time] = 300
php_value[max_input_vars] = 10000
php_value[date.timezone] = Asia/Shanghai

#安装中文包
[root@zabbix-server ~]#apt -y install language-pack-zh-hans

root@ubuntu2004:~# systemctl disable --now apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable apache2
Removed /etc/systemd/system/multi-user.target.wants/apache2.service.
root@ubuntu2004:~# systemctl restart  zabbix-server zabbix-agent2 nginx php7.4-fpm
root@ubuntu2004:~# systemctl enable   zabbix-server
Synchronizing state of zabbix-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable zabbix-server
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-server.service → /lib/systemd/system/zabbix-server.service.
root@ubuntu2004:~# 

第一次登录, 默认的用户名是 Admin,密码是zabbix
在这里插入图片描述

安装zabbix5.0脚本

wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb
dpkg -i zabbix-release_5.0-1+focal_all.deb
sed -i.bak 's#http://repo.zabbix.com#https://mirror.tuna.tsinghua.edu.cn/zabbix#' /etc/apt/sources.list.d/zabbix.list
apt update
apt -y  install zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-agent2
apt -y install mysql-server
cat << EOF | mysql
create database zabbix character set utf8 collate utf8_bin;
create user zabbix@localhost identified by '123456';
grant all privileges on zabbix.* to zabbix@localhost;
EOF

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123456  zabbix

sed -i.bak '/# DBPassword/a DBPassword=123456' /etc/zabbix/zabbix_server.conf
sed -i.bak '/^server/a\        listen 80;\n        server_name     zabbix.ehuo.org;' /etc/zabbix/nginx.conf

sed -i.bak '/date.timezone/c php_value[date.timezone] = Asia/Shanghai' /etc/zabbix/php-fpm.conf

systemctl disable --now apache2

systemctl restart  zabbix-server zabbix-agent2 nginx php7.4-fpm


systemctl enable   zabbix-server

因为字体兼容性的原因,在显示中文时可能会有乱码出现

root@zabbix-server fonts]#pwd
/usr/share/zabbix/assets/fonts
#支持ttf和ttc后缀的字体文件
[root@zabbix-server fonts]#mv graphfont.ttf graphfont.ttf.bak
[root@zabbix-server fonts]#mv SIMYOU.TTF graphfont.ttf
[root@zabbix-server fonts]#ls
graphfont.ttf graphfont.ttf.bak
#注意:字体文件路径和名称的定义在文件/usr/share/zabbix/include/defines.inc.php中配置
#可以修改下面FONT_NAME指定新字体件,注意不需加文件后缀
[root@zabbix-server ~]#grep FONT_NAME /usr/share/zabbix/include/defines.inc.php
define('ZBX_GRAPH_FONT_NAME', 'graphfont'); // font file name
define('ZBX_FONT_NAME', 'graphfont');


root@ubuntu2004:/usr/share/zabbix/assets/fonts# mv SIMYOU.TTF graphfont.ttf
root@ubuntu2004:/usr/share/zabbix/assets/fonts# ls
graphfont.ttf  graphfont.ttf.bak  MSYH.TTC
root@ubuntu2004:/usr/share/zabbix/assets/fonts# grep FONT_NAME /usr/share/zabbix/include/defines.inc.php
define('ZBX_GRAPH_FONT_NAME',		'graphfont'); // font file name
define('ZBX_FONT_NAME', 'graphfont');
root@ubuntu2004:/usr/share/zabbix/assets/fonts# 

注意:乱码和语言环境无关,英语环境也支持中文显示,但会有乱码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5YAFnDn8-1655950813411)(C:\Users\78715\AppData\Roaming\Typora\typora-user-images\image-20220622224459883.png)]

范例: Rocky8 安装Zabbix Agent2

#从国内镜像下载
[root@rocky8 ~]#rpm -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/zabbix-agent2-6.0.5-1.el8.x86_64.rpm
[root@localhost ~]# rpm -ql https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/zabbix-agent2-6.0.5-1.el8.x86_64.rpm
warning: https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/zabbix-agent2-6.0.5-1.el8.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
/etc/logrotate.d/zabbix-agent2
/etc/zabbix/zabbix_agent2.conf
/etc/zabbix/zabbix_agent2.d
/etc/zabbix/zabbix_agent2.d/plugins.d/ceph.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/docker.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/memcached.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/modbus.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/mongodb.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/mqtt.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/mysql.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/oracle.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/postgres.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/redis.conf
/etc/zabbix/zabbix_agent2.d/plugins.d/smart.conf
/usr/lib/.build-id
/usr/lib/.build-id/88
/usr/lib/.build-id/88/e20f765be60a306e65225d3110fae01371ddee
/usr/lib/systemd/system/zabbix-agent2.service
/usr/lib/tmpfiles.d/zabbix_agent2.conf
/usr/sbin/zabbix_agent2
/usr/share/doc/zabbix-agent2
/usr/share/doc/zabbix-agent2/AUTHORS
/usr/share/doc/zabbix-agent2/COPYING
/usr/share/doc/zabbix-agent2/ChangeLog
/usr/share/doc/zabbix-agent2/NEWS
/usr/share/doc/zabbix-agent2/README
/usr/share/man/man8/zabbix_agent2.8.gz
/var/log/zabbix
/var/run/zabbix
root@rocky8 ~]#vim /etc/zabbix/zabbix_agent2.conf
Server=zabbix.ehuo.org
[root@rocky8 ~]#systemctl enable --now zabbix-agent2.service
[root@rocky8 ~]#ss -ntlp|grep zabbix
LISTEN 0    128        *:10050      *:*  users:
(("zabbix_agent2",pid=22792,fd=8))

[root@zabbix-server ~]#zabbix_get -s 10.0.0.8 -k agent.ping
1

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fihb5nmN-1655950813412)(C:\Users\78715\AppData\Roaming\Typora\typora-user-images\image-20220623094335910.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4MFgjt7Y-1655950813413)(C:\Users\78715\AppData\Roaming\Typora\typora-user-images\image-20220623094317648.png)]

范例: 原来生成环境zabbix agent 升级到zabbix agent2

#停止agent 防止端口10050/tcp冲突
[root@rocky8 ~]#system disable --now zabbix-agent

#安装
[root@ubuntu1804 ~]#apt -y install zabbix-agent2
[root@centos8 ~]#yum -y install zabbix-agent2

#配置文件和agent相同
[root@ubuntu1804 ~]#grep -vE '^$|#' /etc/zabbix/zabbix_agent2.conf
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server=10.0.0.100     #修改此行
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agent2.d/*.conf
ControlSocket=/tmp/agent.sock
[root@ubuntu1804 ~]#systemctl restart zabbix-agent2

#在Zabbix Server测试
[root@zabbix-server ~]#zabbix_get -s 10.0.0.58 -k 'agent.ping'
1

#agent2是多线程
[root@rocky8 ~]#pstree -p|grep zabbix_agent2
     `-zabbix_agent2(1825)-+-{zabbix_agent2}(1826)
                |-{zabbix_agent2}(1827)
                |-{zabbix_agent2}(1828)
                |-{zabbix_agent2}(1829)
                |-{zabbix_agent2}(1830)
                |-{zabbix_agent2}(1831)
                `-{zabbix_agent2}(1833)
                
 #agent是多进程
[root@rocky8 ~]#pstree -p | grep zabbix_agent
     `-zabbix_agentd(1448)-+-zabbix_agentd(1449)
                |-zabbix_agentd(1450)
                |-zabbix_agentd(1451)
                |-zabbix_agentd(1452)
                `-zabbix_agentd(1453)               

案例:将Zabbix Server的MySQL 数据库迁移到独立的MySQL服务器

#备份数据库
[root@zabbix-server ~]#mysqldump -uroot -A -F --single-transaction > /data/all.sql
[root@zabbix-server ~]#systemctl stop zabbix-server.service
[root@zabbix-server ~]#systemctl disable --now mysql

#在独立数据库服务器上安装并恢复数据库

[root@mysql-server ~]#apt -y install mysql-server
[root@mysql-server ~]#mysql <  all.sql

#重新授权用户允许远程连接zabbix数据库
[root@mysql-server ~]#mysql
mysql> grant all privileges on zabbix.* to zabbix'10.0.0.%' identified by '123456';

#将php的配置指向新的数据库服务器IP
[root@zabbix-server ~]#vim /usr/share/zabbix/conf/zabbix.conf.php
$DB['SERVER']      = '10.0.0.101';

#将Zabbix Server的配置指向新的数据库服务器IP
[root@zabbix-server ~]#vim /etc/zabbix/zabbix_server.conf
DBHost=10.0.0.101
DBPort=3306

#重启服务生效
[root@zabbix-server ~]#systemctl start zabbix-server.service

rivileges on zabbix.* to zabbix’10.0.0.%’ identified by ‘123456’;

#将php的配置指向新的数据库服务器IP
[root@zabbix-server ~]#vim /usr/share/zabbix/conf/zabbix.conf.php
$DB[‘SERVER’] = ‘10.0.0.101’;

#将Zabbix Server的配置指向新的数据库服务器IP
[root@zabbix-server ~]#vim /etc/zabbix/zabbix_server.conf
DBHost=10.0.0.101
DBPort=3306

#重启服务生效
[root@zabbix-server ~]#systemctl start zabbix-server.service






















Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐