-
Notifications
You must be signed in to change notification settings - Fork 14
/
process-metrics-collector.sh
executable file
·149 lines (114 loc) · 4.12 KB
/
process-metrics-collector.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# process id to monitor
pid=$1
if [ -z $1 ]; then
echo "ERROR: Process ID not specified."
echo
echo "Usage: $(basename "$0") <PID>"
exit 1
fi
# check if process exists
kill -0 $pid > /dev/null 2>&1
pid_exist=$?
if [ $pid_exist != 0 ]; then
echo "ERROR: Process ID $pid not found."
exit 1
fi
current_time=$(date +"%Y_%m_%d_%H%M")
dir_name="data/${pid}-${current_time}"
csv_filename="${dir_name}/metrics.csv"
# create data directory
mkdir -p $dir_name
# Read collected metrices from the CSV file and plot graphs
#
# This function will end script execution.
#
# This function is to be called after an interrupt like SIGINT or SIGKILL
# is received.
#
function plotGraph() {
# bring cursor to next line after interrupt
echo
# plot graphs if there is a data file
if [ -f $csv_filename ]; then
echo "Plotting graphs..."
gnuplot <<- EOF
# Output to png with a font size of 10, using pngcairo for anti-aliasing
set term pngcairo size 1024,800 noenhanced font "Helvetica,10"
# Set border color around the graph
set border ls 50 lt rgb "#939393"
# Hide left and right vertical borders
set border 16 lw 0
set border 64 lw 0
# Set tic color
set tics nomirror textcolor rgb "#939393"
# Set horizontal lines on the ytics
set grid ytics lt 1 lc rgb "#d8d8d8" lw 2
# Rotate x axis lables
set xtics rotate
# Set graph size relative to the canvas
set size 1,0.85
# Set separator to comma
set datafile separator ","
# Move legend to the bottom
set key bmargin center box lt rgb "#d8d8d8" horizontal
# Plot graph,
# xticlabels(1) - first column as x tic labels
# "with lines" - line graph
# "smooth unique"
# "lw 2" - line width
# "lt rgb " - line style color
# "t " - legend labels
#
# CPU and memory usage
set output "${dir_name}/cpu-mem-usage.png"
set title "CPU and Memory Usage for Proces ID $pid"
plot "$csv_filename" using 2:xticlabels(1) with lines smooth unique lw 2 lt rgb "#4848d6" t "CPU Usage %",\
"$csv_filename" using 3:xticlabels(1) with lines smooth unique lw 2 lt rgb "#b40000" t "Memory Usage %"
# TCP count
set output "${dir_name}/tcp-count.png"
set title "TCP Connections Count for Proces ID $pid"
plot "$csv_filename" using 4:xticlabels(1) with lines smooth unique lw 2 lt rgb "#ed8004" t "TCP Connection Count"
# Thread count
set output "${dir_name}/thread-count.png"
set title "Thread Count for Proces ID $pid"
plot "$csv_filename" using 5:xticlabels(1) with lines smooth unique lw 2 lt rgb "#48d65b" t "Thread Count"
# All together
set output "${dir_name}/all-metrices.png"
set title "All Metrics for Proces ID $pid"
plot "$csv_filename" using 2:xticlabels(1) with lines smooth unique lw 2 lt rgb "#4848d6" t "CPU Usage %",\
"$csv_filename" using 3:xticlabels(1) with lines smooth unique lw 2 lt rgb "#b40000" t "Memory Usage %", \
"$csv_filename" using 4:xticlabels(1) with lines smooth unique lw 2 lt rgb "#ed8004" t "TCP Connection Count", \
"$csv_filename" using 5:xticlabels(1) with lines smooth unique lw 2 lt rgb "#48d65b" t "Thread Count"
EOF
fi
echo "Done!"
exit 0
}
# add SIGINT & SIGTERM trap
trap "plotGraph" SIGINT SIGTERM SIGKILL
echo "Writing data to CSV file $csv_filename..."
touch $csv_filename
# write CSV headers
echo "Time,CPU,Memory,TCP Connections,Thread Count" >> $csv_filename
# check if process exists
kill -0 $pid > /dev/null 2>&1
pid_exist=$?
# collect until process exits
while [ $pid_exist == 0 ]; do
# check if process exists
kill -0 $pid > /dev/null 2>&1
pid_exist=$?
if [ $pid_exist == 0 ]; then
# read cpu and mem percentages
timestamp=$(date +"%b %d %H:%M:%S")
cpu_mem_usage=$(top -b -n 1 | grep -w -E "^ *$pid" | awk '{print $9 "," $10}')
tcp_cons=$(lsof -i -a -p $pid -w | tail -n +2 | wc -l)
tcount=$(ps -o nlwp h $pid | tr -d ' ')
# write CSV row
echo "$timestamp,$cpu_mem_usage,$tcp_cons,$tcount" >> $csv_filename
sleep 5
fi
done
# draw graph
plotGraph