前言

什么是监控,为什么需要监控

监控:监视,控制

随着用户的增多,服务随时可能会被系统oom out of memory 内存溢出 kill -9 mysql

你怎么判断,web服务是因为用户访问过多,达到了瓶颈?还是程序代码bug导致的,内存过多?
上线一个新网站: 压力测试 2000并发

常见的linux监控命令

总结:cpu,内存,硬盘,网络

使用shell脚本来监控服务器

内存:每隔1分钟监控一次内存,当你的可用内存低于100m,发邮件报警,要求显示剩余内存值

1
2
3
4
5
6
7
8
9
10
#!/bin/bash 
while true
do
Free=`free -m | awk 'NR==2{print $NF}'`
if [ $Free -lt 100 ]
then
echo $Free | mail -s "当前内存" xxx@163.com
fi
sleep 60
done

ab -n 10000 -c 3 <http://10.0.0.100/zabbix/index.php

zabbix的基础服务架构

image-20200715094741900

zabbix-agent c语言 —-> zabbix-server c语言 —-> 数据库 mysql <— zabbix-web lamp

zabbix生产环境安装(部署)

zabbix LTS 5年 zabbix 标准版 7个月

ip地址:10.0.0.61 硬件配置:1c1g 主机名:zabbix-server

配置zabbix yum仓库

1
wget http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
1
rpm -ivh zabbix-release-4.0-1.el7.noarch.rpm

epel

yum -y install epel-release

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root\@zabbix-server \~]# cat /etc/yum.repos.d/zabbix.repo [zabbix]

name=Zabbix Official Repository - \$basearch
baseurl=https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/
enabled=1

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX

[zabbix-non-supported]

name=Zabbix Official Repository non-supported - \$basearch
baseurl=https://mirror.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/7/\$basearch/
enabled=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX gpgcheck=1

安装zabbix服务端和zabbix-web前端

yum install zabbix-server-mysql zabbix-web-mysql -y

安装mariadb,创建zabbix库,授权zabbix用户

1
2
3
4
5
yum install mariadb-server -y
systemctl start mariadb
systemctl enable mariadb
mysql_secure_installation
一路y
1
2
3
4
>   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';
1
2
3
4
5
6
7
8
导入zabbix表结构和初始数据
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123456 zabbix

检查zabbix库是否导入成功
mysql -uroot zabbix -e 'show tables'

验证包
Rpm -qa | grep zabbix

配置启动zabbix-server

1
2
3
4
5
6
7
8
9
10
11
12
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

修改Zabbix前端的PHP配置,并启动httpd

1
2
3
4
5
6
7
vim /etc/httpd/conf.d/zabbix.conf 

php_value date.timezone Asia/Shanghai

systemctl start httpd

systemctl enable httpd

前端zabbix-web的安装

浏览器:http://10.0.0.61/zabbix

后期修改zabbix数据库密码的时候,需要修改的配置文件:

/etc/zabbix/web/zabbix.conf.php

http://10.0.0.61/zabbix/zabbix.php

登录的账号密码

Admin zabbix

监控一台服务器主机(集群)

安装zabbix-agent(同一台机器直接跳到c)

1
rpm -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

配置zabbix-agent

1
2
vim /etc/zabbix/zabbix_agentd.conf 
Server=10.0.0.61

启动zabbix-agent

1
systemctl start zabbix-agent

zabbix-web界面,添加主机

image-20200715095936299

主机名称:标识主机

群组分类:一种以业务划分(商城业务,论坛业务),一种以功能分组(数据库,web服务,缓存,存储软件)

Agent接口:填写主机地址 或 DNS名称(DNS要有做host解析,建议填IP地址),默认端口