#!/bin/sh
#Version History:
#1.0.00 Added Basic Monitoring
#1.1.00 Added function Expires
#1.1.01 Added Version Check
#1.2.00 Added conf File
#1.3.00 Added Basic Backup Check
#1.3.01 Enhanced Backup Check 
#1.3.02 Enhanced Backup Check 
#1.3.03 Enhanced Backup Check 
#1.3.04 Fixed Backup Check Errors 
#1.3.05 Added Instance Name Variable
#1.3.06 Enhanced Backup Check
#1.3.07 Enhanced Backup Check
#1.3.08 Enhanced Backup Check
#1.3.09 Enhanced Backup Check 
#1.3.10 Enhanced Backup Check
#1.3.11 Fixed Backup Check  
#1.3.12 Enhanced Backup Check
#1.3.13 Fixed IndexServer Check
#1.3.14 Enhanced Backup Check
#1.4.00 IndexServer Check Enhanced
#1.4.01 Fixed IndexServer Check
#1.4.02 Enhanced Backup Check
#1.5.00 Backup Cloud Check
source /hana/scripts/hana.conf
function Version(){
echo 1.5.00
}
function Indexserver() {
if /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList | grep -q "Indexserver-$IndexServerName, GREEN, Running"; then
  echo "1"
 else
  echo "0"
fi
}

function Locked() {
if "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT LOCKED_DOWN FROM M_LICENSE;'| grep -q '"TRUE"'; then
  echo "yes"
 else
  echo "no"
fi
}

function Permanent() {
if "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'select EXPIRATION_DATE from M_LICENSE'| grep -q '?'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'select EXPIRATION_DATE from M_LICENSE'| grep -q '2099-12-31'; then
  echo "yes"
 else
  echo "no"
fi
}

function Expires() {
if "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^10$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^9$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^8$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^7$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^6$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^5$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^4$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^3$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^2$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^1$'; then
  echo "yes"
elif "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT DAYS_BETWEEN(CURRENT_DATE, EXPIRATION_DATE) AS License_Info FROM M_LICENSE;' |grep -q '^0$'; then
  echo "yes"
 else
  echo "no"
fi
 }

function RAM() {
if "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT count(*) FROM M_LICENSE WHERE "PRODUCT_USAGE" >= "PRODUCT_LIMIT";' | grep -q '0'; then
  echo "no"
 else
  echo "yes"
fi
}

function Alerts() {
if "/hana/shared/$InctanceName/HDB00/exe/hdbsql" -i "00" -d "SYSTEMDB" -n "127.0.0.1" -u "$dbusr" -p "$dbPass" 'SELECT "ALERT_ID"  FROM "_SYS_STATISTICS"."STATISTICS_CURRENT_ALERTS";' | grep -q 'ALERT_ID
0 rows selected'; then
  echo "0"
 else
  echo "1"
fi
}

function Backup() {
if cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Backup HANA System exit code: 3'; then
  echo "HANA Backup Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Backup HANA System exit code: 1'; then
  echo "HANA Backup Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Upload full backup files to iSystems cloud - exit code: 1'; then
  echo "Upload to Cloud/SMB Share Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Upload full backup files to iSystems cloud - exit code: 3'; then
  echo "Upload to Cloud/SMB Share Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Upload full backup files to iSystems cloud - exit code: 255'; then
  echo "Upload to Cloud/SMB Share Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Reclaim process - exit code: 1'; then
  echo "Reclaim process Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Reclaim process - exit code: 2'; then
  echo "Reclaim process Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Reclaim process - exit code: 3'; then
  echo "Reclaim process Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Defragment process - exit code: 1'; then
  echo "Defragment process Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Defragment process - exit code: 2'; then
  echo "Defragment process Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Defragment process - exit code: 3'; then
  echo "Defragment process Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Clean process - exit code: 1'; then
  echo "Clean process Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR - Clean process - exit code: 3'; then
  echo "Clean process Failed"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'rsync error'; then
  echo "rsync error found"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'exit code: 1'; then
  echo "Undefined Error found"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'exit code: 3'; then
  echo "Undefined Error found"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'WARNING'; then
  echo "Undefined Error found"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'error'; then
  echo "Undefined Error found"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'Error'; then
  echo "Undefined Error found"
elif cat /var/log/SAP/backupcontrol.log| grep -q 'ERROR'; then
  echo "Undefined Error found"
 else
  echo "No Error found"
fi
}

function CloudBackup() {
if cat /hana/scripts/backup.conf | grep -q 'isysCLOUD="1"'; then
  echo "Cloud Backup enabled"
 else
  echo "Cloud Backup disabled"
fi
}

case "$1" in
    Version)
        Version
        ;;
    Locked)
        Locked
        ;;
	Indexserver)
        Indexserver
        ;;
	Permanent)
        Permanent
        ;;
	Expires)
        Expires
        ;;
	RAM)
        RAM
        ;;
	Alerts)
        Alerts
        ;;
	Backup)
        Backup
        ;;
	CloudBackup)
        CloudBackup
        ;;
	*)
        echo "Usage: $0 {Version|Locked|Indexserver|Permanent|Expires|RAM|Alerts|Backup}"
        exit 1
        ;;
esac


