普拉多VX

人生一路,不问来时,不知归期

0%

微信报警

微信报警我们要借助企业微信来实现,以前有个迪三方平台“方糖”也可以实现消息发送,有兴趣后面可以学习研究下。

注册地址:https://work.weixin.qq.com

创建企业微信

1.注册一个企业微信,填写相关信息

2.下载企业微信或者直接进入后台

3.进入管理后台

4.在通讯录中创建一个部门

5.可以手动添加一个成员

也可以分享二维码给其他用户,让其加入进来

6.在应用管理中-创建应用

可见范围可以选择人也可以选择部门,最好是选择部门

7.记录下Agentid和secret,以及企业号

查看企业号

报警脚本

python 报警脚本样例

头部需要声明python 否则zabbix执行时无法识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
# @Time : 2017/4/23 11:13
# @Author : roddy
# @Email : xblinux05@gmail.com
import sys
import requests
import json

class WeChatAlert(object):
def __init__(self,user,subject,content):
self.user=user
self.subject = subject
self.content = content
self.agent_id = "1000002" #agentid
self.corpid = "xxxxx" # 企业号
self.secret = "zN6xTHu-xxxxxxxxx" #密钥


def get_token(self):
token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={secret}'.format(
corpid=self.corpid,secret=self.secret)

token_data = requests.get(token_url).json()

try:
token = token_data["access_token"]
except:
print("token 获取失败")
sys.exit(1)
else:
return token

def send(self):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}'.format(self.get_token())
send_values = {
"touser": "LuoDi", # 企业号中的用户帐号
"msgtype": "text",
"toparty": 1, # 部门id,鼠标移动到部门上可查看,群发使用
"agentid": self.agent_id,
"text": {
"content": self.subject + "\n" + self.content
},
"safe": "0"
}

if int(sys.version_info[0]) == 3:
r = requests.post(send_url, data=json.dumps(send_values,ensure_ascii=False).encode("utf-8"))
else:
r = requests.post(send_url, data=json.dumps(send_values, ensure_ascii=False))
return r.content


if __name__ == "__main__":
wechat = WeChatAlert(str(sys.argv[1]),str(sys.argv[2]),str(sys.argv[3]))
print(wechat.send())
1
2
3
(july_3.5) luodi@roddydeMacBook-Pro:~/PycharmProjects/huobipool$ python wechatMessage.py  "" "测试主题" "报警了"
b'{"errcode":0,"errmsg":"ok","invaliduser":""}'
(july_3.5) luodi@roddydeMacBook-Pro:~/PycharmProjects/huobipool$

检查用户是否收到消息

配置zabbix

安装环境

1
2
3
4
5
6
7
8
9
[root@iZ2ze7k1pc9lk7pay5rcawZ alertscripts]# yum install python-pip  #安装python-pip

[root@iZ2ze7k1pc9lk7pay5rcawZ alertscripts]# pip install requests #安装requests
[root@iZ2ze7k1pc9lk7pay5rcawZ alertscripts]# python
Python 2.7.5 (default, Apr 2 2020, 13:16:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>

我们使用yum安装的zabbix,默认脚本路径可以在zabbix_server.conf中查看”AlertScriptsPath”.可以自定义路径

上传脚本文件到该目录下

1
2
3
4
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]# cd /usr/lib/zabbix/alertscripts/
[root@iZ2ze7k1pc9lk7pay5rcawZ alertscripts]# ls
wechatMessage.py
[root@iZ2ze7k1pc9lk7pay5rcawZ alertscripts]#

再次测试

(务必要在真实服务器上测试)

1
2
3
[root@iZ2ze7k1pc9lk7pay5rcawZ alertscripts]# python wechatMessage.py "" "jj" "22"
{"errcode":0,"errmsg":"ok","invaliduser":""}
[root@iZ2ze7k1pc9lk7pay5rcawZ alertscripts]#

添加报警媒介

注意脚本名为alertscripts中的文件名

1
2
3
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

选择模版

添加用户,并且选择报警媒介为wechat 收件人填写应用ID,我是直接在admin用户上添加的多报警方式。

测试

down掉一个服务或者主机来查看,或者自定义报警测试。

报警

恢复

注意

  • 脚本一定要放到服务器上测试
  • 添加收件人应为应用id
  • 选择部门id群发消息
  • 建议部门中设定报警级别,不同报警级别给不同的部门

邮件报警

邮件报警对于运维来说还是比较实用的,免费且方便配置,只是如果报警邮件过多可能会导致报警信息遗漏。这点需要运维知悉!

1.登录qq邮箱-账户-获取独立密码

2.添加报警媒介 QQemail

3.设置用户报警媒介并且指定消息接收人

4.启用trigger actions

新版本把actions的分类移动到了上面进行选择

测试报警结果

手动测试我们的报警规则

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@iZ2zecgq3cou36re3sxh4bZ ~]# zabbix_sender -s "webserver.roddypy.com" -z 47.93.184.140 -p 10051 -k "UsersCount" -o 601 -vv
zabbix_sender [23894]: DEBUG: answer [{"response":"success","info":"processed: 1; failed: 0; total: 1; seconds spent: 0.000029"}]
Response from "47.93.184.140:10051": "processed: 1; failed: 0; total: 1; seconds spent: 0.000029"
sent: 1; skipped: 0; total: 1
[root@iZ2zecgq3cou36re3sxh4bZ ~]# zabbix_sender -s "webserver.roddypy.com" -z 47.93.184.140 -p 10051 -k "UsersCount" -o 603 -vv
zabbix_sender [23899]: DEBUG: answer [{"response":"success","info":"processed: 1; failed: 0; total: 1; seconds spent: 0.000078"}]
Response from "47.93.184.140:10051": "processed: 1; failed: 0; total: 1; seconds spent: 0.000078"
sent: 1; skipped: 0; total: 1
[root@iZ2zecgq3cou36re3sxh4bZ ~]# zabbix_sender -s "webserver.roddypy.com" -z 47.93.184.140 -p 10051 -k "UsersCount" -o 604 -vv
zabbix_sender [23901]: DEBUG: answer [{"response":"success","info":"processed: 1; failed: 0; total: 1; seconds spent: 0.000087"}]
Response from "47.93.184.140:10051": "processed: 1; failed: 0; total: 1; seconds spent: 0.000087"
sent: 1; skipped: 0; total: 1
[root@iZ2zecgq3cou36re3sxh4bZ ~]#

仪表盘已经提示出问题

打开邮箱确认下报警信息

发现邮件为啥是html的文本格式呢,貌似我们在报警媒介那个地方选择过。现在调整成html看看。

修改后的结果

1
2
3
4
5
6
Problem started at 15:36:56 on 2020.09.22
Problem name: UsersCount
Host: webserver.roddypy.com
Severity: Average
Operational data: 608 个
Original problem ID: 74

修改报警模版

默认模版为以上英文信息,同时主题和内容我们详细信息不多。我们是否可以设置一个新的模版呢,答案可以。

在报警媒介-模版中可以设置-记着点更新

注意:如果以下信息作为模版请选择Message format为 ”文本”,如果以html格式请选择html模版

文本

报警通知模版

1
2
3
4
5
6
7
8
9
10
11
12
主题:
Problem: {EVENT.NAME}故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!
内容:

告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}

报警恢复模版

1
2
3
4
5
6
7
8
9
10
11
主题:
恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!
内容:
告警主机:{HOSTNAME1}<b>故障问题已恢复</b>
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}

html

报警通知模版

1
2
3
4
5
6
7
8
9
10
11
12
主题:
Problem: {EVENT.NAME}故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!
内容:

<b>告警主机:</b>{HOSTNAME1}<br>
<b>告警时间:</b>{EVENT.DATE} {EVENT.TIME}<br>
<b>告警等级:</b>{TRIGGER.SEVERITY}<br>
<b>告警信息: </b>{TRIGGER.NAME}<br>
<b>告警项目:</b>{TRIGGER.KEY1}<br>
<b>问题详情:</b>{ITEM.NAME}:{ITEM.VALUE}<br>
<b>当前状态:</b>{TRIGGER.STATUS}:{ITEM.VALUE1}<br>
<b>事件ID:</b>{EVENT.ID}<br>

报警恢复模版

1
2
3
4
5
6
7
8
9
10
11
12
13
主题:
恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!

内容:
<b>故障问题已恢复</b><br>
<b>告警主机:</b>{HOSTNAME1}<br>
<b>告警时间:</b>{EVENT.DATE} {EVENT.TIME}<br>
<b>告警等级:</b>{TRIGGER.SEVERITY}<br>
<b>告警信息:</b> {TRIGGER.NAME}<br>
<b>告警项目:</b>{TRIGGER.KEY1}<br>
<b>问题详情:</b>{ITEM.NAME}:{ITEM.VALUE}<br>
<b>当前状态:</b>{TRIGGER.STATUS}:{ITEM.VALUE1}<br>
<b>事件ID:</b>{EVENT.ID}<br>

结果:

故障日志排查

如果配置了报警仪表盘有提示,但是没有收到报警,这个时候就要去查看zabbix_server.log日志

最开始我就是配置端口错误

1
2
3
4
5
6
7
8
9
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]# tailf /var/log/zabbix/zabbix_server.log
28695:20200922:135040.315 housekeeper [deleted 0 hist/trends, 0 items/triggers, 0 events, 0 problems, 0 sessions, 0 alarms, 0 audit, 0 records in 0.002269 sec, idle for 1 hour(s)]
28695:20200922:145040.534 executing housekeeper
28695:20200922:145040.537 housekeeper [deleted 0 hist/trends, 0 items/triggers, 0 events, 0 problems, 0 sessions, 0 alarms, 0 audit, 0 records in 0.002225 sec, idle for 1 hour(s)]
28719:20200922:151620.874 watchdog: 1 recipient(s) found for database down messages
28722:20200922:151922.945 failed to send email: Couldn't connect to server: Failed connect to mail.qq.com:25; Operation now in progress
28720:20200922:152007.968 failed to send email: Couldn't connect to server: Failed connect to mail.qq.com:25; Operation now in progress
28721:20200922:152052.989 failed to send email: Couldn't connect to server: Failed connect to mail.qq.com:25; Operation now in progress
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]#

以上方式由平台提供的email配置,还有一种方法是通过py脚本来发邮件,两种方式都可以。哪个方便用哪个即可,目前邮件报警还是业内比较常用的报警方式之一

参考

触发器

所谓触发器就是根据监控项指标的返回值来判断属于什么状态。触发器用于给监控项定义具体的阈值,每一个触发器管理一个监控项,但是可以为一个监控项目定义多个触发器。定义的条件不同,就可以实现不同级别的报警。比如cpu使用率 达到40% 什么状态,70%是什么状态 90%又是什么状态。触发器由表达式构成,定义监控项的采集数据的一个阈值,根据触发器表达式的判断决定触发器状态。

设置触发器,可以关联模版,也可以单独对一个主机定义触发器

触发器状态

  • 未分类 (Not classified)
  • 信息(Infomation)
  • 警告(Warning)
  • 一般严重(Average)
  • 严重(High)
  • 灾难(Disaster)

表达式常见用法

官网参考:https://www.zabbix.com/documentation/current/manual/config/triggers/expression

1
2
3
{www.roddypy.com:system.cpu.load[all,avg1].last()}>5

表示:获取到roddypy.com服务器负载system.cpu.load[all,avg1] 最新数据,如果大于5就表示有问题

判断语法

1
2
3
4
5
6
7
8
9
10
last( )  获取到的最新信息(数值) 

max( ) 在一定周期内,收集到的最大值

min( ) 在一定周期内,收集到的最小值

diff() 在一定时间内,判断收集到的信息是否不同

change() 在一定时间内,判断收集到的信息是否不同
avg() 取一段时间的平均值

创建触发器样例

如果UsersCount最后3次获取的数据都>600进行触发

报警媒介

报警媒介就是触发报警后需要将信息通过短信、邮箱、微信、或者第三方应用告知管理员,这个时候短信这种方式就是报警媒介。监控系统最主要的功能就是两个:1.数据采集做监控 2.通过设定的规则进行报警通知

zabbix 5.0默认支持以下报警媒介

对于国内运维常用的报警媒介主要有以下几种

  • SMS 短信报警 需要短信付费
  • Email 邮件报警
  • wechat 微信报警
  • telegram 电报 等等
  • 集成第三方平台

zabbix_sender

zabbix_sender 是用于将监视数据发送到Zabbix服务器或代理的命令行实用程序。也就是说可以在客户端主动上报数据给zabbix服务器,前提需要配置具体的监控项(items)。

zabbix_sender安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]# yum install zabbix-sender -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* centos-sclo-rh: mirrors.huaweicloud.com
* centos-sclo-sclo: mirrors.bfsu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package zabbix-sender.x86_64 0:5.0.3-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================================================
Installing:
zabbix-sender x86_64 5.0.3-1.el7 zabbix 348 k

Transaction Summary
=====================================================================================================================================================================
Install 1 Package

Total download size: 348 k
Installed size: 1.5 M
Downloading packages:
zabbix-sender-5.0.3-1.el7.x86_64.rpm | 348 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : zabbix-sender-5.0.3-1.el7.x86_64 1/1
Verifying : zabbix-sender-5.0.3-1.el7.x86_64 1/1

Installed:
zabbix-sender.x86_64 0:5.0.3-1.el7

Complete!
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]#

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Example(s):
zabbix_sender -z 127.0.0.1 -s "Linux DB3" -k db.connections -o 43

zabbix_sender -z 127.0.0.1 -s "Linux DB3" -k db.connections -o 43 \
--tls-connect cert --tls-ca-file /home/zabbix/zabbix_ca_file \
--tls-server-cert-issuer \
"CN=Signing CA,OU=IT operations,O=Example Corp,DC=example,DC=com" \
--tls-server-cert-subject \
"CN=Zabbix proxy,OU=IT operations,O=Example Corp,DC=example,DC=com" \
--tls-cert-file /home/zabbix/zabbix_agentd.crt \
--tls-key-file /home/zabbix/zabbix_agentd.key

zabbix_sender -z 127.0.0.1 -s "Linux DB3" -k db.connections -o 43 \
--tls-connect psk --tls-psk-identity "PSK ID Zabbix agentd" \
--tls-psk-file /home/zabbix/zabbix_agentd.psk

常见参数说明,具体参考文档:https://www.zabbix.com/documentation/current/manpages/zabbix_sender?s[]=sender

1
2
3
4
5
6
7
8
-z 	指定zabbix server ip
-s 指定zabbix 客户端配置文件中的hostname
-k 指监控项对应的key值
-o 则表示要主动推送的数据
-r 将数据实时提交给服务器
-v 详细模式, -vv 更详细
-p zabbix server端口,默认10051
-i 从输入文件读取数据

参考实例

上报游戏客户端连接数量给zabbix服务端

1.创建一个模版

2.创建监控项

3.关联模版

4.发送测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@iZ2zecgq3cou36re3sxh4bZ ~]# zabbix_sender -s "webserver.roddypy.com" -z 47.93.184.140 -p 10051 -k "UsersCount" -o 1 -vv
zabbix_sender [21938]: DEBUG: answer [{"response":"success","info":"processed: 0; failed: 1; total: 1; seconds spent: 0.000071"}]
Response from "47.93.184.140:10051": "processed: 0; failed: 1; total: 1; seconds spent: 0.000071"
sent: 1; skipped: 0; total: 1

[root@iZ2zecgq3cou36re3sxh4bZ ~]# zabbix_sender -s "webserver.roddypy.com" -z 47.93.184.140 -p 10051 -k "UsersCount" -o 1 -vv
zabbix_sender [21940]: DEBUG: answer [{"response":"success","info":"processed: 1; failed: 0; total: 1; seconds spent: 0.000087"}]
Response from "47.93.184.140:10051": "processed: 1; failed: 0; total: 1; seconds spent: 0.000087"
sent: 1; skipped: 0; total: 1

[root@iZ2zecgq3cou36re3sxh4bZ ~]# zabbix_sender -s "webserver.roddypy.com" -z 47.93.184.140 -p 10051 -k "UsersCount" -o 500 -vv
zabbix_sender [21946]: DEBUG: answer [{"response":"success","info":"processed: 1; failed: 0; total: 1; seconds spent: 0.000092"}]
Response from "47.93.184.140:10051": "processed: 1; failed: 0; total: 1; seconds spent: 0.000092"
sent: 1; skipped: 0; total: 1

[root@iZ2zecgq3cou36re3sxh4bZ ~]# zabbix_sender -s "webserver.roddypy.com" -z 47.93.184.140 -p 10051 -k "UsersCount" -o 501 -vv
zabbix_sender [21951]: DEBUG: answer [{"response":"success","info":"processed: 1; failed: 0; total: 1; seconds spent: 0.000029"}]
Response from "47.93.184.140:10051": "processed: 1; failed: 0; total: 1; seconds spent: 0.000029"
sent: 1; skipped: 0; total: 1

[root@iZ2zecgq3cou36re3sxh4bZ ~]# zabbix_sender -s "webserver.roddypy.com" -z 47.93.184.140 -p 10051 -k "UsersCount" -o 505 -vv
zabbix_sender [21953]: DEBUG: answer [{"response":"success","info":"processed: 1; failed: 0; total: 1; seconds spent: 0.000091"}]
Response from "47.93.184.140:10051": "processed: 1; failed: 0; total: 1; seconds spent: 0.000091"
sent: 1; skipped: 0; total: 1
[root@iZ2zecgq3cou36re3sxh4bZ ~]#

4.通过最新数据获取结果

说明

  • 此时可以把命令写成脚本文件定时执行
  • 可以添加一个触发器进行报警

zabbix_get

zabbix_get 是用于从Zabbix代理获取数据的命令行实用程序。 就是从服务端获取客户端的数据,主要用于测试两边连通行和监控项是否正确。

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]# yum install zabbix-get -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* centos-sclo-rh: mirror.bit.edu.cn
* centos-sclo-sclo: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package zabbix-get.x86_64 0:5.0.3-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================================
Package Arch Version Repository Size
=====================================================================================================================================================================
Installing:
zabbix-get x86_64 5.0.3-1.el7 zabbix 315 k

Transaction Summary
=====================================================================================================================================================================
Install 1 Package

Total download size: 315 k
Installed size: 1.3 M
Downloading packages:
zabbix-get-5.0.3-1.el7.x86_64.rpm | 315 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : zabbix-get-5.0.3-1.el7.x86_64 1/1
Verifying : zabbix-get-5.0.3-1.el7.x86_64 1/1

Installed:
zabbix-get.x86_64 0:5.0.3-1.el7

Complete!
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]#

用法

1
2
Example(s):
zabbix_get -s 127.0.0.1 -p 10050 -k "system.cpu.load[all,avg1]"
1
2
3
4
5
6
7
General options:
-s --host host-name-or-IP 指定客户端主机名或者IP
-p --port port-number 客户端端口,默认10050
-I --source-address IP-address 指定源IP,写上zabbix server的ip地址即可,一般留空
-k --key item-key 你想获取的key
-h --help 打印帮助信息
-V --version 显示版本

官方文档:https://www.zabbix.com/documentation/current/manpages/zabbix_get

参考实例

1.获取客户端CPU负载

1
2
3
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]# zabbix_get -s 59.110.115.72 -p 10050 -k "system.cpu.load[all,avg5]"
0.020000
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]#

2.创建一个自定义监控,并通过zabbix_get获取数据

在客户端的zabbix_agentd.conf中增加自定义监控项

1
2
3
4
5
6
7
8
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
UserParameter=Echo[*],echo $1

重启客户端

1
2
[root@iZ2zecgq3cou36re3sxh4bZ ~]# systemctl  restart zabbix-agent 
[root@iZ2zecgq3cou36re3sxh4bZ ~]#

在服务端测试,输入什么参数就会回显什么

1
2
3
4
5
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]# zabbix_get -s 59.110.115.72 -p 10050 -k "Echo[c]"                  
c
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]# zabbix_get -s 59.110.115.72 -p 10050 -k "Echo[ewewe]"
ewewe
[root@iZ2ze7k1pc9lk7pay5rcawZ ~]#

环境

CentOS Linux release 7.8.2003 (Core)

安装agent

采用rpm包安装方法

1
2
3
4
5
6
7
8
9
10
11
[root@iZ2zecgq3cou36re3sxh4bZ ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
Retrieving https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
warning: /var/tmp/rpm-tmp.GUTI2l: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-release-5.0-1.el7 ################################# [100%]
[root@iZ2zecgq3cou36re3sxh4bZ ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates zabbix zabbix-non-supported
Cleaning up list of fastest mirrors
[root@iZ2zecgq3cou36re3sxh4bZ ~]# yum install zabbix-agent -y

安装完成查看下软件包生成的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@iZ2zecgq3cou36re3sxh4bZ ~]# rpm -ql zabbix-agent
/etc/logrotate.d/zabbix-agent
/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.d
/usr/lib/systemd/system/zabbix-agent.service
/usr/lib/tmpfiles.d/zabbix-agent.conf
/usr/sbin/zabbix_agentd
/usr/share/doc/zabbix-agent-5.0.3
/usr/share/doc/zabbix-agent-5.0.3/AUTHORS
/usr/share/doc/zabbix-agent-5.0.3/COPYING
/usr/share/doc/zabbix-agent-5.0.3/ChangeLog
/usr/share/doc/zabbix-agent-5.0.3/NEWS
/usr/share/doc/zabbix-agent-5.0.3/README
/usr/share/doc/zabbix-agent-5.0.3/userparameter_mysql.conf
/usr/share/man/man8/zabbix_agentd.8.gz
/var/log/zabbix
/var/run/zabbix

修改配置文件

/etc/zabbix/zabbix_agentd.conf

1
2
3
4
5
6
7
PidFile=/var/run/zabbix/zabbix_agentd.pid   #pid文件
LogFile=/var/log/zabbix/zabbix_agentd.log。 #log文件
LogFileSize=0
Server=x.x.x.x #zabbix server ip 地址
ServerActive=x.x.x.x #主动监控 server ip 地址
Hostname=webserver.roddypy.com #自己能被server端识别的地址
Include=/etc/zabbix/zabbix_agentd.d/*.conf # 扩展配置文件

启动agentd

启动agent并开启自启

1
2
3
4
[root@iZ2zecgq3cou36re3sxh4bZ ~]# systemctl  start zabbix-agent             
[root@iZ2zecgq3cou36re3sxh4bZ ~]# systemctl enable zabbix-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@iZ2zecgq3cou36re3sxh4bZ ~]#

添加主机并关联模版

等待1-5分钟即可正常监控

如果出现如下状态需要检查配置或者防火墙,确认是否是ip地址填写错误还是防火墙没有允许10050端口

备注

  • 客户端允许服务器来访问10050端口
  • ip切记不要填写错误
  • 需要选择关联组及监控模版,模版里有默认的监控指标及报警规则,这个可以自己修改。

说明

系统环境是Ubuntu 16.4,采用apt源的安装方式。安装时软件版本最好和Server的版本相匹配,同时如果是大批量的安装的话建议采用saltstack 或者批量脚本安装并设置自动发现。

需要开放10050 和10051端口访问,在客户端上需要允许服务器访问 10050

安装Agent

添加agent源

1
2
3
[root@localhost ~]# wget https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+xenial_all.deb
[root@localhost ~]# dpkg -i zabbix-release_5.0-1+xenial_all.deb
[root@localhost ~]# apt update

安装agent

1
[root@localhost ~]# apt install zabbix-agent -y

配置文件

1
2
3
4
5
6
7
8
[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=47.93.184.xx。 #监控机地址
ServerActive=47.93.184.xx # 填监控机地址 主动模式
Hostname=47.105.47.xx # 被监控端hostname
Include=/etc/zabbix/zabbix_agentd.d/*.conf

设置开机启动

1
2
[root@localhost ~]#  systemctl restart zabbix-agent
[root@localhost ~]# systemctl enable zabbix-agent

手动添加被监控机

1.在配置-主机添加一台被监控机

2.添加时指定群组,并且设置一个监控模版Template OS Linux by Zabbix agent

3.添加后等几分钟查看状态

参考资料

Hexo关闭文章评论

这里不单单可以对某篇文章关闭,也可以关闭某个页面。比如分类、标签、关于等。升级next主题后,我加了valine评论系统,默认情况下所有页面都可以评论,但是有些页面不想开评论咋办呢。

经我亲测以下方法可行,目前我的环境是hexo+next+valine评论插件

方法

以分类页面为例source/categories/index.md

添加:comments: false

1
2
3
4
5
6
7
---
title: categories
date: 2020-04-22 15:40:45
type: "categories"
layout: "categories"
comments: false
---

参考

zabbix

老牌监控系统,自我运维生涯开始用过nagios、icinga、cacti(仙人掌)、zabbix。唯有zabbix在中小规模监控环境中值得推荐,无论是安装,配置,还是报警、绘图。结合第三方报警也好,适配性都特别好。
目前还可以集合grafana作图形可视化,中小型企业运维监控软件首选zabbix。如果有能力二次开发或者有兴趣也可以研究下小米开源的open-falcon、滴滴改进的Nightingale、Talkingdata开源的OWL等

5.0版本貌似最近才更新没多久,我们部署起来看看有啥变化没,话说回来。从2.0到3.0确实好看了不少,今天看看5.0长啥样,话不多说,开干!

5.0新功能参考 https://www.zabbix.com/cn/whats_new_5_0

准备环境

准备环境

  • Centos 7.8
  • PHP 7.2 +
  • Apache 1.3.12 +
  • Mysql or MariaDB 5.7+

具体要求参考一下链接 https://www.zabbix.com/documentation/current/manual/installation/requirements

硬件条件参考下表

安装

关闭selinux

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]#

安装zabbix源

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
Retrieving https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
warning: /var/tmp/rpm-tmp.vw5pXe: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-release-5.0-1.el7 ################################# [100%]
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates zabbix zabbix-non-supported
Cleaning up list of fastest mirrors
[root@localhost ~]#

安装server和agnet

1
[root@localhost ~]# yum -y install zabbix-server-mysql zabbix-agent

因某些不能说的原因安装可能会出现如下情况

1
2
3
4
5
6
http://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-server-mysql-5.0.3-1.el7.x86_64.rpm: [Errno 14] curl#18 - "transfer closed with 1461988 bytes remaining to read"
Trying other mirror.


Error downloading packages:
zabbix-server-mysql-5.0.3-1.el7.x86_64: [Errno 256] No more mirrors to try.

换成阿里源

1
2
3
4
5
[root@localhost ~]# sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base epel extras updates zabbix zabbix-non-supported
Cleaning up list of fastest mirrors

再次执行安装

安装前端组建

1
[root@localhost ~]# yum install centos-release-scl -y

启用zabbix前端源,编辑配置文件 /etc/yum.repos.d/zabbix.repo 修改enabled为1

1
2
3
4
5
6
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=https://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

软件包安装,包括httpd php web组件等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@localhost ~]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y 
...
Installing:
zabbix-apache-conf-scl noarch 5.0.3-1.el7 zabbix-frontend 13 k
zabbix-web-mysql-scl noarch 5.0.3-1.el7 zabbix-frontend 12 k
Installing for dependencies:
apr x86_64 1.4.8-5.el7 base 103 k
apr-util x86_64 1.5.2-6.el7 base 92 k
audit-libs-python x86_64 2.8.5-4.el7 base 76 k
checkpolicy x86_64 2.5-8.el7 base 295 k
dejavu-fonts-common noarch 2.33-6.el7 base 64 k
dejavu-sans-fonts noarch 2.33-6.el7 base 1.4 M
fontpackages-filesystem noarch 1.44-8.el7 base 9.9 k
httpd x86_64 2.4.6-93.el7.centos base 2.7 M
httpd-tools x86_64 2.4.6-93.el7.centos base 92 k
libX11 x86_64 1.6.7-2.el7 base 607 k
libX11-common noarch 1.6.7-2.el7 base 164 k
libXau x86_64 1.0.8-2.1.el7 base 29 k
libXpm x86_64 3.5.12-1.el7 base 55 k
libcgroup x86_64 0.41-21.el7 base 66 k
libjpeg-turbo x86_64 1.2.90-8.el7 base 135 k
libsemanage-python x86_64 2.5-14.el7 base 113 k
libwebp x86_64 0.3.0-7.el7 base 170 k
libxcb x86_64 1.13-1.el7 base 214 k
libxslt x86_64 1.1.28-5.el7 base 242 k
mailcap noarch 2.1.41-2.el7 base 31 k
policycoreutils-python x86_64 2.5-34.el7 base 457 k
python-IPy noarch 0.75-6.el7 base 32 k
rh-php72 x86_64 1-2.el7 centos-sclo-rh 4.3 k
rh-php72-php-bcmath x86_64 7.2.24-1.el7 centos-sclo-rh 60 k
rh-php72-php-cli x86_64 7.2.24-1.el7 centos-sclo-rh 2.9 M
rh-php72-php-common x86_64 7.2.24-1.el7 centos-sclo-rh 680 k
rh-php72-php-fpm x86_64 7.2.24-1.el7 centos-sclo-rh 1.5 M
rh-php72-php-gd x86_64 7.2.24-1.el7 centos-sclo-rh 151 k
rh-php72-php-json x86_64 7.2.24-1.el7 centos-sclo-rh 54 k
rh-php72-php-ldap x86_64 7.2.24-1.el7 centos-sclo-rh 59 k
rh-php72-php-mbstring x86_64 7.2.24-1.el7 centos-sclo-rh 553 k
rh-php72-php-mysqlnd x86_64 7.2.24-1.el7 centos-sclo-rh 166 k
rh-php72-php-pdo x86_64 7.2.24-1.el7 centos-sclo-rh 101 k
rh-php72-php-pear noarch 1:1.10.5-1.el7 centos-sclo-rh 357 k
rh-php72-php-process x86_64 7.2.24-1.el7 centos-sclo-rh 62 k
rh-php72-php-xml x86_64 7.2.24-1.el7 centos-sclo-rh 160 k
rh-php72-php-zip x86_64 7.2.24-1.el7 centos-sclo-rh 90 k
rh-php72-runtime x86_64 1-2.el7 centos-sclo-rh 1.1 M
scl-utils x86_64 20130529-19.el7 base 24 k
setools-libs x86_64 3.3.8-4.el7 base 620 k
zabbix-web noarch 5.0.3-1.el7 zabbix-frontend 3.1 M
zabbix-web-deps-scl noarch 5.0.3-1.el7 zabbix-frontend 13 k

安装数据库

安装并设置开机启动

1
2
[root@localhost ~]# yum install mariadb-server -y
[root@localhost ~]# systemctl enable --now mariadb

初始化数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@localhost ~]# 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] y。#是否设置密码
New password: #新密码
Re-enter new password: #新密码确认
Password updated successfully!
Reloading privilege tables..
... Success!


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] #移除匿名用户
... skipping.

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] n #使用禁用root远程连接
... skipping.

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!

创建zabbix所需要的数据库及用户并设置密码 zabbix123

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
MariaDB [(none)]>
MariaDB [(none)]>
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user zabbix@localhost identified by 'zabbix123';
Query OK, 0 rows affected (0.00 sec)

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

MariaDB [(none)]> quit
Bye

导入初始数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
Enter password:
[root@localhost ~]# mysql -u root -p #检查是否导入成功
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.65-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 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
MariaDB [zabbix]> show tables;
+----------------------------+
| Tables_in_zabbix |
+----------------------------+
| acknowledges |
| actions |
| alerts |
| application_discovery |
| application_prototype |
| application_template |
| applications |
| auditlog |
.......

修改配置文件

配置数据库密码 /etc/zabbix/zabbix_server.conf

1
DBPassword=zabbix123

修改php时区 /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf,修改成Asia/Shanghai 并且去掉前面的注释

1
php_value[date.timezone] = Asia/Shanghai

设置开机启动

1
2
[root@localhost ~]# systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm
[root@localhost ~]# systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm

初始化

登录系统http://x.x.x.x/zabbix

开始初始化

检查环境

配置数据库

确认server详情

安装确认

安装完成

登录系统账户密码 Admin ,默认密码zabbix

系统页面,默认是英文,可以在个人中心切换语言

zabbix 乱码问题

将Win10操作系统C:\Windows\Fonts目录下的任意字体 上传字体文件到fonts目录下

1
2
3
4
5
6
7
[root@localhost fonts]# pwd
/usr/share/zabbix/assets/fonts
[root@localhost fonts]# ll
total 11512
lrwxrwxrwx 1 root root 33 Sep 18 16:34 graphfont.ttf -> /etc/alternatives/zabbix-web-font
-r-xr-xr-x 1 root root 11787328 Sep 18 17:05 simkai.ttf
[root@localhost fonts]#

编辑配置文件 /usr/share/zabbix/include/defines.inc.php

1
2
3
4
5
6
7
 81 define('ZBX_GRAPH_FONT_NAME',           'simkai'); // font file name
122 define('ZBX_FONT_NAME', 'simkai');
```

重启
```bash
[root@localhost fonts]# systemctl restart zabbix-server

参考资料

两者区别

":x"":wq" 的区别如下:

1
2
(1) :wq 强制性写入文件并退出(存盘并退出 write and quite)。即使文件没有被修改也强制写入,并更新文件的修改时间。
(2) :x 写入文件并退出。仅当文件被修改时才写入,并更新文件修改时间;否则不会更新文件修改时间。

尽量习惯性用:x 避免在没有修改时也更新文件

参考:

查看Selinux状态

1
2
3
4
5
[root@localhost ~]# getenforce
Enforcing //enforceing代表开启,
Permissive // permissive代表警告
Disabled //disabled代表关闭
[root@localhost ~]#

关闭selinux(临时关闭重启失效)

1
[root@localhost ~]# setenforce 0   //开始 设置数字1

关闭selinux(永久)

1
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux