Skip to content

Instantly share code, notes, and snippets.

@matt448
Last active February 13, 2023 15:38
Show Gist options
  • Save matt448/8200821 to your computer and use it in GitHub Desktop.
Save matt448/8200821 to your computer and use it in GitHub Desktop.

Revisions

  1. matt448 revised this gist Apr 8, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion slack_nagios.sh
    Original file line number Diff line number Diff line change
    @@ -15,7 +15,7 @@
    # Twitter: @slackhq, @slackapi
    #
    # My info
    # Website: matthewmcmillan.blogspot.com
    # Website: http://matthewcmcmillan.blogspot.com/
    # Twitter: @matthewmcmillan

    #Modify these variables for your environment
  2. matt448 revised this gist Dec 31, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion slack_nagios.sh
    Original file line number Diff line number Diff line change
    @@ -44,4 +44,4 @@ else
    fi

    #Send message to Slack
    curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOTNAME}\", \"text\": \"${ICON} HOST: ${NAGIOS_HOSTNAME} SERVICE: ${NAGIOS_SERVICEDISPLAYNAME} MESSAGE: ${NAGIOS_SERVICEOUTPUT} <https://${MY_NAGIOS_HOSTNAME}/cgi-bin/nagios3/status.cgi?host=${NAGIOS_HOSTNAME}|See Nagios>\"}" https://${SLACK_HOSTNAME}/services/hooks/incoming-webhook?token=${SLACK_TOKEN}
    curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"text\": \"${ICON} HOST: ${NAGIOS_HOSTNAME} SERVICE: ${NAGIOS_SERVICEDISPLAYNAME} MESSAGE: ${NAGIOS_SERVICEOUTPUT} <https://${MY_NAGIOS_HOSTNAME}/cgi-bin/nagios3/status.cgi?host=${NAGIOS_HOSTNAME}|See Nagios>\"}" https://${SLACK_HOSTNAME}/services/hooks/incoming-webhook?token=${SLACK_TOKEN}
  3. matt448 revised this gist Dec 31, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions slack_nagios.sh
    Original file line number Diff line number Diff line change
    @@ -20,8 +20,8 @@

    #Modify these variables for your environment
    MY_NAGIOS_HOSTNAME="nagios.yourdomain.com"
    SLACK_HOSTNAME=yourslack.slack.com"
    SLACK_TOKEN=xyxyxyourslackkey
    SLACK_HOSTNAME="yourslack.slack.com"
    SLACK_TOKEN="xyxyxyourslackkey"
    SLACK_CHANNEL="#alerts"
    SLACK_BOTNAME="nagios"

  4. matt448 created this gist Dec 31, 2013.
    47 changes: 47 additions & 0 deletions slack_nagios.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/bash

    # This script is used by Nagios to post alerts into a Slack channel
    # using the Incoming WebHooks integration. Create the channel, botname
    # and integration first and then add this notification script in your
    # Nagios configuration.
    #
    # All variables that start with NAGIOS_ are provided by Nagios as
    # environment variables when an notification is generated.
    # A list of the env variables is available here:
    # http://nagios.sourceforge.net/docs/3_0/macrolist.html
    #
    # More info on Slack
    # Website: https://slack.com/
    # Twitter: @slackhq, @slackapi
    #
    # My info
    # Website: matthewmcmillan.blogspot.com
    # Twitter: @matthewmcmillan

    #Modify these variables for your environment
    MY_NAGIOS_HOSTNAME="nagios.yourdomain.com"
    SLACK_HOSTNAME=“yourslack.slack.com"
    SLACK_TOKEN=“xyxyxyourslackkey”
    SLACK_CHANNEL="#alerts"
    SLACK_BOTNAME="nagios"


    #Set the message icon based on Nagios service state
    if [ "$NAGIOS_SERVICESTATE" = "CRITICAL" ]
    then
    ICON=":exclamation:"
    elif [ "$NAGIOS_SERVICESTATE" = "WARNING" ]
    then
    ICON=":warning:"
    elif [ "$NAGIOS_SERVICESTATE" = "OK" ]
    then
    ICON=":white_check_mark:"
    elif [ "$NAGIOS_SERVICESTATE" = "UNKNOWN" ]
    then
    ICON=":question:"
    else
    ICON=":white_medium_square:"
    fi

    #Send message to Slack
    curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOTNAME}\", \"text\": \"${ICON} HOST: ${NAGIOS_HOSTNAME} SERVICE: ${NAGIOS_SERVICEDISPLAYNAME} MESSAGE: ${NAGIOS_SERVICEOUTPUT} <https://${MY_NAGIOS_HOSTNAME}/cgi-bin/nagios3/status.cgi?host=${NAGIOS_HOSTNAME}|See Nagios>\"}" https://${SLACK_HOSTNAME}/services/hooks/incoming-webhook?token=${SLACK_TOKEN}