#!/bin/bash

TYPE=${1}
URL=${2}
USER=${3}
PASSWORD=${4}
MULTIPATH_NAME=${5}
#REGEX="(^active;$|^active;active;$|^active;active;active;$|^active;active;active;active;$|^active;active;active;active;active;$|^active;active;active;active;active;active;$|^active;active;active;active;active;active;active;$)"

# Function zabbix external-check for discovering multipath on host.
function discovery_multipathing()
{
   counter=1
   input=$( cat | grep "config.multipathState.path" | sed 's/config.multipathState.path//g;s/^ *//;s/ /\n/g' | paste -d" " - - | cut -d" " -f 1 )
   echo -en "{ \"data\" : [ \n";

   for discoverying_multipath in $( echo "${input}" )
   do
      if [ $(echo $discoverying_multipath | grep -o ":" | wc -l) -eq '2' ]; then
         discoverying_multipath=$(echo $discoverying_multipath | awk -F ':' '{print $3}' | awk -F '.' '{print $1}')
      fi
      if [ ${#discoverying_multipath} -ge '3' ]
      then

         if [ ${counter} == "0" ]
            then echo -ne ",\n";
         fi

         counter=0
         echo -ne "{"
         echo -ne " \"{#STORAGE_MULTIPATH}\" : \"${discoverying_multipath}\"";
         echo -ne " }"

      fi
   done

   echo -ne "\n]";
   echo -ne "}\n";
}

# Function zabbix external-check for check discovering multipath on host. Need for trigger about missing data from API.
function check_discovery_multipathing()
{
   status="1"
   input=$( cat | grep "config.multipathState.path" )
   if [[ -z ${input} ]]
   then
      status="0"
   fi

   echo "${status}"

}

# Function zabbix external-check for get value of specify multipath name
function check_multipath()
{
   declare -A luns_status
   local MULTIPATH_NAME=${1}
   input=$( cat | grep "config.multipathState.path" | sed 's/config.multipathState.path//g;s/^ *//;s/ /\n/g' | paste -d" " - - | sed 's/$/;/' )

   while read line
   do
      KEY=$( echo "${line}" | awk '{ print $1 }' )
      if [ $( echo "${line}" | awk '{ print $1 }' | grep -o ":" | wc -l) -eq '2' ]; then
          KEY=$( echo "${line}" | awk '{ print $1 }' | awk -F ':' '{print $3}' | awk -F '.' '{print $1}' )
      fi
      VALUE=$( echo "${line}" | awk '{ print $2 }' )
         if [ ! -z ${KEY} ]
         then
            luns_status+=([${KEY}]=${VALUE})
         fi

   done <<< "$input"

   if [ -z "${luns_status[$MULTIPATH_NAME]}" ]
   then
      echo 0
      exit 0;
   fi
   COUNT_LUNS=$( echo "${luns_status[$MULTIPATH_NAME]}" | grep -o 'active;' | grep -c 'active;' )
   echo "${COUNT_LUNS}"

}

# Check type of check. Is this discovery or specify check of multipath name?
if [ "${TYPE}" == 'discovery' ]
then
   /usr/lib/zabbix/externalscripts/vsphere_api.py --user "${USER}" --no-cert-check --secret "${PASSWORD}" "${URL}" | discovery_multipathing

elif [ "${TYPE}" == 'check_discovery' ]
then
   /usr/lib/zabbix/externalscripts/vsphere_api.py --user "${USER}" --no-cert-check --secret "${PASSWORD}" "${URL}" | check_discovery_multipathing

elif [ "${TYPE}" == 'monitor' ]
then
   /usr/lib/zabbix/externalscripts/vsphere_api.py --user "${USER}" --no-cert-check --secret "${PASSWORD}" "${URL}" | check_multipath ${MULTIPATH_NAME}

fi

