收集系统资源脚本如下:

collection_info_product.sh


#!/bin/bash

# The login server, display the relevant information.

# write to Linuxtone:liweizhong

# version 1.0 update 2013.08.13

# email: liweizhong@anytracking.com by gosafe


# Product INFO

Manufacturer=`/usr/sbin/dmidecode | grep "Manufacturer"|sed -n 1p|awk '{print $2,$3}'`

Name=`/usr/sbin/dmidecode |grep "Product Name"|sed -n 1p|awk '{print $3,$4}'`

Height=`/usr/sbin/dmidecode |grep "Heig"|awk '{print $2,$3}'`

Number=`/usr/sbin/dmidecode | grep 'Serial Number'|sed -n 1p|awk '{print $3}'`


# CPU INFO

Cores=`cat /proc/cpuinfo |grep "cpu cores"|sort -u|awk '{print $4}'`

US=`vmstat |grep [0-9]|awk '{print $13}'`

SY=`vmstat |grep [0-9]|awk '{print $14}'`

ID=`vmstat |grep [0-9]|awk '{print $15}'`

WA=`vmstat |grep [0-9]|awk '{print $16}'`


# Memory INFO

Total=`free -m -o -t |grep Mem|awk '{print $2}'`

Used=`free -m -o -t |grep Mem|awk '{print  $3}'`

Free=`free -m -o -t |grep Mem|awk '{print  $4}'`

Cached=`free -m -o -t |grep Mem|awk '{print $7}'`


# System INFO

IP_eth0=`/sbin/ifconfig eth0|grep "inet addr:"|grep -v "127.0.0.1"|awk '{print $2}'|cut -c 6-20`

IP_eth1=`/sbin/ifconfig eth1|grep "inet addr:"|grep -v "127.0.0.1"|awk '{print $2}'|cut -c 6-20`

Version=`lsb_release -a|awk '{print $2,$3,$4}'|sed -n "3p"`

Process=`ps aux |wc -l`

UP=`uptime |awk '{print $3,$4}'`

Load=`uptime | awk '{print $(NF-2)}' | sed 's/,//g'| awk -F '.' '{print $1}'`


# Disk INFO

Totall=`/sbin/fdisk -l | grep Disk |awk '{print $2,$3,$4}'`

Root_total=`df -H |awk '$5=="/" {print $0 }'|awk '{print $1}'`

Root_use=`df | sed -n '/\/$/p' | gawk '{print $4}' | sed  's/%//'`

Root_free=`df -H |awk '$5=="/" {print $0 }'|awk '{print $3}'`

Boot_total=`df -H |awk '$6=="/boot" {print $0 }'|awk '{print $2}'`

Boot_use=`df | sed -n '/\/boot$/p' | gawk '{print $5}' | sed  's/%//'`

Boot_free=`df -H |awk '$6=="/boot" {print $0 }'|awk '{print $4}'`


# Login user INFO

Login=`who`


#echo -e "\033[31m 红色字 \033[0m"

#echo -e "\033[32m 绿色字 \033[0m"

#echo -e "\033[33m ×××字 \033[0m"

#echo -e "\033[36m 天蓝字 \033[0m"


echo -e "---------------------------------------------------------      

               \033[35m [ Product info ] \033[0m          

\033[37m manufacturer: \033[36m $Manufacturer \033[0m            

\033[37m name: \033[36m $Name \033[0m                            

\033[37m height: \033[36m $Height \033[0m                            

\033[37m number: \033[36m $Number \033[0m"

echo -e "---------------------------------------------------------

\033[35m [ CPU info ] \033[0m

\033[37m cores: \033[36m $Cores n\033[0m"

if [ $US -ge 90 ] && [ $SY -ge 90 ] && [ $WA -ge 90 ];then

echo -e "\033[37m us: \033[31m $US %\033[0m"                                

echo -e "\033[37m sy: \033[31m $SY %\033[0m"

echo -e "\033[37m wa: \033[31m $WA %\033[0m"

fi

if [ $US -ge 85 ] && [ $SY -ge 85 ] && [ $WA -ge 85 ];then

echo -e "\033[37m us: \033[33m $US %\033[0m"                                

echo -e "\033[37m sy: \033[33m $SY %\033[0m"

echo -e "\033[37m wa: \033[33m $WA %\033[0m"

else

echo -e "\033[37m us: \033[32m $US %\033[0m"                                

echo -e "\033[37m sy: \033[32m $SY %\033[0m"

echo -e "\033[37m wa: \033[32m $WA %\033[0m"

fi

echo -e "\033[37m id: \033[32m $ID %\033[0m"

echo -e "---------------------------------------------------------

\033[35m [ Memory info ] \033[0m

\033[37m total:  \033[36m $Total MB\033[0m"

if [ $Used -ge 23500 ];then

echo -e "\033[37m used:   \033[31m $Used MB\033[0m"

fi

if [ $Used -ge 23000 ];then

echo -e "\033[37m used:   \033[33m $Used MB\033[0m"

else

echo -e "\033[37m used:   \033[32m $Used MB\033[0m"

fi

if [ "$Free" -lt 512 ] && [ "$Cached" -lt 200 ];then

echo -e "\033[37m free:   \033[31m $Free MB \033[0m"

echo -e "\033[37m cached: \033[31m $Cached MB \033[0m"

fi

if [ "$Free" -lt 1024 ] && [ "$Cached" -lt 2048 ];then

echo -e "\033[37m free:   \033[33m $Free MB \033[0m"

echo -e "\033[37m cached: \033[33m $Cached MB \033[0m"

else

echo -e "\033[37m free:   \033[32m $Free MB \033[0m"

echo -e "\033[37m cached: \033[32m $Cached MB \033[0m"

fi

echo -e "---------------------------------------------------------

\033[35m [ System info ] \033[0m

\033[37m ip:\033[0m \033[36m $IP_eth0   $IP_eth1\033[0m

\033[37m version:   \033[36m $Version\033[0m

\033[37m up:   \033[36m $UP\033[0m"

if [ $Process -ge 300 ];then

echo -e "\033[37m process:   \033[31m $Process\033[0m"

fi

if [ $Process -ge 250 ];then

echo -e "\033[37m process:   \033[33m $Process\033[0m"

else

echo -e "\033[37m process:   \033[32m $Process\033[0m"

fi

if [ $Load -ge 16 ];then

echo -e "\033[37m load average: \033[31m $Load\033[0m"

fi

if [ $Load -ge 12 ];then

echo -e "\033[37m load average: \033[33m $Load\033[0m"

else

echo -e "\033[37m load average: \033[32m $Load\033[0m"

fi

echo -e "---------------------------------------------------------

\033[35m [ Disk info ] \033[0m

\033[37m total:  \033[36m $Totall\033[0m

\033[37m / total:   \033[36m $Root_total\033[0m"

if [ $Root_use -ge 90 ];then

echo -e "\033[37m / free: \033[31m $Root_free\033[0m"

echo -e "\033[37m / use: \033[31m $Root_use %\033[0m"

fi

if [ $Root_use -ge 85 ];then

echo -e "\033[37m / free: \033[33m $Root_free\033[0m"

echo -e "\033[37m / use: \033[33m $Root_use %\033[0m"

else

echo -e "\033[37m / free: \033[32m $Root_free\033[0m"

echo -e "\033[37m / use: \033[32m $Root_use %\033[0m"

fi

echo -e "\033[37m /boot total:   \033[36m $Boot_total\033[0m"

if [ $Boot_use -ge 90 ];then

echo -e "\033[37m /boot free: \033[31m $Boot_free\033[0m"

echo -e "\033[37m /boot use: \033[31m $Boot_use %\033[0m"

fi

if [ $Boot_use -ge 85 ];then

echo -e "\033[37m /boot free: \033[33m $Boot_free\033[0m"

echo -e "\033[37m /boot use: \033[33m $Boot_use %\033[0m"

else

echo -e "\033[37m /boot free: \033[32m $Boot_free\033[0m"

echo -e "\033[37m /boot use: \033[32m $Boot_use %\033[0m"

fi

echo -e "---------------------------------------------------------

\033[35m [ Login user info ] \033[0m

\033[36m $Login\033[0m"


如套用此脚本,需自行修改下磁盘收集信息,此脚本执行后字体颜色说明如下:

白色字: 信息名称

紫色字: 资源名称

红色字: 严重

绿色字: 正常

×××字: 报警

天蓝字: 固定参数


用户登录后显示,只需将此脚本追加入到/etc/profile

chmod 755 collection_info_product.sh

echo "collection_info_product.sh" >> /etc/profile


注:在Product info收集时,普通用户会提示权限拒绝!

我这里设置了dmidecode命令suid权限。

101119986.jpg



登录后效果截图如下:093616496.jpg