C001 Linux系统巡检脚本

当服务器达到一定数量级时,使用脚本巡检就显得非常必要!

脚本文件放在D:\OScheck目录下,截图如下:

C001 Linux系统巡检脚本_第1张图片

CRT是SecureCRT软件,LOGS存放生成的日志文件,ServerList是服务器列表

 

DAILYCheck.bat文件内容:

REM DAILYCheck.bat
@echo off
Setlocal ENABLEDELAYEDEXPANSION
Set strDate=%date:~0,4%%date:~5,2%%date:~8,2%
REM=================================================================================
cls
color 0a
title  巡检工具
:begin
cls
echo.
%~d0
cd %~dp0
set TempDir=d:\OScheck\temp
if not exist  %TempDir% mkdir %TempDir%
echo.
echo             ﹎.巡检工具——服务器巡检.﹎    
echo.
echo            ╔═════════════╗
echo            ║  1、服务器巡检(每日)   ║
echo            ║  2、服务器巡检(每周)   ║
echo            ║  3、服务器巡检(每月)   ║
echo            ╚═════════════╝
echo                0、退出程序!
echo.
set num=99
set /p num=" 请输入选择数字,然后按回车:"
if %num%==1 goto bat1
if %num%==2 goto bat2
if %num%==3 goto bat3
if %num%==0 exit
echo.
goto begin

:bat1
set SvrList=d:\OScheck\ServerList\svr.list
set Checklog=%TempDir%\os_dailycheck_%strDate%.log
set CheckDir=d:\OSCheck\Log_%strDate%\Date
goto end
:bat2
set SvrList=d:\OScheck\ServerList\svrWeek.list
set Checklog=%TempDir%\os_dailycheck_%strDate%_Week.log
set CheckDir=d:\OSCheck\Log_%strDate%\Week
goto end
:bat3
set SvrList=d:\OScheck\ServerList\svrMonth.list
set Checklog=%TempDir%\os_dailycheck_%strDate%_Month.log
set CheckDir=d:\OSCheck\Log_%strDate%\Month
goto end
:end

echo.
echo  :::Start OS Daily checking on %date%
echo  :::Start OS Daily checking on %date% >> %checklog%
echo.
echo  ===================================================================
echo.
For /F "tokens=1,2,3,4,5 delims=," %%i in (%SvrList%) do (
echo %date%:: %%k :: %%i :: %%j >> %checklog%
echo  ==================主机: %%k ,开始检测,请稍等==================
"D:\OScheck\CRT\SecureCRT.exe" /TITLEBAR "OSCheck:%%k:%%i:%%j"/NOMENU /NOTOOLBAR /SCRIPT D:\OScheck\Dailycheck.vbs /ARG %%l /ARG %%m /ARG %%i /ARG %%j >> %checklog%
echo  ==================主机: %%k ,完成检测,请核查==================
echo.
)

REM - 检查完毕后将下载到本地的检查日志移到一个目录中。
if not exist  %CheckDir% mkdir %CheckDir%
Move /Y %TempDir%\*.log %checkDir%
goto begin

Dailycheck.vbs文件内容:

# $language = "VBScript"
# $interface = "1.0"

Sub Main
   dim objTab, objSFTPTab
   dim strHost, strUserid, strUserPwd, strRootPwd, strPort
   dim strConnect
   IF crt.arguments.count <> 4 then
       crt.Daiglog.MessageBox("No Enough Paratmeters!")
       crt.quit
       exit sub
   ELSE
   strUserid = crt.arguments(0)
   strUserPwd = crt.arguments(1)
        strHost = crt.arguments(2)
        strPort = crt.arguments(3)
   END IF
  
'======================================================================================================
   'Connect to a host and start OS check daily 
   strConnect="/SSH2 /P " & strPort & " /L " & strUserId & " /PASSWORD " & strUserPwd & " " & strHost 
  set objTab=crt.session.connectInTab(strConnect)
  objTab.screen.synchronous = True

  'Create an SFTP tab associated with the existing tab
   set objSFTPTab=objTab.connectSFTP
  'Wait for the SFTP tab to be ready for input
  objSFTPTab.screen.synchronous = True
  crt.Sleep 300
  objSFTPTab.screen.Send vbcr
  objSFTPTab.Screen.WaitForString "sftp>"
  objSFTPTab.Screen.Send "lcd D:\OScheck\"  & vbcr
  objSFTPTab.Screen.WaitForString "sftp>"
  objSFTPTab.Screen.Send "cd /root/gyl/OSCheck/"  & vbcr
  objSFTPTab.Screen.WaitForString "sftp>"
  objSFTPTab.Screen.Send "put -a oscheck.sh"  & vbcr
  objSFTPTab.screen.WaitForString "Uploading"
  objSFTPTab.screen.WaitForString "sftp>"

  objTab.activate()
   crt.sleep 10
   objTab.screen.waitforstring("#")
   objTab.screen.send("cd /root/gyl/OSCheck/") & chr(13)
   objTab.screen.waitforstring("OSCheck") 
   objTab.Screen.send("rm -f /root/gyl/OSCheck/*.log ; echo r000") & chr(13)
   objTab.Screen.waitForString("r000")
   objTab.screen.send("chmod +x /root/gyl/OSCheck/oscheck.sh") & chr(13)
   objTab.screen.waitforstring("OSCheck") 
   objTab.screen.send("sh /root/gyl/OSCheck/oscheck.sh") & chr(13)
   objTab.screen.waitforstring("successfully")

   objSFTPTab.activate()
   ' download the check log to d:\OSCheck\temp from remote working Directory /root/gyl/OSCheck/
  objSFTPTab.Screen.Send "lcd D:\OScheck\temp\"  & vbcr
  objSFTPTab.Screen.WaitForString "sftp>"
   objSFTPTab.Screen.Send ("get -a /root/gyl/OSCheck/*.log" ) & vbcr
   objSFTPTab.screen.WaitForString "Downloading"
   objSFTPTab.screen.WaitForString "sftp>"
   'Close the SFTPTab
   objSFTPTab.screen.send ("exit") & vbcr
   
   objTab.activate()
   objTab.Screen.send("echo r001") & chr(13)
   objTab.Screen.WaitForString("r001") 
   objTab.Session.Disconnect
   'objTab.close
 
   'Done, quit SecureCRT
    crt.quit
End Sub

oscheck.sh文件内容:

#!/bin/sh
#================================================================================================
# This Scripts is used to collect Linux Configuration Data and health check daily.
#
#================================================================================================
# -----------------------------------------------------------------------------------------------
# Part 1 - Get the Basic Information
# Part 2 - Get the Detail Information
# Section 1 - OS Specific Release Information
# Section 2 - System Information
# Section 3 - Filesystem Information
# Section 4 - VG Information
# Section 5 - Services Configuration
# Section 6 - The last 40 lines in syslog
# Section 7 - Root's Mail (Status closed)
# Section 8 - Login History since Last Day
# Section 9 - Root Crontab and Error Log
# Section 10 - Check Process
# Section 11 - Network Information
# Section 12 - Software Information
# Part 3 - Check System Performance
# Part 4 - run local check scripts if available
# -----------------------------------------------------------------------------------------------
IPAddress=`ifconfig|grep 'inet addr:'|head -1|awk '{print $2}'|sed 's/addr://g'`;
LOGFILE=detail.$IPAddress.`hostname`.`date +"%y%m%d"`.log
ConfFile=conf.$IPAddress.`hostname`.`date +"%y%m%d"`.log
CheckLog=check.$IPAddress.`hostname`.`date +"%y%m%d"`.log
TmpOut=TmpOut.$IPAddress.`hostname`.`date +"%y%m%d"`.log
userid=`whoami`
SectionN=0
FSThreshold=78
CPUThreshold=50
SWAPThreshold=30
MEMThreshold=50
MemCachedThreshold=85
ProcessCPUThreshold=20
ProcessMEMThreshold=20
CPUAvgThreshold=20
CPUMaxThreshold=50
CheckDir="/root/gyl/OSCheck"
[ ! -d "$CheckDir" ] && mkdir -p $CheckDir
cd $CheckDir
# ===============================================================================================
# Preapre the output file
#
cp -f /dev/null $LOGFILE
cp -f /dev/null $ConfFile
cp -f /dev/null $CheckLog
if [ `id|cut -c1-6` != "uid=0(" ] ; then
echo "" >> $CheckLog
echo "This script must run as root!" >> $CheckLog
echo "" >> $CheckLog
echo "Script end unsuccessfully!"
exit 1
fi
# Check Linux Distribution
distrib="unknown"
if [ -f /etc/SuSE-release ] ; then
distrib="`head -1 /etc/SuSE-release`"
SUSE="yes"
else
SUSE="no"
fi
if [ -f /etc/redhat-release ] ; then
distrib="`head -1 /etc/redhat-release`"
REDHAT="yes"
else
REDHAT="no"
fi
HostName=`hostname`
KernelVer=`uname -r`
CPUNum=`cat /proc/cpuinfo | grep "processor" | wc -l `
CPUMHz=`cat /proc/cpuinfo | grep MHz | uniq | awk -F: '{print $2}'`
MemTotal=`free -m | head -2 | tail -1 | awk '{print $2}'`
MemFree=`free -m | head -3 | tail -1 | awk '{print $3}'`
echo "============================== Begin  $(date +%Y"."%m"."%d" "%k":"%M":"%S) =================================" >> $LOGFILE
echo "" >>$LOGFILE
#================================================================================================
# Part 1 : Get the Basic Information
#================================================================================================
#
echo "============================== Part 1 : Basic Information =================================" >> $LOGFILE
echo "System OS Health Check Date : " `date +%Y%m%d` >> $LOGFILE
echo "Hostname : " $HostName >>$LOGFILE
echo "Hostname : " $HostName >>$ConfFile
echo "IP address : " $IPAddress >>$LOGFILE
echo "IP address : " $IPAddress >>$ConfFile
echo "OS Version : " $distrib >>$LOGFILE
echo "OS Version : " $distrib >>$ConfFile
echo "OS Kernel Version : " $KernelVer >>$LOGFILE
echo "OS Kernel Version : " $KernelVer >>$ConfFile
echo "Processor Number : " $CPUNum >>$LOGFILE
echo "Processor Number : " $CPUNum >>$ConfFile
echo "Processsor CPU MHz : " $CPUMHz >>$LOGFILE
echo "Total Memory : " $MemTotal >>$LOGFILE
echo "Total Memory : " $MemTotal >>$ConfFile
echo "Free Memroy : " $MemFree >>$LOGFILE
echo "System Uptime : " `uptime | awk -F, '{print $1,$2}'` >>$LOGFILE
Upday=`uptime | awk '{print $3}'`
if [ $Upday -lt 1 ]
then
echo "System Uptime : " `uptime | awk -F, '{print $1,$2}'` >> $CheckLog
echo "Server is reboot recently, please have a check " >> $CheckLog
echo "" >> $CheckLog
fi
#================================================================================================
# Part 2 : Get the Detail Information
#================================================================================================
#
echo "============================== Part 2 : Detail Information ===============================" >>$LOGFILE
#
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - OS Specific Release Information:" >>$LOGFILE
echo "Section $SectionN - OS Specific Release Information:" >>$ConfFile
if [ -x /usr/bin/lsb_release ]
then
/usr/bin/lsb_release -a >> $LOGFILE 2>/dev/null
/usr/bin/lsb_release -a >> $ConfFile 2>/dev/null
else
echo "Linux Standard Base Verion " >> $LOGFILE
echo "Linux Standard Base Verion " >> $ConfFile
fi
#
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - System Infomation:" >>$LOGFILE
echo "CPU and Model Info:" >> $LOGFILE
cat /proc/cpuinfo >> $LOGFILE
echo "" >> $LOGFILE
echo "System Ulimit:" >> $LOGFILE
ulimit -a >> $LOGFILE
echo "" >> $ConfFile
echo "System Ulimit:" >> $ConfFile
ulimit -a >> $ConfFile
echo "" >> $ConfFile
echo "" >> $LOGFILE
echo "Hardware Info:" >> $LOGFILE
dmidecode >>$LOGFILE 2>/dev/null
echo " " >>$LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - Filesystem Infomation:" >>$LOGFILE
echo "Filesystem and Usage : " >> $LOGFILE
df -hP >>$LOGFILE 2>/dev/null
echo " " >> $LOGFILE
df -lhP >>$LOGFILE 2>/dev/null
echo " " >> $LOGFILE
echo "rc.local:" >> $LOGFILE
cat /etc/rc.local >> $LOGFILE
echo " " >> $LOGFILE
echo "/etc/sysconfig/iptables:" >> $LOGFILE
cat /etc/sysconfig/iptables >> $LOGFILE
echo " " >> $LOGFILE
echo "Local MountPoints:" >> $LOGFILE
mount >> $LOGFILE
echo " " >> $LOGFILE
echo "Local MountPoints:" >> $ConfFile
mount >> $ConfFile
echo " " >>$ConfFile
echo "FileSystemSystem Tab: " >> $LOGFILE
cat /etc/fstab >> $LOGFILE
echo "" >> $LOGFILE
echo "FileSystemSystem Tab: " >> $ConfFile
cat /etc/fstab >> $ConfFile
echo "" >> $ConfFile
echo "NFS Filesystems: " >> $LOGFILE
grep -vE '^#|^ *$' /etc/exports >> $LOGFILE
echo "" >> $LOGFILE
echo "NFS Filesystems: " >> $ConfFile
grep -vE '^#|^ *$' /etc/exports >> $ConfFile
echo "" >> $ConfFile
# check if mounted Filesystem varied?
mount > mount.now
if [ -f mount.ref ] ; then
diff mount.now mount.ref > mount.diff
if [ `cat mount.diff | wc -l` -gt 0 ] ; then
echo " The mounted filesystem varied! " >> $CheckLog
echo "" >> $CheckLog
fi
mv -f mount.now mount.ref
else
mv -f mount.now mount.ref
fi
echo " " >> $ConfFile
#echo "FilesysteUtiliationa are: " >> $ConfFile
#df -lhP | awk '{print $1,$2,$5,$6}' >> $ConfFile
#echo " " >> $ConfFile
# check if utilization of Filesystem exceed the threshold!
DF_Out=`df -lhP | grep -v "^/dev/hdc"|grep -v ":" | awk '{print $5}' | sed '1d' | sed 's/%//g'`
Num=0
for Line in $DF_Out
do
if [ `echo "$Line>$FSThreshold"|bc` -eq 1 ] ; then
Num=`expr $Num + 1`
fi
done
if [ $Num -gt 0 ] ; then
echo "There are $Num filesystem exceed the Threshold " >> $CheckLog
df -hP >> $CheckLog
echo "" >> $CheckLog
fi
#Check if /etc/fstab be modified
if [ -f fstab.ref ] ; then
diff /etc/fstab fstab.ref > fstab.diff
if [ `cat fstab.diff | wc -l` -gt 0 ] ; then
echo "File /etc/fstab has been modified!" >> $CheckLog
echo "" >> $CheckLog
fi
cp -f /etc/fstab fstab.ref
else
cp /etc/fstab fstab.ref
fi
#Check Disk Partition!
echo "Disk Partition:" >> $LOGFILE
if [ -x /sbin/fdisk ]
then
if [ -x /sbin/parted ]
then
for i in $(fdisk -l 2>/dev/null | grep "^Disk " | grep "/dev/"|cut -f1 -d:|cut -f2 -d " ")
do
/sbin/parted -s $i print >> $LOGFILE 2>/dev/null
done
else
/sbin/fdisk -l >> $LOGFILE
fi
fi
echo " " >>$LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - VG Information : " >> $LOGFILE
vgdisplay -v >> $LOGFILE 2>/dev/null
echo " " >>$LOGFILE
echo "Report Logical Volume inforation: " >> $LOGFILE
lvs >> $LOGFILE 2>/dev/null
echo "" >>$LOGFILE
echo "Report Logical Volume inforation: " >> $ConfFile
lvs >> $ConfFile 2>/dev/null
echo "" >>$ConfFile
echo "Report Pyschial Volume Information: " >> $LOGFILE
pvs >> $LOGFILE 2>/dev/null
echo "" >>$LOGFILE
echo "Report Pyschial Volume Information: " >> $ConfFile
pvs >> $ConfFile 2>/dev/null
echo "" >>$ConfFile
echo "Report Volume Group Information: " >> $LOGFILE
vgs >> $LOGFILE 2>/dev/null
echo " " >> $LOGFILE
echo "Report Volume Group Information: " >> $ConfFile
vgs >> $ConfFile 2>/dev/null
echo "" >>$ConfFile
echo " " >> $LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - Services Configuration: " >> $LOGFILE
if [ -x /sbin/chkconfig ] ; then
echo "Services Startup : " >> $LOGFILE
/sbin/chkconfig --list >> $LOGFILE
fi
echo " " >> $LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - System Log: " >> $LOGFILE
if [ -r /var/log/messages ]
then
LDate=`tail -1 /var/log/messages | cut -c 1-6`
cmdGetLog="grep \"$LDate\" /var/log/messages"
eval $cmdGetLog >> $LOGFILE
fi
if [ -x /bin/dmesg ]
then
echo "Last 40 lines of dmesg: " >> $LOGFILE
/bin/dmesg | tail -n 40 >> $LOGFILE
fi
echo " " >> $LOGFILE
#echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
#echo "Section $SectionN - Show root's mail: " >> $LOGFILE
#if [ -r /var/spool/mail/root ]
#then
#cat /var/spool/mail/root > mail.now
#if [ -f mail.ref ]
#then
#diff -e mail.ref mail.now | awk 'NR>1 {print $0}' >> $LOGFILE 2>/dev/null
#mv -f mail.now mail.ref
#else
#mv mail.now mail.ref
#fi
#fi
#echo "" >> $LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - Show User Login History Since Last Day: " >> $LOGFILE
last > last.now 2>/dev/null
if [ -f last.ref ]
then
diff -e last.ref last.now | awk 'NR>1 {print $0}' >> $LOGFILE 2>/dev/null
mv -f last.now last.ref
else
mv last.now last.ref
fi
echo "" >> $LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - Root Crontab and Error Log: " >> $LOGFILE
if [ "$SUSE" = "yes" ] ; then
usercron="/var/spool/cron/tabs"
fi
if [ "$REDHAT" = "yes" ] ; then
usercron="/var/spool/cron"
fi
if [ "$DEBIAN" = "yes" ] ; then
usercron="/var/spool/cron/crontabs"
fi
ls $usercron/root > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "" >> $LOGFILE
echo "Root Crontab :" >> $LOGFILE
cat $usercron/root >> $LOGFILE
echo "" >> $LOGFILE
echo "" >> $ConfFile
echo "Root Crontab :" >> $ConfFile
cat $usercron/root >> $ConfFile
echo "" >> $ConfFile
else
echo "" >> $LOGFILE
echo "No crontab files for root" >> $LOGFILE
echo "" >> $LOGFILE
fi
ls /var/log/cron > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Last 40 lines in cron log:" >> $LOGFILE
tail -n 40 /var/log/cron >>$LOGFILE
fi
echo "" >> $LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - Check Process:" >> $LOGFILE
echo "" >> $LOGFILE
echo "Active Process Overview:" >> $LOGFILE
/usr/bin/pstree -p -a -l -G -A >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
echo " Top5: CPU Process : " >> $LOGFILE
ps aux | head -1 >> $LOGFILE
ps aux | grep -v USER | sort -nr -k 3 | head -5 >> $LOGFILE
echo "" >> $LOGFILE
echo " Top5: Memory Process:" >> $LOGFILE
ps aux | head -1 >> $LOGFILE
ps aux | grep -v USER | sort -nr -k 4 | head -5 >> $LOGFILE
echo "" >> $LOGFILE
NumOfProcess=`ps -efl | wc -l`
echo "$NumOfProcess processes are running" >> $LOGFILE
NumOfDead=`ps -ef | grep defunc | grep -v "grep" | wc -l`
if [ $NumOfDead -gt 0 ]
then
echo "$NumOfDead processes were dead" >> $CheckLog
ps -ef | grep defunc | grep -v "grep" >> $CheckLog
echo "" >> $CheckLog
fi
echo "" >> $LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - Network Information:" >> $LOGFILE
echo "" >> $LOGFILE
if [ "$REDHAT" = "yes" ] ; then
echo " Network Service status: " >> $LOGFILE
service network status >> $LOGFILE
echo "" >> $LOGFILE
fi
echo " Network Intface configruation: " >> $LOGFILE
ifconfig >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
netstat -in >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
netstat -rn >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
echo "" >> $LOGFILE
netstat -nt >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
netstat -lntp >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
echo "" >> $ConfFile
echo "IP Address: " >> $ConfFile
ip addr >> $ConfFile
echo "" >> $ConfFile
echo "Route Settings: " >> $ConfFile
netstat -rn >> $ConfFile
echo "" >> $ConfFile
echo "NTP Settings: " >> $ConfFile
grep -vE '^#|^ *$' /etc/ntp.conf >> $ConfFile
echo "" >> $ConfFile
#echo "SSHD Settings: " >> $ConfFile
#grep -vE '^#|^ *$' /etc/ssh/sshd_config >> $ConfFile
#echo "" >> $ConfFile
#echo "SSH Settings: " >> $ConfFile
#grep -vE '^#|^ *$' /etc/ssh/ssh_config >> $ConfFile
echo "" >> $LOGFILE
echo "-------------------------------------------------------------------------------------------" >>$LOGFILE
SectionN=`expr $SectionN + 1`
echo "Section $SectionN - Software Information:" >> $LOGFILE
# REDHAT
if [ "$REDHAT" = "yes" ] ; then
echo " Installed Packages: " >> $LOGFILE
rpm -qa | sort -d -f >> $LOGFILE
fi
# end REDHAT
# SUSE
if [ "$SUSE" = "yes" ] ; then
echo "Installed Packages: " >> $LOGFILE
rpm -qa | sort -d -f >> $LOGFILE
fi
# end SUSE
# Debian
if [ "$DEBIAN" = "yes" ] ; then
echo "Installed Packages: " >> $LOGFILE
dpkg --get-selections | awk '!/deinstall/ {print $1}' >> $LOGFILE
echo "Miconfigured Packages:" >> $LOGFILE
dpkg -C >> $LOGFILE
echo "Detail list of installed Packages: " >> $LOGFILE
dpkg -l >> $LOGFILE
fi
# end Debian
#
echo "" >> $LOGFILE
#================================================================================================
# Part 3 : Performance
#================================================================================================
#
echo "============================== Part 3 : Performance Information==============================" >>$LOGFILE
echo "------------------------------ Performance Checking ---------------------------------------" >>$LOGFILE
#
if [ -x /usr/bin/vmstat ] ; then
echo "VM-Statistics: " >> $LOGFILE
/usr/bin/vmstat 2 4 >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
echo "VM-Statistics Summary:" >> $LOGFILE
/usr/bin/vmstat -dn >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
/usr/bin/vmstat -f >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
fi
if [ -x /usr/bin/mpstat ] ; then
echo "MP-Statistics: " >> $LOGFILE
/usr/bin/mpstat 2 4 >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
fi
if [ -x /usr/bin/iostat ] ; then
echo "IO-Statistics:" >> $LOGFILE
/usr/bin/iostat 2 4 >> $LOGFILE 2>/dev/null
echo "" >> $LOGFILE
fi
if [ -x /usr/bin/free ] ; then
echo "RAM/SWAP Statistics:" >> $LOGFILE
/usr/bin/free -m >> $LOGFILE
echo "" >> $LOGFILE
fi
if [ -e /proc/sysvipc ] ; then
echo " IPC status : " >> $LOGFILE
ipcs >> $LOGFILE
echo "" >> $LOGFILE
echo "IPC Summary: " >> $LOGFILE
ipcs -u >> $LOGFILE
echo "" >> $LOGFILE
echo "IPC Limits:" >> $LOGFILE
ipcs -l >> $LOGFILE
echo "" >> $LOGFILE
fi
# Check the CPU utilization if exceed the threshold
if [ -x /usr/sbin/sar ]
then
CPUUtil=`/usr/sbin/sar -u 1 10 | awk '/Average/ { print ( $3 + $5 ) }'`
echo "Current CPU utilization is : " $CPUUtil >> $LOGFILE
if [ `echo "$CPUUtil>$CPUThreshold"|bc` -eq 1 ]
then
echo "CPU utilization is " $CPUUtil " exceed the threshold" >> $CheckLog
echo "" >> $CheckLog
fi
fi
# Check the SWAP utilization if exceed the threshold
SWAPUsed=`free -m | grep Swap | awk '{print $3}'`
SWAPTotal=`free -m | grep Swap | awk '{print $2}'`
SWAPUtil=`echo "scale=2; $SWAPUsed/$SWAPTotal"|bc`
echo "Current SWAP utilization is : " $SWAPUtil >> $LOGFILE
echo "Current SWAP utilization is : " $SWAPUtil >> $TmpOut
echo "$SWAPUtil % = ($SWAPUsed/$SWAPTotal)*100%" >> $LOGFILE
echo "" >> $LOGFILE
if [ `echo "$SWAPUtil>$SWAPThreshold"|bc` -eq 1 ]
then
echo "SWAP utilization is " $SWAPUtil " ,exceed the threshold" >> $CheckLog
echo "$SWAPUtil % = ($SWAPUsed/$SWAPTotal)*100%" >> $CheckLog
echo "" >> $CheckLog
fi
# Check the MemFree utilization if exceed the threshold
MemTotal=`free -m | head -2 | tail -1 | awk '{print $2}'`
MemFree=`free -m | head -3 | tail -1 | awk '{print $3}'`
MemPercentage=`expr $MemFree \* 100 / $MemTotal`
echo "Current MemFree utilization is : " $MemPercentage >> $LOGFILE
echo "Current MemFree utilization is : " $MemPercentage >> $TmpOut
echo "$MemPercentage % = ($MemFree/$MemTotal)*100%" >> $LOGFILE
echo "" >> $LOGFILE
if [ `echo "$MemPercentage>$MEMThreshold"|bc` -eq 1 ]
then
echo "MemFree utilization is " $MemPercentage " ,exceed the threshold" >> $CheckLog
echo "$MemPercentage % = ($MemFree/$MemTotal)*100%" >> $CheckLog
echo "" >> $CheckLog
fi
# Check the MemCached utilization if exceed the threshold
MemTotal=`free -m | head -2 | tail -1 | awk '{print $2}'`
MemCached=`free -m | head -2 | tail -1 | awk '{print $7}'`
MemCachedPercentage=`expr $MemCached \* 100 / $MemTotal`
echo "Current MemCached utilization is : " $MemCachedPercentage >> $LOGFILE
echo "Current MemCached utilization is : " $MemCachedPercentage >> $TmpOut
echo "$MemCachedPercentage % = ($MemCached/$MemTotal)*100%" >> $LOGFILE
echo "" >> $LOGFILE
#if [ `echo "$MemCachedPercentage>$MemCachedThreshold"|bc` -eq 1 ]
#then
#echo "MemCached utilization is " $MemCachedPercentage " ,exceed the threshold" >> $CheckLog
#echo "$MemCachedPercentage % = ($MemCached/$MemTotal)*100%" >> $CheckLog
#echo "" >> $CheckLog
#fi
# Check the ProcessCPU Maximum utilization if exceed the threshold
ps aux | grep -v "ps aux" | grep -v "oscheck" | grep -v USER | sort -nr -k 3 | head -1 > Process_CPU_Max
ProcessCPU=`cat Process_CPU_Max | awk '{print $3}' | cut -f 1 -d "."`
if [ $ProcessCPU -gt $ProcessCPUThreshold ]; then
echo "ProcessCPU Maximum utilization is " $ProcessCPU " ,exceed the threshold" >> $CheckLog
ps aux | head -1 >> $CheckLog
cat Process_CPU_Max >> $CheckLog
echo "" >> $CheckLog
fi
# Check the ProcessMEM Maximum utilization if exceed the threshold
ps aux | grep -v "ps aux" | grep -v "oscheck" | grep -v USER | sort -nr -k 4 | head -1 > Process_MEM_Max
ProcessMEM=`cat Process_MEM_Max | awk '{print $4}' | cut -f 1 -d "."`
if [ $ProcessMEM -gt $ProcessMEMThreshold ]; then
echo "ProcessMEM Maximum utilization is " $ProcessMEM " ,exceed the threshold" >> $CheckLog
ps aux | head -1 >> $CheckLog
cat Process_MEM_Max >> $CheckLog
echo "" >> $CheckLog
fi
# Check the CPU Max,Avg utilization if exceed the threshold
if [ -x /usr/bin/vmstat ] ; then
CPUMax=0
CPUTotal=0
for x in `vmstat 2 6 | tail -5 | awk '{print 100-$15}'`
do
if [ $x -gt $CPUMax ]; then
CPUMax="$x"
fi
CPUTotal=`expr $CPUTotal + $x`
done
CPUAvg=`expr $CPUTotal / 5`
echo "Current CPUMax utilization is : " $CPUMax >> $LOGFILE
echo "Current CPUMax utilization is : " $CPUMax >> $TmpOut
echo "" >> $LOGFILE
echo "Current CPUAvg utilization is : " $CPUAvg >> $LOGFILE
echo "Current CPUAvg utilization is : " $CPUAvg >> $TmpOut
echo "" >> $LOGFILE
if [ $CPUAvg -gt $CPUAvgThreshold ]; then
echo "CPUAvg utilization is " $CPUAvg " exceed the threshold" >> $CheckLog
echo "" >> $CheckLog
fi
if [ $CPUMax -gt $CPUMaxThreshold ]; then
echo "CPUMax utilization is " $CPUMax " exceed the threshold" >> $CheckLog
echo "" >> $CheckLog
fi
fi
# ***********************************************************************************************
# TmpOut of Check log
echo "" >> $TmpOut
echo " Top5: CPU Process : " >> $TmpOut
ps aux | head -1 >> $TmpOut
ps aux | grep -v USER | sort -nr -k 3 | head -5 >> $TmpOut
echo "" >> $TmpOut
echo "System Uptime : " `uptime | awk -F, '{print $1,$2}'` >> $TmpOut
echo "" >> $TmpOut
echo `ps -efl | wc -l` "processes are running" >> $TmpOut
echo "" >> $TmpOut
echo " Top5: Memory Process:" >> $TmpOut
ps aux | head -1 >> $TmpOut
ps aux | grep -v USER | sort -nr -k 4 | head -5 >> $TmpOut
echo "" >> $TmpOut
df -lhP >>$TmpOut 2>/dev/null
echo "" >> $TmpOut
# ***********************************************************************************************
# ***********************************************************************************************
# Add Part 5 for running local check scripts if available on 2015/08/18
#
echo "============================== Part 5 : Run Local Check scripts==============================" >>$LOGFILE
#if [ -f "/OSCheck/localchk.sh" ]
#then
#echo "Local check scripting is running " >> $LOGFILE
#chkcmd="/bin/sh localchk.sh $LOGFILE $CheckLog"
#eval $chkcmd
#echo "--- End of running of local check scripts " >> $LOGFILE
#fi
#================================================================================
# Check if the system configuration was changed!
if [ -f osconf.ref ]
then
diff $ConfFile osconf.ref > osconf.diff
if [ `cat osconf.diff | wc -l` -gt 0 ]
then
echo "The System Confiruation was changed, have a check!" >> $CheckLog
cat osconf.diff >> $CheckLog
fi
cp -f $ConfFile osconf.ref
else
cp -f $ConfFile osconf.ref
fi
echo "Checking finished successfully!" >>$LOGFILE
echo "" >>$LOGFILE
echo "============================== End  $(date +%Y"."%m"."%d" "%k":"%M":"%S) =================================" >> $LOGFILE
echo "Checking finished successfully!"

D:\OScheck\ServerList目录下是服务器列表,根据服务器重要性,可分成每日、周、月巡检

服务器列表文件格式(ip,端口,主机名,用户名,密码)如下:

 

双击执行DAILYCheck.bat文件,界面如下:

C001 Linux系统巡检脚本_第2张图片

 

你可能感兴趣的:(shell脚本)