The goal here is to get notification for something like CPU reached 90% or nginx access log reached 100 requests (GET 200 code responses).
Setup CRON
Schedule the script via CRON. Example every 5 or 15 minutes */5 * * * to call a specific script.
Create your bash script
That specific script will get information from another server like CPU reached 90% or nginx access log reached 100 requests (GET 200 code responses).
Example basic bash script
#!/bin/bash
temp_file=/tmp/nginx_count.txt
read_file=$(/usr/bin/ssh -i /Users/johnmarkcausing/.ssh/your_private_key [email protected] sudo -i -u johnmark /bin/bash /home/johnmark/count_200_requests.sh)
echo $read_file > $temp_file
send_to_slack=$(cat $temp_file)
if [ "$host_per_line_count" -ge "100" ]; then
# Send this to slack!
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\"Nginx alert! \`$send_to_slack\` Request\"}" https://hooks.slack.com/services/YOUR_HOOK_SLACK_API_URL
fi
So that code above mentioned a curl command with slack webhook url to send message to a slack channel.
Setup Slack Incoming Web Hook using this step by step guide.
Example screenshot from slack:
XBAR MacOS status/menu bar
Optional – You can use xbar app to display some data in your MacOS menu bar. Code ad screenshot below will refresh and display the data from slack message every 3 seconds. The file is slack.3s.sh – 3s = 3 seconds.
#!/bin/bash
# Setup path and JQ
export PATH="/usr/local/bin:$PATH"
JQ=$(command -v jq)
slack=$(curl -s -X GET -H 'Authorization: Bearer YOUR_AUTH_TOKE_API' "https://slack.com/api/conversations.history?channel=C025CKTRFR9&limit=1&pretty=1")
latest_slack_message=$($JQ -r ".messages | .[] | .text" <<< $slack)
echo $latest_slack_message
Retrieve a message from Slack.
Go to your slack channel options (drop down) -> Integrations -> Apps -> Add an App then select your app.
Testing slack retrieve message
We will use “conversations.history” method to get messages from a slack channel.
Tester Slack link: https://api.slack.com/methods/conversations.history/test
Go to slack app link here: https://api.slack.com/apps -> Select your app -> Go to OAuth & Permissions. See screenshots below to get your OAuth token and channel ID.
See this screenshot on how you can test for retrieving a message from slack. You will notice I put only 1 message under “limit” and make sure you put your OAuth and channel ID.