监控: 监视, 控制
随着用户的增多, 服务器随时可能会被OOM(out of memory)
当系统的内存不足的时候, 会触发OOM
当swap分区被大量占用, 系统会特别卡
$ free -m
total used free shared buff/cache available
Mem: 972 495 67 7 409 316
Swap: 0 0 0
$ df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 4.0K 487M 1% /dev/shm
tmpfs 487M 7.8M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 2.9G 15G 17% /
/dev/sda1 1014M 193M 822M 19% /boot
tmpfs 98M 0 98M 0% /run/user/0
%Cpu(s): 43.6 us, 43.6 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 12.9 si, 0.0 st
us: 用户态, 跟用户的操作有关
sy: 内核态, 跟内核的处理有关
$ time df -hP
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 4.0K 487M 1% /dev/shm
tmpfs 487M 7.8M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 3.4G 14G 20% /
/dev/sda1 1014M 193M 822M 19% /boot
tmpfs 98M 0 98M 0% /run/user/0
real 0m0.098s
user 0m0.001s >>>> 用户态进程CPU处理的事件
sys 0m0.004s >>>> 内核态进程CPU处理的事件
$ htop
$ uptime
00:15:57 up 2 days, 2:03, 2 users, load average: 0.10, 0.05, 0.05
cat /proc/uptime
180350.18 176528.13 >>>> 系统启动时间 系统空闲时间
$ echo 180350.18/3600|bc
50
说明 一般来说, 内存中的系统启动时间/proc/uptime
和uptime
获取的系统的启动时间的差值越小, 表示系统越闲
$ iftop
$ yum -y install sysstat -y
$ rpm -ql sysstat |grep bin
/usr/bin/cifsiostat
/usr/bin/iostat
/usr/bin/mpstat
/usr/bin/nfsiostat-sysstat
/usr/bin/pidstat
/usr/bin/sadf
/usr/bin/sar
/usr/bin/tapestat
$ iostat
Linux 3.10.0-1062.4.1.el7.x86_64 (xmaster) 2019年11月24日 _x86_64_ (1 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1.08 0.00 1.00 0.01 0.00 97.91
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.73 7.25 18.05 1314429 3272661
dm-0 0.76 7.07 17.68 1282562 3204894
dm-1 0.04 0.10 0.07 18848 13560
主要参数解释
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.73 7.25 18.05 1314429 3272661
设备名 每秒传输次数 每秒从硬盘读的大小 每秒写入硬盘的大小 从硬盘读的总大小 写入硬盘的总大小
$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 156356 0 496688 0 0 7 18 49 114 1 1 98 0 0
查看端口
$ netstat -at|grep ESTABLISHED|grep http|grep -v 'tcp6'
tcp 0 0 xmaster:33938 xmaster:http ESTABLISHED
tcp 0 0 xmaster:33942 xmaster:http ESTABLISHED
tcp 0 0 xmaster:33940 xmaster:http ESTABLISHED
$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 172.16.240.2 0.0.0.0 UG 0 0 0 ens33
172.16.240.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
$ yum -y install nethogs
$ nethogs
配置mail
$ yum -y install mailx
$ vim /etc/mail.rc
set from=zabix1219server@163.com ? ? ? ?
set smtp=smtp.163.com
set smtp-auth-user=zabix1219server@163.com
set smtp-auth-password=ak1234
set smtp-auth=login
$ echo "Hello" | mail -s "标题" 1392263019@qq.com
#!/bin/bash
mon=`free -m |awk 'NR==2 {print $NF}'`
if [ $mon -le 500 ];then
echo "`date` memory is less than 500" >> /tmp/check_mem.log
echo -e "please check the memory is less than 500" |mail -s "check memory for the hostname `hostname`" 1392263019@qq.com
fi
说明: 规模比较小的时候, 使用shell脚本+定时任务来做监控
zabbix-agent(采集数据) --> zabbix-server(清洗数据, 报警) --> mysql(数据存储) <-- zabbix-web(数据展示)
zabbix-release-3.0-1.el7.noarch.rpm
$ wget https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
$ rpm -ivh zabbix-release-3.0-1.el7.noarch.rpm
zabbix.repo
文件, 将源改为清华源$ vim /etc/yum.repos.d/zabbix.repo
baseurl=https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/3.0/rhel/7/x86_64/
baseurl=https://mirror.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/7/x86_64/
$ yum install zabbix-server-mysql zabbix-web-mysql
======================================================================================================================================================================
Package 架构 版本 源 大小
======================================================================================================================================================================
正在安装:
zabbix-server-mysql x86_64 4.2.8-1.el7 zabbix 2.3 M
zabbix-web-mysql noarch 4.2.8-1.el7 zabbix 9.4 k
为依赖而安装:
OpenIPMI x86_64 2.0.27-1.el7 base 243 k
OpenIPMI-libs x86_64 2.0.27-1.el7 base 523 k
OpenIPMI-modalias x86_64 2.0.27-1.el7 base 16 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
fping x86_64 3.10-4.el7 epel 46 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
libevent x86_64 2.0.21-4.el7 base 214 k
libjpeg-turbo x86_64 1.2.90-8.el7 base 135 k
libxcb x86_64 1.13-1.el7 base 214 k
libzip x86_64 0.10.1-8.el7 base 48 k
net-snmp-libs x86_64 1:5.7.2-43.el7 base 750 k
php x86_64 5.4.16-46.1.el7_7 updates 1.4 M
php-bcmath x86_64 5.4.16-46.1.el7_7 updates 58 k
php-cli x86_64 5.4.16-46.1.el7_7 updates 2.7 M
php-common x86_64 5.4.16-46.1.el7_7 updates 565 k
php-gd x86_64 5.4.16-46.1.el7_7 updates 128 k
php-ldap x86_64 5.4.16-46.1.el7_7 updates 53 k
php-mbstring x86_64 5.4.16-46.1.el7_7 updates 505 k
php-mysql x86_64 5.4.16-46.1.el7_7 updates 101 k
php-pdo x86_64 5.4.16-46.1.el7_7 updates 99 k
php-xml x86_64 5.4.16-46.1.el7_7 updates 126 k
t1lib x86_64 5.1.2-14.el7 base 166 k
unixODBC x86_64 2.3.1-14.el7 base 413 k
zabbix-web noarch 4.2.8-1.el7 zabbix 2.8 M
原文:https://www.cnblogs.com/cjwnb/p/11922195.html