I want to use a script sensor to monitor used disk space of a volume on a Linux or Unix system via Secure Shell. How can I achieve this? Is there a script for this?


Article Comments

This article applies as of PRTG Network Monitor 12

Monitoring Used Disk Space of a Volume Under Linux/Unix

There is already an SSH sensor available which allows you to monitor the free disk space on Linux and Unix systems. However, if you’re interested in monitoring the space that is currently used by the system, you can write your own script to do so.

SSH Script Sensor

To monitor used disk space of a volume via SSH you can use PRTG’s SSH Script Sensor in combination with your own shell script. This sensor type connects to a Linux/Unix system and executes a script file located on the target system. The sensor can show a value returned by the script as well as a return message in plain text.

Creating the Script

The first thing to do is to create the script file. Open an editor and paste the following script:

#A shell script to show used disk space in percent

output=$(df -k /$1 | tail -1 | nawk '{print $5}' | sed 's/.$//')
print 0:$output:OK
  • df -k (abbreviation for disk free) prints the free disk space on the system based on 1024 bytes/block. On our system the output format for this command is:
user@host:~# df -k
Filesystem          1024-blocks   Used Free %Used Mounted on
rootfs                20642428  10983220   8610632  57% /
devtmpfs               1919012       328   1918684   1% /dev
tmpfs                  1925988       804   1925184   1% /dev/shm
/dev/sda6             20642428  10983220   8610632  57% /
/dev/sda7            150323452   5455520 143798884   4% /home

  • /$1 returns the first two lines of the disk free table
  • tail -1 cuts the first line, so now the script returns:
rootfs 20642428 10983220 8610632 57% /
  • nawk '(print $5)' returns the fifth column of this line: 57%
  • sed 's/.$' removes the percentage sign
  • So, result of the script for this disk free table is: 57
  • Note: Depending on your specific Unix system, you might have to adjust the script for the df-output appropriately in order to get the right lines, columns, and values.
  • print 0:$output:Scanning State OK is the return value in the correct format exitcode:value:message
  • exitcode will determine the sensor status (“0” will result in an “Up” status and will usually do well for this script); value will return the percentage of used disk space; message will appear as Last Message in the sensor’s overview tab.
  • In order for the sensor to show the expected values and sensor status, you must use the right format for the returned value.
  • Note: For details about the expected return format for SSH Script sensors, please see PRTG Manual: Custom Sensors.

After creating the script, set up PRTG in order to monitor used disk space.

Steps to Go

  1. Save the script file in the /var/prtg/scripts directory on the target Linux/Unix system.
  2. Please make sure the script file has executable rights.
  3. If not done yet: In the context menu of a group or probe, choose Add Device… to add a device to PRTG which represents your Linux/Unix device.
  4. Add the SSH Script Sensor to your device in PRTG, e.g., via Add Sensor… in the device’s context menu. Note: For this sensor type Credentials for Linux (SSH/WBEM) systems must be defined for the device you want to use the sensor on.
  5. Enter a name to identify the sensor, for example, Sensor SSH Script Used Disk.
  6. The Script list will show all available shell scripts on the target machine. Select the correct script from the list. If it does not appear in the list, please make sure the file is stored in the correct directory on the target machine.
  7. As Value Type choose Integer.
  8. Click on Continue.

The created script sensor will start to monitor used disk space on your Linux/Unix system immediately. It will show the used disk in percent.

See Also


Feb, 2013 - Permalink

hi, nawk '(print $5)' returns the fifth column of this line: 57% is not correct, correct is awk '(print $5)'


Aug, 2017 - Permalink

Hi ahberyani,

Thank you for your input. Feedback from devs was that this depends on the used Linux distribution:

  • nawk = newer awk, not available in all distribitions
  • gawk = GNU awk, needs to be installed separately eventually
  • awk = available in most distributions, sometimes also linked to nawk / gawk, depending on the used Linux "flavor"

Kind regards,

Erhard


Aug, 2017 - Permalink