添加一台被监控服务器流程
本次测试被监控主机ip:10.55.2.83,OS:RHEL 6.5
a、配置zabbix yum源并安装
#yum install zabbix-agent zabbix-sender
b、修改配置,agent分Passiv checks(等待zabbix主机来抓取数据)和Active checks(主动向zabbix主机提供数据)两种,
本次测试先使用Passive checks模式。
/etc/zabbix/zabbix_agentd.conf
*Server=10.55.2.81 以逗号分隔授权给哪些zabbix-server或zabbix-proxy过来采集数据的服务器地址; ListenPort=10050 ListenIP=0.0.0.0 StartAgents=3
注意点:在配置active模式时Hostname必须填写
##### Active checks related(主动检测相关的配置:agent主动向server发送监控数据)
*ServerActive=IP[:Port]
以逗号分隔的、当前agent主动发送监控数据过去的server端;
*Hostname=HOSTNAME
c、启动zabbix-agent,并设置开机自启动,agent端启动的默认端口为10050
~]# chkconfig zabbix-agent on
~]# chkconfig --list zabbix-agent
zabbix-agent 0:off 1:off 2:on 3:on 4:on 5:on 6:off
~]# service zabbix-agent start
Starting Zabbix agent: [ OK ]
ss -tnl 10050端口
LISTEN 0 128 :::10050 :::*
LISTEN 0 128 *:10050 *:*
一次完整的简单监控配置:
host group-->host-->[applications]-->item-->trigger(Events)-->(Media Type,User Group,User)-->action(conditions,operations(send message,remote script))
定义hostgroup
本次测试我使用zabbix内置的Linux servers
定义hosts
定义监控项application
定义items
item中设置,type选择zabbix agent选项
此时便可以在Monitoring中的Latest data中看到最新采样的数据
添加trigger
触发器 trigger events:
一个监控项可以多个trigger;但一个trigger只能关联一个监控项
触发器的等级:
触发器表达式:
{<server>:<key>.<fuction>(parameter)}<operator><constant>
<constant>:阈值;
diff():比较采样值是否事先值的相同;通常通过checksum进行;
min():指定区间或时间范围内的最小值;
max()
avg()
nodata()
trigger状态
定义媒介
Administration---->Media types
添加media用户组合用户
注意点:使用zabbix自带的邮件功能,收件人中的邮箱地址只能填写一个
基于trigger添加action
action为zabbix的全局配置,所以在hosts中没有
这里我定义前3个10分钟发送给ops用户
报警升级设定,第4-6个10分钟发送给boss用户
当一台服务器出现告警时,首先会做的是自行先处理(remote command),如果自行处理失败后会进行告警升级,进行send message
本次测试一台httpd的服务器,监控其80端口,如果down了后自动恢复启动
服务器地址:10.55.10.79
添加监控项,监控其80端口
手动关闭服务进行event测试:——--->ok
[root@itblog yum.repos.d]# systemctl stop httpd.service
[root@itblog yum.repos.d]# systemctl start httpd.service
在agent端需要做的配置
tty问题说明:
usermod zabbix -s /bin/bash
(1)zabbix用户有所需要管理权限(基于sudo授权);
编辑/etc/sudoers(visudo)
1)Defaults requiretty,修改为#Defaults requiretty,表示不需要控制终端。
2)Defaults requiretty,修改为Default:nobody !requiretty,表示仅nobody用户不需要控制终端。
(2)agent进程要允许执行远程命令;
EnableRemoteCommands=1
添加remote command脚本,首先要添加zabbix的sudo权限
~]# visudo
添加
zabbix ALL=(ALL) NOPASSWD: ALL
zabbix可在此主机上运行所有操作并无需密码
注释 centos7会有此类要求,不然会报tty的错误
#Defaults requiretty
编辑zabbix配置文件,开启remote command功能
~]# vim /etc/zabbix/zabbix_agentd.conf
EnableRemoteCommands=1
LogRemoteCommands=1 #记录日志
~]# systemctl restart zabbix-agent.service
添加action
当httpd服务无法自动修复时,进行告警升级,发送邮件
测试结果
手动关闭httpd
[root@itblog ~]# systemctl stop httpd.service
[root@itblog ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 :::10050 :::*
此时会激活action:remote command,自动开启服务
[root@itblog ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 :::10050 :::*
send message:
Media Type:传递消息的通道;
script:用来定义信息通道。完成信息传递的脚本;
(1)脚本放置路径:/etc/zabbix/zabbix_server.conf
AlertScriptPath=/usr/lib/zabbix/alertscripts
(2)zabbix会向脚本传递三个参数:
$1:经由此信道发送的信息的目标;
$2:信息的subject;
$3:信息的body;
Users:利用指定信道接收消息;
User Groups:用户的逻辑容器;
编写测试script
这里我改了一个python的发邮件脚本,send_mail函数接受上面所述的三个参数
python版本RHEL 6.5自带的2.6.6
[root@wuzabbix ~]# vim /usr/lib/zabbix/alertscripts/altermail.py
#!/usr/bin/python
# -*- coding:utf-8 -*-
import smtplib
import sys
from email.mime.text import MIMEText
import time
#reload(sys)
#sys.setdefaultencoding('utf8')
current_time=time.strftime('%Y-%m-%d%H:%M',time.localtime(time.time()))
mail_host ='10.55.1.13'
mail_user ='wuzabbix@*******'
#mail_pwd = '******'
def send_email( content,mailto, get_sub ):
#msg = MIMEText( content.encode('utf8'),_subtype = 'html', _charset = 'utf8')
msg = MIMEText(content,_subtype='plain',_charset='gb2312')
msg['From'] = mail_user
msg['Subject'] =get_sub
msg['To'] = ",".join( mailto )
try:
s = smtplib.SMTP()
s.connect(mail_host,25)
s.sendmail(mail_user,mailto,msg.as_string())
s.close()
except Exception as e:
print 'Exception: ', e
title=sys.argv[2]
cont="""
---------------------------------
Abstract: %s
---------------------------------
Date: %s
---------------------------------
"""%(sys.argv[3],current_time)
to_list = [
'%s'%(sys.argv[1]),
]
with open('/tmp/sendmail_qs.log','ab') as f:
f.write('%s Receive address: %s Title: %s \n'%(current_time,sys.argv[1],title))
send_email(cont,to_list,title)
添加权限并在本机进行发送测试:
chmod +x altermail.py
[root@wuzabbix alertscripts]# python altermail.py yaominghe@******* test test
邮件接受OK
自定义media type
编辑我刚才定义的webservice的action
修改刚才定义的ops users,将users中定义的媒介也修改
关闭httpd进行测试
[root@itblog ~]# systemctl stop httpd.service
[root@itblog ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 127.0.0.1:199 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 :::10050 :::*
基于key+parameter的item添加
相关使用方法可以查看官方文档中的documentation
https://www.zabbix.com/documentation/3.0/manual/config/items/itemtypes/zabbix_agent
测试定义一个网卡的入站流量
store value:
As is:数据不做任何处理;
Delta(Simple change):本次采样数据减去前一次采样数据;
Delta(speed per second):本次采样数据减去前一次采样数据,而后除以采样间隔时长;
此时数据已经过来
UserParameter
在10.55.2.83服务器上继续添加多个item,监控网络流量
在主机设定中选中graphs进行配置
add进行添加,在monitorring就可以进行查看了
把多个graph整合于同一个屏幕进行展示
把多个screen以slide show的方式进行展示
应用模板测试
[root@wulmail zabbix]# vim zabbix_agentd.conf
Server=10.55.2.81
EnableRemoteCommands=1
LogRemoteCommands=1
ServerActive=127.0.0.1,10.55.2.81
Hostname=wulmail #开启主动监控时必须指定hostname
[root@wulmail ~]# service zabbix-agent start
Starting Zabbix agent: [确定]
[root@wulmail ~]# chkconfig zabbix-agent on
[root@wulmail ~]# chkconfig --list | grep zabbix-agent
zabbix-agent 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
由于套用了模板,所有点击add后很多之前需手动添加的东西均自动完成
模板可以使用系统自带的,也可以根据自己需求自己定义。
两种:
内置
自定义{$MACRO}
命名方式:大写字母,数字和下划线;
级别:
全局
模板
主机
主机-->模板-->全局
全局宏
自定义一个{HTTP_PORT}全局宏进行测试
模板宏
主机宏
主机定义一个宏进行测试
位置:在zabbix agent端实现;
zabbix_agentd.conf
UserParameter
语法格式:
UserParameter=<key>,<command>
使用示例:
监控内存信息
UserParameter=memory.usage[*],cat /proc/meminfo | awk '/^$1:/{print $$2}'
监控nginx状态页面
UserParameter=ngx.active[*],curl -s http://$1:$2/$3 | awk '/^Active/{print $$3}'
UserParameter=ngx.accepts[*],curl -s http://$1:$2/$3 | awk '/^[[:space:]]
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。