#!/usr/bin/env bash
#
# Filter incoming messages and determine if we should collect PH data
# This filter is activated by isi_ph_trace
#
# For more information please view the isi_ph_trace help:
# isi_ph_trace --help


while read -t 1 STDIN; do
  if [[ $STDIN == *"nfs response from client"* && $STDIN == *"was slow"* ]]; then
    PCDATA_COUNT=`find /ifs/pcdata -maxdepth 1 -type d | wc -l`
    if [ $PCDATA_COUNT -le 10 ]; then
      PCDATA_OUT=`timeout -k 30 14m isi_ph_dump -r -l -t 30`
      if [ -z "$PCDATA_OUT" ]; then
        logger "PCData collection timed out"
      else
        logger $PCDATA_OUT
      fi
    else
      logger "Not collecting PCData for slow IO, maximum number of collections has been reached"
    fi
  else
    exit 0
  fi
done
