zabbix搭建

1、在zabbix服务端配置zabbix源(实验环境为centos7,如果使用centos6将连接地址中的7修改为6即可)

#m01 web01 web02
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm

2、安装相应软件

#m01
yum install zabbix-server-mysql zabbix-web-mysql httpd php -y
yum -y install mariadb-server
yum install -y zabbix-agent zabbix-get

3、配置数据库

#配置 mariadb
mysql
create database zabbix character set utf8 collate utf8_bin;
grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';

#实际操作命令
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> select user,host from mysql.user;
+--------+-----------+
| user | host |
+--------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| zabbix | localhost |
| | m01 |
| root | m01 |
+--------+-----------+
7 rows in set (0.00 sec)

MariaDB [(none)]> Bye

MariaDB [(none)]> show tables from zabbix;
Empty set (0.00 sec)

4、同步数据库

[root@m01 ~]# zcat /usr/share/doc/zabbix-server-mysql-3.0.19/create.sql.gz|mysql zabbix

5、启动zabbix、数据库、apache

[root@m01 ~]# systemctl start zabbix-server.service httpd.service mariadb



修改zabbix的登录密码
sed -i '115a DBPassword=zabbix' /etc/zabbix/zabbix_server.conf
# 修改apache-php配置
sed -i.ori '18a php_value date.timezone Asia/Shanghai' /etc/httpd/conf.d/zabbix.conf


[root@m01 ~]# grep -i ^DBpas /etc/zabbix/zabbix_server.conf
DBPassword=zabbix

6、客户端安装(客户端也需要添加zabbix源 可以参考第一项)

#web01 web02
yum install -y zabbix-agent

zabbix yum源
repo.zabbix.com

mirrors.aliyun.com
https://mirrors.tuna.tsinghua.edu.cn

7、在服务端进行测试  看客户端是否添加成功

[root@m01 ~]# zabbix_get -s 172.16.1.61 -p10050 -k agent.ping
1

8、修改zabbix的中文乱码

yum -y install wqy-microhei-fonts
cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf

监控项 item


web01 监控服务器登录的用户数量 >3

创建自定义监控项

1.命令测试命令
[root@web01 ~]# who|wc -l
2

2.写入到客户端的自定义文件中
自定义key(键值)
agent.ping

/etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
/etc/zabbix/zabbix_agentd.d/user.conf
/etc/zabbix/zabbix_agentd.d/web.conf

用户自定义的键值
UserParameter=mysql.version,mysql -V
UserParameter=key,命令/脚本
UserParameter=key[*],命令/脚本 $1

UserParameter=login.user,who|wc -l


[root@web01 ~]# tail -1 /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=login.user,who|wc -l
[root@web01 ~]# systemctl restart zabbix-agent.service

[root@m01 ~]# zabbix_get -s 172.16.1.7 -klogin.user
3

3.web页面的操作


监控项:
30秒1次 1个小时120次


历史数据保留时长(单位天)
趋势数据存储周期(单位天)
每小时取得一次数据

触发器-trigger


#监控nginx状态
nginx.conf
[root@web01 ~]# cat /etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /nginx.status {
stub_status on;
access_log off;
allow 172.16.1.0/24;
deny all;
}
}
}

[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]#
[root@web01 ~]# systemctl restart nginx


[root@web01 ~]# curl 172.16.1.7/nginx.status
Active connections: 1
server accepts handled requests
5 5 5
Reading: 0 Writing: 1 Waiting: 0


#1.确认需求 找出对应命令

[root@web01 ~]# curl 172.16.1.7/nginx.status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0

[root@web01 ~]# curl -s 172.16.1.7/nginx.status |awk 'NR==1{print $NF}'
1
active curl -s 172.16.1.7/nginx.status |awk 'NR==1{print $NF}'
accept curl -s 172.16.1.7/nginx.status |awk 'NR==3{print $1}'
handle curl -s 172.16.1.7/nginx.status |awk 'NR==3{print $2}'
request curl -s 172.16.1.7/nginx.status |awk 'NR==3{print $3}'
read curl -s 172.16.1.7/nginx.status |awk 'NR==4{print $2}'
write curl -s 172.16.1.7/nginx.status |awk 'NR==4{print $4}'
wait curl -s 172.16.1.7/nginx.status |awk 'NR==4{print $6}'

#2.把命令写入到 用户自定义监控目录
UserParameter=active,curl -s 172.16.1.7/nginx.status |awk 'NR==1{print $NF}'
UserParameter=accept,curl -s 172.16.1.7/nginx.status |awk 'NR==3{print $1}'
UserParameter=handle,curl -s 172.16.1.7/nginx.status |awk 'NR==3{print $2}'
UserParameter=request,curl -s 172.16.1.7/nginx.status |awk 'NR==3{print $3}'
UserParameter=read,curl -s 172.16.1.7/nginx.status |awk 'NR==4{print $2}'
UserParameter=write,curl -s 172.16.1.7/nginx.status |awk 'NR==4{print $4}'
UserParameter=wait,curl -s 172.16.1.7/nginx.status |awk 'NR==4{print $6}'

#3.通过shell脚本完成
[root@web01 ~]# mkdir -p /server/scripts/
[root@web01 ~]# vim /server/scripts/ng-stat.sh

nginx.stat[*]

#!/bin/bash
CMD=curl -s 172.16.1.7/nginx.status

case "$1" in

active) $CMD |awk 'NR==1{print $NF}' ;;
accept) $CMD |awk 'NR==3{print $1}' ;;
handle) $CMD |awk 'NR==3{print $2}' ;;
request)$CMD |awk 'NR==3{print $3}' ;;
read) $CMD |awk 'NR==4{print $2}' ;;
write) $CMD |awk 'NR==4{print $4}' ;;
wait) $CMD |awk 'NR==4{print $6}' ;;
*) echo "USAGE:$0 " ;;

esac


#!/bin/bash

aa=(`curl -s 172.16.1.8/nginx.status| grep -o '[0-9]*' `)

case "$1" in
active)
echo ${aa[0]}
;;
accept)
echo ${aa[1]}
;;
handle)
echo ${aa[2]}
;;
request)
echo ${aa[3]}
;;
read)
echo ${aa[4]}
;;
write)
echo ${aa[5]}
;;
wait)
echo ${aa[6]}
;;
*)
echo "输入不对"
;;
esac

[root@web01 ~]# sh /server/scripts/ng-stat.sh
USAGE:/server/scripts/ng-stat.sh
[root@web01 ~]# sh /server/scripts/ng-stat.sh active
1
[root@web01 ~]# sh /server/scripts/ng-stat.sh read
0
[root@web01 ~]# sh /server/scripts/ng-stat.sh wait
0


[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/web.conf
UserParameter=nginx.stat[*],/bin/sh /server/scripts/ng-stat.sh "$1"


zabbix_get -s 172.16.1.7 -k nginx.stat[active]
zabbix_get -s 172.16.1.7 -k nginx.stat[wait]


自动发现 zdfx auto discovery (被动模式)
服务端挨个询问 客户端是否是新来的

自动注册 auto reg (主动模式)
新来的自动寻找服务端 提交自己的信息 让服务端把自己加入到 zabbix中

原文地址:https://www.cnblogs.com/xiaoyaoren/p/9255862.html