Skip to content

Instantly share code, notes, and snippets.

@albertmatyi
Created November 16, 2016 18:13
Show Gist options
  • Save albertmatyi/00ba8b551f35f0ef31231c13291e31f2 to your computer and use it in GitHub Desktop.
Save albertmatyi/00ba8b551f35f0ef31231c13291e31f2 to your computer and use it in GitHub Desktop.
pingdome - Personal pingdom linux script/service

Pingdome

It is a simple script that checks if a website is up. If it's down, it notifies you via notify-send (linux UI notification system)

Prerequisites

  • curl
  • notify-send (Ubuntu: sudo apt-get install notify-osd | Arch pacman -S libnotify )

Installation

Simply copy all files to /opt/pingdome/

Usage

  • $ pingdome yoursite.com: Checks if yoursite.com is up, if it isn't it displays a notification
  • $ pingdomed yoursite.com: Does the pingdome check every 60 seconds
  • $ pingdome-install yoursite.com: Creates a systemd service with the name pingdome_yoursite.com, then enables and starts it which in turn runs pingdomed yoursite.com
  • $ pingdome-remove yoursite.com: Removes the created service

Notes

  • The service will only work for the current user & if it has the UID 1000 (otherwise pingdome-setup has to be updated)
#!/bin/bash -x
function checkDependency(){
type $1
if [[ "$?" != "0" ]]; then
echo "Please make sure you have $1 installed"
exit 1
fi
}
function doVerification() {
ADDRESS=$1
if [[ "$ADDRESS" == "" ]]; then
echo "Specify a website to check"
exit 1
fi
function checkAddress() {
curl -I $1 | grep -E "302|200|301"
}
checkAddress google.com
GOOGLE_DOWN=$?
checkAddress $ADDRESS
WEBSITE_DOWN=$?
if [[ "$GOOGLE_DOWN" != "$WEBSITE_DOWN" ]]; then
notify-send "$ADDRESS is down"
fi
}
checkDependency curl
checkDependency notify-send
doVerification $1
#!/bin/bash -xe
sudo systemctl stop pingdome_$1.service
sudo systemctl disable pingdome_$1.service
#!/bin/bash -xe
if [[ "$1" == "" ]]; then
echo "Please specify a target address"
exit 1
fi
ADDRESS=$1
SERVICE_NAME="pingdome_$ADDRESS"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
(
printf "\n" | cat | sudo tee /etc/systemd/system/$SERVICE_NAME.service <<- EndOfString
[Unit]
Description=Checks if $ADDRESS is up
[Service]
ExecStart=/usr/bin/su $USER -c "DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus $SCRIPT_DIR/pingdomed $ADDRESS"
[Install]
WantedBy=multi-user.target
EndOfString
)
sudo systemctl enable $SERVICE_NAME.service
sudo systemctl start $SERVICE_NAME.service
#!/bin/bash -x
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
while [[ true ]]; do
$SCRIPT_DIR/pingdome $1
sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment