在生产环境中,我们时常要注意主机的各种硬件状态,如果是规模较大的服务集群zabbix等健康工具当然好用,如果只是一些小规模的服务主机,shell就会表现的更灵活,也更适用,今天就和大家分享一个系统巡检脚本!
1 #!/bin/bash 2 3 function system(){ 4 echo "#########################系统信息#########################" 5 OS_TYPE=`uname` 6 OS_VER=`cat /etc/redhat-release` 7 OS_KER=`uname -a|awk ‘{print $3}‘` 8 OS_TIME=`date +%F_%T` 9 OS_RUN_TIME=`uptime |awk ‘{print $3}‘|awk -F, ‘{print $1}‘` 10 OS_LAST_REBOOT_TIME=`who -b|awk ‘{print $2,$3}‘` 11 OS_HOSTNAME=`hostname` 12 13 echo " 系统类型:$OS_TYPE" 14 echo " 系统版本:$OS_VER" 15 echo " 系统内核:$OS_KER" 16 echo " 当前时间:$OS_TIME" 17 echo " 运行时间:$OS_RUN_TIME" 18 echo "最后重启时间:$OS_LAST_REBOOT_TIME" 19 echo " 本机名称:$OS_HOSTNAME" 20 } 21 function network(){ 22 23 echo "#########################网络信息#########################" 24 INTERNET=(`ifconfig|grep ens|awk -F: ‘{print $1}‘`) 25 for((i=0;i<`echo ${#INTERNET[*]}`;i++)) 26 do 27 OS_IP=`ifconfig ${INTERNET[$i]}|head -2|grep inet|awk ‘{print $2}‘` 28 echo " 本机IP:${INTERNET[$i]}:$OS_IP" 29 done 30 curl -I http://www.baidu.com &>/dev/null 31 if [ $? -eq 0 ] 32 then echo " 访问外网:成功" 33 else echo " 访问外网:失败" 34 fi 35 } 36 37 function hardware(){ 38 39 echo "#########################硬件信息#########################" 40 CPUID=`grep "physical id" /proc/cpuinfo |sort|uniq|wc -l` 41 CPUCORES=`grep "cores" /proc/cpuinfo|sort|uniq|awk -F: ‘{print $2}‘` 42 CPUMODE=`grep "model name" /proc/cpuinfo|sort|uniq|awk -F: ‘{print $2}‘` 43 44 echo " CPU数量: $CPUID" 45 echo " CPU核心:$CPUCORES" 46 echo " CPU型号:$CPUMODE" 47 48 MEMTOTAL=`free -m|grep Mem|awk ‘{print $2}‘` 49 MEMFREE=`free -m|grep Mem|awk ‘{print $7}‘` 50 51 echo " 内存总容量: ${MEMTOTAL}MB" 52 echo "剩余内存容量: ${MEMFREE}MB" 53 54 disksize=0 55 swapsize=`free|grep Swap|awk {‘print $2‘}` 56 partitionsize=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk {‘print $3‘}`) 57 for ((i=0;i<`echo ${#partitionsize[*]}`;i++)) 58 do 59 disksize=`expr $disksize + ${partitionsize[$i]}` 60 done 61 ((disktotal=\($disksize+$swapsize\)/1024/1024)) 62 63 echo " 磁盘总容量: ${disktotal}GB" 64 65 diskfree=0 66 swapfree=`free|grep Swap|awk ‘{print $4}‘` 67 partitionfree=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk ‘{print $5}‘`) 68 for ((i=0;i<`echo ${#partitionfree[*]}`;i++)) 69 do 70 diskfree=`expr $diskfree + ${partitionfree[$i]}` 71 done 72 73 ((freetotal=\($diskfree+$swapfree\)/1024/1024)) 74 75 echo "剩余磁盘容量:${freetotal}GB" 76 } 77 78 79 function secure(){ 80 echo "#########################安全信息#########################" 81 82 countuser=(`last|grep "still logged in"|awk ‘{print $1}‘|sort|uniq`) 83 for ((i=0;i<`echo ${#countuser[*]}`;i++)) 84 do echo "当前登录用户:${countuser[$i]}" 85 done 86 87 md5sum -c --quiet /opt/passwd.db &>/dev/null 88 if [ $? -eq 0 ] 89 then echo " 用户异常:否" 90 else echo " 用户异常:是" 91 fi 92 } 93 94 function chksys(){ 95 system 96 network 97 hardware 98 secure 99 }
脚本思路:因为监控的信息比较多,所以,用函数将这些功能分成4个模块、system() 系统信息模块、network()网络信息模块、hardware()硬件信息模块、secure()安全信息模块,最后通过chksys()将各个模块调用出来!
原文地址:https://www.cnblogs.com/liujunqiao/p/10738298.html
时间: 2024-10-09 19:51:07