Technology

#How to View and Monitor Disk Space Usage From the Linux Command Line – CloudSavvy IT

“#How to View and Monitor Disk Space Usage From the Linux Command Line – CloudSavvy IT”

Bash Shell

While it’s usually pretty clear if your system is running out of memory or using too much CPU time, disk usage is another key metric that can sneak up on you over time if you leave your server unattended. You’ll want to regular check your disk usage using these commands.

Checking Disk Usage On Linux

The utility used to quickly check disk usage on almost all Linux systems is df, which stands for “disk filesystems.” It simply prints out a list of all the filesystems on your system.

df -hT

This command here is invoked with two flags, -h for “human readable,” which prints out byte numbers in KB, MB, and GB, and -T, which displays the type of the filesystem.

df will print out each filesystem alongside the size, how much is used and available, and where it’s mounted to on your system.

You’ll quickly notice that you probably have a lot more “filesystems” than you’d expect. This server only has one solid state drive, but has over 20 filesystems. Most of these are backend stuff used for other programs and services, such as compressed squashfs, virtual tmpfs , and Docker’s overlay systems used for containers.

In this example, ext4 is the real drive, which is obvious because it’s mounted at root, but in multi-drive systems that may not be immediately clear. If you want, you can filter this list by specifying which type you want to see with the lowercase -t flag:

df -hT -t ext4

Or by removing what you don’t want to see manually with the -x flag:

df -hT -x squashfs -x overlay -x tmpfs -x devtmpfs

You can also ask df for info on any specific filesystem, even including wildcards to match multiple systems by name:

df -h /dev/md*

Or, you can ask it for info on a specific mount, which is most useful for quickly getting the info on your root system:

df -h /

Monitoring At a Glance

Most of the time though, you probably don’t want to remember and type a bunch of commands with specific flags. That’s what the glances utility was made for, and we highly recommend giving it a try.

It’s basically a replacement for built-in utilities like top and htop, except it displays a lot of different performance metrics about your system, the disks. It’s not included in most Linux distros by default, but it’s open source and can be installed from pip, given it’s a Python app.

sudo pip install glances

Then simply run the app to open up the glances dashboard:

glances

You’ll find in the lower left corner some info on disk usage, including current I/O speeds, as well as each physical disk’s total usage. Using this, you can easily spot problems with disks filling up before it breaks your system.

There are plenty of other utilities for monitoring your system, but glances checks all the boxes, so it’s a pretty nice tool to have.

Sending Alerts When Your Disk Usage Is Too High

The main problem with command line tools is that they require you to be proactive about checking for problems. But problems can pop up unexpectedly, so it’s good to get notified about them beforehand.

So, the solution is to set up a daily cron job that will run df automatically to check the usage on the root system. It will compare this with a set value, and if it’s greater, it’ll send a message.

#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=90

if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
curl -X POST -H 'Content-type: application/json' --data "{"text":"Your server `$(hostname)` is currently at ${CURRENT}% disk capacity."}"
fi

You have plenty of options for how to get messages, and it will depend on your preferred contact. The simplest would be to set up the mail utility to send you emails from the command line. You can read our article on setting up Postfix to handle this for you.

A much cooler method is to send yourself a message directly on a messaging platform you’re active on, such as setting up Slack notifications from your server, which can be easily done using webhooks with curl POST requests.

You can read our article on setting up Slack webhooks for disk space alerts to learn more.

RELATED: How to Setup Slack Notifications for Low Disk Space On Your Server

If you liked the article, do not forget to share it with your friends. Follow us on Google News too, click on the star and choose us from your favorites.

For forums sites go to Forum.BuradaBiliyorum.Com

If you want to read more like this article, you can visit our Technology category.

Source

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close

Please allow ads on our site

Please consider supporting us by disabling your ad blocker!