什么是监控?
- 监控:安防的监控 看监控,事后追责
- linux监控: 事前预警,数据分析
常见的linux监控命令
1.cpu
top
htop
uptime
vmstat
2.内存
free
3.磁盘
df
iotop
4.网卡
iftop
nethogs
5.系统进程
ps
pstree
6.系统日志 ELK
使用shell脚本来监控服务器
shell+crontab适合服务器数量少的场景
#!/bin/bash
avai_mem=`free -m|awk '/^Mem/{print $NF}'`
if [ $avai_mem -lt 100 ];then
echo "可用内存过低,当前内存是:$avai_mem" >>/tmp/monitor_mem.log
echo "可用内存过低,当前内存是:$avai_mem"|mail -s "服务可用内存低" 1751447508@qq.com
fi
定时任务: * * * * * cmd
zabbix的基础服务架构
zabbix生产环境安装
#安装zabbix前提,关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
#IP 10.0.0.71
1:配置zabbix的yum源
#下载zabbix-release
wget http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
#安装zabbix-release
rpm -ivh zabbix-release-4.0-1.el7.noarch.rpm
#将官方仓库地址替换为清华仓库地址
sed -i 's#http://repo.zabbix.com#https://mirror.tuna.tsinghua.edu.cn/zabbix#g' /etc/yum.repos.d/zabbix.repo
2:安装zabbix服务端和zabbix-web前端
yum install zabbix-server-mysql zabbix-web-mysql -y
3:安装mariadb,创建zabbix库,授权zabbix用户
yum install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
先回车
再n
然后一路y
mysql
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by '123456';
导入zabbix表结构和初始数据
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123456 zabbix
检查zabbix库是否导入成功
mysql -uroot zabbix -e 'show tables'
4:配置启动zabbix-server
vi /etc/zabbix/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456
启动zabbix-server
systemctl start zabbix-server
systemctl enable zabbix-server
检查:
netstat -lntup|grep 10051
5:修改Zabbix前端的PHP配置,并启动httpd
vi /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
systemctl start httpd
systemctl enable httpd
6:前端zabbix-web的安装
浏览器:http://10.0.0.71/zabbix
后期修改zabbix数据库密码的时候,需要修改的配置文件:
/etc/zabbix/web/zabbix.conf.php
http://10.0.0.71/zabbix/zabbix.php
登录的账号密码;
Admin
zabbix
监控一台服务器主机
监控zabbix-server自己
yum install zabbix-agent.x86_64 -y
systemctl enable zabbix-agent.service
systemctl start zabbix-agent.service
使用zabbix-agent监控其他主机(10.0.0.8)
wget https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.16-1.el7.x86_64.rpm
rpm -ivh zabbix-agent-4.0.16-1.el7.x86_64.rpm
vim /etc/zabbix/zabbix_agentd.conf
Server=10.0.0.71
systemctl enable zabbix-agent.service
systemctl start zabbix-agent.service
自定义监控项
命令行取值
#没有iostat
yum install sysstat -y
iostat|awk ‘/^sda/{print $3}’
修改zabbix-agent配置文件
vim /etc/zabbix/zabbix_agentd.conf
UserParameter=disk_read_speed,iostat|awk ‘/^sda/{print $3}’
systemctl restart zabbix-agent.service
在web界面上添加监控项
监控项
自定义触发器
触发器的作用,实现报警
触发器的方法:
diff() 比较两次取到的值
last() 最新值
avg(5m) 平均5分钟,改值都大于>1
max(5m) 最大值
min(5m) 最小值
nodate(5m) 没有收到数据报警
邮件报警
配置发件人
QQ邮箱授权码
配置收件人
启动发送消息动作
微信报警
#上传weixin.py的脚本
python weixin.py
yum install python-pip -y
pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple
chmod +x weixin.py
mv weixin.py /usr/lib/zabbix/alertscripts
weixin.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#author: bgx
#date: 2018
#comment: zabbix接入微信报警脚本
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='wxd074861951c67ba6'
appsecret='QtraZrI936DZ0jZ3aSWTZ-ydrLa1bBXqhurzrQyxve0'
agentid=1
#获取accesstoken
ken_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
▽eq=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)
[root@zabbix-server ~]# vim weixin.py.bck
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#author: bgx
#date: 2018
#comment: zabbix接入微信报警脚本
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='wxd074861951c67ba6'
#获取accesstoken
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)
配置报警媒介
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
配置收件人
启动警报接受信息
问题: 登录企业微信-->我的企业(企业id对应脚本corpid)
-->应用管理-->创建应用 (对应修改脚本 AgentId Secret )
我的企业-->微工作台-->微信扫码关注接收信息
自定义故障告警内容
服务器:{HOST.NAME}发生: {TRIGGER.NAME}故障!
{
告警主机:{HOST.NAME}
告警地址:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID}
}
短信报警 建议 https://blog.csdn.net/guyan0319/article/details/78739451
http://www.zhangqiongjie.com/1432.html
钉钉报警 建议 https://blog.csdn.net/fengwuxichen/article/details/9040648
https://www.jianshu.com/p/d0910126e909