Extending Nagios functionality with libnagios

Have you ever attempted to write a function in C to execute a command and parse the output? I think I’d rather just let the Nagios library do the heavy lifting for me.

This blog post is going to cover the basics of compiling libnagios, and linking the Nagios library to your application. I’ll be focusing on using some of the built-in Nagios functionality, specifically the runcmd_open() function.

I’m going to assume you have a sane build environment set up (where tools like make and ./configure are working) before we go any further. If you are following along, now would be the time to get these in order.

First, download the source code and extract it. You can get a copy of the Nagios Core source at https://github.com/NagiosEnterprises/nagioscore/archive/master.zip. Once you’ve downloaded it and extracted the files, open up nagioscore-master/lib/runcmd.h (https://github.com/NagiosEnterprises/nagioscore/blob/master/lib/runcmd.h). Search for “extern int runcmd_open”, as of the time of this writing, that should bring you to line 77, where our function is declared:

 

So what does all that mean? It means we need a command to execute, a file descriptor for stdout, another filedescriptor for stderr. Our application doesn’t need a callback function to register iobrokers or a value to pass. But, since these are declared non null, we’ll have to get creative.

Let’s create a file, named test.c in the root of the nagioscore-master directory. First, we need to include our libnagios header.

 

Then we define our fake iobroker_register function. This is essentially just a placeholder, as we aren’t (yet) particularly interested in assigning a function to execute when our stdout/stderr stops reading.

 

Next, we set up our variables that we’ll be using to pass to the runcmd_open() function. We don’t need an env variable, since that argument can accept a NULL value, we’re just going to pass that in (especially since it is unused anyway).

 

Now we execute runcmd_open(), and let the Nagios library do its magic! This will put stdout in pfd[0] and stderr in pfderr[0].

 

Let’s copy the stdout to our out var and print some information relating to the command we executed and that command’s output.

 

Finally, we clean up our memory and exit the program.

 

Here’s the file in its entirety:

test.c

 

Let’s see it in action! First we’re going to compile our Nagios library! Open up your terminal and let’s get to library compilin’:

 

Those commands should have compiled your Nagios library and then placed it in /usr/local/nagios/lib. Now, we’re finally ready to compile our program:

 

Now, if everything went well up this point, you should be able to execute our basic program with the following command:

Your output should be similar to the following:

I hope that you’ve learned a few things about using the Nagios library in your own code. Questions, comments, and suggestions for future posts are all welcome below in the comments section.

 

– Bryan Heden

How To Set Up A Nagios XI License Maintenance Status Check

There are a few ways to set up a Nagios XI license maintenance status check. It is also very easy to check the Nagios XI license maintenance status inside Nagios XI. Here’s how…

Once logged in to Nagios XI you can navigate to the Admin menu then click on the License Information link on the left (However, many administrators simply forget to do so!). From the License Information screen you can view the time remaining on your license and/or click the renew now link to begin the renewal process. That’s all there is to it; nice and easy!

Setting Up a license Status Check in Nagios XI

Some Nagios XI users showed interest in setting up a license status check in XI. Having such a check makes perfect sense.  If you were notified that your license is about to expire, you would renew it on time thus avoiding any possible service interruptions. Also, you may be running several Nagios XI instances. You wouldn’t want to log in to each one just to check when each license expires. You could monitor all of the licenses from one central Nagios XI server.

I wrote a small bash script, which can be used to set up a license status check in XI. You can download the plugin from here:

http://assets.nagios.com/downloads/nagiosxi/scripts/check_license.sh

Install it as you would normally install any other plugin in Nagios XI.

Admin -> Manage Plugins -> Browse -> check_license.sh -> Upload Plugin

You can test the plugin from the command line by running:

To view the usage (help) menu, run the plugin with passing a “-h” or “–help” flag.

nagios license status check

After you verify that the plugin works from the command line (see the example in the help menu), you can proceed with setting up a command and a service check under the Core Config Manager in Nagios XI.

check nagios license status

 

nagios check license

Save and Apply Configuration.

From the Nagios XI web interface go to the “Service Detail” menu and click on the newly added service. Schedule a forced immediate check to make sure the check returns the expected output.

service detail screen of license check status in nagios xi

For more information, please review our documentation (“Nagios XI – How To Set Up A Nagios XI License Maintenance Status Check“):

https://assets.nagios.com/downloads/nagiosxi/docs/How-To-Set-Up-A-Nagios-XI-License-Maintenance-Status-Check.pdf

Happy Monitoring!

Enabling SNMP on Cisco Router – Cisco Devices

SNMP – Enabling SNMP on Cisco Router

—-

We’ve had a lot of questions on the process for configuring SNMP on Cisco devices, mainly routers and switches, in this article we will give you the steps on how to configure SNMP on Cisco routers (and Catalyst switches).

Note: We’ve tested these commands in our lab but if you have any additional questions on what may be required for your environment, please reach out to your network administrator and/or Cisco to verify that they will work with your specific devices.

1. SSH or Telnet into your router/switch:

* We recommend enabling SSH and disabling telnet wherever possible because telnet will send all the information you enter (including usernames and passwords) as plaintext across your network because it is a insecure protocol.

If you’re connecting from Linux
——————————-
ssh user@X.X.X.X

OR

telnet X.X.X.X

If you’re connecting from Windows
———————————
You can connect with Putty or another SSH/Telnet client. Just type in the router/switch IP address and select the SSH or Telnet protocol when connecting.

2. Enter enable mode:
enable

3. Enter into configuration mode:
configure terminal

4. Setup a read-only SNMP community on the device so that you can monitor it with Nagios:
snmp-server community YOURCOMMUNITY ro

* We recommend that you come up with a complex SNMP community string (capitals, lowercase, alpha-numeric characters) but don’t use any special characters because not all software will work with them in it.

5. Exit configuration mode and save the changes you’ve made:
exit
write memory

Here are the configuration commands (simplified):

MYROUTER>enable
Password:
MYROUTER#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
MYROUTER(config)#snmp-server community YOURCOMMUNITY ro
MYROUTER(config)#exit
*Mar  1 00:14:40.987: %SYS-5-CONFIG_I: Configured from console by console
MYROUTER#write memory
Building configuration…
[OK]
MYROUTER#exit

Configuring SNMP Traps

———-

SNMP traps are a great way of getting alerted from a device in near real-time. When you setup SNMP traps on your compatible device and it detects an error (or another piece of information you’ve set as a trap), the device will send a trap immediately through SNMP to your Nagios server so that you can act on it according to your Nagios configurations.

Here is the process for settings up SNMP traps on your Cisco devices:

1. SSH or telnet into your router/switch.
ssh user@X.X.X.X

* We recommend enabling SSH and disabling telnet wherever possible because telnet will send all the information you enter (including usernames and passwords) as plaintext across your network.

OR

telnet X.X.X.X

2. Enter enable mode:
enable

3. Enter into configuration mode:
configure terminal

4. Setup your Nagios server as the trap target:
snmp-server host NAGIOSIPADDRESS version 2c YOURCOMMUNITY

5. Setting up the type of SNMP traps that you want to send:
You can enable all SNMP traps with this command:

snmp-server enable traps

or you can enable individual traps with the command template:

snmp-server enable traps [notification-type [notification-options]]

As an example, to setup SNMP traps for OSPF errors you would run this command:

snmp-server enable traps ospf errors

6. Exit configuration mode and save the changes you’ve made:
exit
write memory

Here are the configuration commands (simplified):

MYROUTER>enable
Password:
MYROUTER#configure terminal
Enter configuration commands, one per line.  End with CNTL/Z.
MYROUTER(config)#snmp-server host IPADDRESS version 2c YOURCOMMUNITY
MYROUTER(config)#snmp-server enable traps
MYROUTER(config)#exit
*Mar  1 00:14:40.987: %SYS-5-CONFIG_I: Configured from console by console
MYROUTER#write memory
Building configuration…
[OK]
MYROUTER#exit

You can read more about setting up SNMP traps with Nagios XI here.

https://assets.nagios.com/downloads/nagiosxi/docs/Integrating_SNMP_Traps_With_Nagios_XI.pdf

Utilizing a RAM Disk in Nagios XI – the Easy Way

If you have a very large Nagios XI instance, and you are experiencing high I\O wait time, and high check latencies, you have a couple of options – buy extremely fast hard drives or add RAM disk on the local filesystem.

Setting up a RAM disk manually requires a modification of numerous files – nagios.cfg, config.inc.php, npcd.cfg, etc. Skipping just one of the required steps or making a typo will result in various issues with performance data files not being processed, graphs not being displayed, etc.

In order to make the process of utilizing a RAM disk in Nagios XI a lot easier for users, we developed a bash script that automates the whole process. All you need to do now is run four simple commands:

The script will check for old or incomplete RAM disk installs, and will exit if any are found. If no previous RAM disk installs are found, the script will:

1. Backup all of the configs that are about to be modified and place them in the newly created “/tmp/ramdiskbackup/” directory.

2. Determine the recommended size of the RAM disk that is needed, and set it up automatically. You have an option to change the size of the RAM disk if you need to use a different value.

3. Modify various configs such as nagios.cfg, config.inc.php, npcd.cfg, etc.

4. Restart services.

5. Give you a confirmation that the RAM disk was installed successfully.

If you prefer to set up a RAM disk manually, please follow the steps outlined in our documentation (under the “Manual RAM Disk Installation” section):

https://assets.nagios.com/downloads/nagiosxi/docs/Utilizing_A_RAM_Disk_In_NagiosXI.pdf

As always, we would appreciate any feedback – good or bad, tips for improvement, etc.

Happy monitoring!