Nagios plugin to check Juniper devices. GitHub Gist: instantly share code, notes, and snippets. Nagios plugin to check Juniper devices. GitHub Gist: instantly share code, notes, and snippets. # Bash script to check the status of a SRX cluster via Nagios. # You can use -h to see the help file. This works by connecting via ssh to a junos. How to install and configure Nagios ittutorials.net is a tech website for IT professionals and power computer users. Here you will find tutorials on how to setup and configure many enterprise Microsoft applications, and other open source software like Linux and many applications that run on it.
On this page
- Use NRPE to run on Clients
This tutorial was tested using Nagios Core 4.3.4 on Debian 9.2
Even though Nagios Exchange has thousands of available plugins to freely download, sometimes the status needed to be checked is very specific for your scenario.
Considerations
It is assumed that:
- You have Nagios installed and running (You can follow this Tutorial if not).
- You know the basics on Nagios administration.
Nagios server in this example is hosted on 192.168.0.150 and an example client is hosted on IP 192.168.0.200
Exit Codes
To identify the status of a monitored service, Nagios runs a check plugin on it. Nagios can tell what the status of the service is by reading the exit code of the check.
Nagios understands the following exit codes:
- 0 - Service is OK.
- 1 - Service has a WARNING.
- 2 - Service is in a CRITICAL status.
- 3 - Service status is UNKNOWN.
A program can be written in any language to work as a Nagios check plugin. Based on the condition checked, the plugin can make Nagios aware of a malfunctioning service.
Example Plugin
I will use a simple example. I wrote a plugin in a bash script to check for current Warnings. Let's consider I have the Nagios server configured to alert only on critical status, so I want an alert if I have too many services on a Warning status.
Consider the following script (check_warnings.sh):
Based on the information provided by the nagiostats tool, I assume everything is ok if there are five or less services in Warning state.
I will leave this script with all the other Nagios plugins inside /usr/local/nagios/libexec/ (This directory may be different depending on your confiugration).
Like every Nagios plugin, you will want to check from the command line before adding it to the configuration files.
Remember to allow the execution of the script:
And then run it as any other script:
The result is a text message and an exit code:
Set a New Checking Command and Service
This step will be the same with your own plugins, and if you download a third-party plugin from the internet as well.
First you should define a command in the commands.cfg file. This file location depends on the configuration you've done, in my case it is in /usr/local/nagios/etc/objects/commands.cfg.
So I will add at the end of the file the following block:
Remember that the $USER1$ variable, is a local Nagios variable set in the resource.cfg file, in my case pointing to /usr/local/nagios/libexec.
After defining the command you can associate that command to a service, and then to a host. In this example we are going to define a service and assign it to localhost, because this check is on Nagios itself.
Edit the /usr/local/nagios/etc/objects/localhost.cfg file and add the following block:
Now we are all set, the only thing pending is reloading Nagios to read the configuration files again.
Always remember, prior to reloading Nagios, check that there are no errors in the configuration. You do this with nagios -v command as root:
You should get something like this:
Ensure it returns 0 errors and 0 warnings and proceed to reload the service:
After reloading the service, you will see the associated check in the localhost. First as pending:
And after the execution with its result:
Use NRPE to run on Clients
To run a script on a remote client, you will need to set up the Nagios Remote Plugin Executor (NRPE)
As this tutorial is based on Debian 9, I will show as an example how to install it, but you can find instructions for any distribution.
Generic installation on Debian-based Client
Note that all the configuration in this section is done on the client to be checked, not in the nagios server.
Install NRPE and Nagios plugins:
Allow Nagios server to run commands on the client by adding it to the allowed_hosts entry in /etc/nagios/nrpe.cfg. The line should look like:
Define the standard checks that you will perform on every client with NRPE. Define the checks on /etc/nagios/nrpe_local.cfg. For instance, a model for the file could be:
The idea of having that generic file is so that you can check the same on every client.
Ensure that the local file and .d directory are included in the main configuration file with:
Restart the service:
Check that the NRPE service is running:
Now check one of the previously defined NRPE commands from the Nagios server:
Note that the check_users NRPE command was defined in the /etc/nagios/nrpe_local.cfg file to run /usr/lib/nagios/plugins/check_users -w 5 -c 10.
In case you don't have the plugin in the Nagios server, you can install it with:
So, summarizing, the NRPE will run a script in a remote host, and return the exit code to the Nagios server.
Configuration for Custom Scripts
To use a custom script as a plugin to run remotely through NRPE, you should first write the script on the server, for instance in /usr/local/scripts/check_root_home_du.sh:
Allow the execution of the script:
The previous script is a very simple example, checking the disk usage of the directory /root and setting a threshold for considering it OK, Warning or Critical.
Add the command to the NRPE configuration file on the client (/etc/nagios/nrpe_local.cfg):
And restart the NRPE listener:
Now we can access the server and test it like any standard plugin
Set the NRPE Check on the Server Configuration Files
Now we know that the custom plugin is working on the client and on the server, and that the NRPE is communicating correctly, we can go ahead and configure Nagios files for checking the remote device. So in the server set the files:
/usr/local/nagios/etc/objects/commands.cfg:
/usr/local/nagios/etc/objects/nrpeclient.cfg:
Note that the ! mark separates the command from the arguments in the check_command entry. This defines that check_nrpe is the command and check_root_home_du is the value of $ARG1$.
Also, depending on your configuration you should add this last file to the main file (/usr/local/nagios/etc/nagios.cfg):
Check the configuration and, if no errors or warnings, reload the service:
And now you have a new custom check on a host:
Conclusion
Nagios has a huge library of plugins available at Nagios Exchange. However, in a big environment it is very likely to need some custom checks for specific uses, for instance: Checking on a certain task result, monitoring an in-house developed application, among others.
The flexibility provided by Nagios is perfect for these case scenarios.
I am trying to monitor a web service with check_http plug-in of Nagios.
The url I am trying to monitor includes url parameter.
And it turns out that check_http plugin will ignore the url parameter when checking.
Herewith my configuration.
ChuckChuck59344 gold badges88 silver badges1616 bronze badges
1 Answer
Try replacing the value passed into
-u
with a relative path instead of the full URL.In this example the hostname (
-H
) will be supplied by $HOSTADDRESS$
which is taken from the address
field of the pear
host definition.The value passed into the -u
parameter should be a relative path, for example: /total?eId=12345&env=abcde
.We'll add the -u
to the check_http_with_args
command definition so we don't have to pass it as part of our parameters in our service definition.In the end, the command that Nagios execute should get translated into something that looks like this:
You can try executing the above from the command line to make sure it works for you.
Note: Replace the path to
check_http
with the actual path corresponding to your installation location on your Nagios server.Relevant parts of the
check_http
man pages we're referencing:Source: https://www.monitoring-plugins.org/doc/man/check_http.html
EDIT:
![Recheck Recheck](/uploads/1/2/5/7/125737503/767918201.png)
To answer your questions from the comment, the
-k
or --header=
will allow you to pass in headers.Source: https://www.monitoring-plugins.org/doc/man/check_http.html
So to specify an Accept header, I'd modify the following to look like:
...adding
-k '$ARG2$'
to the command_line
of the command
definition and adding the Accept: <MIME types>
(for example Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
) to the check_command
of the service
definition.Also, I wrapped
Joe YoungJoe Young$ARG1$
of -u '$ARG1$'
part of the command_line
in double quotation marks as I suspect the &
in eId=12345&env=abcde
is causing the shell to think the end of the command is terminated just before the &
. Wrapping the parameter in double quotes should make it see the whole string as one whole argument.![Recheck Recheck](/uploads/1/2/5/7/125737503/268284706.jpg)
4,83122 gold badges1717 silver badges2222 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.