-
Notifications
You must be signed in to change notification settings - Fork 0
/
Performance_Monitor.sh
128 lines (104 loc) · 3.02 KB
/
Performance_Monitor.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/bash
# Performance Monitor Script
#
# This script will capture the performance of given program using OS specific tools and stores it in file.
# On Linux OS it will use "top" command to capture performance parameters.
# On Solaris OS it will use "prstat" command to capture performance parameters.
#
# Author : Sameer Mahant
#
# SCRIPT CONSTANTS
PROCESS_NAME=<your_process_name>
WAIT_SEC=5
# OS DEPENDENT CONSTANTS
OS=""
#
# This function will set script variables depending on operating system
# If the OS is not as per required it will exit the script
#
DetectOSAndSetScriptVariables() {
OSNAME=$(uname);
echo "Operating System:" $(uname -a)
echo ""
case $OSNAME in
Linux )
OS="linux"
;;
SunOS )
OS="solaris"
;;
* )
echo "Unknown Operating System." 1>&2
echo "Bye." 1>&2
exit 1
;;
esac
}
MonitorPerformanceOnLinux() {
# Get the PID from process name
pid=$(pgrep $PROCESS_NAME | tail -n 1)
if [ -z "$pid" -o "$pid" == " " ]; then
echo "Given process is not running."
exit;
fi
curr_time=$(date +"%T")
outfile=memory_profile_${pid}_$curr_time.log
echo "Time PID CPU VIRT RES SHR MEM" >> $outfile
while ! [ -z "$pid" -o "$pid" == " " ]
do
curr_time=$(date +"%T")
echo -n $curr_time "" | tee -a $outfile
COLUMNS=1024 top -n 1 -b p $pid | grep $pid | awk '{ print $1, $9, $5, $6, $7, $10 }' | tee -a $outfile
sleep $WAIT_SEC;
curr_pid=$(pgrep $PROCESS_NAME | tail -n 1)
if [ -z "$curr_pid" -o "$curr_pid" == " " ]; then
echo "Process is terminated."
exit 0;
fi
if [ "$curr_pid" -ne "$pid" ]; then
break;
fi
done
}
MonitorPerformanceOnSolaris() {
# Get the PID from process name
pid=$(pgrep $PROCESS_NAME | gtail -n 1)
if [ -z "$pid" -o "$pid" == " " ]; then
echo "Given process is not running."
exit;
fi
curr_time=$(date +"%T")
outfile=memory_profile_${pid}_$curr_time.log
echo "Time PID CPU RSS SIZE" >> $outfile
while ! [ -z "$pid" -o "$pid" == " " ]
do
curr_time=$(date +"%T")
printf $curr_time | tee -a $outfile
prstat -p $pid 1 1 | grep $pid | awk '{ print " " $1, $9, $4, $3 }' | tee -a $outfile
sleep $WAIT_SEC;
curr_pid=$(pgrep $PROCESS_NAME | gtail -n 1)
if [ -z "$curr_pid" -o "$curr_pid" == " " ]; then
echo "Process is terminated."
exit 0;
fi
if [ "$curr_pid" -ne "$pid" ]; then
break;
fi
done
}
#
# This function is the starting point of script
# It invokes other functions as required
#
main() {
DetectOSAndSetScriptVariables
if [ "$OS" == "linux" ]; then
echo "Monitoring on Linux"
MonitorPerformanceOnLinux
elif [ "$OS" == "solaris" ]; then
echo "Monitoring on Solaris"
MonitorPerformanceOnSolaris
fi
}
# call the main function
main