#!/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 }