RTFM.WIKI

Ordnung muß sein. Ordnung über alles (18+)

Инструменты пользователя

Инструменты сайта


linux:bash:df_alert

Уведомление о свободном месте (df) по крону

Вариант 1

#!/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=user@gmail.com
PART=xvda1
 
USE=`df -h |grep $PART | awk '{ print $5 }' | cut -d'%' -f1`
if [ $USE -gt $MAX ]; then
  echo "Percent used: $USE" | mail -s "Running out of disk space" $EMAIL
fi

https://gist.github.com/fduran/1870429

Вариант 2

#!/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="root"
# set alert level 90% is default
ALERT=90
# Exclude list of unwanted monitoring, if several partions then use "|" to separate the partitions.
# An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5"
EXCLUDE_LIST="/auto/ripper"
#
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
function main_prog() {
while read output;
do
#echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)
  partition=$(echo $output | awk '{print $2}')
  if [ $usep -ge $ALERT ] ; then
     echo "Running out of space \"$partition ($usep%)\" on server $(hostname), $(date)" | \
     mail -s "Alert: Almost out of disk space $usep%" $ADMIN
  fi
done
}
 
if [ "$EXCLUDE_LIST" != "" ] ; then
  df -H | grep -vE "^Filesystem|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | main_prog
else
  df -H | grep -vE "^Filesystem|tmpfs|cdrom" | awk '{print $5 " " $6}' | main_prog
fi

http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html

Вариант 3

#!/usr/bin/env bash
 
# vars #
disk="/dev/simfs" # disk to monitor
current_usage=$(df -h | grep ${disk} | awk {'print $5'}) # get disk usage from monitored disk
max_usage="90%" # max 90% disk usage
mail="[email protected]" # mail to sent alert to
 
# 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..."; exit; 
    }
 
    # if the mail program exist we continue to this and send the alert
    mailbody="Max usage (${max_usage}) exceeded. Your disk (${disk}) usage is at ${current_usage}."
    echo ${mailbody} | mail -s "Disk alert!" "${mail}"
 
    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%?} -ge ${max_usage%?} ]; then 
            # 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://aarvik.dk/simple-disk-usage-monitor-script-with-bash-and-mail/

Вариант 4

#!/bin/bash
for mnt in / /var/log /var/www /mnt/shared; do
chk_avail=`df --output=pcent $mnt | tail -n +2 | tr -cd '0-9\n'`
flg=`echo $(( 100 - $chk_avail ))`
  if [ $flg -lt 15 ]; then
    echo 'Dataserver lack of free space, only '$flg' percent available in '$mnt' partition' | mail -s 'Dataserver lack of free space' alerts@gmail.com
  fi
done

http://ubuntuhowtoo.blogspot.com/2021/09/linux-bash-check-disk-space.html

Обсуждение

Ваш комментарий. Вики-синтаксис разрешён:
 
linux/bash/df_alert.txt · Последнее изменение: 2021/10/12 02:48 — dx