shell脚本获取Centos服务器信息

  • sysInfo.sh

#!/bin/bash
#*************************************************
#
# description   : This scirpt used for get centos machine basic info.
#             
# File name     : sysInfo.sh
#
#*************************************************

CentosVersion=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`

#cpu usage

if [ $CentosVersion -eq 6 ]; then
	cpuUsage=`top -n 1 | awk -F '[ %]+' 'NR==3 {print $2}'`
else
	cpuUsage=`top -n 1 | awk -F '[ ]+' 'NR==3 {print $2}'`
fi

#disk usage
diskUsage=`df -P | grep -w "/" | awk '{print $5 }' | cut -d'%' -f1`

logFile=./alarm-sysInfo.log

#mem
mem_total=`free -m | awk -F '[ :]+' 'NR==2{print $2}'`
mem_used=`free -m | grep Mem | awk '{print  $3}'`

#echo "mem total : ${mem_total}"
#echo "mem used  : ${mem_used}"

#mem usage
mem_used_persent=`awk 'BEGIN{printf "%.0f\n",('$mem_used'/'$mem_total')*100}'`

#current time
now_time=`date '+%F %T'`



function check(){
        if [[ "$cpuUsage" > 80 ]] || [[ "$diskUsage" > 80 ]] || [[ "$mem_used_persent" > 80 ]];then
        echo "Time       : ${now_time}"           > $logFile
		echo "CPU  usage : ${cpuUsage}%"         >> $logFile
		echo "Disk usage : ${diskUsage}%"        >> $logFile
		echo "MEM  usage : ${mem_used_persent}%" >> $logFile
        fi
}

function main(){
	   echo "${cpuUsage}-${diskUsage}-${mem_used_persent}"
}

main


你可能感兴趣的:(Linux,C)