Passing an argument containing a minus sign

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
joedebattista
Posts: 7
Joined: Tue Sep 09, 2014 8:07 pm

Passing an argument containing a minus sign

Post by joedebattista »

Greetings,
We have had an issue where we occasionally get multiple nrpe daemons running on an AIX client. I want to only have a single daemon running. On the AIX client, I have the following in the nrpe.cfg:
command[check_local_procs_range]=/usr/local/nagios/libexec/check_procs -c $ARG1$ -a $ARG2$

From the nagios server (running Redhat Linux), I try escaping the spaces and quotes.
check_nrpe -H vx30 -c check_local_procs_range -a \\"1:1\\ nrpe.cfg\\ -d\\"

On the AIX client I keep getting
Error: Request contained illegal metachars!

Can anyone spot the error? Another question is whether there's another way to ensure there's only a single nrpe daemon running. Any suggestions or help will be greatly appreciated.

Joe DeBattista
Unix system administrator
University of California, San Francisco
joed@ucsf.edu
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Passing an argument containing a minus sign

Post by eloyd »

Yuck. You're making life very difficult for yourself. Try something simpler:

Code: Select all

commands.cfg:

define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a "$ARG2$"
}


services.cfg:

define service{
        use                     SOME TEMPLATE
        service_description     Check Local Procs Range
        host_name              vx30
        check_command           check_nrpe!check_local_procs_range!-c 1:1 nrpe.cfg -d
}


nrpe.cfg:

command[check_local_procs_range]=/usr/local/nagios/libexec/check_procs -a $ARG1$
You might need to put quotes around the $ARG1$ in the nrpe.cfg.


Let me know how that works for you.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Passing an argument containing a minus sign

Post by abrist »

eloyd is spot on as usual. It is usually best practice to to wrap your $ARGn$s in quotes in general.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Passing an argument containing a minus sign

Post by eloyd »

Oh, I forgot this part:
Another question is whether there's another way to ensure there's only a single nrpe daemon running. Any suggestions or help will be greatly appreciated.
Yes. Look at check_procs. Specifically:
/usr/local/nagios/libexec/check_procs -c 1 -C nrpe
You may have to adjust the -C parameter a bit, but that's the basics for what you're looking for.
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
abrist
Red Shirt
Posts: 8334
Joined: Thu Nov 15, 2012 1:20 pm

Re: Passing an argument containing a minus sign

Post by abrist »

OP: Let us know if you need any additional help.
Former Nagios employee
"It is turtles. All. The. Way. Down. . . .and maybe an elephant or two."
VI VI VI - The editor of the Beast!
Come to the Dark Side.
joedebattista
Posts: 7
Joined: Tue Sep 09, 2014 8:07 pm

Re: Passing an argument containing a minus sign

Post by joedebattista »

Hi,
I appreciate all the help. However, I'm still having some issues, now with the count of nrpe daemons when I run the command on the remote host and when I run it on the nagios server. To simplify things for myself, I created a simple ksh script on the remote host called check_4nrped and placed it in /usr/local/nagios/libexec. Here's the script:
#!/bin/ksh
######################################
# check_4nrped #
######################################
#
# This script will check for ensuring that there's only a single nagios
# daemon. If there is more than 1, then print an error.
# JD 9/11/14

count=`ps -ef | grep "nrpe.cfg -d" | grep -v grep | wc -l | sed -e 's/^ *//'`

if [ "$count" = "1" ]
then
msg="OK. There is a single nrpe daemon running"
rc=0
else
msg="CRITICAL: $count nrpe daemons running"
rc=2
fi
echo $msg
return $rc
================================================================================
I do a grep for the nrpe.cfg -d string, remove the grep command itself, get a line count and then remove leading spaces from the answer and store it in a variable called count. If the count is 1, then send back an OK message. If not, then send the number of nrpe.cfg -d processes found and mark it as critical. A normal ps -ef | grep nrpe.cfg -d usually gives:
root 19792122 9437310 1 18:12:51 pts/1 0:00 grep nrpe.cfg -d
nagios 32506110 1 0 17:31:19 - 0:00 /usr/local/nagios/nrpe -c /usr/local/nagios/nrpe.cfg -d

On the remote host, in my nrpe.cfg I put in this entry:
command[check_4nrped]=/usr/local/nagios/libexec/check_4nrped

On the nagios server, in checkcommands.cfg I have:
# 'check_4nrped' command definition
define command{
command_line check_4nrped
command_line $USER1/check_4nrped

My service entry says:
check_command check_nrpe!check_4nrped


When I run the /usr/local/nagios/libexec/check_4nrped on the remote host, I get what I would expect:
OK. There is a single nrpe daemon running

However, when I run the following on the nagios server, I get a different answer:
/usr/local/nagios/libexec/check_nrpe -H hostname -c check_4nrped
CRITICAL: 3 nrpe daemons running

Can someone tell me what is causing the different answers?
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Passing an argument containing a minus sign

Post by eloyd »

I still say you're better off using check_procs as intended:

Code: Select all

/usr/local/nagios/libexec/check_procs -c 1:1 -C nrpe -a nrpe.cfg
That will look for processes that are running a command called "nrpe" that have an argument called "nrpe.cfg" and return a critical if there are any number other than one running. But that's not the question you asked.

I see nothing wrong with your script, but there is something wrong with your logic. When NRPE fires off its check, it talks to the NRPE daemon on the remote machine. The NRPE daemon forks a copy of itself to handle the request. Thus, you have more than one process running at any given time that matches your "ps" because the act of doing the "ps" over NRPE creates multiple processes.

So given that the NRPE daemon forks copies of itself, and you never know if more than one request is being handled at any given time, you may not be happy with trying to find only 1 NRPE process running at any given time using ps. The check_procs command, however, also gives you the ability to check with additional options:
This plugin checks the number of currently running processes and
generates WARNING or CRITICAL states if the process count is outside
the specified threshold ranges. The process count can be filtered by
process owner, parent process PID, current state (e.g., 'Z'), or may
be the total number of running processes
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
sreinhardt
-fno-stack-protector
Posts: 4366
Joined: Mon Nov 19, 2012 12:10 pm

Re: Passing an argument containing a minus sign

Post by sreinhardt »

OP, let us know if you give eloyd's post a try. I tend to agree though, check_procs is designed for this very thing and should make life a whole lot easier on you, but as always the choice is yours as to how you wish to monitor things.
Nagios-Plugins maintainer exclusively, unless you have other C language bugs with open-source nagios projects, then I am happy to help! Please pm or use other communication to alert me to issues as I no longer track the forum.
joedebattista
Posts: 7
Joined: Tue Sep 09, 2014 8:07 pm

Re: Passing an argument containing a minus sign

Post by joedebattista »

Thanks. I appreciate the information and your patience with me. I've gone back to trying things with check_procs. In my nrpe.cfg on the remote host, I created
command[check_4nrped2]=/usr/local/nagios/libexec/check_procs -c "$ARG1$" -C "$ARG2$" -a "$ARG3$"

In checkcommands.cfg on the nagios server, I have
# 'check_4nrped2' command definition
define command{
command_line check_4nrped2
command_line $USER1$/check_4nrped2
}

My services entry has
check_command check_nrpe!check_4nrped2!1:1!nrpe!nrpe.cfg

On the remote host I ran the command ok, and that seemed to work
i22m6:/usr/local/nagios/libexec/check_procs -c 1:1 -C nrpe -a nrpe.cfg
PROCS OK: 1 process with command name 'nrpe', args 'nrpe.cfg'

But on the nagios server, I'm still getting an incorrect number of processes.
vx8:/usr/local/nagios/libexec/check_nrpe -H i22m6 -c check_4nrped2 -a "1:1" "nrpe" "nrpe.cfg"
PROCS CRITICAL: 3 processes with command name 'nrpe', args 'nrpe.cfg'

Rats!
User avatar
eloyd
Cool Title Here
Posts: 2129
Joined: Thu Sep 27, 2012 9:14 am
Location: Rochester, NY
Contact:

Re: Passing an argument containing a minus sign

Post by eloyd »

What does the output of "ps -aef | grep nrpe" look like on the Nagios serveR?
Image
Eric Loyd • http://everwatch.global • 844.240.EVER • @EricLoydI'm a Nagios Fanatic!
Locked