#!/bin/bash
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
#
# Produce process status for Trafodion processes.
# The output goes to stdout and may be displayed to the user or used
# by other scripts that need to obtain a list of all Trafodion
# processes.
#
# The list of Trafodion programs in variable PROGS below must include
# all Trafodion programs.   Addition of a new Trafodion program requires
# a corresponding addition to PROGS.
#

# Distinguished program commands for benefit of pkillall
SAFEPROGS=monitor,sqwatchdog

# Trafodion program and script command names.
# Add new Trafodion programs and scripts to this list.
# Observe the following:
# 1. KEEP THE LIST IN ALPHABETICAL ORDER: A-Z then a-z
# 2. Separate each entry with a comma.
# 3. NO SPACES WHATSOEVER; this list is an argument to ps -C.
PROGS=PTPA,TPA_Publish,UNA,amp,backout,check_ntp_peer,compression_publish,\
cpu_metrics,dp2,dtmci,filesystem_metrics,gomon.cold,harness.pl,idtmsrv,import,\
lc_core_metrics_sh,lc_disk_metrics_sh,lc_filesystem_metrics_sh,\
lc_loadavg_metrics_sh,lc_memory_metrics_sh,lc_network_metrics_sh,\
lc_virtualmem_metrics_sh,lunmgr,lx_disk_metrics,lx_loadavg_metrics,\
lx_physnode_metrics,measurestats,memory_metrics,monitor_check_status_m5,\
mpid,mpirun,mxbdrdrc,mxbdrsrv,mxlobsrvr,mxoas,mxocfg,mxosrvr,mxsscp,mxssmp,\
network_metrics,nsa,ntp_peer_check,ntp_peer_flow,problem_management,\
process_metrics,pstartd,qpidd,qscom,qsmgr,qsmgr2,qsoffndr,qsrecev,\
qsrule,qssend,qsstats,qssync,se_metrics_aaa_delta,serevive,shell,\
snmp_protocol_adapter,sp_proxy,sp_wrapper,spqpid_p,sqcheck,sqlci,sqregck,\
sqstart,ssd_lun_metric_publish,ssd_lun_state_publish,ssd_metric_publish,\
ssd_state_publish,tdm_arkcmp,tdm_arkesp,tdm_sqlc,tdm_udrserv,\
textprotocoladapter,timer_pop_generator,tm,tmshutdown,universal_action_agent,\
universal_consumer,volrcov


function usage {

typeset MYNAME="$(basename $0)"
cat <<EOF

usage:  $MYNAME [-h] [-l] [-s] [-H]

-h      Suppress header

-l      List Trafodion programs.  When -s is also used,
        list only programs used in safe mode.

-s      Safe mode, does not show distinguished programs:
                $SAFEPROGS

-H      Show help text

EOF
}

SUPPRESS_HEADER=0
LIST=0
while getopts ":hlsH" Option
do
  case $Option in
    h) SUPPRESS_HEADER=1 ;;
    l) LIST=1 ;;
    s) SAFE=1 ;;
    H) usage
       exit 0;;
  esac
done
shift $(($OPTIND - 1))

if [[ -z $SAFE ]]; then
  PROGS="$PROGS,$SAFEPROGS"
fi

if (( LIST == 1 )); then
    echo "$PROGS"
  exit 0
fi

if (( SUPPRESS_HEADER == 0 )); then
  echo "uid          pid   ppid  wchan   rss   vsz   time     stat cmd"
  echo "---          ---   ----  -----   ---   ---   ----     ---- ---"
fi




ps --sort=cmd,pid -C $PROGS -o user:12,pid,ppid,wchan,rss,vsz,time,stat,cmd | grep -w ^$USER | grep -v '<defunct>'$
