diff options
author | Austin S. Hemmelgarn <austin@netdata.cloud> | 2020-01-29 07:19:42 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-29 07:19:42 -0500 |
commit | f5a043ce21642a68c7bef7363163d308edd97074 (patch) | |
tree | 929dfe72b297236e72ddbe56c134f1d2a0410f81 /daemon | |
parent | c8fd8441f769bc71208db8b63d66466345e84bc6 (diff) |
Add disk size detection to system-info.sh. (#7866)
* Add rudimentary disk size detection to system-info.sh.
This adds really basic detection of total disk size to the
system-info.sh script. This detection is _wildly_ inaccurate in many
cases, but it's not realistically possible to make it much better than
what's added here without making the script exponentially more
complicated (there are just way too many edge cases and special
conditions to worry about).
This adds the following keys to the output of the script:
* NETDATA_TOTAL_DISK_SIZE: This indicates the total detected disk size
in bytes.
* NETDATA_DISK_DETECTION: This indicates what detection method was used
for determining the total disk size. It may return either `df` (which
works almost everywhere but is wildly inaccurate) or `sysfs` (which is
linux specific, but is 100% accurate for a majority of cases).
On most platforms, this parses the output of the `df` command, limiting
only to filesystems that are used on fixed disks, which provides
reasonably accurate results in most trvial cases, but kind of falls
apart the moment people are doing anything remotely complicated in terms
of storage (like using a logical volume manager or under-comitting their
disk space).
On Linux, it will preferentially try to parse the info out of
`/sys/block`, filtering on device major numbers that are actually used
for fixed disks and excluding devices that are indicated to be
removable. This provides a very accurate result if the system does not
use removable media as primary storage, but requires that the user who
runs the script can read the contents of `/sys/block`.
* Add VirtIO block device major number to the list of scanned devices.
* Actually handle VirtIO block devices correctly.
* Fixes for macOS handling.
Diffstat (limited to 'daemon')
-rwxr-xr-x | daemon/system-info.sh | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/daemon/system-info.sh b/daemon/system-info.sh index 3ffb3863aa..6c19c6e636 100755 --- a/daemon/system-info.sh +++ b/daemon/system-info.sh @@ -286,6 +286,70 @@ elif [ -r /proc/meminfo ] ; then TOTAL_RAM="$((TOTAL_RAM*1024))" fi +# ------------------------------------------------------------------------------------------------- +# Detect the total system disk space + +DISK_SIZE="unknown" +DISK_DETECTION="none" + +if [ "${KERNEL_NAME}" = "Darwin" ]; then + types='hfs' + + if (lsvfs | grep -q apfs) ; then + types="${types},apfs" + fi + + if (lsvfs | grep -q ufs) ; then + types="${types},ufs" + fi + + DISK_DETECTION="df" + total="$(/bin/df -k -t ${types} | tail -n +1 | awk '{s+=$2} END {print s}')" + DISK_SIZE="$((total * 1024))" +elif [ "${KERNEL_NAME}" = FreeBSD ] ; then + types='ufs' + + if (lsvfs | grep -q zfs) ; then + types="${types},zfs" + fi + + DISK_DETECTION="df" + total="$(df -t ${types} -c -k | tail -n 1 | awk '{print $2}')" + DISK_SIZE="$((total * 1024))" +else + if [ -d /sys/block ] ; then + # List of device majors we actually count towards total disk space. + # The meanings of these can be found in `Documentation/admin-guide/devices.txt` in the Linux sources. + # The ':' surrounding each number are important for matching. + dev_major_whitelist=':3:8:9:21:22:28:31:33:34:44:45:47:48:49:50:51:52:53:54:55:56:57:65:66:67:68:69:70:71:72:73:74:75:76:77:78:79:88:89:90:91:93:94:96:98:101:104:105:106:107:108:109:110:111:112:114:116:128:129:130:131:132:134:135:136:137:138:139:140:141:142:143:153:160:161:179:180:202:256:257:' + + if [ "${VIRTUALIZATION}" != "unknown" ] ; then + # We're running virtualized, add the local range of device major numbers so that we catch paravirtualized block devices. + dev_major_whitelist="${dev_major_whitelist}240:241:242:243:244:245:246:247:248:249:250:251:252:253:254:" + fi + + DISK_DETECTION="sysfs" + DISK_SIZE="0" + for disk in /sys/block/* ; do + if [ -r "${disk}/size" ] && \ + (echo "${dev_major_whitelist}" | grep -q ":$(cut -f 1 -d ':' "${disk}/dev"):") && \ + grep -qv 1 "${disk}/removable" + then + size="$(cat "${disk}/size")" + DISK_SIZE="$((DISK_SIZE + size))" + fi + done + elif df --version 2>/dev/null | grep -qF "GNU coreutils" ; then + DISK_DETECTION="df" + DISK_SIZE="$(df -x tmpfs -x devtmpfs -x squashfs -l --total -B1 --output=size | tail -n 1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" + else + DISK_DETECTION="df" + include_fs_types="ext*|btrfs|xfs|jfs|reiser*|zfs" + total="$(df -T -P | grep "${include_fs_types}" | awk '{s+=$3} END {print s}')" + DISK_SIZE="$((total * 1024))" + fi +fi + echo "NETDATA_CONTAINER_OS_NAME=${CONTAINER_NAME}" echo "NETDATA_CONTAINER_OS_ID=${CONTAINER_ID}" @@ -313,4 +377,6 @@ echo "NETDATA_CPU_FREQ=${CPU_FREQ}" echo "NETDATA_CPU_DETECTION=${CPU_INFO_SOURCE}" echo "NETDATA_TOTAL_RAM=${TOTAL_RAM}" echo "NETDATA_RAM_DETECTION=${RAM_DETECTION}" +echo "NETDATA_TOTAL_DISK_SIZE=${DISK_SIZE}" +echo "NETDATA_DISK_DETECTION=${DISK_DETECTION}" |