Различия
Показаны различия между двумя версиями страницы.
| linux:bash:df_alert [2018/06/28 22:39] – внешнее изменение 127.0.0.1 | linux:bash:df_alert [2021/10/12 02:48] (текущий) – dx | ||
|---|---|---|---|
| Строка 1: | Строка 1: | ||
| + | ====== Уведомление о свободном месте (df) по крону ====== | ||
| + | ==== Вариант 1 ==== | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | # www.fduran.com | ||
| + | # script that will send an email to EMAIL when disk use in partition PART is bigger than %MAX | ||
| + | # adapt these 3 parameters to your case | ||
| + | MAX=90 | ||
| + | [email protected] | ||
| + | PART=xvda1 | ||
| + | |||
| + | USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d' | ||
| + | if [ $USE -gt $MAX ]; then | ||
| + | echo " | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | https:// | ||
| + | |||
| + | ==== Вариант 2 ==== | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/sh | ||
| + | # set -x | ||
| + | # Shell script to monitor or watch the disk space | ||
| + | # It will send an email to $ADMIN, if the (free available) percentage of space is >= 90%. | ||
| + | # ------------------------------------------------------------------------- | ||
| + | # Set admin email so that you can get email. | ||
| + | ADMIN=" | ||
| + | # set alert level 90% is default | ||
| + | ALERT=90 | ||
| + | # Exclude list of unwanted monitoring, if several partions then use " | ||
| + | # An example: EXCLUDE_LIST="/ | ||
| + | EXCLUDE_LIST="/ | ||
| + | # | ||
| + | #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | ||
| + | # | ||
| + | function main_prog() { | ||
| + | while read output; | ||
| + | do | ||
| + | #echo $output | ||
| + | usep=$(echo $output | awk '{ print $1}' | cut -d' | ||
| + | partition=$(echo $output | awk ' | ||
| + | if [ $usep -ge $ALERT ] ; then | ||
| + | echo " | ||
| + | mail -s " | ||
| + | fi | ||
| + | done | ||
| + | } | ||
| + | |||
| + | if [ " | ||
| + | df -H | grep -vE " | ||
| + | else | ||
| + | df -H | grep -vE " | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | http:// | ||
| + | |||
| + | ==== Вариант 3 ==== | ||
| + | |||
| + | <code bash> | ||
| + | # | ||
| + | |||
| + | # vars # | ||
| + | disk="/ | ||
| + | current_usage=$(df -h | grep ${disk} | awk {' | ||
| + | max_usage=" | ||
| + | mail=" | ||
| + | |||
| + | # functions # | ||
| + | function max_exceeded() { | ||
| + | |||
| + | # tell that mail is being sent | ||
| + | echo "Max usage (${max_usage}) exceeded. Your disk usage is it at ${current_usage}. Trying to send mail-alert..." | ||
| + | |||
| + | # check if the mail program exist | ||
| + | type mail > /dev/null 2>&1 || { | ||
| + | echo >&2 "Mail does not exist. Install it and run script again. Aborting script..."; | ||
| + | } | ||
| + | |||
| + | # if the mail program exist we continue to this and send the alert | ||
| + | mailbody=" | ||
| + | echo ${mailbody} | mail -s "Disk alert!" | ||
| + | |||
| + | echo "Mail was sent to ${mail}" | ||
| + | } | ||
| + | |||
| + | function no_problems() { | ||
| + | |||
| + | echo "No problems. Disk (${disk}) usage at ${current_usage}. Max is set to ${max_usage}." | ||
| + | } | ||
| + | |||
| + | function main() { | ||
| + | |||
| + | # check if a valid disk is chosen | ||
| + | if [ ${current_usage} ]; then | ||
| + | # check if current disk usage is greater than or equal to max usage. | ||
| + | if [ ${current_usage%? | ||
| + | # if it is greater than or equal to max usage we call our max_exceeded function and send mail | ||
| + | max_exceeded | ||
| + | else | ||
| + | # if it is ok we do nothing | ||
| + | no_problems | ||
| + | fi | ||
| + | else | ||
| + | # if the disk is not valid, print valid disks on system | ||
| + | echo "Set a valid disk, and run script again. Your disks:" | ||
| + | df -h | ||
| + | fi | ||
| + | } | ||
| + | |||
| + | # init # | ||
| + | main | ||
| + | </ | ||
| + | |||
| + | http:// | ||
| + | |||
| + | ==== Вариант 4 ==== | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | for mnt in / /var/log /var/www / | ||
| + | chk_avail=`df --output=pcent $mnt | tail -n +2 | tr -cd ' | ||
| + | flg=`echo $(( 100 - $chk_avail ))` | ||
| + | if [ $flg -lt 15 ]; then | ||
| + | echo ' | ||
| + | fi | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | http:// | ||