#!/bin/bash

# usage: ./vpxd_live_core
# Default creates the live core
# zip  argument is optional and is used for vc-support where we need to stream
#      the live core
usage ()
{
     echo "Usage: vpxd_live_core"
     echo "Default: captures live core under /var/core"
     exit
}

zip=0
core_dir="/var/core"

while getopts hz arg ; do
   case "$arg" in
      "h") usage ;;
      "z") zip=1 ;;
        *) usage ;;
   esac
done

# Create live core of vpxd
live_core_added=0
VMON_OUTPUT=`/usr/lib/vmware-vmon/vmon-cli -d vpxd`
# Assume the output is of the format:
# */var/core/livecore.vpxd-${Timestamp}.${VPXD_PID}
if [ $? -eq 0 ]; then
  core_file_name=${VMON_OUTPUT##*/}
  live_core_added=1
fi

if [ "${live_core_added}" -eq "0" ]; then
   echo "VPXD is not running. Cannot collect live core dump."
   exit 1
fi

# Create a tar ball and stream it to output so that it is captured by vc-support
if [ $zip == 1 ];then
   tar czf - ${core_dir}/${core_file_name} 2>/dev/null
else
   echo "`date --utc` Captured live core: ${core_dir}/${core_file_name}"
fi
