Nagios plugin: check_cpu.sh and check_mem.sh

I have written a couple more Nagios plugins for use with NRPE on linux machines. The first one, “check_cpu.sh ,” grabs the cpu state from “/proc/stats” and sends back a status result and perfdata. You can tell it to send back either the aggregate data from all cpus as a single total or can have it return all cpus individually. Be aware though, that if you wish to have it send data on all cpu’s you will need to patch Nagios to allow for a larger perfdata return buffer. I didn’t want to mess with doing that, so I just have it watch the aggregate data.

check_cpu.sh preview

The second plugin, “check_mem.sh,” will parse the output of “free -mt” to give you a look at the current memory and swap utilization.

check_mem.sh preview

13 Responses to “Nagios plugin: check_cpu.sh and check_mem.sh”

  1. Patrick says:

    is there a pnp4nagios template for these?

  2. Shawn Flynn says:

    The tar downloads include the scripts and pnp4nagios templates

  3. Oliver says:

    Hi,
    i really like your plugins, but i somehow hit a bug in check_cpu. if the load is less than 1.0, i get the following output:
    /usr/local/nagios/libexec/check_cpu: line 191: [: -ge: unary operator expected
    /usr/local/nagios/libexec/check_cpu: line 192: [: -ge: unary operator expected
    OK: CPU=.76 | used=.76;;;; system=.38;;;; user=.13;;;; nice=.04;;;; iowait=.21;;;; irq=0;;;; softirq=0;;;;
    the script continues to run, but nagios tells me the result is unknown:Remote command execution failed: /usr/local/nagios/libexec/check_cpu: line 191: [: -ge: unary operator expected
    it would be nice to have a fix for this.
    thank you in advance.
    regards
    oliver

  4. Shawn Flynn says:

    I don’t see this happening on my system. Can you give me the exact command line you are using? I suspect you are putting invalid data in the -w (line 191) and -c (line 192) values. These should be passed as whole numbers, not decimal values…

    The command line should read…
    check_cpu.sh -w 75 -c 90
    not…
    check_cpu.sh -w 0.75 -c 0.90

  5. Ville says:

    This happens to me too, but only when the total cpu load goes below one percent.

    I assume the tests on line 191 and 192 round a cpu load value below 1.0 as zero, which is somehow transformed into a null value, hence the -ge test fails “unary operator expected”.

  6. Ville says:

    You can simply fix the bug by adding the following four lines just above line 191. It just adds an extra zero if necessary:

    if [[ ${pct_used[${cpucount}]} == .* ]]
    then
    pct_used[${cpucount}]=0${pct_used[${cpucount}]}
    fi

  7. Shawn Flynn says:

    OK. I see what’s happening… your distro must be passing the result from procstat without any leading zero on a value of less than one. Mine passes it with the leading zero on small decimal numbers. That is why I don’t see this error happening. Ville’s patch will certainly work to fix this. Thanks for bringing this to my attention.

  8. Oliver Fuhrer says:

    Hi,
    Sorry for the late Answer. I’d like to thank you for the fix.
    I will add those lines to the Script.
    Meanwhile made a quick-n-dirty solution by replacing the lines with
    [ $(cut -d’.’ -f 1 /dev/null 2>&1 ${warning} ] && exitstatus=1
    [ $(cut -d’.’ -f 1 /dev/null 2>&1 ${critical} ] && exitstatus=2
    and redirecting the Output to /dev/null which also seems to work.

    Oliver

  9. Gene says:

    Howdy!

    Great plugin… alas – it appears that the PHP template spits out a Undefined offset: 8 for line 48… for check_cpu. Any ideas?

    Thanks!!

  10. idegz says:

    Hello guys, I used check_mem.sh plugin, but the problem is still using the default template even though I have put check_nrpe_mem.php in template.dist directory, but the way I’m using pnp4nagios, please advise how to fix this so that check_mem.sh will use check_nrpe_mem.php template for graphing.

    Thanks!

  11. idegz says:

    btw, great and very useful plugin. Thanks for sharing …

  12. Mickael says:

    @Gene @comment9
    Change line 48 to be “while (isset($DS[$i]) != “” ) {”

    @idegz @comment10,
    You need to change CUSTOM_TEMPLATE to “CUSTOM_TEMPLATE = 0,1” in pnp4nagios/etc/check_commands/check_nrpe.cfg (maybe you need to create the file)

    Than place check_nrpe_mem.php in pnp4nagios/share/templates and rename it to check_nrpe_check_mem.sh.php

  13. […] Nagios plugin: check_cpu.sh and check_mem.sh […]

Leave a Reply

You must be logged in to post a comment.