一、實(shí)現(xiàn)磁盤分區(qū)的
只支持分配主分區(qū)和標(biāo)準(zhǔn)的linux文件系統(tǒng)(ext4/xfs)的分區(qū)
#!/bin/bash #Function:對(duì)硬盤進(jìn)行分區(qū),得到一個(gè)標(biāo)準(zhǔn)的linux文件系統(tǒng)(ext4/xfs)的主分區(qū) cat/proc/partitions>old read-p"請(qǐng)輸入你要分區(qū)的硬盤(寫絕對(duì)路徑,如:/dev/sda):"A if[-e$A];then echo"true" else echo"該設(shè)備不存在??!" exit fi read-p"請(qǐng)輸入你要?jiǎng)?chuàng)建的磁盤分區(qū)類型(這里只能是主分區(qū),默認(rèn)按回車即可):"B read-p"請(qǐng)輸入分區(qū)數(shù)字,范圍1-4,默認(rèn)從1開(kāi)始,默認(rèn)按回車即可:"C read-p"請(qǐng)輸入扇區(qū)起始表號(hào),默認(rèn)按回車即可:"D read-p"請(qǐng)輸入你要分區(qū)的分區(qū)大小(格式:如+5G ):"E fdisk$A<new F=`diffnewold|grep"<"?|?awk?'{print?$5}'` echo?"-------------------------------" echo?$F echo?"你想對(duì)新分區(qū)設(shè)定什么類型的文件系統(tǒng)?有以下選項(xiàng):" echo?"A:ext4文件系統(tǒng)" echo?"B:xfs文件系統(tǒng)" read?-p?"請(qǐng)輸入你的選擇:"?G case?$G?in ????????a|A) ???????????mkfs.ext4?/dev/$F ???????????echo?"該分區(qū)將被掛載在?"/mnt/$F"?下"? ???????????m=`ls?/mnt/|grep?$F?|?wc?-l` ???????????if?[?$m?-eq?0?];then ????????????mkdir?/mnt/$F ???????????fi ???????????n=`cat?/etc/fstab?|?grep?/dev/$F|?wc?-l` ???????????if?[?$n?-eq?0?];then ??????????????echo?"/dev/$F?????/mnt/$F?????ext4?????????defaults??????????0??????0"?>>/etc/fstab else sed-i'/^/dev/$F/c/dev/$F/mnt/$Fext4defaults00'/etc/fstab fi mount-a df-Th ;; b|B) mkfs.xfs-f/dev/$F echo"該分區(qū)將被掛載在"/mnt/$F"下" m=`ls/mnt/|grep$F|wc-l` if[$m-eq0];then mkdir/mnt/$F fi n=`cat/etc/fstab|grep/dev/$F|wc-l` if[$n-eq0];then echo"/dev/$F/mnt/$Fxfsdefaults00">>/etc/fstab else sed-i'/^/dev/$F/c/dev/$F/mnt/$Fxfsdefaults00'/etc/fstab fi mount-a df-Th ;; *) echo"你的輸入有誤??!" esac
二、服務(wù)器初始化
#/bin/bash # 設(shè)置時(shí)區(qū)并同步時(shí)間 ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime if ! crontab -l |grep ntpdate &>/dev/null ; then (echo "* 1 * * * ntpdate time.windows.com >/dev/null 2>&1";crontab -l) |crontab fi # 禁用selinux sed -i '/SELINUX/{s/permissive/disabled/}' /etc/selinux/config # 關(guān)閉防火墻 if egrep "7.[0-9]" /etc/redhat-release &>/dev/null; then systemctl stop firewalld systemctl disable firewalld elif egrep "6.[0-9]" /etc/redhat-release &>/dev/null; then service iptables stop chkconfig iptables off fi # 歷史命令顯示操作時(shí)間 if ! grep HISTTIMEFORMAT /etc/bashrc; then echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/bashrc fi # SSH超時(shí)時(shí)間 if ! grep "TMOUT=600" /etc/profile &>/dev/null; then echo "export TMOUT=600" >> /etc/profile fi # 禁止root遠(yuǎn)程登錄 sed -i 's/#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config # 禁止定時(shí)任務(wù)向發(fā)送郵件 sed -i 's/^MAILTO=root/MAILTO=""/' /etc/crontab # 設(shè)置最大打開(kāi)文件數(shù) if ! grep "* soft nofile 65535" /etc/security/limits.conf &>/dev/null; then cat >> /etc/security/limits.conf << EOF * soft nofile 65535 * hard nofile 65535 EOF fi # 系統(tǒng)內(nèi)核優(yōu)化 cat >> /etc/sysctl.conf << EOF net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_tw_buckets = 20480 net.ipv4.tcp_max_syn_backlog = 20480 net.core.netdev_max_backlog = 262144 net.ipv4.tcp_fin_timeout = 20 EOF # 減少SWAP使用 echo "0" > /proc/sys/vm/swappiness # 安裝系統(tǒng)性能分析工具及其他 yum install gcc make autoconf vim sysstat net-tools iostat if
三、查看網(wǎng)卡實(shí)時(shí)流量
#!/bin/bash NIC=$1 echo -e " In ------ Out" while true; do OLD_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev) OLD_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev) sleep 1 NEW_IN=$(awk '$0~"'$NIC'"{print $2}' /proc/net/dev) NEW_OUT=$(awk '$0~"'$NIC'"{print $10}' /proc/net/dev) IN=$(printf "%.1f%s" "$((($NEW_IN-$OLD_IN)/1024))" "KB/s") OUT=$(printf "%.1f%s" "$((($NEW_OUT-$OLD_OUT)/1024))" "KB/s") echo "$IN $OUT" sleep 1 done
四、nginx日志按天切割
#!/bin/bash LOG_DIR=/usr/local/nginx/logs YESTERDAY_TIME=$(date -d "yesterday" +%F) LOG_MONTH_DIR=$LOG_DIR/$(date +"%Y-%m") LOG_FILE_LIST="default.access.log" for LOG_FILE in $LOG_FILE_LIST; do [ ! -d $LOG_MONTH_DIR ] && mkdir -p $LOG_MONTH_DIR mv $LOG_DIR/$LOG_FILE $LOG_MONTH_DIR/${LOG_FILE}_${YESTERDAY_TIME} done kill -USR1 $(cat /usr/local/nginx/nginx.pid)
五、nginxacc日志分析
#!/bin/bash # 日志格式: $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" LOG_FILE=$1 echo "統(tǒng)計(jì)訪問(wèn)最多的10個(gè)IP" awk '{a[$1]++}END{print "UV:",length(a);for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr |head -10 echo "----------------------" echo "統(tǒng)計(jì)時(shí)間段訪問(wèn)最多的IP" awk '$4>="[01/Dec/201820:25" && $4<="[27/Nov/201820:49"{a[$1]++}END{for(v in a)print v,a[v]}' $LOG_FILE |sort -k2 -nr|head -10 echo "----------------------" echo "統(tǒng)計(jì)訪問(wèn)最多的10個(gè)頁(yè)面" awk '{a[$7]++}END{print "PV:",length(a);for(v in a){if(a[v]>10)print v,a[v]}}' $LOG_FILE |sort -k2 -nr echo "----------------------" echo "統(tǒng)計(jì)訪問(wèn)頁(yè)面狀態(tài)碼數(shù)量" awk '{a[$7" "$9]++}END{for(v in a){if(a[v]>5) print v,a[v]}}'
六、監(jiān)控服務(wù)器磁盤利用率
#!/bin/bash HOST_INFO=host.info # 主機(jī)列表,實(shí)現(xiàn)采集多臺(tái)主機(jī)信息,需要能夠ssh到目標(biāo)主機(jī) for IP in $(awk '/^[^#]/{print $1}' $HOST_INFO); do USER=$(awk -v ip=$IP 'ip==$1{print $2}' $HOST_INFO) PORT=$(awk -v ip=$IP 'ip==$1{print $3}' $HOST_INFO) TMP_FILE=/tmp/disk.tmp ssh -p $PORT $USER@$IP 'df -h' > $TMP_FILE USE_RATE_LIST=$(awk 'BEGIN{OFS="="}/^/dev/{print $NF,int($5)}' $TMP_FILE) for USE_RATE in $USE_RATE_LIST; do PART_NAME=${USE_RATE%=*} USE_RATE=${USE_RATE#*=} if [ $USE_RATE -ge 80 ]; then echo "Warning: $PART_NAME Partition usage $USE_RATE%!" fi done done
七、mysql備份
#!/bin/bash DATE=$(date +%F_%H-%M-%S) HOST=localhost USER=backup PASS=passwd BACKUP_DIR=/data/db_backup DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys") for DB in $DB_LIST; do BACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE} [ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/null TABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null) for TABLE in $TABLE_LIST; do BACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sql if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; then echo "$BACKUP_NAME 備份失敗!" fi done done
八、統(tǒng)計(jì)進(jìn)程數(shù),找出并kill掉僵尸進(jìn)程
#!/bin/ bash ALL_PROCESS=$(1s /proc/ l egrep "[0-9]+") running_count=0 stoped_count=0s1eeping_count=0zombie_count=O for pid in ${ALL_PROCESS[*]}do test -f /proc/$pid/status && state=$(egrep "state"/proc/$pid/status / awk'{print $2}') case ""$state" in R) running_count=$ ((running_count+1)) ; T) stoped_count=$((stoped_count+1)) ;; s) sleeping_count=$((sleeping_count+1)); z) zombie_count=$( (zombie_count+1)) echo "$pid" >>zombie.txt kill -9 "$pid" ;; esacdone echo -e "total: $((running_count+stoped_count+sleeping_count+zombie_count)) running:$running_count stoped: $stoped_count sleeping: $sleeping_count zombie:$zombie_count"
九、封禁大量惡意訪問(wèn)的IP
#!/bin/bash DATE=$(date +%d/%b/%Y:%H:%M) LOG_FILE=/usr/local/nginx/logs/access.log ABNORMAL_IP=$(tail -n10000 $LOG_FILE |grep $DATE |awk '{a[$1]++}END{for(i in a)if(a[i]>10)print i}') for IP in $ABNORMAL_IP; do if [ $(iptables -vnL |grep -c "$IP") -eq 0 ]; then iptables -I INPUT -s $IP -j DROP echo "$(date +'%F_%T') $IP" >> /tmp/drop_ip.log fi done
審核編輯:湯梓紅
-
Linux
+關(guān)注
關(guān)注
87文章
11304瀏覽量
209535 -
服務(wù)器
+關(guān)注
關(guān)注
12文章
9165瀏覽量
85436 -
磁盤
+關(guān)注
關(guān)注
1文章
379瀏覽量
25209 -
文件系統(tǒng)
+關(guān)注
關(guān)注
0文章
284瀏覽量
19911 -
nginx
+關(guān)注
關(guān)注
0文章
149瀏覽量
12176
原文標(biāo)題:linux運(yùn)維拿來(lái)即用序列~
文章出處:【微信號(hào):良許Linux,微信公眾號(hào):良許Linux】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論