1

I have a collection of Linux servers, each of which has a local hard drive with a /tmp directory. Occasionally, the /tmp directory on a server will fill up and cause errors. I want to automate the process of checking for free space on each local drive so that /tmp can be cleaned before it causes a problem. Is there an elegant, programmatic way to determine the amount of free space on a disk on Linux? I know the "classical" answer is to run

df -h

on each server (perhaps using a distributed shell tool such as dsh) and write a script to parse the output. Many such scripts can be found online. I am looking for a more elegant solution, such as a newer disk utility that returns structured data, or some information buried in the /proc filesystem.

Craig Finch
  • 370
  • 1
  • 4
  • 12
  • 6
    This is one of many reasons why you should have [monitoring](http://serverfault.com/q/44/126632). – Michael Hampton Jul 31 '13 at 21:09
  • The nagios check_disk plugin (http://nagiosplugins.org/man/check_disk) is one option. It can be installed and used without Nagios, but why not actually setup Nagios, or some other monitoring system. – Zoredache Jul 31 '13 at 21:27
  • Monitoring is really the answer to my question. – Craig Finch Sep 11 '13 at 21:52

1 Answers1

0

The DU command uses statfs to collect data from the mounted filesystems. The mount filesystems seems to be determined by looking at the mtab file.

# strace du /
...
open("/etc/mtab", O_RDONLY|O_CLOEXEC)   = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=793, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6fdd497000
read(3, "/dev/mapper/vg-root / ext"..., 4096) = 793
read(3, "", 4096)                       = 0
close(3)                                = 0
munmap(0x7f6fdd497000, 4096)            = 0
stat("/", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
statfs("/", {f_type="EXT2_SUPER_MAGIC", f_bsize=4096, f_blocks=118071114, f_bfree=115631354, f_bavail=109633684, f_files=29990912, f_ffree=29574660, f_fsid={-611634627, -1860045897}, f_namelen=255, f_frsize=4096}) = 0
...

So if you want to connect the details you could quickly hack up a python (or whatever) script that calls statfs/statvfs and then give you the information in whatever format you like.

Zoredache
  • 128,755
  • 40
  • 271
  • 413
  • Also see: http://stackoverflow.com/questions/1653163/difference-between-statvfs-and-statfs-system-calls – Zoredache Jul 31 '13 at 22:49