zabbix4.0

zabbix4.0

zabbix安装

清华大学开源网址

1:下载zabbix的rpm包
wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
2:解压包

rpm -ivh zabbix-release-4.0-2.el7.noarch.rpm

3:查看安装包
rpm -ql zabbix-release
/etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX(依赖包)
/etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
/etc/yum.repos.d/zabbix.repo(zabbix的yum源)
/usr/share/doc/zabbix-release-4.0
/usr/share/doc/zabbix-release-4.0/GPL(GPL共用许可协议)
4:修改zabbix的yum源
  1. 预先做一个备份:touch /etc/yum.repos.d/zabbix.repo{,.bak}

  2. vim命令模式输入:%s#http://repo.zabbix.com/#https://mirrors.tuna.tsinghua.edu.cn/zabbix/#g

  3. vim命令模式输入:%s#gpgcheck=1#gpgcheck=0#g

vim /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.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.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck
:%s#http://repo.zabbix.com/#https://mirrors.tuna.tsinghua.edu.cn/zabbix/#g(在命令模式输入此代码完成yum源修改)
%s#gpgcheck=1#gpgcheck=0#g
5:安装zabbix组件
  • zabbix-server

  • zabbix-web

  • 两者支持两种数据库mysqlpgsql

yum -y install zabbix-server-mysql zabbix-web-mysql
6:安装mariadb数据库
yum -y install mariadb-server.x86_64
systemctl start mariadb.service
systemctl enable mariadb.service
7:mariadb安全初始化
mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
    SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] n
... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
nyyyy
8:创建zabbix库和授权
mysql
create database zabbix character set utf8 collate utf8_bin;
grant all on zabbix.* to zabbix@localhost identified by '123456';

 

9:给zabbix导入初识数据
[root@localhost ~]# rpm -ql zabbix-server-mysql|grep sql
/usr/sbin/zabbix_server_mysql
/usr/share/doc/zabbix-server-mysql-4.0.31
/usr/share/doc/zabbix-server-mysql-4.0.31/AUTHORS
/usr/share/doc/zabbix-server-mysql-4.0.31/COPYING
/usr/share/doc/zabbix-server-mysql-4.0.31/ChangeLog
/usr/share/doc/zabbix-server-mysql-4.0.31/NEWS
/usr/share/doc/zabbix-server-mysql-4.0.31/README
/usr/share/doc/zabbix-server-mysql-4.0.31/create.sql.gz
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123456 zabbix
mysql zabbix -e'show tables;' (查看数据表是否导入成功)
10:修改zabbix配置文件
vim /etc/zabbix/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456
#(找到这几行进行修改)
systemctl start zabbix-server
systemctl enable zabbix-server
11:修改zabbix时区
vim /etc/httpd/conf.d/zabbix.conf
#
# Zabbix monitoring system php web frontend
#
Alias /zabbix /usr/share/zabbix

<Directory "/usr/share/zabbix">
Options FollowSymLinks
AllowOverride None
Require all granted

<IfModule mod_php5.c>
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 always_populate_raw_post_data -1
# php_value date.timezone Europe/Riga 修改成php_value date.timezone Asia/Shanghai
</IfModule>
</Directory>

<Directory "/usr/share/zabbix/conf">
Require all denied
</Directory>

<Directory "/usr/share/zabbix/app">
Require all denied
</Directory>

<Directory "/usr/share/zabbix/include">
Require all denied
</Directory>

<Directory "/usr/share/zabbix/local">
Require all denied
</Directory>

----------------------------------------
systemctl enable httpd
systemctl start httpd

image-20210612183542555

12:账号和密码

账号:Admin

密码:zabbix

image-20210612183955245

常用的监控命令

free			#监控内存的
df #查看磁盘挂载情况
top #动态查看cpu内存进程一些负载情况
htop #跟top很相似可支持鼠标点击查看操作
uptime #将top里面的其中一部分拿出来显示,用来取值的,静态的
iftop #用来监控流量的
iostat #查看磁盘io的情况
iotop #查看某一个进程占了硬盘多少空间
netstat #查看进程监听了哪些端口
du -sh #查看文件大小
ab #Apache服务器的性能测试工具,用来测试服务器性能的linux命令/usr/bin/ab
----------------
通过不同的监控对服务器状态进行判断取值,通过ab命令对服务器进行压力测试

使用shell脚本监控

当公司服务器不是很多的时候不用搭建监控系统,写几个shell脚本监控

#!/bin/bash
while ture
do
FREE=`free -m | awk 'NR==2{print $NF}'`
if [ $FREE -lt 100 ]then;
echo $FREE | mail -s "当前内存" 1500538296@qq.com
fi
done

监控一台服务器主机

  • 安装好zabbix服务后他会有一个报警

image-20210613134329680

  • 需要安装zabbixagent端监控自己

yum -y install zabbix-agent.x86_64
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service

image-20210613134625162

报警不在了

监控客户端

wget -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.11-1.el7.x86_64.rpm
rpm -ivh zabbix-agent-4.0.11-1.el7.x86_64.rpm
vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.200.173
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service
-----------------------------------
#添加完成后要进行个性化监控
-主机群组:数据zabbixagent端的分组
-模板:所要选择的监控模板服务(mysql,linux,nginx等等)
-主机:主机为agent被监控端
-应用集:把监控指标分类
-监控项:有模板有自定义监控项,键值的形式存在的监控某一指标
-触发器:当监控指标到达一定程度时,触发触发器,进行一系列动作操作
-媒介:通过媒介定义是发送报警状态
-动作:可以发送邮件或者定义远程命令

自定义监控项

在server配置文件中添加自定义监控项

vim /etc/zabbix/zabbix_agentd.conf
UserParameter=sda_tps,iostat | awk '/sda/{print $2}' (监控sda盘的tps值)
------------------
#zabbix页面操作
配置-->主机-->监控项-->创建监控项

image-20210614153542873

创建监控项,监控项作为当前主机所要监控的选项内容。触发器,当监控达到一定阈值时触发器发出报警,报警信息通过动作实现邮件报警或者远程命令

自定义触发器

  • 当监控指标到达一定程度时,触发触发器,进行一系列动作操作

#触发器的组成
{Zabbix server:system.users.num.last()}>3
{主机名:key:last()}函数 > 3(最后的范围值)
------------------------
Zabbix server:zabbix监控的主机名字
system.users.num:key的名字
last():调用的函数方法
-------------------------
#常用的触发器函数表达式
diff:最后一个值和前一个值之间的差异(1-true,0-false)
last:最后(最近)的T值
avg:周期T内的平均值
nodata:在周期T内没有收到数据(1-true,0-false)

邮件报警

A.发件人

  • 管理-->报警媒介

image-20210613214557492

B.收件人

用户-->报警媒介-->添加

image-20210613214955261

c.启动动作

image-20210613221041699

报警邮件: {EVENT.NAME}
---------------
报警时间: {EVENT.TIME} on {EVENT.DATE}
故障名称: {EVENT.NAME}
受影响的主机: {HOST.NAME}
严重性: {EVENT.SEVERITY}

Original problem ID: {EVENT.ID}
{TRIGGER.URL}

zabbix监控微信报警

微信报警脚本

  1. 配置微信企业号

  2. 记录企业id号

  3. 生成Secret码,用哪个要发送消息生成哪个

  4. 微信企业账号不是普通微信,配置在电脑上配置,手机用来接收信息

yum -y install python-pip
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
vim weixin.py
#######################分隔符#####################
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import requests
import sys
import os
import json
import logging


logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = os.path.join('/tmp','weixin.log'),
filemode = 'a')

corpid='微信企业号corpid'
appsecret='应用的Secret'
agentid=应用的id

token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']


msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken

touser=sys.argv[1]
subject=sys.argv[2]
#toparty='3|4|5|6'
message=sys.argv[2] + "\n\n" +sys.argv[3]

params={
"touser": touser,
# "toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": message
},
"safe":0
}

req=requests.post(msgsend_url, data=json.dumps(params))
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)
------------
chmod +x weixin.py
#测试脚本
python weixin.py 'ZhaiZhangWei' 'xxx' 'yyy'
#要传三个参数
1.账号
2.标题
3.内容
确认邮件路径将脚本移动过去
egrep -v '^$|#' /etc/zabbix/zabbix_server.conf 
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts ##发送邮件脚本路径
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000

配置zabbix-server微信报警

管理--->报警媒介类型--->创建媒介
变量名对应内容
{ALERT.SENDTO} 发邮件发给谁
{ALERT.SUBJECT} 标题
{ALERT.MESSAGE} 内容

image-20210614134137086

用户--->报警媒介

收件人:企业微信的用户名

image-20210614134327430

微信脚本有问题未完待续......

zabbix故障预处理和故障升级

故障预处理

vim /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1 ###支持远程命令
AllowRoot=1 ###支持root用户权限

动作--->操作--->操作细节---->远程命令--->目标列表--->命令

image-20210614152532015

操作详解:当zabbix通过触发器发送了报警信息通过动作定义的时候,进行故障升级,由发送报警信息在经过预处理,处理不了最后发送报警到领导知道报警问题被解决

自定义图像

解决图形乱码问题

C:\Windows\Fonts (电脑中文字符集文件路径)

将电脑中的simhei.ttf文件移动到/usr/share/zabbix/assets/fonts下

mv simkai.ttf /usr/share/zabbix/assets/fonts
vim /usr/share/zabbix/include/defines.inc.php
define('ZBX_GRAPH_FONT_NAME', 'simhei'); // font file name
#刷新一下zabbix界面

主机--->图形--->监控项--->添加你所要监控的监控项

Granfana

安装Granfana
yum localinstall grafana-8.0.1-1.x86_64.rpm -y
systemctl start grafana-server.service
systemctl enable grafana-server.service

在页面登入192.168.200.173:3000 (granfana的默认端口3000)

账号:admin

密码:admin

安装zabbix插件

granfana不支持zabbix插件,要自己进行下载插件

grafana-cli plugins list-remote | grep zabbix	#过滤zabbix插件
grafana-cli plugins install alexanderzobnin-zabbix-app #安装zabbix插件
✔ Downloaded alexanderzobnin-zabbix-app v4.1.5 zip successfully

Please restart Grafana after installing plugins. Refer to Grafana documentation for instructions if necessary.
------------插件下载完毕自动解压到了/var/lib/grafana/plugins/alexanderzobnin-zabbix-app下
------------重启grafana-server.service
systemctl restart grafana-server.service

image-20210614171712264

新建一个zabbix数据源

配置--->数据源--->zabbix

URL:http://192.168.200.173/zabbix/api_jsonrpc.php

Username:Admin

Password:zabbix

Save & test(测试能否连接到zabbix)

image-20210614172248441

点击Dashboards导入

image-20210614172409025

点击仪表盘---->General/Home---->Zabbix Template Linx Server

image-20210614172741215

自定义模板

tcp11种状态

ESTABLISHED:代表一个打开的连接,数据可以传送给用户 SYN_SENT:在发送连接请求后等待匹配的连接请求 SYN_RECV:在收到和发送一个连接请求后等待对连接请求的确认 FIN_WAIT1:等待远程TCP的连接中断请求,或先前的连接中断请求的确认 FIN_WAIT2:从远程TCP等待连接中断请求 TIME_WAIT:等待足够的时间以确保远程TCP接收到连接中断请求的确认 CLOSE:等待远程TCP对连接中断的确认 CLOSE_WAIT:等待从本地用户发来的连接中断请求 LAST_ACK:等待原来发向远程TCP的连接中断请求的确认 LISTEN:侦听来自远方TCP端口的连接请求 CLOSING:没有任何连接状态

监控tcp11种状态
man netstat
state
复制11种状态导入tcps.txt中再用命令抓取出11种状态重新写入tcp.txt文件
awk -F " " '{print $1}' tcps.txt | grep -v "^$" | grep -v "The\|Connection" |sed -r '4d'
ESTABLISHED
SYN_SENT
SYN_RECV
FIN_WAIT1
FIN_WAIT2
TIME_WAIT
CLOSE
CLOSE_WAIT
LAST_ACK
LISTEN
CLOSING
监听tcp11种状态的配置文件
  • 配置自定义模板文件时候确定Include=/etc/zabbix/zabbix_agentd.d/*.conf开启了

cat /etc/zabbix/zabbix_agentd.d/user_def.conf
UserParameter=ESTABLISHED,netstat -ant | grep -c ESTABLISHED
UserParameter=SYN_SENT,netstat -ant | grep -c SYN_SENT
UserParameter=SYN_RECV,netstat -ant | grep -c SYN_RECV
UserParameter=FIN_WAIT1,netstat -ant | grep -c FIN_WAIT1
UserParameter=FIN_WAIT2,netstat -ant | grep -c FIN_WAIT2
UserParameter=TIME_WAIT,netstat -ant | grep -c TIME_WAIT
UserParameter=CLOSE,netstat -ant | grep -c CLOSE
UserParameter=CLOSE_WAIT,netstat -ant | grep -c CLOSE_WAIT
UserParameter=LAST_ACK,netstat -ant | grep -c LAST_ACK
UserParameter=LISTEN,netstat -ant | grep -c LISTEN
UserParameter=CLOSING,netstat -ant | grep -c CLOSING
  • 创建模板--->应用集--->监控项--->图形,触发器(看情况创建)

首先创建一个tcp11应用集,在创建监控项当然选择在tcp11应用集内创建的,11种监控项要添加必须保证有此类配置文件的存在才可生效,创建好监控项可以根据实际应用情况来创建触发器图形丰富模板,以上操作步骤就是自定义模板的方法,当你想要将模板带到哪里都可以用的时候,可以导出你写好的模板下载到电脑上以xml的格式存在,到时要用导入即可

监控nginx服务

使用snmp监控

snmp硬件设备监控服务

oid:objct id(目标id)

一个oid对应一个指标,这个oid是唯一的

mib:所有oid信息

zabbix自动发现

应对批量新主机监控,通过自动发现设置ip搜索范围,范围越大搜索时间越长

动作--->事件源--->自动发现--->添加主机

自动发现检查zabbix客户端是否存在system.uname,如果存在在检查他是否是linux核实正确在自动填加

效率不高,增加zabbixserver压力,方便

zabbix自动注册

动作--->自动注册--->创建动作

vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.200.173 #注册的主机
ServerActive=192.168.200.173 #数据传给谁
Hostname=192.168.200.175 #被监控名字
HostMetadata=xxx #主机元数据

image-20210615181326619

image-20210615181337571

zabbix-agent主动和被动模式

主动模式:agent向server主动请求对应监控项列表,在本机收集对应的监控数据,提交给server/proxy

被动模式:agent打开一个端口默认为10050,等待server来获取数据,然后agent进行数据首席再发送到server(默认是出于被动模式)

建议主动模式,大大缓解了server的压力,但是主动模式,是不打开agent端的端口的。

配置主动模式
  1. 配置主动模板!

  2. agent配置选项

    • Server:允许谁来管我取值

    • ServerActive:我主动向谁汇报

    • Hostame:主机名,区分每一个agent节点

  3. 配置--->模板--->选中要克隆的模板--->全客隆--->监控项--->批量更新--->类型(主动模式)image-20210617152932713--->更新(主动模式模板创建完毕)

  4. 并不是全都改造成主动模式,留几个被动模式,让zabbix监控时绿标ZBX亮起来(想让ZBX绿只有被动模式才可以绿)

image-20210617164049230

 

posted @ 2021-06-24 09:02  苦练  阅读(102)  评论(0)    收藏  举报