#!/bin/bash helpFunction() { echo "Usage: $0 -H -p -P " echo " -p: port default 8080" echo " -P: Project Name WITHOUT URL-Encoded" } host_port=8080 while getopts "H:p:P:h" opt do case "$opt" in H ) host_ip="$OPTARG" ;; p ) host_port="$OPTARG" ;; P ) project_name="$OPTARG" ;; h ) help=1 ;; esac done if [ "$help" == 1 ] then helpFunction exit 0 fi # Confirm required args if [ -z "$host_ip" ] || [ -z "$project_name" ] then echo "Command argument error:Some or all of the parameters are empty."; helpFunction exit 3 fi urlencode() { local string="$1" local length="${#string}" for ((i = 0; i < length; i++)); do local char="${string:i:1}" case $char in [a-zA-Z0-9.~_-]) printf "$char" ;; *) printf '%%%02X' "'$char" ;; esac done } project_urlencoded=$(urlencode "$project_name") apiurl=http://$host_ip:$host_port/job/$project_urlencoded/lastBuild/api/json res_json=$(curl -f $apiurl) || { echo "CRITICAL - Failed to fetch data from: $apiurl" exit 3 } res_result=$(echo "$res_json" | jq -r '.result') if [ "$res_result" == "SUCCESS" ]; then echo "OK - Build was successful!" exit 0 else echo "CRITICAL - Build failed! Please check response: $apiurl" exit 2 fi