Linux Disk Functions

Created by CecilWesterhof

A handy proc and a script that uses it. The proc to see the mounted partitions. The script to see which partitions are becoming to full.

mounted

On a Linux system there are a lot of mounted partitions that are virtual. Sometimes I am just interested in which partitions are physical. For this I wrote the proc mounted.

proc mounted {} {
    set mount   [open "/proc/self/mounts" RDONLY]
    set mounted [list]
    while {-1 != [gets ${mount} line]} {
        if {[string index ${line} 0] == "/"} {
            lappend mounted [lindex [split ${line}] 1]
        }
    }
    close ${mount}
    lsort -nocase ${mounted}
}

A real partition starts with a '/', so all the lines that do not start with a '/' are skipped. The third entry is the mountpoint, so this is added to the list. When all entries are processed the sorted list is returned.

zashi: Probably better to open /proc/self/mounts than the mount command. Same data, but the /proc/self/mounts interface is a more-direct source and doesn't waste a process.

CecilWesterhof: Thanks for the tip. I changed the code.

checkDiskUsage.tcl

The following script I wrote to use in a cronjob to check if partitions are becoming to full.

#!/usr/bin/env tclsh

package require dcblUtilities

namespace import ::dcblUtilities::mounted


# Fetch from database
set critical 90
set watch    80

set dfCmd  [open "|df -P [mounted]" RDONLY]
set dfList [list]
# Skip header
gets ${dfCmd}
while {-1 != [gets ${dfCmd} lineStr]} {
    set lineLst [regexp -inline -all {\S+} ${lineStr}]
    set matches [llength ${lineLst}]
    if {${matches} != 6} {
        error "Got ${matches} instead of six from:\n    ${lineStr}"
    }
    lassign ${lineLst} fileSystem blocks used avail capacity mountedOn
    # remove percent sign
    set percentage [string range ${capacity} 0 end-1]
    if {${percentage} >= ${watch}} {
        if {${percentage} >= ${critical}} {
            set message CRITICAL:
        } else {
            set message Watch:
        }
        lappend dfList [list ${message} ${capacity} ${mountedOn}]
    }
}
close ${dfCmd}
set dfList [lsort -decreasing -dictionary -index 1 ${dfList}]
foreach list ${dfList} {
    lassign ${list} first second third
    puts [format "%-10s %4s %s" ${first} ${second} ${third}]
}

Change the lines containing package and namespace into the proc mounted.

It uses mounted to retrieve the real partitions and fetches information about those. It appends the entries that are critical or have to be watched to a list. Sorts the list. Outputs the list.

Comments and tips are appreciated.

If you have a certain type of script you would like to see implemented: let me know. Maybe I will do it. ;-)

By the way: it was first implemented in bash, but it seems that the tcl version is about two times as fast. Not really important, but nice to know.