1. ホーム
  2. スクリプト・コラム
  3. リナックスシェル

シェルユーティリティスクリプトを仕事で使う

2022-01-05 05:06:28

1. メモリ監視スクリプト

#! /bin#memory use
mem_war_file=/tmp/mem_war.txt
mem_use=`free -m | grep Mem | awk '{print $3}'`
mem_total=`free -m | grep Mem | awk '{print $2}'`
mem_percent=$((mem_use*100/mem_total))
# echo "$mem_percent"%
if (($mem_percent > 80));then
   echo "`date +%F-%H-%M` mem: ${mem_percent}%" >$mem_war_file
   echo "`date +%F-%H-%M` mem: ${mem_percent}%" | mail -s "mem warning" root 
fi


2. NICのトラフィックを検出し、所定のフォーマットでログに記録する。

#! /bin#######################################################
#Detect NIC traffic and record it in the log in the specified format
#Logging once a minute as specified
#The log format is as follows:
#2021-07-08 18:55
#eth0 input: 1234bps
#eth0 output: 1235bps
######################################################3
while :
do
#set the language to English to ensure the output is in English, otherwise there will be a bug
LANG=en
logfile=/tmp/`date +%d`.log
#Redirect the output of the following command to the logfile log
exec >> $logfile
date +"%F %H:%M"
#The sar command counts traffic in kb/s, and the log format is bps, so it should be *1000*8
sar -n DEV 1 59|grep Average|grep eth0|awk '{print $2,"\t","input:","\t",$5*1000*8,"bps","\n& quot;,$2,"\t","output:","\t",$6*1000*8,"bps"}'
echo "####################"
#Sleep is not needed because it takes 59 seconds to execute the sar command
done


3. Nginx のアクセスログで 502 の状態を監視し、それに応じて対処する。
/{br

サーバーが lnmp 環境で、最近のアクセスではよく 502 が表示され、php-fpm サービスを再起動すると 502 エラーが消えると仮定して、502 が発生したら php-fpm サービスを自動的に再起動する監視スクリプトを作成します。

#scenarios.
#1. path to access log file: /data/log/access.log
#2. The script is in a dead loop, detected every 10 seconds, the number of logs in 10 seconds is 300, and the percentage of 502 is not less than 10% (30), then the php-fpm service needs to be restarted
#3. The restart command is: /etc/init.d/php-fpm restart
#! /bin###########################################################
#Monitor the Nginx access log for 502 conditions and do the appropriate actions
###########################################################
log=/data/log/access.log
N=30 #Set the threshold
while :
do
 #Check the latest 300 access logs and count the number of 502s
    err=`tail -n 300 $log |grep -c '502" '`
 if [ $err -ge $N ]
 then
 /etc/init.d/php-fpm restart 2> /dev # Set a 60s delay to prevent script bugs from causing unlimited restarts of the php-fpm service
     sleep 60
 sleep 60
 sleep 10
done


4. ホストポートの状態をスキャンする

#! /binHOST=$1
PORT="22 80 8080 3306"
for PORT in $PORT; do
    if echo &>/dev/null > /dev/tcp/$HOST/$PORT; then
        echo "$PORT open"
    else
        echo "$PORT close"
    fi
done


5.両サーバーでディレクトリ内のファイルの整合性を確認する。

#! /bin#####################################
# Check the consistency of the files in the specified directory on both servers
#####################################
#Check for consistency by comparing the md5 values of files on both servers
dir=/datab_ip=192.168.88.10
#Inspect all the files in the specified directory and use them as arguments to the md5sum command to get the md5 values of all the files and write them to the specified file
find $dir -type f|xargs md5sum > /tmp/md5_a.txt
ssh $b_ip "find $dir -type f|xargs md5sum > /tmp/md5_b.txt"
scp $b_ip:/tmp/md5_b.txt # Compare file names one by one as traversal objects
for f in `awk '{print 2} /tmp/md5_a.txt'`
do
# Use machine a as the standard, when machine b does not exist in the traversal object file directly output the non-existent results
if grep -qw "$f" /tmp/md5_b.txt
then
md5_a=`grep -w "$f" /tmp/md5_a.txt|awk '{print 1}'`
md5_b=`grep -w "$f" /tmp/md5_b.txt|awk '{print 1}'`
# Output file changes if md5 values don't match when file exists
if [ $md5_a ! = $md5_b ]
then
echo "$f changed."
fi
else
echo "$f deleted."
" fi
done


6. ファイル内容の時間的な空白、ファイルサイズの時間的な記録

#! /bin################################################################
# Execute the script once an hour (task schedule), when the time is 0:00 or 12:00, empty the contents of all files in the target directory, but do not delete files, other times, only count the size of individual files, a file line, output to a file named with the time and date, you need to consider the target directory under the second level, third level and other subdirectories of files
################################################################
logfile=/tmp/`date +%H-%F`.log
n=`date +%H`
if [ $n -eq 00 ] || [ $n -eq 12 ]
then
# For loop, use the find command as a traversal condition to traverse all files in the target directory and do the corresponding operation
for i in `find /data/log/ -type f`
do
true > $i
done
else
for i in `find /data/log/ -type f`
do
du -sh $i >> $logfile
done
fi


7. LAN上のホストが生きているかどうかを確認する

#! /usr/bin# check host status
for i in {1..254}
do
        {
    ip=192.168.8.$i
    ping -c 2 -W 1 $ip &>/dev    if [ $? -eq 0 ];then
        echo "$ip is online" | tee -a /tmp/host_online.txt
    else
       # echo "$ip is offline" | tee -a /tmp/host_offline.txt
       echo "$ip is offline" &>/dev    fi
        }&
done
wait


8.自動応答でパスワード不要のログインを配布

#! /usr/bin# ssh keygen

>ip_ok.txt
>ip_false.txt
user=root
passwd=123456

rpm -qa | grep expect &>/devif [ $? -ne 0 ];then
  echo "expect is not install"
  yum -y install expect
fi

if [ ! -f ~/.ssh/id_rsa ];then
  ssh-keygen -P "" -f ~/.ssh/id_rsa
fi

for i in {15..30}
do
  {
  ip=192.168.1."$i"
  ping -c 1 -W1 "$ip"
  if [ $? -eq 0 ];then
     echo "$ip" >> ip_ok.txt
     /usr/bin/expect <<-EOF
     spawn ssh-copy-id $user@$ip
     expect {
        "yes/no" { send "yes\r"; exp_continue }
        "password:" { send "$passwd\r" }
     }
     expect eof
        EOF
  else
    echo "$ip" >>ip_false.txt
  fi
  }&
done
wait
echo "finish"



9.コードライブスクリプト

#! /bin# code online
# author: ren
PROJT_DIR=/usr/local/nginxOLD_DIR=/usr/local/nginx/htmlPROJT=web1
BACKUP_DIR=/dataDATA_CHMOD=www
DATE=`date +%F`
NEW_DIR=/data# Shutdown nginx
function stop_nginx() {
  /usr/bin/systemctl stop nginx
  if [ $? -eq 0 ];then
     echo "nginx is stopd"
  else
     echo "nginx is not stop please check... "
     exit 1
  fi
}

#2 Back up the original data
function backup_data() {
  if [ -d $BACKUP_DIR/$DATE'-'$PROJT ];then
    echo "DIR $BACKUP_DIR/$DATE'-'$PROJT is exist"
    exit 2
  else
    mv $OLD_DIR $BACKUP_DIR/$DATE'-'$PROJT
  fi
}

# 3 Move the new code project directory Note: This code directory needs to be manually uploaded and unpacked 
function new_code() {
  if [ -d $NEW_DIR ];then
     mv $NEW_DIR $PROJT_DIR
  else
     echo "NEW_DIR is not exist"
     exit 3
  fi
}

# 4 Modify permissions
function chmod_news() {
  chown -R $DATA_CHMOD.$DATA_CHMOD $OLD_DIR
}

# 5 Start the service

function start_nginx() {
  /usr/bin/systemctl start nginx
  if [ $? -eq 0 ];then
    echo "nginx start ok"
  else
    echo "ngin is not start,please check... "
  fi
}

stop_nginx
backup_data
new_code
chmod_news
start_nginx



10. MySQL マスタースレーブレプリケーションの例外を検出する

#! /binuser="root"
password="123456"
mycmd="mysql -u$user -p$password -h 192.168.1.88"

function chkdb() {
list=($($mycmd -e "show slave status \G"|egrep "Running|Behind"|awk -F: '{print $2}'))
if [ ${list[0]} = "Yes" -a ${list[1]} = "Yes" -a ${list[2]} -lt 120 ]
then echo "Mysql slave is ok"
else echo "Mysql slave replation is filed"
fi
}

function main() {
while true
do chkdb
   sleep 3
done
}
main



11. MySQLデータベースバックアップスクリプト(mysqldump)

#! /bin#Delete 15-day old backups

source /etc/profile #Load system environment variables
source ~/.bash_profile #Load user environment variables
set -o nounset #Exit when referencing uninitialized variables
#set -o errexit #Exit if shell command encounters an error

user="root"
password="123456"
host="localhost"
port="3306"
#Database to be backed up, array
db=("test")
#Lock method for backup.
#MyISAM for locking tables --lock-all-tables
#InnoDB is lock-row-single-transaction
lock="--single-transaction"
mysql_path="/usr/local/mysql"
backup_path="${mysql_path}/backup"
date=$(date +%Y-%m-%d_%H-%M-%S)
day=15
backup_log="${mysql_path}/backup.log"

# Create backup directory
if [ ! -e $backup_path ];then
    mkdir -p $backup_path
fi

#Delete previous backups
find $backup_path -type f -mtime +$day -exec rm -rf {} \; > /dev/null 2>&1

echo "Start backup database: ${db[*]}"

#Backup and compress
backup_sql(){
    dbname=$1
    backup_name="${dbname}_${date}.sql"
    # -R backup stored procedures, functions, triggers
    mysqldump -h $host -P $port -u $user -p$password $lock --default-character-set=utf8 --flush-logs -R $dbname > $backup_path/$backup_name    
    if [[ $? == 0 ]];then
        cd $backup_path
        tar zcpvf $backup_name.tar.gz $backup_name
        size=$(du $backup