Advertisement
Open Source Projects by Phil Schwartz

Building a Python Script to Detect and Log Stale NFS Mounts

Every Australian office that has ever run a small Linux file server eventually meets the same ugly morning: the laptop takes ten minutes to open a spreadsheet because the NAS that used to live in the server cupboard is gone. The network was re-cabled, the Synology was traded in for a Microsoft 365 tenancy, and the mount point still sits in /mnt/finance on three workstations. Commands crawl. Editors report stale file handles. Someone in Perth blames the NBN, someone in Sydney blames the firewall, and the truth is usually simpler.

A stale NFS mount is a mount the kernel still believes is live even though the server behind it has vanished. Left alone it turns into a stream of ESTALE errors, wedged backup jobs and processes that refuse to exit. The temptation is to unmount everything by hand, but on a shared machine you can knock out a colleague's session and a running payroll export with it. A Python script that finds dead mounts, records what it found and leaves the decision to unmount with a human is a tidy middle ground.

This is the same territory I have been poking around in for years with tools like DenyHosts, which watches SSH login failures and blocks repeat offenders, and Scratchy, which turns Apache logs into something readable. Those projects share a shape: read a messy source, sort out what matters, write a plain text record. The stale mount checker follows the same pattern, and it slots comfortably into a kit of Linux utilities that Australian sysadmins tend to deploy without much fuss.

What counts as a stale NFS mount

The kernel tracks mounted filesystems in /proc/self/mountinfo, and each entry carries a filesystem type. Only entries tagged nfs, nfs4 or nfsd are candidates; CIFS and SMB mounts can look similar in a directory listing but behave differently when the backing server disappears, so filtering by type avoids noisy false positives.

A mount becomes stale when the client cannot reach the server or the export path behind it. On a hard mount, operations block until the kernel gives up, which is why a cat on a dead share can appear to hang forever. On a soft mount, the kernel returns an error straight away. Both are worth catching, though the hard mount is the more dangerous one because it quietly stalls cron jobs and file watchers.

The Australian version of this story is mundane. A Canberra research group retires a file server but leaves the export mounted on lab workstations. A Dubbo farm office still maps a seasonal grain share that was moved to a new host years ago. A consumer NAS gets sold on Marketplace, the router gets replaced, and the mount table keeps pretending everything is fine. None of these systems are broken; they are orphaned.

Probing mounts without freezing the desktop

The obvious way to test a mount is to touch a file inside it, but that is exactly the operation that hangs on a hard mount. Instead of probing in the main process, run the check in a child with a hard timeout, then treat a timeout as the signal rather than an error.

In practice, subprocess.run with the timeout parameter does the heavy lifting: invoke a small helper such as stat against the mount point, give it a few seconds, and catch subprocess.TimeoutExpired. A FileNotFoundError or an OSError carrying errno 116 (ESTALE) confirms the dead export without any risk of wedging the caller. Keep the timeout modest — five seconds is usually plenty on a LAN — and remember that a machine on a thin rural broadband link in regional Victoria may need more patience than a desktop in a Sydney CBD office.

Reading the mount table is the other half of the job. Parse /proc/self/mountinfo for the mount point, the server address and the export path, then check the option string for soft, hard, intr or nointr. If the script is going to run unattended, it should also skip pseudo-filesystems, autofs entries that have not been triggered, and any path that is not actually mounted at that moment.

Writing logs that survive a reboot

A log is only useful if the next person can act on it, so write one JSON object per line with the hostname, mount point, server, export, filesystem type, probe outcome, errno name, probe duration in milliseconds and a UTC timestamp. A plain text summary alongside it keeps the file greppable for anyone who opens it with less on a terminal in a mine site control room.

Time zones deserve attention. Australia spans UTC+8, UTC+9:30 and UTC+10, with half-hour and three-quarter-hour oddities in the north-west and on Lord Howe Island. Daylight saving shifts make local timestamps ambiguous twice a year, so store UTC as the source of truth and derive an Australian local time using zoneinfo with a configured zone such as Australia/Brisbane or Australia/Adelaide.

Where the script writes matters too. Keep files under /var/log/nfs-watch/ owned by root with restrictive permissions, because university and council environments in Australia often have audit requirements around log retention and who can read operational data. On hosts running systemd, sending the same records to the journal with structured fields makes them queryable without managing a separate file at all.

Scheduling checks on Australian infrastructure

Cron is still the workhorse. Plenty of Australian servers — a box in a Kalgoorlie mine camp, a small VPS in a Sydney colocation rack, a shared cPanel host running on AWS ap-southeast-2 — have no systemd timers, and cron handles the job perfectly well. A systemd timer is nicer where it exists, with Persistent=true catching up after a reboot.

Two scheduling details matter in this market. First, add a small random delay so a fleet of machines does not all probe the dead server on the hour and turn a stuck export into a small denial of service against your own network. Second, remember that the clock shifts at two in the morning on the first Sunday in October in Sydney, Melbourne, Adelaide and Hobart; a UTC-based log keeps the history clean through that change.

Keep the dependencies minimal while you are at it. The script should run on Python 3.9 or newer using only the standard library, because plenty of Australian hosting environments are locked down against pip installs and nobody wants to add a requirements file for a two-minute check.

A practical checklist for stale mount detection

Before the first run, confirm the scope and the settings rather than letting the script roam across every mount on the machine.

After the first run, expect a handful of surprises and tidy them deliberately.

The wider aim is an open source tool that sits alongside the rest of the projects listed here, with plain downloads, clear licensing and no telemetry. Work in this area tends to drift from parsing binaries to explaining what they contain, and the companion piece on decrypting TLS sessions follows the same habit of turning opaque captures into readable evidence. A stale mount checker is a much smaller job, but it answers the same question: what exactly is the machine trying to talk to, and why is nobody answering?