1. 內存監(jiān)控腳本
#!/bin/bash
#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. 檢測網(wǎng)卡流量,并按規(guī)定格式記錄在日志中
#!/bin/bash
#######################################################
#檢測網(wǎng)卡流量耐床,并按規(guī)定格式記錄在日志中
#規(guī)定一分鐘記錄一次
#日志格式如下所示:
#2021-07-08 18:55
#eth0 input: 1234bps
#eth0 output: 1235bps
######################################################3
while :
do
#設置語言為英文密幔,保障輸出結果是英文,否則會出現(xiàn)bug
LANG=en
logfile=/tmp/`date +%d`.log
#將下面執(zhí)行的命令結果輸出重定向到logfile日志中
exec >> $logfile
date +"%F %H:%M"
#sar命令統(tǒng)計的流量單位為kb/s撩轰,日志格式為bps胯甩,因此要*1000*8
sar -n DEV 1 59|grep Average|grep eth0|awk '{print $2,"\t","input:","\t",$5*1000*8,"bps","\n",$2,"\t","output:","\t",$6*1000*8,"bps"}'
echo "####################"
#因為執(zhí)行sar命令需要59秒,因此不需要sleep
done
3. 監(jiān)測Nginx訪問日志502情況堪嫂,并做相應動作
假設服務器為lnmp環(huán)境偎箫,近期訪問經常出現(xiàn)502現(xiàn)象,且502錯誤在重啟php-fpm服務后消失皆串,因此編寫監(jiān)控腳本淹办,一旦出現(xiàn)502,則自動重啟php-fpm服務恶复。
#場景:
#1.訪問日志文件的路徑:/data/log/access.log
#2.腳本死循環(huán)怜森,每10秒檢測一次,10秒的日志條數(shù)為300條谤牡,出現(xiàn)502的比例不低于10%(30條)則需要重啟php-fpm服務
#3.重啟命令為:/etc/init.d/php-fpm restart
#!/bin/bash
###########################################################
#監(jiān)測Nginx訪問日志502情況副硅,并做相應操作
###########################################################
log=/data/log/access.log
N=30 #設定閾值
while :
do
#查看訪問日志的最新300條,并統(tǒng)計502的次數(shù)
err=`tail -n 300 $log |grep -c '502" '`
if [ $err -ge $N ]
then
/etc/init.d/php-fpm restart 2> /dev/null
#設定60s延遲防止腳本bug導致無限重啟php-fpm服務
sleep 60
fi
sleep 10
done
4. 掃描主機端口狀態(tài)
#!/bin/bash
HOST=$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/bash
#####################################
#檢測兩臺服務器指定目錄下的文件一致性
#####################################
#通過對比兩臺服務器上文件的md5值翅萤,達到檢測一致性的目的
dir=/data/web
b_ip=192.168.88.10
#將指定目錄下的文件全部遍歷出來并作為md5sum命令的參數(shù)恐疲,進而得到所有文件的md5值,并寫入到指定文件中
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 /tmp
#將文件名作為遍歷對象進行一一比對
for f in `awk '{print 2} /tmp/md5_a.txt'`
do
#以a機器為標準,當b機器不存在遍歷對象中的文件時直接輸出不存在的結果
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}'`
#當文件存在時培己,如果md5值不一致則輸出文件改變的結果
if [ $md5_a != $md5_b ]
then
echo "$f changed."
fi
else
echo "$f deleted."
fi
done
6. 定時清空文件內容碳蛋,定時記錄文件大小
#!/bin/bash
################################################################
#每小時執(zhí)行一次腳本(任務計劃),當時間為0點或12點時漱凝,將目標目錄下的所有文件內容清空疮蹦,但不刪除文件,其他時間則只統(tǒng)計各個文件的大小茸炒,一個文件一行愕乎,輸出到以時間和日期命名的文件中,需要考慮目標目錄下二級壁公、三級等子目錄的文件
################################################################
logfile=/tmp/`date +%H-%F`.log
n=`date +%H`
if [ $n -eq 00 ] || [ $n -eq 12 ]
then
#通過for循環(huán)感论,以find命令作為遍歷條件,將目標目錄下的所有文件進行遍歷并做相應操作
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. 查看局域網(wǎng)內主機是否存活
#!/usr/bin/bash
# check host status
for i in {1..254}
do
{
ip=192.168.8.$i
ping -c 2 -W 1 $ip &>/dev/null
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/null
fi
}&
done
wait
8. 自動應答分發(fā)免密登錄
#!/usr/bin/bash
# ssh keygen
>ip_ok.txt
>ip_false.txt
user=root
passwd=123456
rpm -qa | grep expect &>/dev/null
if [ $? -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/bash
# code online
# author: ren
PROJT_DIR=/usr/local/nginx/html
OLD_DIR=/usr/local/nginx/html/web1
PROJT=web1
BACKUP_DIR=/data/backup
DATA_CHMOD=www
DATE=`date +%F`
NEW_DIR=/data/web1
# 關閉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 備份原有數(shù)據(jù)
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移動新的代碼 項目目錄 注:此代碼目錄需手動上傳解壓
function new_code() {
if [ -d $NEW_DIR ];then
mv $NEW_DIR $PROJT_DIR
else
echo "NEW_DIR is not exist"
exit 3
fi
}
# 4 修改權限
function chmod_news() {
chown -R $DATA_CHMOD.$DATA_CHMOD $OLD_DIR
}
# 5 啟動服務
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主從復制是否異常
#!/bin/bash
user="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數(shù)據(jù)庫備份腳本(mysqldump)
#!/bin/bash
#刪除15天以前備份
source /etc/profile #加載系統(tǒng)環(huán)境變量
source ~/.bash_profile #加載用戶環(huán)境變量
set -o nounset #引用未初始化變量時退出
#set -o errexit #執(zhí)行shell命令遇到錯誤時退出
user="root"
password="123456"
host="localhost"
port="3306"
#需備份的數(shù)據(jù)庫紊册,數(shù)組
db=("test")
#備份時加鎖方式比肄,
#MyISAM為鎖表--lock-all-tables,
#InnoDB為鎖行--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"
#建立備份目錄
if [ ! -e $backup_path ];then
mkdir -p $backup_path
fi
#刪除以前備份
find $backup_path -type f -mtime +$day -exec rm -rf {} \; > /dev/null 2>&1
echo "開始備份數(shù)據(jù)庫:${db[*]}"
#備份并壓縮
backup_sql(){
dbname=$1
backup_name="${dbname}_${date}.sql"
#-R備份存儲過程囊陡,函數(shù)芳绩,觸發(fā)器
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_name.tar.gz -sh | awk '{print $1}')
rm -rf $backup_name
echo "$date 備份 $dbname($size) 成功 "
else
cd $backup_path
rm -rf $backup_name
echo "$date 備份 $dbname 失敗 "
fi
}
#循環(huán)備份
length=${#db[@]}
for (( i = 0; i < $length; i++ )); do
backup_sql ${db[$i]} >> $backup_log 2>&1
done
echo "備份結束,結果查看 $backup_log"
du $backup_path/*$date* -sh | awk '{print "文件:" $2 ",大小:" $1}'