Published on May 7 2020 in Nagios

Restoring Nagios audio alerts in modern browsers

Nagios still uses <object> tag for its WAV files. This have not been working for quite some time as browsers dropped Flash and Quicktime support and also the <object> tag seems to be not interpreted like in the past. HTML5 uses <audio> tag for the purpose and we will replace <object> with <audio>, then build RPM and extract the 2 files responsible for sending audio to browser.

If you have Nagios installed from RPM (this article is based on Centos 6) one way to enable audio is to add patch to source RPM and build a new RPM set, then extract respective files from it. The files we are going to replace in our muted Nagios are tac.cgi and status.cgi from /usr/lib64/nagios/cgi-bin directory.

Here is the patch:

--- ./cgi/tac.c.audio   2017-08-24 21:43:48.000000000 +0000
+++ ./cgi/tac.c 2020-05-07 13:23:53.000000000 +0000
@@ -241,11 +241,7 @@ int main(void) {
    else if(services_unknown_unacknowledged == 0 && services_warning_unacknowledged == 0 && services_critical_unacknowledged == 0 && hosts_down_unacknowledged == 0 && hosts_unreachabl
        sound = normal_sound;
    if(sound != NULL) {
-       printf("<object type=\"audio/x-wav\" data=\"%s%s\" height=\"1\" width=\"1\">", url_media_path, sound);
-       printf("<param name=\"filename\" value=\"%s%s\">", url_media_path, sound);
-       printf("<param name=\"autostart\" value=\"true\">");
-       printf("<param name=\"playcount\" value=\"1\">");
-       printf("</object>");
+       printf("<audio autoplay><source src=\"%s%s\" type=\"audio/x-wav\"></audio>", url_media_path, sound);
        }
 
 
--- ./cgi/status.c.audio    2017-08-24 21:43:48.000000000 +0000
+++ ./cgi/status.c  2020-05-07 13:28:53.000000000 +0000
@@ -441,11 +441,7 @@ int main(void) {
    else if(problem_services_unknown == 0 && problem_services_warning == 0 && problem_services_critical == 0 && problem_hosts_down == 0 && problem_hosts_unreachable == 0 && normal_sou
        sound = normal_sound;
    if(sound != NULL) {
-       printf("<object type=\"audio/x-wav\" data=\"%s%s\" height=\"1\" width=\"1\">", url_media_path, sound);
-       printf("<param name=\"filename\" value=\"%s%s\">", url_media_path, sound);
-       printf("<param name=\"autostart\" value=\"true\">");
-       printf("<param name=\"playcount\" value=\"1\">");
-       printf("</object>");
+       printf("<audio autoplay><source src=\"%s%s\" type=\"audio/x-wav\"></audio>", url_media_path, sound);
        }
 
    /* Special case where there is a host with no services */

and can be downloaded as nagios-0014-fix-audio.patch

First install build prerequisites:

yum install rpm-build gd-devel gperf libpng-devel  perl-generators libtool perl-Test-Simple perl-ExtUtils-Embed
yum install perl-generators perl-Test-More perl-Test-HTML-Lint perl-HTML-Lint --enablerepo=epel

Get and install source package matching your installed version - in our case nagios-4.3.4-7.el6.src.rpm. Place the above patch in ~/rpmbuild/SOURCES.

Add 2 lines below similar existing lines in 2 places in ~/rpmbuild/SPECS/nagios.specs:

Below last Patch…

Patch14: nagios-0014-fix-audio.patch

Below last %patch…

%patch14 -p1 -b .fix_audio

The number may be different in your version - in our version last patch was #13 so we used 14 for audio patch.

Build with rpmbuild -bb ~/rpmbuild/SPECS/nagios.spec. Now Nagios RPMS are in ~/rpmbuild/RPMS/x86_64. Extract the audio-related files and overwrite existing ones:

cd / && rm -f /usr/lib64/nagios/cgi-bin/tac.cgi && rpm2cpio ~/rpmbuild/RPMS/x86_64/nagios-4.3.4-7.el6.x86_64.rpm | cpio -idv ./usr/lib64/nagios/cgi-bin/tac.cgi
cd / && rm -f /usr/lib64/nagios/cgi-bin/status.cgi && rpm2cpio ~/rpmbuild/RPMS/x86_64/nagios-4.3.4-7.el6.x86_64.rpm | cpio -idv ./usr/lib64/nagios/cgi-bin/status.cgi

After refreshing Nagios page with a service/host in a state that produces sound alert, Firefox may show small strikethru speaker icon just before https:// in your address bar that indicates audio is blocked. Click on it and in Autoplay permissions choose Allow Audio & Video. The setting is for the current page only. For Chrome you may need one more line with hidden iframe. Look for ‘iframe delegation’.

Enjoy the audio again!

You may adapt the procedure to DEB-based distros or source code installation.