Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement a Shell script that monitors disk usage

Shulou Source: shulou.com Published: 2022-06-03 08:46:53 09月18日 Update

This article will explain in detail the Shell script on how to monitor disk usage. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Monitordisk.sh is as follows

The code is as follows:

#! / bin/bash

# Updated:2008-03-03 PM By:leif (liangliwen@163.com)

EMAIL=/usr/local/bin/email

/ bin/df-h > / tmp/df.txt

USE= `df-H | grep-o [0-9] *% | grep-o'[0-9]\ +'`

For i in $USE

Do

If (($I > 95))

Then

$EAMIL-s "WARNING Low disk space for $I" liangliwen@163.com break

Fi

If (($I > 90))

Then

$EMAIL-s "Low disk space for $I" liangliwen@163.com fi

Done

/ bin/rm-f / tmp/df.txt

To achieve the goal, any partition will send an email to the designated recipient when it reaches 90%, and a warning (warning) will be issued on the subject of the email at 95%, indicating that the email program EMAIL is downloaded and installed from http://www.cleancode.org/projects/email, which is more flexible.

Put this shell in the crontab implementation to check the disk condition regularly as needed.

The following is a supplementary content:

Shell script to monitor remote host disk usage, filename: disklog.sh

The code is as follows:

#! / bin/bash

# File name: disklog.sh

# purpose: monitor disk usage of remote systems

Logfile= "diskusage.log"

If [[- n $1]]

Then

Logfile=$1

If

If [!-e $logfile]

Then

Printf "%-8s%-14s%-9s%-8s%-6s%-6s%-6s%\ n" Date "," IP ADDRESS "," Device "," Capacity "," Used "," Free "," Percent "," Status "> $logfile

Fi

IP_LIST= "127.0.0.1 0.0.0.0"

# provide remote host IP address list

(

For ip in $IP_LIST

Do

Ssh slynux@$ip'df-H' | grep ^ / dev/ > / tmp/$$.df

While read line

Do

Cur_date=$ (date +% D)

Printf "%-8s%-14s" $cur_date $ip

Echo $line | awk'{printf ("%-9s%-8s%-6s%-6s%-8s", $1mine2);}'

Pusg=$ (echo $line | egrep-o "[0-9] +%")

Pusg=$ {pusg/\% /}

If [$pusg-lt 80]

Then

Echo SAFT

Else

Echo ALERT

Fi

Done

< /tmp/$$.df done )>

> $logfile

We can use cron to schedule script execution at regular intervals, such as adding an entry like this to crontab to run the script automatically at 10:00 every morning:

00 10 * / home/sh/disklog.sh / home/log/diskusg.log

Execute the crontab-e command, add the above line, and save it.

You can also perform it manually:

$. / disklog.sh

This is the end of this article on "how to implement a Shell script for monitoring disk usage". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Situation disk script monitoring content article mail host code file name more good practical one line subject command address manual recipient Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info Shulou Technology OPPO Reno MariaDB