@cryptotaxi247 / netdata-1 / commits / f5a043ce2

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.

Austin S. Hemmelgarn committed Jan 29, 2020 at 07:19 UTC f5a043ce21642a68c7bef7363163d308edd97074
1 file changed +66
daemon/system-info.sh
+66
@@ -286,6 +286,70 @@ elif [ -r /proc/meminfo ] ; then
286 TOTAL_RAM="$((TOTAL_RAM*1024))"
287 fi
288
289 +# -------------------------------------------------------------------------------------------------
290 +# Detect the total system disk space
291 +
292 +DISK_SIZE="unknown"
293 +DISK_DETECTION="none"
294 +
295 +if [ "${KERNEL_NAME}" = "Darwin" ]; then
296 + types='hfs'
297 +
298 + if (lsvfs | grep -q apfs) ; then
299 + types="${types},apfs"
300 + fi
301 +
302 + if (lsvfs | grep -q ufs) ; then
303 + types="${types},ufs"
304 + fi
305 +
306 + DISK_DETECTION="df"
307 + total="$(/bin/df -k -t ${types} | tail -n +1 | awk '{s+=$2} END {print s}')"
308 + DISK_SIZE="$((total * 1024))"
309 +elif [ "${KERNEL_NAME}" = FreeBSD ] ; then
310 + types='ufs'
311 +
312 + if (lsvfs | grep -q zfs) ; then
313 + types="${types},zfs"
314 + fi
315 +
316 + DISK_DETECTION="df"
317 + total="$(df -t ${types} -c -k | tail -n 1 | awk '{print $2}')"
318 + DISK_SIZE="$((total * 1024))"
319 +else
320 + if [ -d /sys/block ] ; then
321 + # List of device majors we actually count towards total disk space.
322 + # The meanings of these can be found in `Documentation/admin-guide/devices.txt` in the Linux sources.
323 + # The ':' surrounding each number are important for matching.
324 + 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:'
325 +
326 + if [ "${VIRTUALIZATION}" != "unknown" ] ; then
327 + # We're running virtualized, add the local range of device major numbers so that we catch paravirtualized block devices.
328 + dev_major_whitelist="${dev_major_whitelist}240:241:242:243:244:245:246:247:248:249:250:251:252:253:254:"
329 + fi
330 +
331 + DISK_DETECTION="sysfs"
332 + DISK_SIZE="0"
333 + for disk in /sys/block/* ; do
334 + if [ -r "${disk}/size" ] && \
335 + (echo "${dev_major_whitelist}" | grep -q ":$(cut -f 1 -d ':' "${disk}/dev"):") && \
336 + grep -qv 1 "${disk}/removable"
337 + then
338 + size="$(cat "${disk}/size")"
339 + DISK_SIZE="$((DISK_SIZE + size))"
340 + fi
341 + done
342 + elif df --version 2>/dev/null | grep -qF "GNU coreutils" ; then
343 + DISK_DETECTION="df"
344 + DISK_SIZE="$(df -x tmpfs -x devtmpfs -x squashfs -l --total -B1 --output=size | tail -n 1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
345 + else
346 + DISK_DETECTION="df"
347 + include_fs_types="ext*|btrfs|xfs|jfs|reiser*|zfs"
348 + total="$(df -T -P | grep "${include_fs_types}" | awk '{s+=$3} END {print s}')"
349 + DISK_SIZE="$((total * 1024))"
350 + fi
351 +fi
352 +
353
354 echo "NETDATA_CONTAINER_OS_NAME=${CONTAINER_NAME}"
355 echo "NETDATA_CONTAINER_OS_ID=${CONTAINER_ID}"
@@ -313,4 +377,6 @@ echo "NETDATA_CPU_FREQ=${CPU_FREQ}"
377 echo "NETDATA_CPU_DETECTION=${CPU_INFO_SOURCE}"
378 echo "NETDATA_TOTAL_RAM=${TOTAL_RAM}"
379 echo "NETDATA_RAM_DETECTION=${RAM_DETECTION}"
380 +echo "NETDATA_TOTAL_DISK_SIZE=${DISK_SIZE}"
381 +echo "NETDATA_DISK_DETECTION=${DISK_DETECTION}"
382