From 5fa283671045b2dc934c3d09596c515c249e7835 Mon Sep 17 00:00:00 2001 From: Shivinder <6997655+shivinder@users.noreply.github.com> Date: Thu, 14 Apr 2022 14:30:48 +1000 Subject: [PATCH 1/8] decorated the readme and added additional info --- README.md | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ec93be7..31d7cb1 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,22 @@ -These scripts are designed to be run via a cron job with the MAILTO variable set to generate an email(or any other notification method) when output occurs. +# Description -Output will occur when an IP is added/removed or changed for the service you are checking. +The supplied bash scripts are supposed to be run via a cron job and assume that the MAILTO variable in the cron is set to send out a notification email when any IP address change happens. -- `check_events.sh` - Checks events.pagerduty.com for changed IP addressses. +## When the IP addresses change -- `check_mailservers.sh` - Checks acme.pagerduty.com for changed MX record IP addressses. -(Used when you utilize an email integrated service and need to whitelist outgoing traffic to PagerDuty) +The script(s) will display a list of all the new IP addresses fetched from the server on the stdout and will send out a notification email to the email address set in the MAILTO variable in the cron. -- `check_webhooks.sh` - Checks webhooks.pagerduty.com for changed IP addressses. +This notification email may then be captured to initiate an incident in PagerDuty. -- `check_webhooks_and_alert.sh` - Same as `check_webhooks.sh` but triggers a PagerDuty incident. +## When the IP addresses do not change + +There is no change in the files or output on the stdout and no notification email is sent out. + +## Description of the files in the repository + +| File name | Remarks | +|-|-| +| `check_events.sh` | Checks events.pagerduty.com for changed IP addressses. | +| `check_mailservers.sh` | Checks `acme.pagerduty.com` for changed MX record IP addressses. (Used when you utilize an email integrated service and need to whitelist outgoing traffic to PagerDuty) | +| `check_webhooks.sh` | Checks `webhooks.pagerduty.com` for changed IP addressses. | +| `check_webhooks_and_alert.sh` | Checks `webhooks.pagerduty.com` for changed IP addressses, same as `check_webhooks.sh`, but also triggers a PagerDuty incident. | From a3192266d15bf1ae424eafda7ebb2f1cc6f3f0c1 Mon Sep 17 00:00:00 2001 From: Shivinder <6997655+shivinder@users.noreply.github.com> Date: Thu, 14 Apr 2022 14:32:37 +1000 Subject: [PATCH 2/8] changed to the more universal/portable shebang --- check_events.sh | 2 +- check_mailservers.sh | 2 +- check_webhooks.sh | 2 +- check_webhooks_and_alert.sh | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/check_events.sh b/check_events.sh index 43b95bc..88a4a64 100755 --- a/check_events.sh +++ b/check_events.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ## This script is designed to be run via a cron job with the MAILTO variable set to generate an email when output occurs (an IP is added/removed/changed) diff --git a/check_mailservers.sh b/check_mailservers.sh index e62d3c7..1e7ed2c 100755 --- a/check_mailservers.sh +++ b/check_mailservers.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ## This script is designed to be run via a cron job with the MAILTO variable set to generate an email when output occurs (an IP is added/removed/changed) diff --git a/check_webhooks.sh b/check_webhooks.sh index 6654112..7b902b1 100755 --- a/check_webhooks.sh +++ b/check_webhooks.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ## This script is designed to be run via a cron job with the MAILTO variable set to generate an email when output occurs (an IP is added/removed/changed) diff --git a/check_webhooks_and_alert.sh b/check_webhooks_and_alert.sh index 506af77..6730dd3 100755 --- a/check_webhooks_and_alert.sh +++ b/check_webhooks_and_alert.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Webhook delivery service IP address change notification script # # Same as check_webhooks.sh, but triggers a PagerDuty incident if there are changes. From a3c6f96417ef3f51744d26628cb8d6b95e43ef2a Mon Sep 17 00:00:00 2001 From: Shivinder <6997655+shivinder@users.noreply.github.com> Date: Thu, 14 Apr 2022 15:25:59 +1000 Subject: [PATCH 3/8] modified the readme further --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 31d7cb1..5d272c0 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,30 @@ The supplied bash scripts are supposed to be run via a cron job and assume that the MAILTO variable in the cron is set to send out a notification email when any IP address change happens. +# Prerequisites to run this script + +The scripts depend on the external `dig` command to get the list of IP addresses from the PagerDuty servers. You should be able to install the `dig` command using the following steps: + +## On RedHat-based GNU/Linux servers + +Install it from the `bind-utils` package available in the repositories. + +Command: `dnf install bind-utils` + +You will require root permissions to install the package. + +## On Debian-based GNU/Linux servers + +Install it from the `dnsutils` package available in the repositories. + +Command: `apt install dnsutils` + +You will require root permissions to install the package. + +# Output + +It is assumed that there are two scenario's to each script run, which are: + ## When the IP addresses change The script(s) will display a list of all the new IP addresses fetched from the server on the stdout and will send out a notification email to the email address set in the MAILTO variable in the cron. @@ -12,7 +36,7 @@ This notification email may then be captured to initiate an incident in PagerDut There is no change in the files or output on the stdout and no notification email is sent out. -## Description of the files in the repository +# Description of the files in the repository | File name | Remarks | |-|-| From 6af029baa6d821e79f16f1845b2bda692053ccd9 Mon Sep 17 00:00:00 2001 From: Shivinder <6997655+shivinder@users.noreply.github.com> Date: Thu, 14 Apr 2022 15:54:40 +1000 Subject: [PATCH 4/8] reduced disk access by moving to mem vars --- check_events.sh | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/check_events.sh b/check_events.sh index 88a4a64..1a305ec 100755 --- a/check_events.sh +++ b/check_events.sh @@ -1,18 +1,29 @@ #!/usr/bin/env bash -## This script is designed to be run via a cron job with the MAILTO variable set to generate an email when output occurs (an IP is added/removed/changed) +# More information on PagerDuty Events - https://support.pagerduty.com/docs/event-management +# PagerDuty Status Page - https://status.pagerduty.com -if [ -f "events_result.txt" ]; then - mv events_result.txt events_result.txt.old -fi +## This script is designed to be run via a cron job with the MAILTO variable +## set to generate an email when output occurs (an IP is added/removed/changed) -dig +short events.pagerduty.com | sort > events_result.txt +# get the list of IP addresses from the PagerDuty Events URL. Storing in a memory variable to reduce disk access +events_results_current = $(dig +short events.pagerduty.com | sort) -if [ -f "events_result.txt.old" ]; then - DIFF=$(diff -q 'events_result.txt.old' 'events_result.txt' > /dev/null) +if [ -f "events_results.txt" ]; then + # we check for the diff and supress any outputs on the stdout + DIFF=$(echo "${events_results_current}" | diff -q 'events_results.txt' - > /dev/null) + + # diff command returns a status code 0 if no change has been detected if [ $? -ne 0 ]; then - echo "Changes detected! New IPs are:" - cat events_result.txt + # The script has detected that the list of IP addresses has changed. + # we overwrite the existing file with the changed IP addresses. + echo ${events_results_current} > events_results.txt + # display the output on stdout. MAILTO cron variable takes over the job of sending out an email. + echo -e "\nThe script has detected a change in PagerDuty's Events IP addresses. The new IP addresses are:\n\n${events_results_current}" fi - rm events_result.txt.old -fi +else + # we fall in this condition when we run the script for the first time + # or if the events_results.txt has been deleted + # we create a new one and spit the output on the stdout too + echo ${events_results_current} | tee events_results.txt +fi \ No newline at end of file From cf2de2a9dcadb9ad92d7920c58155fc170e8f2c3 Mon Sep 17 00:00:00 2001 From: Shivinder <6997655+shivinder@users.noreply.github.com> Date: Thu, 14 Apr 2022 16:21:04 +1000 Subject: [PATCH 5/8] moved the comparision logic to mem vars --- check_mailservers.sh | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/check_mailservers.sh b/check_mailservers.sh index 1e7ed2c..10eecb0 100755 --- a/check_mailservers.sh +++ b/check_mailservers.sh @@ -1,18 +1,26 @@ #!/usr/bin/env bash -## This script is designed to be run via a cron job with the MAILTO variable set to generate an email when output occurs (an IP is added/removed/changed) +## This script is designed to be run via a cron job with the MAILTO variable +## set to generate an email when output occurs (an IP is added/removed/changed) -if [ -f "mailservers_result.txt" ]; then - mv mailservers_result.txt mailservers_result.txt.old -fi +# get the list of IP addresses from the PagerDuty acme URL. Storing in a memory variable to reduce disk access +mailservers_results_current = $(dig +short mx acme.pagerduty.com | sed 's/.$//g' | sed 's/^[0-9][0-9]* //g' | xargs dig +short | sort) -dig +short mx acme.pagerduty.com | sed 's/.$//g' | sed 's/^[0-9][0-9]* //g' | xargs dig +short | sort > mailservers_result.txt +if [ -f "mailservers_results.txt" ]; then + # we check for the diff and supress any outputs on the stdout + DIFF=$(echo "${mailservers_results_current}" | diff -q 'mailservers_results.txt' - > /dev/null) -if [ -f "mailservers_result.txt.old" ]; then - DIFF=$(diff -q 'mailservers_result.txt.old' 'mailservers_result.txt' > /dev/null) + # diff command returns a status code 0 if no change has been detected if [ $? -ne 0 ]; then - echo "Changes detected! New IPs are:" - cat mailservers_result.txt + # The script has detected that the list of IP addresses has changed. + # we overwrite the existing file with the changed IP addresses. + echo ${mailservers_results_current} > mailservers_results.txt + # display the output on stdout. MAILTO cron variable takes over the job of sending out an email. + echo -e "\nThe script has detected a change in PagerDuty's Events IP addresses. The new IP addresses are:\n\n${mailservers_results_current}" fi - rm mailservers_result.txt.old -fi +else + # we fall in this condition when we run the script for the first time + # or if the mailservers_results.txt has been deleted + # we create a new one and spit the output on the stdout too + echo ${mailservers_results_current} | tee mailservers_results.txt +fi \ No newline at end of file From bf3d2e60357f239ae0c29bdcdbdc49100f4a6a17 Mon Sep 17 00:00:00 2001 From: Shivinder <6997655+shivinder@users.noreply.github.com> Date: Thu, 14 Apr 2022 16:22:45 +1000 Subject: [PATCH 6/8] added the PD status page link --- check_mailservers.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/check_mailservers.sh b/check_mailservers.sh index 10eecb0..40483d3 100755 --- a/check_mailservers.sh +++ b/check_mailservers.sh @@ -1,5 +1,7 @@ #!/usr/bin/env bash +# PagerDuty Status Page - https://status.pagerduty.com + ## This script is designed to be run via a cron job with the MAILTO variable ## set to generate an email when output occurs (an IP is added/removed/changed) From fa7b3dc0f2cb23f04f1fab3cf96bd38f8549d15e Mon Sep 17 00:00:00 2001 From: Shivinder <6997655+shivinder@users.noreply.github.com> Date: Thu, 14 Apr 2022 16:27:38 +1000 Subject: [PATCH 7/8] fixed a typo --- check_mailservers.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check_mailservers.sh b/check_mailservers.sh index 40483d3..c68044c 100755 --- a/check_mailservers.sh +++ b/check_mailservers.sh @@ -18,7 +18,7 @@ if [ -f "mailservers_results.txt" ]; then # we overwrite the existing file with the changed IP addresses. echo ${mailservers_results_current} > mailservers_results.txt # display the output on stdout. MAILTO cron variable takes over the job of sending out an email. - echo -e "\nThe script has detected a change in PagerDuty's Events IP addresses. The new IP addresses are:\n\n${mailservers_results_current}" + echo -e "\nThe script has detected a change in PagerDuty Mailserver's IP addresses. The new IP addresses are:\n\n${mailservers_results_current}" fi else # we fall in this condition when we run the script for the first time From 7596dff6783c6b66f75b0c7c2556dc1009c9eb20 Mon Sep 17 00:00:00 2001 From: Shivinder <6997655+shivinder@users.noreply.github.com> Date: Thu, 14 Apr 2022 16:33:32 +1000 Subject: [PATCH 8/8] moved the comparision logic to mem vars --- check_webhooks.sh | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/check_webhooks.sh b/check_webhooks.sh index 7b902b1..8b95976 100755 --- a/check_webhooks.sh +++ b/check_webhooks.sh @@ -1,18 +1,29 @@ #!/usr/bin/env bash -## This script is designed to be run via a cron job with the MAILTO variable set to generate an email when output occurs (an IP is added/removed/changed) +# PagerDuty Webhooks Documentation - https://support.pagerduty.com/docs/webhooks +# PagerDuty Status Page - https://status.pagerduty.com -if [ -f "webhooks_result.txt" ]; then - mv webhooks_result.txt webhooks_result.txt.old -fi +## This script is designed to be run via a cron job with the MAILTO variable +## set to generate an email when output occurs (an IP is added/removed/changed) -curl -s https://app.pagerduty.com/webhook_ips | tr -d \[\]\" | tr , '\n' | sort > webhooks_result.txt +# get the list of IP addresses from the PagerDuty Webhooks URL. Storing in a memory variable to reduce disk access +webhooks_results_current = $(curl -s https://app.pagerduty.com/webhook_ips | tr -d \[\]\" | tr , '\n' | sort) -if [ -f "webhooks_result.txt.old" ]; then - DIFF=$(diff -q 'webhooks_result.txt.old' 'webhooks_result.txt' > /dev/null) +if [ -f "webhooks_results.txt" ]; then + # we check for the diff and supress any outputs on the stdout + DIFF=$(echo "${webhooks_results_current}" | diff -q 'webhooks_results.txt' - > /dev/null) + + # diff command returns a status code 0 if no change has been detected if [ $? -ne 0 ]; then - echo "Changes detected! New IPs are:" - cat webhooks_result.txt + # The script has detected that the list of IP addresses has changed. + # we overwrite the existing file with the changed IP addresses. + echo ${webhooks_results_current} > webhooks_results.txt + # display the output on stdout. MAILTO cron variable takes over the job of sending out an email. + echo -e "\nThe script has detected a change in PagerDuty Webhook's IP addresses. The new IP addresses are:\n\n${webhooks_results_current}" fi - rm webhooks_result.txt.old -fi +else + # we fall in this condition when we run the script for the first time + # or if the webhooks_results.txt has been deleted + # we create a new one and spit the output on the stdout too + echo ${webhooks_results_current} | tee webhooks_results.txt +fi \ No newline at end of file