From fc4386ec4f8494133f7f16da2650d1ae641e5a60 Mon Sep 17 00:00:00 2001 From: newrain Date: Wed, 16 Jun 2021 17:34:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E7=9B=91=E6=8E=A7=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- osMonitor.sh | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 osMonitor.sh diff --git a/osMonitor.sh b/osMonitor.sh new file mode 100644 index 0000000..697a221 --- /dev/null +++ b/osMonitor.sh @@ -0,0 +1,105 @@ +#!/usr/bin/bash + +# os check +# build 2021-06-16 + +#1、磁盘根分区剩余百分比 #返回数值,并添加触发器报警 +function disk(){ + local _part _disk + _part=$(lsblk -b |grep "centos-root"|awk '{print $(NF-3)}'|awk -F'G' '{print $1}') + _disk=$(lsblk -b |grep sda|grep disk|awk '{print $(NF-2)}'|awk -F'G' '{print $1}') + echo "scale=2;a=${_part}/${_disk};if (length(a)==scale(a)) print 0;print a " |bc +} + +#2、内存剩余百分比 #返回数值 +function memory(){ + local _memFree _memTotal + _memFree=$(free -b |awk 'NR==2{print $4}') + _memTotal=$(free -b |awk 'NR==2{print $2}') + echo "scale=2;a=${_memFree}/${_memTotal};if (length(a)==scale(a)) print 0;print a " |bc +} + +#3、swap使用情况 #返回数值 +function memory(){ + free -m |awk 'NR==3{print $3}' +} + +#4、cpu 1、5、15分钟负载 #返回数值或其他类型 +function cpuLoad(){ + case $1 in + 1) + uptime |awk '{print $(NF-2)}'|awk -F',' '{print $1}' + ;; + 5) + uptime |awk '{print $(NF-1)}'|awk -F',' '{print $1}' + ;; + 15) + uptime |awk '{print $(NF)}'|awk -F',' '{print $1}' + esac +} + +#5、/etc/passwd /etc/shadow /etc/sodoers 等文件修改情况 #返回任意类型,并添加触发器报警 +function fileMD5(){ + [ ! -f /tmp/md5.hash ] && md5sum /etc/passwd /etc/shadow /etc/sudoers > /tmp/md5sum && echo "数据收集中" + md5sum -c /tmp/md5.hash |grep -E "FAILED|失败" +} + +#6、系统启动时间 #返回任意类型 +function uptime(){ + local _uptime _idletime + _uptime=$(cat /proc/uptime |awk '{print ($1/60)}') # 单位为分钟 + _idletime=$(cat /proc/uptime |awk '{print ($2/60)}') # 系统空闲时间 + case $1 in + upTime) + echo $_uptime + ;; + idleTime) + echo $_idletime + ;; + idle) + echo "scale=2;a=${_idletime}/${_uptime};if (length(a)==scale(a)) print 0;print a " |bc + esac +} + +#7、系统连接数 #返回数值 +function conns(){ + case $1 in + all) + netstat -an | grep ESTABLISHED | wc -l + ;; + [1-9]*) + lsof -i:$1 |wc -l + esac +} + +#8、系统开启端口数量 #返回任意类型,需要知道是哪些端口 +function ports(){ + local p + case $1 in + portList) + p="`ss -tnul | grep -v Netid |awk '{print $5}'|awk -F ':' '{print $NF}'|sort -n|uniq`" + echo $p + ;; + port) + ss -tnul | grep -v Netid |awk '{print $5}'|awk -F ':' '{print $NF}'|sort -n|uniq|wc -l + esac +} + +#9、系统开机启动服务 #返回任意类型 +function services(){ + local s + case $1 in + servicesList) + s="`ls /etc/systemd/system/multi-user.target.wants/`" + echo $s + ;; + service) + ls /etc/systemd/system/multi-user.target.wants/ |wc -l + esac +} + +#10、计划任务 #返回文本 +function crontab(){ + sudo crontab -l -u $1 +} +