#!/bin/sh
# Author: Piotr Zalewski
# Maintainer: IBM WebSphere Support, WASSDK team, Piotr Zalewski
#
# Java Shared Resources Monitor
# Monitors /tmp/javasharedresources on AIX and Linux.
# When the directory is created, the script captures timestamped ps and ls -la
# snapshots to help identify which process likely created it and what files
# were present at that time.
#
# Useful for troubleshooting Java SDK shared resources behavior. MONITOR_DIR
# can also be changed to watch another directory if needed.
#
# Maintainer: IBM WebSphere Support, WASSDK team, Piotr Zalewski
INTERVAL=1 # in seconds

SCRIPT_VERSION="2026.06.08"
MONITOR_DIR='/tmp/javasharedresources'
SCREEN_FILE='screen.out'
PID_FILE="/tmp/javasharedmon.pid"
DIR_NAME="javasharedmon_results.$(date +%Y%m%d.%H%M%S)"

####################
# Check supported OS
####################
OS=$(uname)
if [ "$OS" != "AIX" -a "$OS" != "Linux" ]; then
    echo "Unsupported OS: $OS"
    exit 1
fi

#################################################
# Check if javasharedmon script is already running
#################################################
if [ -e "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null; then
    echo "javasharedmon script is already running with PID $(cat "$PID_FILE")"
    echo ""
    exit 1
fi
# Output the script's PID to the pid file. 
echo $$ > "$PID_FILE"

#####################################
# Create a temporary output directory
#####################################
mkdir -p $DIR_NAME
if [ $? -ne 0 ]; then
  echo "Failed to create $DIR_NAME."
  exit 1
fi
# Go into the created output directory and
# get the full path to let the user know the current directory.
cd $DIR_NAME
if [ $? -ne 0 ]; then
  echo "Failed to go into $DIR_NAME."
  exit 1
fi
readonly OUTPUT_DIR="$(pwd)"


# Returns current timestamp. 
get_stamp(){
  echo $(date '+%Y-%m-%d %H:%M:%S')
}

# Returns compact timestamp
get_compact_timestamp(){
  echo "$(date '+%Y%m%d.%H%M%S')"
}

# Trap ctrl+c (INT signal) and kill -15 (TERM) to remove javasharedmon.pid and exit. 
cleanup(){
  rm -f $PID_FILE
  exit 0
}
trap cleanup INT TERM

# Write a message to $SCREEN_FILE
log(){
  # $1 - message
  echo $(get_stamp)  "    $1" | tee -a "$SCREEN_FILE"
}

generate_ps_snapshot(){
  # $1 - output file name
  filename=$(get_compact_timestamp).$1
  echo $(get_stamp) >> $filename
  log "Executing ps command to $filename"
  if [ "$OS" = "Linux" ];then
    ps -eo user,pid,ppid,tname,lstart,args >> $filename
  elif [ "$OS" = "AIX" ]; then
    (ps -eo user,pid,ppid,tty,etime,args | head -1; ps -eo user,pid,ppid,tty,etime,args | tail +2 | sort -r -k5) >> $filename 2>&1 
  fi
}

generate_ls_snapshot(){
  # $1 - output file name
  filename=$(get_compact_timestamp).$1
  echo $(get_stamp) >> $filename
  log "Executing ls command to $filename"
  ls -la "$MONITOR_DIR" >> $filename 2>&1
}

generate_env_out(){
  # $1 - output file name
  filename=$(get_compact_timestamp).$1
  log "Executing a ps command with env variables to $filename"
  echo $(get_stamp) >> $filename
  if [ "$OS" = "Linux" ]; then
    ps axo pid,user,command e >> $filename
  elif [ "$OS" = "AIX" ]; then
    #ps -A -o pid,user,args >> $filename 2>&1
    ps ewww >> $filename 2>&1
  fi
  #ps ewww >> $filename
  echo "" >> $filename
}


##############################################################################
# Monitors $MONITOR_DIR and performs the following:
# - Call generate_ps_snapshot() and generate_ls_snapshot() 
#   when the directory is created. 
# - Continuously monitor for directory removal and re-creation.
##############################################################################
directory_detected=0
check_javashared_directory(){
  if [ -d "$MONITOR_DIR" ]; then
    if [ "$directory_detected" -eq 0 ]; then
      log "$MONITOR_DIR directory detected."
      generate_ps_snapshot "ps.out"
      generate_env_out "ps.env.out"
      generate_ls_snapshot "ls.out"
      log "Snapshot captured in $OUTPUT_DIR. You may stop the script and send this directory to IBM Support for analysis."
      log "Or remove $MONITOR_DIR and leave the script running to capture another snapshot if the directory is created again."
      log "Continuously monitoring"
      directory_detected=1
    fi
  fi
  if [ "$directory_detected" -eq 1 ] && [ ! -d "$MONITOR_DIR"  ]; then 
    log "$MONITOR_DIR no longer exists."
    directory_detected=0
  fi
}

log "$OUTPUT_DIR directory created."
log "Script version: $SCRIPT_VERSION"
log "Starting monitor of $MONITOR_DIR"

while true; do
  check_javashared_directory 
  if  [ "$OS" = "Linux" ]; then
    sleep $INTERVAL
  elif [ "$OS" = "AIX" ]; then
    perl -e "select(undef,undef,undef,$INTERVAL)"
  fi
done
