Zabbix

Zabbix 5.0版本-监控主机信息

2023-04-06  本文已影响0人  Chris0Yang

zabbix流程架构

snmp:  zabbix监控网络性能所依赖协议
IPMI:  用于监控物理硬件性能指标,可以获取物理设备的温度、CPU转速
JMX:  监控java项目所使用的出口
4bddba84ba18b8ab60efbe5a101403f.png

1.安装centos-release-scl

yum install -y centos-release-scl

2.安装部署zabbix服务端

# zabbix 监控的支持有:网络设备(路由器、交换机、防火墙),服务器(windows、Linux系统)
# 下载源地址
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm?spm=a2c6h.25603864.0.0.13432e2f7uMPXn

# 安装rpm
rpm -Uvh /opt/zabbix-release-5.0-1.el7.noarch.rpm

# 查看zabbix源的文件
[root@AdminController ~]# cat /etc/yum.repos.d/zabbix.repo 
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/   # 修改为阿里云源
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/frontend  # 修改为阿里云源
enabled=1 
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/debuginfo/  # 修改为阿里云源
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=1

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/  # 修改为阿里云源
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1

# 添加gpgkey
curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX

# 执行yum源更新命令
yum clean all
yum makecache
yum update -y

# 查看zabbix源
yum list zabbix*

# 安装
yum -y install zabbix-server-mysql zabbix-agent zabbix-web-mysql-scl zabbix-nginx-conf-scl

创建初始数据库

# 安装
yum install -y mariadb-server

# 启动
systemctl start mariadb.service
systemctl enable mariadb.service

# 检查端口
netstat -tnlup
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      3287/mysqld

# 登录mysql
mysql -uroot -ppassword

# 创建zabbix库
create database zabbix character set utf8 collate utf8_bin;
show databases;
# 创建zabbix用户
create user zabbix@localhost identified by 'zabbix';
grant all privileges on zabbix.* to zabbix@localhost;
set global log_bin_trust_function_creators = 1;

#退出
quit;

# 查看zabbix创建sql脚本包
rpm -ql zabbix-server-mysql
...
/usr/share/doc/zabbix-server-mysql-5.0.32/create.sql.gz
...

# sql文件 添加zabbix库
cat /opt/zabbix_create.sql
........
USE zabbix;
CREATE TABLE `users` (
        `userid`                 bigint unsigned                           NOT NULL,
        `alias`                  varchar(100)    DEFAULT ''                NOT NULL,
        `name`                   varchar(100)    DEFAULT ''                NOT NULL,
........

# 导入
1)使用文件直接导入
mysql< /opt/zabbix_create.sql 
2)使用gz包导入到zabbix库
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

# 检查一下表名是否存在
mysql -uroot -ppassword
use zabbix;
show tables;

修改zabbix配置

# 修改Zabbix server配置数据库 
cat -n /etc/zabbix/zabbix_server.conf
...
  116   DBUser=zabbix
  124   DBPassword=zabbix
...

# 修改php配置
cat /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
...
  6 listen.acl_users = apache,nginx
  25 php_value[date.timezone] = Asia/Shangha
...


# 修改nginx配置
cat /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf
...

  1 server {
  2         listen          80;
  3         server_name     localhost;
...

cat /etc/opt/rh/rh-nginx116/nginx/nginx.conf
...
  38 #    server {
  39 #        listen       80 default_server;
  40 #        listen       [::]:80 default_server;
  41 #        server_name  _;
  42 #        root         /opt/rh/rh-nginx116/root/usr/share/nginx/html;
  43 #
  44 #        # Load configuration files for the default server block.
  45 #        include      /etc/opt/rh/rh-nginx116/nginx/default.d/*.conf;
  46 #
  47 #        location / {
  48 #        }
...

启动Zabbix server和agent进程,并为它们设置开机自启

#执行命令
systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm

#检查端口
[root@AdminController nginx]# netstat -ntlup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      34660/zabbix_agentd 
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      34674/zabbix_server 
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      34667/php-fpm: mast 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      34517/mysqld        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      34710/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1258/sshd           
tcp6       0      0 :::10050                :::*                    LISTEN      34660/zabbix_agentd 
tcp6       0      0 :::10051                :::*                    LISTEN      34674/zabbix_server 
tcp6       0      0 :::22                   :::*                    LISTEN      1258/sshd           
udp        0      0 0.0.0.0:8472            0.0.0.0:*                           -                   

# 访问zabbix页面
http://localhost/setup.php

账号:Admin
密码:zabbix

切换中文字体

306139566ac5d2ac251f2f07e3633fa.png

客服端安装zabbix-agent2

520944a848f22fb4bbf7f8920e1424f.png
# 下载源地址
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm?spm=a2c6h.25603864.0.0.13432e2f7uMPXn

# 安装
yum install -y zabbix-agent2

# 查看zabbix-agent2依赖包
[root@k8s-master yum.repos.d]# rpm -ql zabbix-agent2
...
/var/log/zabbix
/var/run/zabbix
...

# zabbix_agent2配置
[root@k8s-master yum.repos.d]# grep -Ev '^#|^$' /etc/zabbix/zabbix_agent2.conf 
...
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agent2.d/*.conf
ControlSocket=/tmp/agent.sock
...

添加主机项

# 修改客户端的agent2配置文件
cat -n  /etc/zabbix/zabbix_agent2.conf|grep Server=
80  Server=192.168.255.129 

# 启动zabbix-agent2服务
systemctl restart zabbix-agent2

# zabbix客户端口
netstat -lntup
tcp6       0      0 :::10050                :::*                    LISTEN      21332/zabbix_agent2

zabbix页面添加主机

705bbc129857aeaafe4665f5d22c104.png
d57fb7ccb0e8c49775ac800fa760106.png
51dcc03ecbd422710385daa9419f3dc.png

添加监控项

# 客户端自定义监控取值
1)通过shel1命令获取到关键数据
2)将命令与key名在配置文件中定义好
3)服务端通过zabbix-get命令测试获取键值对

# 添加客户端的zabbix_agentd.conf配置文件
cat -n /etc/zabbix/zabbix_agentd.conf |grep UserParameter
342 UserParameter=User_num,w|awk 'NR==1{print $4}'

# 修改完成后需要重启zabbix-agent2服务
systemctl restart zabbix-agent2.service
 
# 服务端安装
yum install -y zabbix-get

# 在服务端使用:zabbix-get的命令,有多少连接数
[root@AdminController nginx]# zabbix_get -s 192.168.255.130 -k User_num
1

添加监控项-页面操作

zabbix监控平台 -> 配置 -> 主机 -> 监控项 -> 创建监控项


054f00fd1022996b45b47341ce2ee5d.png
17de74d4095d607bfb1b6029ef98fd6.png

批量定义监控项

[root@k8s-master /]# cat /etc/nginx/conf.d/nginx_status.conf 
server {
        listen 81;
        server_name localhost;

location /status {
        stub_status on;
    }
}

#curl nginx地址
[root@k8s-master /# curl 192.168.255.130:81/status
Active connections: 1 
server accepts handled requests
 6 6 6 
Reading: 0 Writing: 1 Waiting: 0 

#脚本
[root@k8s-master /]# cat nginx_status.sh 
NGINX_PORT=81
NGINX_COMMAND=$1

nginx_active(){
    /usr/bin/curl -s "http://192.168.255.130:"$NGINX_PORT"/status/" |awk '/Active/ {print $NF}'
}
nginx_reading(){
    /usr/bin/curl -s "http://192.168.255.130:"$NGINX_PORT"/status/" |awk '/Reading/ {print $2}'
}
nginx_writing(){
    /usr/bin/curl -s "http://192.168.255.130:"$NGINX_PORT"/status/" |awk '/Writing/ {print $4}'
}

case $NGINX_COMMAND in 
    active)
    nginx_active;
    ;;
    reading)
    nginx_reading;
    ;;
    writing)
    nginx_writing;
    ;;  
    *)
    echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
esac

# nginx_status配置文件
[root@k8s-master /]# cat /etc/zabbix/zabbix_agent2.d/nginx_status.conf 
UserParameter=Ngx_status[*],/etc/zabbix/zabbix_agent2.d/nginx_status.sh $1

# 重启服务
systemctl restart zabbix-agent2.service

# 服务端zabbix-get
zabbix_get -s 192.168.255.130 -k Ngx_status[writing]

添加触发器

# 客户端-编写配置
[root@k8s-master /]# cat /etc/zabbix/zabbix_agent2.d/user_num.conf 
UserParameter= User_ip,w | awk 'NR>2{print $3}'|sort -r|uniq -c|wc -l
UserParameter= User_num,w | awk 'NR==1{print $5}'

# 先在客户端检查,再重启服务
[root@AdminController nginx]# zabbix_agent2 -t User_num
systemctl restart zabbix-agent2.service

# 服务端-测试
[root@AdminController /]# zabbix_get -s 客户端IP -k User_ip

添加触发器-页面操作

f4ba298f6550a56c8c0280409590e05.png 277f6ac2981c18cbd6f4784207534ab.png 60a3422079911aa7e97cf725beb7330.png 3c78c23b353ea88c8948e4350d7545d.png 25ac25207125f9d25697b878df28e7a.png 44a82db1bc7502f0157005209be3f97.png
上一篇下一篇

猜你喜欢

热点阅读