0

From Nagios' Plugin Development Guidelines:

Plugins have a very limited runtime - typically 10 sec. As a result, it is very important for plugins to maintain internal code to exit if runtime exceeds a threshold.

All plugins should timeout gracefully, not just networking plugins.

How can I implement a timeout mechanism into my custom plugin? Basically I want my plugin to return a status code 3 - UNKNOWN instead of the default 1 - CRITICAL when the plugin times out, to reduce the number of false positives generated.

EDIT: My plugin is written in Bash.

Community
  • 1
  • 1
Willman
  • 438
  • 3
  • 6
  • 12
  • There seem to be many suggestions here: https://stackoverflow.com/questions/687948/timeout-a-command-in-bash-without-unnecessary-delay/687994#687994 – Alain Collins Nov 28 '18 at 21:52

1 Answers1

1

You can use timeout. Here is example usage:

timeout 15 ping google.com
if [ $? -eq 124 ]; then
    echo "UNKNOWN - Time limit exceeded."
    exit 3
if

You will get return exit status 124 from timeout when your command don't finish in defined time - 15 sec.

Rohlik
  • 1,286
  • 19
  • 28