第二周
-
总结各系统版本的zabbix安装。
常用安装:包安装、二进制安装、源码编译安装、docker容器安装
如果需要中文建议现在linux安装中文包:
#CentOS安装中文包,再修改语言,否则无法选择 [root@zabbix-server ~]#yum -y install langpacks-zh_CN #Ubuntu安装下面中文包 [root@zabbix-server ~]#apt -y install language-pack-zh-hans 将中文字体上传到 Zabbix Server 的目录 /usr/share/zabbix/assets/fonts 下
以包安装为例:
在Ubuntu20.04 安装 Zabbix Server 6.0
#Install Zabbix repository [root@ubuntu2004 ~]#wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix- release_6.0-1+ubuntu20.04_all.deb [root@ubuntu2004 ~]#dpkg -i zabbix-release_6.0-1+ubuntu20.04_all.deb [root@ubuntu2004 ~]#cat /etc/apt/sources.list.d/zabbix.list deb https://repo.zabbix.com/zabbix/6.0/ubuntu focal main deb-src https://repo.zabbix.com/zabbix/6.0/ubuntu focal main #加速,更新为国内源(可选) [root@ubuntu2004 ~]#sed -i.bak's#https://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/apt/sources.list.d/zabbix.list [root@ubuntu2004 ~]#cat /etc/apt/sources.list.d/zabbix.list deb https://mirrors.aliyun.com/zabbix/zabbix/6.0/ubuntu focal main deb-src https://mirrors.aliyun.com/zabbix/zabbix/6.0/ubuntu focal main [root@ubuntu2004 ~]#apt update #安装Zabbix server,Web前端,agent2 [root@ubuntu2004 ~]#apt -y install zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent2 zabbix-get #安装nginx [root@ubuntu2004 ~]#nginx -v nginx version: nginx/1.18.0 (Ubuntu) #安装php-fpm [root@ubuntu2004 ~]#php-fpm7.4 -v PHP 7.4.3 (fpm-fcgi) (built: May 6 2022 00:16:42) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies #安装zabbix_agent2 [root@ubuntu2004 ~]#zabbix_agent2 -V zabbix_agent2 (Zabbix) 6.0.5 ....... #创建初始数据库 [root@ubuntu2004 ~]#apt -y install mysql-server [root@ubuntu2004 ~]#mysql Server version: 8.0.29-0ubuntu0.20.04.3 (Ubuntu) mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin; mysql> create user zabbix@localhost identified by '123456'; mysql> grant all privileges on zabbix.* to zabbix@localhost; mysql> set global log_bin_trust_function_creators = 1; mysql> quit; #导入初始架构和数据,系统将提示您输入新创建的密码。 [root@ubuntu2004 ~]#zcat /usr/share/doc/zabbix-sql-scripts/mysql/server.sql.gz | mysql -uzabbix -p123456 zabbix #验证数据库和表 [root@ubuntu2004 ~]#mysql -uzabbix -p123456 zabbix -e 'show tables;' |head mysql: [Warning] Using a password on the command line interface can be insecure. Tables_in_zabbix acknowledges actions alerts auditlog autoreg_host conditions config config_autoreg_tls corr_condition [root@ubuntu2004 ~]#mysql -uroot -p password mysql> set global log_bin_trust_function_creators = 0; mysql> quit; #为Zabbix server配置数据库 [root@ubuntu2004 ~]#vim /etc/zabbix/zabbix_server.conf DBPassword=123456 #确认数据库配置 [root@ubuntu2004 ~]#grep -E '^DB|DBHost=' /etc/zabbix/zabbix_server.conf # DBHost=localhost DBName=zabbix DBUser=zabbix DBPassword=123456 #为Zabbix前端配置PHP [root@ubuntu2004 ~]#vim /etc/zabbix/nginx.conf #取消下面两行的注释并修改 listen 80; server_name zabbix.wang.org; #默认安装apache2和nginx冲突,禁用apache2 [root@ubuntu2004 ~]#systemctl disable --now apache2.service #重启服务 [root@ubuntu2004 ~]#systemctl restart zabbix-server zabbix-agent2 nginx php7.4- fpm [root@ubuntu2004 ~]#systemctl enable zabbix-server zabbix-agent2 nginx php7.4- fpm [root@ubuntu2004 ~]#ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* LISTEN 0 128 127.0.0.1:6011 0.0.0.0:* LISTEN 0 4096 0.0.0.0:10051 0.0.0.0:* 条件检测 LISTEN 0 70 127.0.0.1:33060 0.0.0.0:* LISTEN 0 151 127.0.0.1:3306 0.0.0.0:* LISTEN 0 511 0.0.0.0:80 0.0.0.0:* LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 [::1]:6010 [::]:* LISTEN 0 128 [::1]:6011 [::]:* LISTEN 0 4096 *:10050 *:* LISTEN 0 4096 [::]:10051 [::]:* LISTEN 0 511 [::]:80 [::]:* #配置Zabbix前端 #连接到新安装的Zabbix前端: http://server_ip_or_name #根据Zabbix文件里步骤操作: Installing frontend
rocky8下安装zabbix6.0
yum安装zabbix 安装zabbix-server https://www.zabbix.com/cn/download?zabbix=6.0&os_distribution=rocky_linux&os_version=9&components=server_frontend_agent&db=mysql&ws=apache # rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/9/x86_64/zabbix-release-6.0-4.el9.noarch.rpm # dnf clean all # dnf install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent 安装数据库和初始化数据库 #yum install -y mysql-server [root@Rocky8 ~]# mysql mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin; mysql> grant all privileges on zabbix.* to zabbix@localhost; mysql> set global log_bin_trust_function_creators = 1; #导入初始架构和数据 # zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix #验证数据库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | zabbix | +--------------------+ 5 rows in set (0.00 sec) mysql> use zabbix; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------------------+ | Tables_in_zabbix | +----------------------------+ | acknowledges | | actions | | alerts | | auditlog | | autoreg_host | | conditions | | config | | config_autoreg_tls | | corr_condition | | corr_condition_group | | corr_condition_tag | | corr_condition_tagpair | | corr_condition_tagvalue | | corr_operation | | correlation | | dashboard | | dashboard_page | | dashboard_user | | dashboard_usrgrp | | dbversion | | dchecks | | dhosts | | drules | | dservices | | escalations | | event_recovery | | event_suppress | | event_tag | | events | | expressions | | functions | | globalmacro | | globalvars | | graph_discovery | | graph_theme | | graphs | | graphs_items | | group_discovery | | group_prototype | | ha_node | | history | | history_log | | history_str | | history_text | | history_uint | | host_discovery | | host_inventory | | host_tag | | hostmacro | | hosts | | hosts_groups | | hosts_templates | | housekeeper | | hstgrp | | httpstep | | httpstep_field | | httpstepitem | | httptest | | httptest_field | | httptest_tag | | httptestitem | | icon_map | | icon_mapping | | ids | | images | | interface | | interface_discovery | | interface_snmp | | item_condition | | item_discovery | | item_parameter | | item_preproc | | item_rtdata | | item_tag | | items | | lld_macro_path | | lld_override | | lld_override_condition | | lld_override_opdiscover | | lld_override_operation | | lld_override_ophistory | | lld_override_opinventory | | lld_override_opperiod | | lld_override_opseverity | | lld_override_opstatus | | lld_override_optag | | lld_override_optemplate | | lld_override_optrends | | maintenance_tag | | maintenances | | maintenances_groups | | maintenances_hosts | | maintenances_windows | | media | | media_type | | media_type_message | | media_type_param | | module | | opcommand | | opcommand_grp | | opcommand_hst | | opconditions | | operations | | opgroup | | opinventory | | opmessage | | opmessage_grp | | opmessage_usr | | optemplate | | problem | | problem_tag | | profiles | | proxy_autoreg_host | | proxy_dhistory | | proxy_history | | regexps | | report | | report_param | | report_user | | report_usrgrp | | rights | | role | | role_rule | | script_param | | scripts | | service_alarms | | service_problem | | service_problem_tag | | service_status_rule | | service_tag | | services | | services_links | | sessions | | sla | | sla_excluded_downtime | | sla_schedule | | sla_service_tag | | sysmap_element_trigger | | sysmap_element_url | | sysmap_shape | | sysmap_url | | sysmap_user | | sysmap_usrgrp | | sysmaps | | sysmaps_element_tag | | sysmaps_elements | | sysmaps_link_triggers | | sysmaps_links | | tag_filter | | task | | task_acknowledge | | task_check_now | | task_close_problem | | task_data | | task_remote_command | | task_remote_command_result | | task_result | | timeperiods | | token | | trends | | trends_uint | | trigger_depends | | trigger_discovery | | trigger_queue | | trigger_tag | | triggers | | users | | users_groups | | usrgrp | | valuemap | | valuemap_mapping | | widget | | widget_field | +----------------------------+ 173 rows in set (0.00 sec) mysql> #为Zabbix server配置数据库 [root@Rocky8 ~]# cat /etc/zabbix/zabbix_server.conf DBPassword=123456 #重启Zabbix server和agent进程 systemctl restart zabbix-server zabbix-agent httpd php-fpm #zabbix网页配置就完成安装zabbix-server了 #注意问题: 修改时区问题 中文乱码问题 #zabbix-agent安装 # rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/9/x86_64/zabbix-release-6.0-4.el9.noarch.rpm # dnf clean all #dnf install zabbix-agent vi /etc/zabbix/zabbix_agentd.conf 。。。。 server 10.0.0.4 #指向当前zabbix-server ListenPort 10050 #监听端口 startAgents=3 #被动状态时默认启动的实例数 Hostname=10.0.0.24 #区分大小写在zabbix-server唯一的值 重启zabbix——agent服务 [root@localhost src]# systemctl restart zabbix-agent.service [root@localhost src]# systemctl status zabbix-agent.service
-
总结 zabbix主动模式和被动模式简介及实现
Zabbix 监控流程中 Agent 收集数据分为主动和被动两种模式 主动模式和被动模式都是相对 zabbix agent 而言的 Zabbix Agent 默认是被动模式,需要修改配置才能支持主动模式 Zabbix Agent 的关健配置有下面三项: Server=<Zabbix Server> #被动模式指向 Zabbix Server的IP或FQDN,默认模式,在主动 模式此项可选的,建议配置,否则ZBX显示红色 ServerActive=<Zabbix Server> #主动模式指向 Zabbix Server的IP或FQDN Hostname=<当前主机IP> #当前主机的IP,此项必须和Zabbix Server中设置的主机名称相 同,默认使用本机的主机名 HostnameItem=system.hostname #也可以使用此system.hostname监控项自动生成主机名称,默 认就是使用本机的主机名 被动模式:由Server建立TCP链接并向Agent端发送请求。Agent需启动监听端口,等待Server连接。 [root@centos8 ~]#ss -nta|grep 10050 LISTEN 0 128 0.0.0.0:10050 0.0.0.0:* TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48786 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48790 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48866 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48884 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48860 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48848 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48846 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48826 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48774 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48808 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48836 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48824 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48772 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48840 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48820 TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48886 主动模式:由Agent主动建立TCP链接并向Server端发送请求。因为是Agent主动发起的TCP连接,所以Agent所在机器本身无需再启动监听Agent端口。 Zabbix Agent配置主动模式配置: [root@centos8 ~]#grep '^[^#]' /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=10.0.0.100 #被动模式指向 Zabbix Server的IP或FQDN ServerActive=10.0.0.100 #主动模式指向 Zabbix Server的IP或FQDN Hostname=10.0.0.104 #当前主机的IP,此项必须和Zabbix Server中设置的主机名称相 同 Timeout=30 #建议修改 Include=/etc/zabbix/zabbix_agentd.d/*.conf [root@centos8 ~]#systemctl restart zabbix-agent.service [root@centos8 ~]#ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 100 127.0.0.1:25 0.0.0.0:* LISTEN 0 128 0.0.0.0:10050 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* 3.10.2.2.2 基于现有模板将包含的监控项修改为主动模式的监控项 注意: Zabbix 5.0 内置了主动模式的模板 Template OS Linux by Zabbix agent active 无需修改 Zabbix4.0 之前没有主动模式的模板,需要自行修改 以下操作为Zabbix 4.0上实现 LISTEN 0 100 [::1]:25 [::]:* LISTEN 0 128 [::]:10050 [::]:* #Zabbix agent主动模式对应的进程数量只有1个,不能调整 #Zabbix agent被动模式对应的进程数量默认3个,可以调整
-
总结 zabbix proxy主动及被动案例
proxy主动模式案列 yum安装zabbix-proxy rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm dnf install zabbix-proxy-mysql zabbix-sql-scripts zabbix-selinux-policy 在数据库中创建zabbix_proxy_active库 mysql> create database zabbix_proxy_active character set utf8mb4 collate utf8mb4_bin; Query OK, 1 row affected (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_proxy_active.* to zabbix@'10.0.0.%'; Query OK, 0 rows affected (0.00 sec) #修改zabbix_proxy_conf配置文件 [17:41:12 root@proxy ~]#grep '^[a-Z]' /etc/zabbix/zabbix_proxy.conf ProxyMode=0 Server=10.0.0.4 Hostname=10.0.0.18-active ListenPort=10051 LogFile=/var/log/zabbix/zabbix_proxy.log LogFileSize=0 PidFile=/run/zabbix/zabbix_proxy.pid SocketDir=/run/zabbix DBHost=10.0.0.4 DBName=zabbix_proxy_active DBUser=zabbix DBPassword=123456 DBPort=3306 ProxyLocalBuffer=720 ProxyOfflineBuffer=720 HeartbeatFrequency=60 StartPollers=5 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=30 LogSlowQueries=3000 StatsAllowedIP=127.0.0.1 #重启服务 [17:44:52 root@proxy ~]#systemctl restart zabbix-proxy.service #查看端口起来情况 [17:44:52 root@proxy ~]#ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 128 0.0.0.0:10051 0.0.0.0:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 128 [::]:10051 [::]:* LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:* LISTEN 0 100 *:8080 *:*
-
总结自定义监控,监控win, nginx, 并基于短信/微信报警。
安装zabbix-agent的win版本客户端,然后运行框输入services.msc在服务中可以看到一个zabbix-agent服务在运行,然后再zabbix-web添加win主机,选择适合win的监控模板。 安装nginx [root@ubuntu200406 ~]#apt install nginx -y #然后配置nginx状态页 [root@ubuntu200406 ~]#vim /etc/nginx/sites-enabled/default location /basic_status { #basic_status是宏定义路径,Zabbix系统默认监控路径 stub_status; } #语法检查没有问题 [root@ubuntu200406 ~]#nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful #重新加载nginx即可 [root@ubuntu200406 ~]#nginx -s reload 为nginx添加监控模板
-
完成1键安装zabbix agent脚本,可以用于ubuntu系统。
#CentOS和Ubuntu 通用 [root@ubuntu]#cat install_zabbix_agent_src.sh #!/bin/bash ZABBIX_VER=5.0.13 FILE=zabbix-${ZABBIX_VER}.tar.gz INSTALL_DIR=/apps/zabbix_agent SERVER=10.0.0.100 HOST=`hostname -I` . /etc/os-release color () { RES_COL=60 MOVE_TO_COL="echo -en \\033[${RES_COL}G" SETCOLOR_SUCCESS="echo -en \\033[1;32m" SETCOLOR_FAILURE="echo -en \\033[1;31m" SETCOLOR_WARNING="echo -en \\033[1;33m" SETCOLOR_NORMAL="echo -en \E[0m" echo -n "$1" && $MOVE_TO_COL echo -n "[" if [ $2 = "success" -o $2 = "0" ] ;then ${SETCOLOR_SUCCESS} echo -n $" OK " elif [ $2 = "failure" -o $2 = "1" ] ;then ${SETCOLOR_FAILURE} echo -n $"FAILED" else ${SETCOLOR_WARNING} echo -n $"WARNING" fi ${SETCOLOR_NORMAL} echo -n "]" echo } check(){ wget https://cdn.zabbix.com/zabbix/sources/stable/${ZABBIX_VER%.*}/zabbix-$ZABBIX_VER .tar.gz [ -e ${FILE} ] || { color "$FILE 文件不存在,请检查" 1 ; exit 1; } } prepare (){ if [ $ID = "centos" $ID = "rocky" ];then yum -y install gcc make pcre-devel || { color "zabbix agent 相关包安装失 败,请检查" 1 ;exit; } else apt -y install gcc make || { color "zabbix agent 相关包安装失败,请检查" 1 ;exit; } fi id zabbix &> /dev/null || useradd -r zabbix } install() { tar xf $FILE cd zabbix-$ZABBIX_VER ./configure --prefix=${INSTALL_DIR} --enable-agent make && make install } config () { sed -i.bak -e "s/Server=127.0.0.1/Server=$SERVER/" -e "s/Hostname=Zabbix server/Hostname=$HOST/" ${INSTALL_DIR}/etc/zabbix_agentd.conf echo 'PATH=${INSTALL_DIR}/bin/:${INSTALL_DIR}/sbin/:$PATH' > /etc/profile.d/zabbix_agent.sh } service (){ cat > /lib/systemd/system/zabbix-agent.service <<EOF [Unit] Description=Zabbix Agent After=syslog.target After=network.target [Service] Type=forking Restart=on-failure PIDFile=/tmp/zabbix_agentd.pid KillMode=control-group ExecStart=${INSTALL_DIR}/sbin/zabbix_agentd -c ${INSTALL_DIR}/etc/zabbix_agentd.conf ExecStop=/bin/kill -SIGTERM $MAINPID RestartSec=10s User=zabbix Group=zabbix [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable --now zabbix-agent.service systemctl is-active zabbix-agent.service && color "Zabbix Agent 安装完成并启动" 0 } check prepare install config service
-
完成1键脚本,可以基于zabbix api添加zabbix agent到zabbix web。
[root@zabbix-server ~]#vim ./zabbix-api-addhost.sh #!/bin/bash ZABBIX_SERVER=10.0.0.100 TOKEN=$(./zabbix-api-token.sh| awk -F'"' '{print $8}') NET=10.0.0 for HOST in $NET.{200..210};do curl -s -XPOST -H "Content-Type: application/json-rpc" -d ' { "jsonrpc": "2.0", "method": "host.create", "params": { "host": "'web-api-$HOST'", #创建主机名称 "name": "'web-api-$HOST'", #可见的名称 "interfaces": [ { "type": 1, #类型为1表示agent,2是SNMP,3是IPMI,4是JMX "main": 1, #主要接口 "useip": 1, #0是使用DNS,1是使用IP地址 "ip": "'$HOST'", #添加的zabbix agent的IP地址 "dns": "", "port": "10050" #agent使用的端口 } ], "groups": [ { "groupid": "2" #添加到的组的ID } ], "templates": [ { "templateid": "10001" #关联的模板的ID } ] }, "id": 1, "auth": "'$TOKEN'" }' http://${ZABBIX_SERVER}/zabbix/api_jsonrpc.php | python3 -m json.tool