#!/bin/bash 
#
# StackX Pingdom Provisionner (SPP)
# spp.sh / pad_pingdom.stackx.sh
# Author: Christophe Casalegno / Brain 0verride
# Contact: brain@christophe-casalegno.com
# Version 1.1.0
#
# Copyright (c) 2020 Christophe Casalegno
# 
# This program is free software: you can redistribute it and/or modify
#
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>
#
# The license is available on this server here: 
# https://www.christophe-casalegno.com/licences/gpl-3.0.txt

# /spp.sh -n "Customer (Brain 0verride)" -h "host (brain.stackx.run)" -u "monito_url /Brain0verridebrain.php" -t "SX" -a "remove/add"
#
# Don't forget to replace .cfg file in source pingdom-sx.cfg bye your .cfg.  
# Example:
#
# NOTIFTIME='11'
# APIURL='https://api.pingdom.com/api/3.1/checks/'
# USERIDS='66666666'
# TEAMIDS='123456'
# TOKEN='smldfksdlmkflmsdkfmsldkflmsdkfsdoekfeosdkfldkfmdlskfdlskf'

while getopts ":n:h:u:t:a:" OPT 
do
	OPTIONS_FOUND=1

	case ${OPT} in 

		n) CUSTOMER_NAME="$OPTARG" ;;
		h) HOST2CHECK="$OPTARG" ;;
		u) URL2CHECK="$OPTARG" ;;
		t) TEMPLATE="$OPTARG" ;;
		a) ACTION="$OPTARG" ;;
		\?) 
			echo -e "Invalid option: - $OPTARG" >&2 
			exit 1
			;;
		:)	echo -e "Option -$OPTARG requires an argument" >&2
			exit 1
			;;
	esac
done

if [[ "$OPTIONS_FOUND" -ne '1' ]]
then
	echo -e "no option found, I can't work without option"
fi

function template_monitoring()
{
	TEMPLATE="$1"

	if [[ "$TEMPLATE" = 'SX' ]]
	then
		source pingdom-sx.cfg
	else
		source pingdom-sx.cfg
	fi
}

function add_pingdom_monitoring()
{
	POST_COMMAND='curl --silent -X POST'

	$POST_COMMAND $APIURL -H "Authorization:Bearer $TOKEN"  -d name="$CUSTOMER_NAME" -d type="http" -d "resolution=1" \
	-d "sendnotificationwhendown=$NOTIFTIME" -d "notifywhenbackup=true" \
	-d "host=$HOST2CHECK" -d "url=$URL2CHECK" \
	-d "encryption=false" -d "userids=$USERIDS" -d "teamids=$TEAMIDS" \
	-d "shouldnotcontain=ERROR"
}

function remove_pingdom_monitoring()
{
	DELETE_COMMAND='curl --silent -X DELETE'

	IDCHECK=$(curl --silent $APIURL -H "Authorization:Bearer $TOKEN" |python -mjson.tool |grep -w "hostname\|id" |grep -w "$HOST2CHECK" -A1  |grep -w '"id"' |grep -o '[[:digit:]]*')
	
	$DELETE_COMMAND $APIURL -H "Authorization:Bearer $TOKEN" -d "delcheckids=$IDCHECK" -d "type=http" 

}

function check_action()
{
	ACTION="$1"

	if [[ "$ACTION" = 'add' ]] 
	then
		add_pingdom_monitoring
	
	elif [[ "$ACTION" = 'remove' ]]
	then
		remove_pingdom_monitoring
	
	else
		echo -e "no action found, doing nothing"
		exit 1
	fi
}

template_monitoring

check_action $ACTION 
