Page 1 of 1

Trying to integrate Nagios Core with Slack

Posted: Fri Jul 26, 2024 1:53 pm
by justauser2
I've got Nagios monitoring my servers via IPMI and it's working well. Now I'm trying to get it to send alerts through Slack. The instructions seem easy enough and when I run the scripts manually they do generate slack alerts. The issue is that Nagios doesn't automatically run the scripts when an alert is generated and I'm pretty sure it's because there's no slack user defined. When I try to define a user for slack the service fails to run

These are the scripts I'm using https://github.com/obaarne/Nagios2Slack ... /README.md

They are in the plugins folder /usr/local/nagios/libexec/
I also copied the slack.cfg file to /usr/local/nagios/etc/objects/ because I wasn't sure the plugins directory was the right place for it
What am I missing?

slack.cfg

Code: Select all

define contact {
  contact_name                             slack
  alias                                    Slack
  host_notifications_enabled      1
  service_notifications_enabled   1
  service_notification_period              workhours
  host_notification_period                 workhours
  service_notification_options             c,r
  host_notification_options                d,r
  service_notification_commands            notify-service-by-slack
  host_notification_commands               notify-host-by-slack
}

define contactgroup {
  contactgroup_name systems
  alias Systems
  members slack
}

define command {
  command_name  notify-service-by-slack
  command_line  /usr/local/bin/slack_nagios_service $SERVICESTATE$ $HOSTNAME$ "$SERVICEDESC$" "$LONGSERVICEOUTPUT$" "$SERVICEOUTPUT$"
}

define command {
  command_name  notify-host-by-slack
  command_line  /usr/local/bin/slack_nagios_host $HOSTSTATE$ $HOSTNAME$
  

Contacts.cfg

Code: Select all

define contact {

    contact_name            nagiosadmin             ; Short name of user
    use                     generic-contact         ; Inherit default values from generic-contact template (defined above)
    alias                   Nagios Admin            ; Full name of user
    email                   nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}



###############################################################################
#
# CONTACT GROUPS
#
###############################################################################

# We only have one contact in this simple configuration file, so there is
# no need to create more than one contact group.

define contactgroup {

    contactgroup_name       admins
    alias                   Nagios Administrators
    members                 nagiosadmin,slack

}

Re: Trying to integrate Nagios Core with Slack

Posted: Tue Jul 30, 2024 11:21 am
by danderson
Thanks for reaching out @justauser2,

Are you saying that when you add the slack user, then Nagios fails to restart? What is the restart issue?

Make sure you are adding the config file to the main nagios.cfg file. The line will look like so

Code: Select all

cfg_file=/usr/local/nagios/etc/objects/slack.cfg

Re: Trying to integrate Nagios Core with Slack

Posted: Tue Jul 30, 2024 11:49 am
by lgute
Hi @justauser2, thanks for reaching out.

I would look at 3 things.
  1. If I understand correctly, the "slack_host_notify.sh" and "slack_service_notify.sh" scripts (from obaarne/Nagios2Slack) are now in your /usr/local/nagios/libexec folder.

    The slack.cfg you posted, shows the path to the scripts in the command definitions going to /usr/local/bin/slack_nagios_service and /usr/local/bin/slack_nagios_host, instead of /usr/local/nagios/libexec

    If you have not already, try changing the command_line(s) to the following...

    Code: Select all

      command_line  /usr/local/nagios/libexec/slack_service_notify $SERVICESTATE$ $HOSTNAME$ "$SERVICEDESC$" "$LONGSERVICEOUTPUT$" "$SERVICEOUTPUT$"
      command_line  /usr/local/nagios/libexec/slack_host_notify $HOSTSTATE$ $HOSTNAME$
    
  2. The other thing I noticed, is that your argument list for slack_service_notify is a bit different than what obaarne/Nagios2Slack shows in the example. Here's a comparison of the arguments listed by obaarne/Nagios2Slack, vs yours for the slack_nagios_service.

    Code: Select all

    obaarne/Nagios2Slack
      "$NOTIFICATIONTYPE$"  "$HOSTNAME$" "$HOSTADDRESS$" "$SERVICEDESC$" "$SERVICESTATE$" "$SERVICEOUTPUT$" "$LONGDATETIME$"
    justauser2
      $SERVICESTATE$ $HOSTNAME$ "$SERVICEDESC$" "$LONGSERVICEOUTPUT$" "$SERVICEOUTPUT$"
    
    I would try changing the arguments to match the example.
  3. Check that the owner/group and permissions for the scripts match the working scripts.