#!/bin/sh
#######################################################################
#
#Copyright @ 2007 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A.
#All rights reserved.U.S. Government Rights - Commercial software.  Government users are subject to the
#Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supplements.
#Use is subject to license terms.  Sun,  Sun Microsystems,  the Sun logo and  Sun Ray are trademarks or
#registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.UNIX is a registered trademark
#in the U.S. and other countries, exclusively licensed through X/Open Company, Ltd.
#
#SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
#THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
#TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
#PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
#ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
#DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
#
# Licensed 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.
#
## Source: sunrayhelp
## Application Name: Canary
## Application SIC Code:
## CVS ID: 
#
## Author: Sean Meighan, Wayne Chan, Gabriel Bolano
## Date Created: March 2004
#
## General Description(Purpose):  Simple System Monitoring Application is a simple yet powerful monitoring
## tool that lets you quickly identify error conditions on systems running
## Solaris (SPARC and x86) and Linux.   The Monitor is useful for locating
## and monitoring sources of unusual program load, such as runaway processes,
## memory leaks, problems with network interface traffic, etc.
#
## Usage: run by a user when he/she perceives a performance issue on sunray
#
## Required Parameters:
## Optional Parameters:
## Return Codes:
#
## Possible Points of Failure:
#
#
###################################################################################################
#
#	Script: sunrayhelp
#	Author:	Wayne Chan, Sean Meighan, Gabriel Bolano
#
#	Description: This script will be run by a user when he/she perceives a performance issue 
#		on his system. The resulting probe of data will be mailed to a performance alias.
#		We will execute in order:
#			showrev 
#			date -u +%Y%m%d%H%M%S    
#			psrinfo -v  | /usr/bin/grep operates | /usr/bin/sort | /usr/bin/uniq -c 
#			uptime 
#			prtconf -p -v | grep banner-name: 
#			swap -l
#                       vmstat  1 3
#                       iostat  1 3
#			ps -eo s,comm,user,pid  | egrep "^O|^R" | sort 
#			prstat -c -n 99,999 -a 1 1 
#
#			prompt user to select from a list of problems
#			prompt user for a free form description of problem
#			mail the report to an alias so data can be stored into a database
#
#	Comments: the output of this script will be stored in the local /tmp directory under the
#			format of /tmp/${l_node_name}.${l_domain}.${l_date_format}.${USER}.poll
#                       You there fore can do a
#
#		 ls -ltr /tmp/*.poll
#
#		to see all of the performance reports that have been generated. The last step of this
#		script is to email this report to an alias (currently kill9@sun.com). This email
#		will then be parsed and stored into an Oracle Database.
#
###################################################################################################

# force all source-in vars to auto-export

set -a
. ./canary_env.sh
. ./canary_env_static.sh
set +a

#
# set perms to 7777
#

## removed due to security audit
##/usr/bin/umask 000

## if login shell does not support $USER, use id cmd to find out unix id
[ -z "$USER" ] && USER=`id | sed -n -e "s/uid=.*(\(.*\)).*gid=.*(\(.*\))/\1/p"`

#
#       Get node and domain name for this machine
l_node_name=`/usr/bin/uname -n`
l_domain=$DOMAIN;

################################################################
#
# Lets get the system date
#
################################################################
l_date_format=`date -u +%Y%m%d%H%M%S`


## define output log 
TMP_OUT=/tmp/${l_node_name}.${l_domain}.${l_date_format}.${USER}.poll

## set fd3 to $TMP_OUT
exec 3>$TMP_OUT  


####
##
## set stdout to fd3 from now on... i.e. $TMP_OUT
##
## except progress messages for user will be sent to fd2
##
####
exec 1>&3

REVISION=`grep '^##.*Revision:' $0 | awk -F":" '{print $2}'`
REV_DATE=`grep '^##.*Date:' $0     | sed -n -e "s/^.*Date://p"`
echo "`basename $0`: $REVISION $REV_DATE"

################################################################
#
# Echo the system date
#
################################################################
echo "Date1: ${l_date_format}"


/usr/bin/printf "# Gathering some system data to send with your report ....\n" >&2

################################################################
#
# Running showrev to get system os specific information
#
################################################################

/usr/bin/printf "#   Running showrev....\n" >&2
/usr/bin/printf "#   Running showrev....\n"
/usr/bin/showrev
test $? -ne 0 && echo "Failed running showrev on ${l_node_name}" >&2



################################################################
#
# Lets run psrinfo to get the number of CPUs and the speed of CPUs
#
################################################################
/usr/bin/printf "#   Running psrinfo -v ....\n" >&2
/usr/bin/printf "#   Running psrinfo -v ....\n"
/usr/sbin/psrinfo -v | /usr/bin/grep operates | /usr/bin/sort | /usr/bin/uniq -c


################################################################
#
# Lets run prtconf to get system type information 
#
################################################################
/usr/bin/printf "#   Running prtconf -p -v ....\n" >&2
/usr/bin/printf "#   Running prtconf -p -v ....\n"
/usr/sbin/prtconf -p -v | grep "banner-name:"
/usr/sbin/prtconf | grep "Memory"


################################################################
#
# Lets run uptime to get system uptime stats and user counts info 
#
################################################################
/usr/bin/printf "#   Running uptime ....\n" >&2
/usr/bin/printf "#   Running uptime ....\n"
/usr/bin/uptime

################################################################
#
# Lets run vmstat 1 3 to capture system resources info
#
################################################################
/usr/bin/printf "#   Running vmstat 1 3 ....\n" >&2
/usr/bin/printf "#   Running vmstat 1 3 ....\n\n\n"
vmstat  1 3


################################################################
#
# Lets run iostat 1 3 to capture system I/O resources info
#
################################################################
/usr/bin/printf "#   Running iostat 1 3 ....\n" >&2
/usr/bin/printf "#   Running iostat 1 3 ....\n"
iostat  1 3

################################################################
#
# Lets run swap to capture system swap info  
#
################################################################
/usr/bin/printf "#   Running swap -l ....\n" >&2
/usr/bin/printf "#   Running swap -l ....\n"
/usr/sbin/swap -l



################################################################
#
# Lets run ps -eo s,comm,user,pid to gather processes that are
# running/ready-to-run
#
################################################################
/usr/bin/printf "#   Running ps -eo s,comm,user,pid | egrep \"\^O|\^R\" ....\n" >&2
/usr/bin/printf "#   Running ps -eo s,comm,user,pid | egrep \"\^O|\^R\" ....\n"
ps -eo s,comm,user,pid  | egrep "^O|^R" 


################################################################
#
# Lets run prstat for indepth processes info. 
#
#
################################################################
/usr/bin/printf "#   Running prstat -c -n 99,999 -a 1 1 ....\n" >&2
/usr/bin/printf "#   Running prstat -c -n 99,999 -a 1 1 ....\n"
prstat -c -n 99,999 -a 1 1



echo "performance issue reported by $USER from $l_node_name.$l_domain "


################################################################
#
# Lets get the system date
#
################################################################

l_date_format=`date -u +%Y%m%d%H%M%S`
echo "Date2: ${l_date_format}"



#########################################################
#
#	prompt user for a description of his performance
#
#########################################################

(
cat<<!
please identify your performance problem:
  1. Application: browsers (netscape/mozilla)
  2. Application: StarOffice
  3. Application: Gnome
  4. System: Characters type slow on screen
  5. System: My screen froze for some period of time
  6. other
!
) >&2

read y
case "$y" in
 1) problem="browser" 
    problem_desc="Application: browsers (netscape/mozilla)"
    ;;
 2) problem="soffice" 
    problem_desc="Application: StarOffice"
    ;;
 3) problem="gnome" 
    problem_desc="Application: Gnome"
    ;;
 4) problem="slow"
    problem_desc="System: Characters type slow on screen"
    ;;
 5) problem="freeze" 
    problem_desc="System: My screen froze for some period of time"
    ;;
 6|*) problem="other" 
    problem_desc="other"
    ;;
esac


###########
## set problem info header to $TMP_OUT
###########
(
cat<<!

####user input of problem info
#### $problem_desc

#>email: $mail
#> problem type: $problem

!
) >&3

(
cat<<!
pls enter a description of your problem. Information you might include: "
	1 What were you doing. Using spreadsheet, printing, entering a calender entry .etc."
	   IF slowness was in drawing thumbnails in startoffice for instance, please state it"
	2 What version of application if you know it, ie StarOffice 6.0 version c"
	3 How long did problem last? a- it is ongoing, b- few seconds, c- few minutes"

Any other information that might help us in isolating the performance issue"
!
) >&2


###########
## capture user input to fd3 i.e. $TMP_OUT
###########
(
while :
do
/usr/bin/printf "period (.) to exit ? " >&2
/usr/bin/printf "#? " >&3
read xyz
echo $xyz
if [ "$xyz" = "." ]; then
 break
fi
done
) >&3

############
## end of user input
############



##
#### done
##
/usr/bin/printf "####data collection done ...\n" 
/usr/bin/printf "# Thank you $USER. Your report has been sent\n" >&2

upload_method=${CANARY_UPLOAD_METHOD:-nfs}
NFS_DIR="/net/${CANARY_SERVER}"

case "${upload_method}" in

## NFS upload
##
nfs)
sh -x canary_probe_upload.sh  ${TMP_OUT} $NFS_DIR/${CANARY_DATA_DIR}/poll/`basename ${TMP_OUT}`
;; ## end of NFS upload

ftp)
sh -x canary_probe_upload.sh  ${TMP_OUT} ${CANARY_FTPDIR}/poll/`basename ${TMP_OUT}`
;; ## end of ftp upload

scp)
sh -x canary_probe_upload.sh  ${TMP_OUT} $NFS_DIR/${CANARY_DATA_DIR}/poll/`basename ${TMP_OUT}`
;; ## end of scp upload

mail)
sh -x canary_probe_upload.sh "x|sunrayhelp|`basename ${TMP_OUT}`" ${TMP_OUT}
;; ## end of mail upoad

esac
## 

#cleanup
rm ${TMP_OUT}
