Search code examples
linuxubuntuslackchannelnagios

Nagios slack channel integration


I'm using slack_nagios.pl script. Ubuntu 14.05 and Nagios 4.4.5 https://raw.githubusercontent.com/tinyspeck/services-examples/master/nagios.pl I have one channel working, but I need to send notifications to different channels. In my case: warnings have go to "it_warning" and critical alerts must go to "it_critical". I have created 2 set of slack notification commands as below, but I only receive the alerts always in one channel (warning). Is like slack is not able to differ between the 2 commands, despite in the service_notification_options section in the "slack" contact has w,r and in the "slack_critical" has c,r.

No error is received, tested separately the channels works ok. Can you help help me or point me in the right direction?.

define contact {
   contact_name                     slack
   alias                            Slack
   service_notification_period      24x7
   host_notification_period         24x7
   service_notification_options     w,r
   host_notification_options        d,r
   service_notification_commands    notify-service-by-slack
   host_notification_commands       notify-host-by-slack
   }

define contact {
   contact_name                     slack_critical
   alias                            Slack_critical
   service_notification_period      24x7
   host_notification_period         24x7
   service_notification_options     c,r
   host_notification_options        d,r
   service_notification_commands    notify-service-critical-by-slack
   host_notification_commands       notify-host-critical-by-slack
   }

define contactgroup{
    contactgroup_name       admins
    alias                   Nagios Administrators
    members                 slack,nagiosadmin,slack_critical
    }


define command {
   command_name     notify-service-by-slack
   command_line     /usr/local/bin/slack_nagios.pl -field slack_channel=#it-warnings -field HOSTALIAS="$HOSTNAME$" -field SERVICEDESC="$SERVICEDESC$" -field SERVICESTATE="$SERVICESTATE$$
   }

define command {
   command_name     notify-host-by-slack
   command_line     /usr/local/bin/slack_nagios.pl -field slack_channel=#it-warnings -field HOSTALIAS="$HOSTNAME$" -field HOSTSTATE="$HOSTSTATE$" -field HOSTOUTPUT="$HOSTOUTPUT$" -field$
   }

    define command {
   command_name     notify-service-critical-by-slack
   command_line     /usr/local/bin/slack_nagios.pl -field slack_channel=#it-critical -field HOSTALIAS="$HOSTNAME$" -field SERVICEDESC="$SERVICEDESC$" -field SERVICESTATE="$SERVICESTATE$$
   }

define command {
   command_name     notify-host-critical-by-slack
   command_line     /usr/local/bin/slack_nagios.pl -field slack_channel=#it-critical -field HOSTALIAS="$HOSTNAME$" -field HOSTSTATE="$HOSTSTATE$" -field HOSTOUTPUT="$HOSTOUTPUT$" -field$
   }

Solution

  • I have found a solution to this. here the steps to configure warnings alert to be send to a particular channel and critical alerts to another one.

    You have to create 2 separate files with the contact, group and the commands as below, changing the channel, service_notification_options, service_notification_commands, host_notification_commands Do not forget to add into the file you have defined the service the contact_groups (will be 2). Please give me some likes if you can. Thanks!

    define contact {
      contact_name                             slack
      alias                                    Slack
      service_notification_period              24x7
      host_notification_period                 24x7
      service_notification_options             w,u,r
      host_notification_options                d,r
      service_notification_commands            notify-service-by-slack
      host_notification_commands               notify-host-by-slack 
      }
    
    define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin,slack
        }
    
    
    define command {
      command_name     notify-service-by-slack
      command_line     /usr/local/bin/slack_nagios.pl -field slack_channel=#it-alerts-test -field HOSTALIAS="$HOSTNAME$" -field HOSTSTATE="$HOSTSTATE$" -field HOSTOUTPUT="$HOSTOUTPUT$" -field NOTIFICATIONTYPE="$NOTIFICATIONTYPE$"
      }
    
    define command {
      command_name     notify-host-by-slack
      command_line     /usr/local/bin/slack_nagios.pl -field slack_channel=#it-alerts-test -field HOSTALIAS="$HOSTNAME$" -field HOSTSTATE="$HOSTSTATE$" -field HOSTOUTPUT="$HOSTOUTPUT$" -field NOTIFICATIONTYPE="$NOTIFICATIONTYPE$" 
      }
    
    
    define service {
    name                            generic-service  
    retain_status_information       1                   
    retain_nonstatus_information    1              
    is_volatile                     0                      
    check_period                    24x7                 
    max_check_attempts              3                      
    check_interval                  10                      
    retry_interval                  2   
    contact_groups                  admins,admins_critical  
    notification_interval           60                     
    notification_period             24x7
    }