Nagios, or as we call it in-house Nagy, is quite the capable animal, and depending on your installation, you have either some sort of semi-automated configuration tool with it, or like me, have had the installation running for 10+ years, and wrote the configuration files by hand. With some Pico / Nano / Emacs action, you too can have a functioning Nagy environment.
You may wish to perform these steps on a test FreePBX installation, especially if you are new to the linux command lines. Then again, if you have a functioning Nagy box, you likely have worn down the letters on your keyboard caused by the setup process.
Each Nagy installation is completely custom. I cannot give a bullet item list of instructions, as each linux install is different across families: RedHat/CentOS is different than OpenSuSE. But as you already have a working Nagy installation, you should be able to see where I am going with this code.
As mentioned above, you will need to enable an AMI account. This is done in Settings --> Asterisk Manager, and in my case, I created a new Manager. I also modified the permissions to be read only, with the exception of system and command – those two need to have write access. It is also a good idea to change the Deny / Permit fields, and limit the permitted IP numbers to your local subnet where your Nagy box lives. Do not give the bad guys gifts like unsecured ports.
Next, you need to go to your FreePBX box, and open a firewall port on 5038. I made mine on both TCP and UDP.
Next, go to your Nagy box, and open a command line. Telnet into the Asterisk box, making sure that you can make a connection. Telnet -IP-of-FreePBX 5038 This command will initiate a connection on the port, and test your firewall settings. IF you don’t receive an answer, drop the firewall, and see if it connects. Connection? You have a firewall config error. No connection? You have something else wrong and need to fix that first.
Next, you need to go to your Nagy box, and locate where you check scripts are. Mine are in /usr/local/nagios/libexec and in there, I installed the Nagios Plugin for Asterisk described above. Make sure you change the owner (chmod) and permissions (755) for the file so it will execute. I named my script check_pbx_ami
Next, you will need the netcat command. Some linux systems have NC which is the same thing. Type in which netcat, and see if it is present in your path. If not, try which nc and see there. No exe? Then you need to install it. The script above wants netcat, and as I have nc, I changed the script to reference that instead.
Ok, now you can run the script by hand on the Nagy box, and see if Asterisk talks to you.
./check_pbx_ami -H ip-asterisk-box -q sippeers -u ami-username -p amipasswd!
Here is my box’s answer: OK: 15 online, 3 offline SIP peers|online=15 offline=3
That’s good. If you don’t have an answer, you have a problem. Did the Telnet command above work?
Integration into Nagy
Assuming all is good, you now have connectivity from Nagy to FreePBX, and need to bolt the program into Nagy. Because Nagy service checks have to be unique, I needed to make a unique Nagy command to check each feature of the Asterisk box. This is done in the commands.cfg file in a standard installation.
define command{
command_name check_freepbx_sippeers
# Warn me when 5 extensions are Offline, and Critical when 7 are offline
command_line $USER1$/check_pbx_ami -H $HOSTADDRESS$ -q sippeers -u user -p passwd -w 5 -c 7
}
define command{
command_name check_freepbx_channels
# Warn me when more than 5 extensions are active. Might run out of trunks.
command_line $USER1$/check_pbx_ami -H $HOSTADDRESS$ -q channels -u user -p passwd! -w 5
}
define command{
command_name check_freepbx_calls
# Warn me when more than 5 calls are active
command_line $USER1$/check_pbx_ami -H $HOSTADDRESS$ -q calls -u user -p passwd! -w 5
}
define command{
command_name check_freepbx_extension
# Check a particular extension
command_line $USER1$/check_pbx_ami -H $HOSTADDRESS$ -q namepeer -n $ARG1$ -u user -p passwd!
}
define command{
command_name check_freepbx_iaxpeers
command_line $USER1$/check_pbx_ami -H $HOSTADDRESS$ -q iaxpeers -u user -p passwd! -w 5 -c 3
}
The above commands I defined in the commands.cfg file, with the weak part that the usernames and passwords are hard coded into the text file. This is a secure box, and I wish there was a better way, and if you have a better way, tell us how to make it better.
Next, I had to make a matching entry into my services.cfg file
define service{
use servers-alert ; Name of service template to use
host_name FreePBX
service_description Check Calls
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups servers-alert
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_freepbx_calls
}
define service{
use servers-alert ; Name of service template to use
host_name FreePBX
service_description Check Operator Extension
is_volatile 0
check_period 24x7
max_check_attempts 4
normal_check_interval 5
retry_check_interval 1
contact_groups servers-alert
notification_options w,u,c,r
notification_interval 960
notification_period 24x7
check_command check_freepbx_extension!103
}
Notice the two service definitions… you will need a definition for each command you want to check. Also note the second one that has this !103 Did you see, above in the command.cfg file, the $ARG1$ entry? That’s how you pass a variable in Nagy. Here, I want the script to specifically check x103 for a problem.
Go ahead and restart Nagios. If you have any typo problems, the service should alarm you with a warning and prompt you to fix it. If not you have some things to test!
In the command file, I had some -w and -c listings, which are for warning and critical respectively. Note that the code requires a warning defined in order to support a critical, meaning if you leave warning blank, you will never see a critical alarm. You will need to tune the numbers accordingly to your environment.
That’s how I did it. Feel free to try on your own.
Christian