Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a Shell script that monitors disk usage

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report