Skip to content

Instantly share code, notes, and snippets.

@patmandenver
Created July 17, 2015 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patmandenver/855b2239585236799a78 to your computer and use it in GitHub Desktop.
Save patmandenver/855b2239585236799a78 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
#
# This script notifies slack
# when this machine starts up
# or shuts down
#
########################################
#This reads in the webhook URL from a file
#just replace it with you actual webhook URL
slack_url=`cat /etc/init.d/slack_url`
start_text="Name : `hostname`"
start_text+="\nIP : `ip route get 8.8.8.8 | awk '{print $NF; exit}'`"
stop_text+=$start_text
start_text+="\nMemory : `cat /proc/meminfo | grep MemTotal | awk '{mem= $2/1048576; printf("%0.2g GiB",
mem) ; exit}'`"
drive_info="\n Drives :"
drive_info="`df -h | grep Filesystem`"
drive_info+="\n`df -h | grep /dev/sd | sed ':a;N;$!ba;s/\n/\\\n/g'`"
case "$1" in
start)
curl -H "Content-type:application/json" \
-X POST -d \
'{
"channel":"#dev_ops",
"username" : "ESXi",
"icon_emoji" : ":esxi:",
"attachments" : [
{
"fallback": "Server Starting up!",
"color" : "good",
"fields" : [
{
"title" : "Server Starting Up!",
"value" : "'"$start_text"'",
"short" : "true"
},
{
"title" : "Drive Status",
"value" : "'"$drive_info"'",
"short" : "true"
}
]
}
]
}
' $slack_url
;;
stop)
curl -H "Content-type:application/json" \
-X POST -d \
'{
"channel":"#dev_ops",
"username" : "ESXi",
"icon_emoji" : ":esxi:",
"attachments" : [
{
"fallback": "Server Shutting Down!",
"color" : "good",
"fields" : [
{
"title" : "Server Shutting Down!",
"value" : "'"$stop_text"'",
"short" : "true"
}
]
}
]
}
' $slack_url
;;
*)
echo "INFO:Script used to send notifications to Slack Shutdown/Startup" >&2
exit 3
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment