I created a
simple linux function to be used to send post to slack rooms via the slack
Incoming Webhooks integration
Simple Script
If you don't know
how to set up the webhook integration in Slack check out this video to get you
up to speed.
Download the
script (it's a gist on github @ https://gist.github.com/patmandenver/1876ad8070fcbb06a0cb ), and save it to the /etc/init.d folder
> sudo wget -O
/etc/init.d/slack_functions https://gist.githubusercontent.com/patmandenver/1876ad8070fcbb06a0cb/raw/45d89e02527cc9963864d75b5474d00f990f51ae/slack_functions
|
Just in case here
is the text of the script.
And post the following
into it.
#!/bin/bash
#
#
Slack Helper Functions
#
#The
MIT License (MIT)
#
#Copyright
(c) 2015 "whiteboardcoder" Patrick Bailey
#
#Permission
is hereby granted, free of charge, to any person obtaining a copy
#of
this software and associated documentation files (the "Software"),
to deal
#in
the Software without restriction, including without limitation the rights
#to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies
of the Software, and to permit persons to whom the Software is
#furnished
to do so, subject to the following conditions:
#
#The
above copyright notice and this permission notice shall be included in all
#copies
or substantial portions of the Software.
#
#THE
SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR
#IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
#
########################
#The
Slack URL for your webhook
#SLACK_URL_HOOK=`cat
/etc/init.d/slack_url`
SLACK_URL_HOOK="https://hooks.slack.com/services/REPLACE_WITH_YOUR_URL!!!"
#SLACK_URL_HOOK
CHECK
if
[[ $SLACK_URL_HOOK == *"REPLACE_WITH_YOUR_URL!!!"* ]]
then
echo "Update your SLACK_URL_HOOK
Variable!"
exit 1
fi
set_variables()
{
USERNAME=$1
EMOJI=$2
ROOM=$3
TITLE_1="$4"
MSG_1="$5"
TITLE_2="$6"
MSG_2="$7"
}
post()
{
if [ -n "$TITLE_2" ] && [
-n "$MSG_2" ]
then
post_double_msg
elif [ -n "$TITLE_1" ]
&& [ -n "$MSG_1" ]
then
post_single_msg
else
echo "No Message provided"
return 1
fi
}
post_single_msg()
{
curl -H
"Content-type:application/json" \
-X POST -d \
'{
"channel" :
"#'"$ROOM"'",
"username" :
"'"$USERNAME"'",
"icon_emoji" :
":'"$EMOJI"':",
"attachments" : [
{
"fallback" :
"'"$TITLE_1"'",
"color" :
"'"$COLOR"'",
"fields" : [
{
"title" :
"'"$TITLE_1"'",
"value" :
"'"$MSG_1"'"
}
]
}
]
}' $SLACK_URL_HOOK
}
post_double_msg()
{
curl -H
"Content-type:application/json" \
-X POST -d \
'{
"channel" :
"#'"$ROOM"'",
"username" :
"'"$USERNAME"'",
"icon_emoji" :
":'"$EMOJI"':",
"attachments" : [
{
"fallback" :
"'"$TITLE_1"'",
"color" :
"'"$COLOR"'",
"fields" : [
{
"title" :
"'"$TITLE_1"'",
"value" :
"'"$MSG_1"'",
"short" : true
},
{
"title" :
"'"$TITLE_2"'",
"value" :
"'"$MSG_2"'",
"short" : true
}
]
}
]
}' $SLACK_URL_HOOK
}
info()
{
COLOR="#d3d3d3"
set_variables "$1" "$2"
"$3" "$4" "$5" "$6" "$7"
post
}
good()
{
COLOR="good"
set_variables "$1" "$2"
"$3" "$4" "$5" "$6" "$7"
post
}
warning()
{
COLOR="warning"
set_variables "$1" "$2"
"$3" "$4" "$5" "$6" "$7"
post
}
bad
() {
COLOR="danger"
set_variables "$1" "$2"
"$3" "$4" "$5" "$6" "$7"
post
}
|
Make sure to
change the SLACK_URL_HOOK
to your own.
Now as a test let
me create something that will notify one of my slack rooms when my machine
boots up.
> sudo vi /etc/init.d/slack_boot_shutdown_notification
|
And place the
following in it.
#!/bin/bash
#
#
Send notifcation to slack room
#
########################################
#
source slack function library
.
/etc/init.d/slack_functions
#
Set username and their emoji
USERNAME="OpenStack"
EMOJI="openstack"
#
Set which room to post to
ROOM="dev_ops"
start()
{
TITLE_1="Server is Starting Up"
MSG_1="Name :
`hostname`"
MSG_1+="\nIP : `ip route get 8.8.8.8 | awk '{print $NF;
exit}'`"
MSG_1+="\nMemory :
`cat /proc/meminfo | grep MemTotal | awk '{mem= $2/1048576;
printf("%0.2g GiB", mem) ; exit}'`"
MSG_1+="\nUptime : `uptime -p`"
TITLE_2="Drive Status"
MSG_2="`df -h | grep Filesystem`"
MSG_2+="\n`df -h | grep /dev/sd`"
info $USERNAME $EMOJI $ROOM "$TITLE_1" "$MSG_1"
"$TITLE_2" "$MSG_2"
good $USERNAME $EMOJI $ROOM "$TITLE_1" "$MSG_1"
"$TITLE_2" "$MSG_2"
warning $USERNAME $EMOJI $ROOM "$TITLE_1" "$MSG_1"
"$TITLE_2" "$MSG_2"
bad $USERNAME $EMOJI $ROOM "$TITLE_1" "$MSG_1"
"$TITLE_2" "$MSG_2"
}
stop()
{
TITLE_1="Server is Shutting Down"
MSG_1="Name :
`hostname`"
MSG_1+="\nIP : `ip route get 8.8.8.8 | awk '{print $NF;
exit}'`"
MSG_1+="\nMemory :
`cat /proc/meminfo | grep MemTotal | awk '{mem= $2/1048576;
printf("%0.2g GiB", mem) ; exit}'`"
MSG_1+="\nUptime : `uptime -p`"
good $USERNAME $EMOJI $ROOM
"$TITLE_1" "$MSG_1"
}
case
"$1" in
start)
start
;;
stop)
stop
;;
*)
echo "Usage: $0 {start|stop}"
esac
|
You can see you
pass it
·
Your
Username
·
An
emoji for the user posting
·
Room
to post to
·
Title
of first message
·
First
Message
·
(Optional)
Title of Second message
·
(Optional)
Second Message
Make it
executable
> sudo chmod
u+x /etc/init.d/slack_boot_shutdown_notification
|
> sudo
update-rc.d slack_boot_shutdown_notification defaults
|
Now test it.
> sudo service slack_boot_shutdown_notification
start
> sudo service
slack_boot_shutdown_notification stop
|
Happy Slacking!!
No comments:
Post a Comment