1#!/bin/sh -e 2# 3# disk: print the current disk space and usage 4# 5# Copyright (C) 2008 Canonical Ltd. 6# Copyright (C) 2011-2014 Dustin Kirkland 7# 8# Authors: Dustin Kirkland <kirkland@byobu.org> 9# 10# This program is free software: you can redistribute it and/or modify 11# it under the terms of the GNU General Public License as published by 12# the Free Software Foundation, version 3 of the License. 13# 14# This program is distributed in the hope that it will be useful, 15# but WITHOUT ANY WARRANTY; without even the implied warranty of 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17# GNU General Public License for more details. 18# 19# You should have received a copy of the GNU General Public License 20# along with this program. If not, see <http://www.gnu.org/licenses/>. 21 22__disk_detail() { 23 if [ $(uname) = "FreeBSD" ]; then 24 df -h 25 else 26 df -h -P 27 fi 28} 29 30__disk() { 31 local out="" MP="" size="" pct="" unit="" 32 # Default to /, but let users override 33 [ -z "$MONITORED_DISK" ] && MP="/" || MP="$MONITORED_DISK" 34 case $MP in 35 /dev/*) MP=$(awk '$1 == m { print $2; exit(0); }' "m=$MP" /proc/mounts);; 36 esac 37 # this could be done faster with 'stat --file-system --format' 38 # but then we'd have to do blocks -> human units ourselves 39 if [ $(uname) = "FreeBSD" ]; then 40 out=$({ df -h "$MP" 2>/dev/null || df -h "$MP"; } | awk 'END { printf("%s %s", $2, $5); }') 41 else 42 out=$({ df -h -P "$MP" 2>/dev/null || df -h "$MP"; } | awk 'END { printf("%s %s", $2, $5); }') 43 fi 44 set -- ${out} 45 size=${1}; pct=${2}; 46 unit=${size#${size%?}} # get the unit (last char) 47 size=${size%?}; # take the unit off 48 pct=${pct%?}; # take off the '%' 49 case "$unit" in 50 k*|K*) unit="$ICON_KB" ;; 51 m*|M*) unit="$ICON_MB" ;; 52 g*|G*) unit="$ICON_GB" ;; 53 t*|T*) unit="$ICON_TB" ;; 54 esac 55 [ -n "$size" ] || return 56 color b m W; printf "%s" "$size"; color -; color m W; printf "%s" "$unit"; color -; 57 color b m W; printf "%s" "$pct"; color -; color m W; printf "%s" "$PCT"; color --; 58} 59 60# vi: syntax=sh ts=4 noexpandtab 61