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

--------------------- 
作者:我是李果 
来源:CSDN 
原文:https://blog.csdn.net/li12412414/article/details/79418740 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值