1. 程式人生 > >簡單的linux伺服器巡檢指令碼

簡單的linux伺服器巡檢指令碼

背景:

這篇文章主要介紹了根據公司需求寫的一個linux 巡檢小指令碼,可以用來檢查伺服器的一些執行狀況,需要的朋友可以參考下。

巡檢的基本步驟:

 1.在每臺伺服器上部署巡檢的指令碼,查詢相應的日誌。

 2.將每臺伺服器上的日誌傳送到ftp伺服器的指定目錄下。

 3.遍歷ftp伺服器指定目錄,並且將各個的檔案資訊整理到一個檔案中。

 4.將整理後的檔案通過郵件傳送給指定的人員。

5.刪除冗餘的日誌檔案。

對應的指令碼:

1.巡檢的指令碼:

##############################################start########################################################################
#!/bin/sh

hostname=`hostname`
date=`date +%c`
filename=`hostname`_check_`date -d '-1 day' +%Y%m%d`.txt
tempfile="/tmp/$filename"
if [ ! -f "$tempfile" ];then
  touch "$tempfile"
fi
echo "----------------daily check begin-------">>$tempfile
echo "-----------------$hostname--------------------------" >>$tempfile
echo "-----------磁碟使用情況-----------------------------" >>$tempfile
df -h >>$tempfile

echo "----------記憶體使用情況-----------------------------" >>$tempfile
free -m >>$tempfile

echo "----------cpu佔用情況----------------------------------" >>$tempfile
top >>$tempfile

#cd /var/log/nginx
cd /data/log/nginx/`date -d '-1 day' +%Y%m%d`



myfile1="hotapps_access.log"
myfile2="hotapps-ssl_access.log"
myfile3="hotapps_access.log.gz"
myfile4="hotapps-ssl_access.log.gz"


echo "--------nginx請求總量---------------------------- ">>$tempfile
if [ -f "$myfile1" ]; then
wc -l $myfile1 $myfile2 >>$tempfile
#gzip $myfile1;
fi
#if [ -f "$myfile2" ]; then
#gzip $myfile2;
#fi
if [ -f "$myfile3" ]; then
zcat  $myfile3  $myfile4  |wc -l >>$tempfile
fi

echo "--------nginx狀態碼------------------------- ">>$tempfile 
if [ -f "$myfile1" ]; then
awk ' {print $2}' $myfile1 $myfile2 | sort | uniq -c | sort  -rn >>$tempfile
#gzip $myfile1
fi
if [ -f "$myfile3" ]; then
zcat  $myfile3   $myfile4 |awk ' {print $2}'|sort | uniq -c | sort  -rn >>$tempfile
fi

cd /data/log/jetty

echo "---------jetty錯誤日誌----------------------------" >>$tempfile
myfile5="googleinstaller-server.log.`date -d '-1 day' +%Y%m%d`"
myfile6="googleinstaller-server.log.`date -d '-1 day' +%Y%m%d`.gz"
if [ -f "$myfile5" ]; then
awk -v line=0 '{if (line==1) print($0); if ($3=="ERROR") {line=1;} else {line=0;}}' $myfile5 |sort | uniq -c | sort  -rn >>$tempfile
#gzip $myfile5
fi
if [ -f "$myfile6" ]; then
zcat $myfile6 |awk -v line=0 '{if (line==1) print($0); if ($3=="ERROR") {line=1;} else {line=0;}}' |sort | uniq -c | sort  -rn >>$tempfile
fi


echo "---------daily check end -------------------------" >>$tempfile

#rm $tempfile
#####################################end############################################################

2.將伺服器的日誌傳送到ftp伺服器上。

############################start###############################
LOFFILE="/opt/server_check_log/ftp.log"
ftp -n >>$LOFFILE <<EOF
open 此處為ftp伺服器的ip
user ftp伺服器的使用者名稱稱  ftp伺服器的密碼
binary
cd /opt/server_check_log
put test.txt
bye
EOF
 ########################指令碼結束###########################

3.將指定目錄下的所有的檔案整理為一個檔案,方便閱讀

##############################################start#########################################
#!bin/bash
#print the directory and file

for file in /opt/server_check_log/log/*
do

   if test -f $file
   then
      #echo "$file is file"
      cat  $file >> /opt/server_check_log/getAll.txt

   fi
done
###########################################end#############################################

4.通過郵件傳送正在開發當中。。。。。。。。。。。。

5.刪除冗餘日誌:

#!/bin/sh

date=`date +%c`
filename=`hostname`_check_`date -d '-1 day' +%Y%m%d`.txt
tempfile="/tmp/$filename"

rm -rf $tempfile

6.cron表示式,所有的指令碼都通過cron,定時任務來進行排程

00 02 * * * /tmp/server_daily_check.sh  restart
00 14 * * * /tmp/delete_server_daily_check.sh restart