#!/bin/sh
#
# Nagios plugin to check the uniqueness of gpt partitioned devices
# written by Gerd Koenig <geko@deb.ymc.ch>
# and Daniel Beyer <dabe@deb.ymc.ch>
#
# Description:
#
# This plugin will return information about the
# uniqueness of gpt partitioned devices

. /usr/lib/lib-fliwi/fliwi-drives.sh

drives=$(fliwi_get_drives_with_partition_tables)
message="OK"
detected_guids=""
affected_devices=""
duplicate_detected=0
exit_code=0

for device in $drives
do
  guid=$(gdisk -l /dev/$device | grep "Disk identifier" | awk '{print $4}')
  if [ -n "$guid" ]; then
    if [ $(echo $detected_guids | grep -c $guid) -gt 0 ]; then
      duplicate_detected=1
      if [ -n "$affected_devices" ]; then
        affected_devices=$affected_devices","$device
      else
        affected_devices=$device
      fi
    fi
    detected_guids=$detetected_guids","$guid
  fi
done

if [ $duplicate_detected -eq 1 ]; then
  message="Duplicate disk GUID detected on: $affected_devices"
  exit_code=1
else
  message="OK"
  exit_code=0
fi


echo $message
exit $exit_code