#!/bin/sh

##########################################################################
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

# Variables
CURL=/usr/bin/curl
CERT_PATH=/etc/nagios/globus/robotcert.pem
KEY_PATH=/etc/nagios/globus/robotkey.pem

if [ "x$1" != "x" ]; then
  PERUN_URL_BASE=$1
else
  PERUN_URL_BASE=https://perun.egi.eu/cert/rpc/
fi

# Query Perun RPC
RET=`$CURL -s -4 -u nobody:nopass --cert $CERT_PATH --key $KEY_PATH ${PERUN_URL_BASE}json/utils/getPerunStatus`

# Get PerunDB version
VERSION=$(echo $RET | sed 's/.*"Version of PerunDB: \([^"]*\).*/\1/')

# Evaluate the result
if [ $(echo $VERSION | grep -E "^[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*$" | wc -l) -eq 1 ]; then
  echo "OK - Version of PerunDB: $VERSION"
  exit 0
else
  echo "CRITICAL - Returned: $RET"
  exit 2
fi

