linux常用技巧

1,备份家目录 #!/bin/bashBACKUP_DIR=/share/nas5/wanghj/dataTIMESTAMP=$(date +%Y%m%d%H%M%S)tar -zcvf $BACKUP_DIR/backup_$TIMESTAMP.tar.gz /home/wanghj 2,监控系统内存,i/o及存储等...

1,备份家目录

#!/bin/bash
BACKUP_DIR=/share/nas5/wanghj/data
TIMESTAMP=$(date +%Y%m%d%H%M%S)
tar -zcvf $BACKUP_DIR/backup_$TIMESTAMP.tar.gz /home/wanghj

2,监控系统内存,i/o及存储等

#!/bin/bash
# Set the threshold values
DISK_THRESHOLD=90
LOAD_THRESHOLD=20
MEMORY_THRESHOLD=50
IO_THRESHOLD=50
# User to write the message
USERNAME="wanghj"
# Log file
LOGFILE="/home/wanghj/logfile.log"
while true; do
    # Check disk usage
    DISK_USAGE=$(df /share/backup01/DATA/ | grep / | awk '{ print $5}' | sed 's/%//g')
    if [ "$DISK_USAGE" -gt "$DISK_THRESHOLD" ] ; then
        DISK_MESSAGE="Your root partition remaining free space is critically low. Used: $DISK_USAGE%"
        echo "$(date) - $DISK_MESSAGE" >> $LOGFILE
        echo "$DISK_MESSAGE" | write $USERNAME
    else
        DISK_MESSAGE="Your root partition disk usage is normal. Used: $DISK_USAGE%"
        echo "$(date) - $DISK_MESSAGE" >> $LOGFILE
    fi
    # Check system load
    LOAD=$(uptime | awk -F'load average: ' '{ print $2 }' | cut -d, -f1)
    if (( $(echo "$LOAD > $LOAD_THRESHOLD" |bc -l) )); then
        LOAD_MESSAGE="High load on server. Current load is: $LOAD"
        echo "$(date) - $LOAD_MESSAGE" >> $LOGFILE
        echo "$LOAD_MESSAGE" | write $USERNAME
    else
        LOAD_MESSAGE="Server load is normal. Current load is: $LOAD"
        echo "$(date) - $LOAD_MESSAGE" >> $LOGFILE
    fi
    # Check memory usage
    MEMORY_USAGE=$(free | grep Mem | awk '{print $3/$2 * 100.0}')
    if (( $(echo "$MEMORY_USAGE > $MEMORY_THRESHOLD" |bc -l) )); then
        MEMORY_MESSAGE="High memory usage on server. Current usage is: $MEMORY_USAGE%"
        echo "$(date) - $MEMORY_MESSAGE" >> $LOGFILE
        echo "$MEMORY_MESSAGE" | write $USERNAME
    else
        MEMORY_MESSAGE="Server memory usage is normal. Current usage is: $MEMORY_USAGE%"
        echo "$(date) - $MEMORY_MESSAGE" >> $LOGFILE
    fi
    # Check IO usage
    IO_USAGE=$(iostat -dx | awk 'NR==4 {print $14}')
    if (( $(echo "$IO_USAGE > $IO_THRESHOLD" |bc -l) )); then
        IO_MESSAGE="High I/O usage on server. Current usage is: $IO_USAGE%"
        echo "$(date) - $IO_MESSAGE" >> $LOGFILE
        echo "$IO_MESSAGE" | write $USERNAME
    else
        IO_MESSAGE="Server I/O usage is normal. Current usage is: $IO_USAGE%"
        echo "$(date) - $IO_MESSAGE" >> $LOGFILE
    fi
    # Wait for 10 minutes
    sleep 600
done

可以把该脚本用&挂在后台,该脚本可以让系统资源异常占用或磁盘快满的时候通知你

attachments-2023-05-InrT4siT645dd3291206c.png


  • 发表于 2023-05-12 13:43
  • 阅读 ( 839 )
  • 分类:linux

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
xun
xun

电路元件工程师

82 篇文章

作家榜 »

  1. omicsgene 702 文章
  2. 安生水 351 文章
  3. Daitoue 167 文章
  4. 生物女学霸 120 文章
  5. xun 82 文章
  6. rzx 78 文章
  7. 红橙子 78 文章
  8. CORNERSTONE 72 文章