欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

日常巡检(脚本)

程序员文章站 2022-06-07 22:23:45
Centos7 ......
#!/bin/bash

function system(){
echo "#########################系统信息#########################"
os_type=`uname`
os_ver=`cat /etc/redhat-release`
os_ker=`uname -a|awk '{print $3}'`
os_time=`date +%f_%t`
os_run_time=`uptime |awk '{print $3}'|awk -f, '{print $1}'`
os_last_reboot_time=`who -b|awk '{print $2,$3}'`
os_hostname=`hostname`

echo "    系统类型:$os_type"
echo "    系统版本:$os_ver"
echo "    系统内核:$os_ker"
echo "    当前时间:$os_time"
echo "    运行时间:$os_run_time"
echo "最后重启时间:$os_last_reboot_time"
echo "    本机名称:$os_hostname"
}
system

function network(){
echo "#########################网络信息#########################"
internet=(`ifconfig|grep ens|awk -f: '{print $1}'`)
for((i=0;i<`echo ${#internet[*]}`;i++))
do 
  os_ip=`ifconfig ${internet[$i]}|head -2|grep inet|awk '{print $2}'`
  echo "      本机ip:${internet[$i]}:$os_ip"
done
curl -i http://www.baidu.com &>/dev/null
if [ $? -eq 0 ]
then echo "    访问外网:成功"
else echo "    访问外网:失败"
fi
}
network

function hardware(){
echo "#########################硬件信息#########################"
cpuid=`grep "physical id" /proc/cpuinfo |sort|uniq|wc -l`
cpucores=`grep "cores" /proc/cpuinfo|sort|uniq|awk -f: '{print $2}'`
cpumode=`grep "model name" /proc/cpuinfo|sort|uniq|awk -f: '{print $2}'`

echo "     cpu数量: $cpuid"
echo "     cpu核心:$cpucores"
echo "     cpu型号:$cpumode"

memtotal=`free -m|grep mem|awk '{print $2}'`
memfree=`free -m|grep mem|awk '{print $7}'`

echo "  内存总容量: ${memtotal}mb"
echo "剩余内存容量: ${memfree}mb"

disksize=0
swapsize=`free|grep swap|awk {'print $2'}`
partitionsize=(`df -t|sed 1d|egrep -v "tmpfs|sr0"|awk {'print $3'}`)
for ((i=0;i<`echo ${#partitionsize[*]}`;i++))
do
disksize=`expr $disksize + ${partitionsize[$i]}`
done
((disktotal=\($disksize+$swapsize\)/1024/1024))

echo "  磁盘总容量: ${disktotal}gb"

diskfree=0
swapfree=`free|grep swap|awk '{print $4}'`
partitionfree=(`df -t|sed 1d|egrep -v "tmpfs|sr0"|awk '{print $5}'`)
for ((i=0;i<`echo ${#partitionfree[*]}`;i++))
do
diskfree=`expr $diskfree + ${partitionfree[$i]}`
done

((freetotal=\($diskfree+$swapfree\)/1024/1024))

echo "剩余磁盘容量:${freetotal}gb"
}
hardware

function secure(){
echo "#########################安全信息#########################"

countuser=(`last|grep "still logged in"|awk '{print $1}'|sort|uniq`)
for ((i=0;i<`echo ${#countuser[*]}`;i++))
do echo "当前登录用户:${countuser[$i]}"
done

md5sum -c --quiet /opt/passwd.db &>/dev/null
if [ $? -eq 0 ]
then echo "    用户异常:否"
else echo "    用户异常:是"
fi
}
secure

  centos7