1. Overview
There are many Nagios plugins out there available to download. However, you many difficult or even cannot find it one that meet our requirement for server monitoring or network monitoring in our own Data Center infrastructure. There are many scripts that we can use to develop Nagios plugin such as , Bash script, Perl, Python, etc, but Bash scripts is the most easy one.
In this article we will learn how to create Nagios plugin with Bash script for server monitoring or network monitoring with our specific requirement in our own Data Center infrastructure.
2. Prerequisites
In this tutorial of how to create Nagios plugin with Bash script for server monitoring or network monitoring with our specific requirement, it is supposed that:
a. You have already installed Nagios network monitoring program. Please refer to this link Installing Nagios Core For IT Infrastructure Monitoring on RHEL/CentOS 7
b. You have already installed and configured NRPE Nagios agent on the remote Linux server. Please refer to this link Monitoring CentOS/RHEL 7 Linux Server With Nagios Core Web Application
3. Nagios Return Codes
Nagios use exit codes/return codes to trigger an alert whether it is OK, WARNING, CRITICAL, or UNKNOWN. When we create the Bash script as the Nagios plugin, we need to tell plugin have to send a return codes/exit codes to Nagios monitoring server.
The following are the Nagios exit codes/return codes.
Exit/Return Codes | Nagios Status |
0 | OK |
1 | WARNING |
2 | CRITICAL |
3 | UNKNOWN |
4.Scenario Setup
To demonstrate how to develop Nagios plugin with Bash script for server monitoring or network monitoring with our specific requirement, we will use the following scenario.
We have Nagios monitor server and a remote dedicated Linux server with NRPE agent installed. We will create the plugin and run it on remote client dedicated Linux server, and be executed via NRPE agent.
5. On Remote Linux Host
5.1 Create Nagios Plugin With Bash Script
To be an example, we will create a simple script to check the internet connection status on remote client dedicated Linux server using ping command utility. Go into Nagios plugin directory “/usr/local/nagios/libexec/” on remote client dedicated Linux server and create a Bash script file as the following.
# cd /usr/local/nagios/libexec/ # vim check_internet.sh
This plugin script will read domain name and ping count time from the user input. Base on the ping result, the out put of the script will show us an friendly connection status message.
#!/bin/bash # if [[ -z "$1" ]] || [[ -z "$2" ]]; then echo "Missing parameters!" echo "Usage: ./check_internet.sh DomainName Counter" exit 2 fi # PING=/bin/ping # $PING $1 -c $2 >> /dev/null status=$? # if [ $status -eq 0 ] then echo "OK! Connected to $1 after checking $2 ECHO_REQUEST packets" exit 0 else echo "Critical! Loss connection to $1 after checking $2 ECHO_REQUEST packets" exit 2 fi
Then, we need to make this bash scrip plugin to be executable.
# chmod +x check_internet.sh
Let check if our new created plugin working fine, we can executing file “check_internet.sh” as the following.
# ./check_internet.sh techspacekh.com 3 OK! Connected to techspacekh.com after checking 3 ECHO_REQUEST packets # ./check_internet.sh techspacekh.commm 3 Critical! Loss connection to techspacekh.commm after checking 3 ECHO_REQUEST packets
The use of this script, we need to input the domain name and ping count time as above. Otherwise, it will display the the following usage information.
# ./check_internet.sh Missing parameters! Usage: ./check_internet.sh DomainName Counter
We can use the following command to debug the plugin with Bash script.
# bash -x check_internet.sh techspacekh.com 3 + [[ -z techspacekh.com ]] + [[ -z 3 ]] + PING=/bin/ping + /bin/ping techspacekh.com -c 3 + status=0 + '[' 0 -eq 0 ']' + echo 'OK! Connected to techspacekh.com after checking 3 ECHO_REQUEST packets' OK! Connected to techspacekh.com after checking 3 ECHO_REQUEST packets + exit 0
5.2 Configure NRPE Commands
To execute the new created plugin, on the remote client dedicated Linux server, we need to edit NRPE configuration file as the following and then restart NRPE service.
# vim /usr/local/nagios/etc/nrpe.cfg command[check_internet]=/usr/local/nagios/libexec/check_internet.sh # systemctl restart nrpe
6. On Nagios Monitoring Host
To check if the Nagios performance monitoring application can connected to the client dedicated Linux server with IP address of 10.0.0.5 and execute new created plugin file with NRPE, we can run command “check_nrpe” from Nagios monitor server as the following.
# cd /usr/local/nagios/libexec # ./check_nrpe -H 10.0.0.5 -c check_internet -t 20 OK! Connected to techspacekh.com after checking 3 ECHO_REQUEST packets
Right now let add a configuration to monitor the client dedicated Linux server with 10.0.0.5 as the following.
# cd /usr/local/nagios/techspacekh/remotehosts # vim centos7-linux.cfg define host{ use linux-server host_name CentOS7-Linux alias CentOS7-Linux notes Production Server address 10.0.0.5 hostgroups linux-servers } define service{ use generic-service host_name CentOS7-Linux service_description Server Internet check_command check_nrpe!check_internet -t 20 }
Then, we need to restart Nagios service.
# systemctl restart nagios
Go to the web interface of Nagios performance monitoring application and we should see one host name “CentOS7-Linux” and the services defined as the following.
When the internet is not reachable by the remote client dedicated Linux server, we will see the following error alert not Nagios monitoring server.
7. Conclusion
That’s all about how to create Nagios plugin with Bash script for server monitoring or network monitoring with our specific requirement in our own Data Center infrastructure from Tech Space KH. Nagios performance monitoring application is one of the best network monitoring tools and server monitoring tools. Hopefully, you can find this guide informative. If you have any questions or suggestions you can always leave your comments below. I will try all of my best to review and reply them.