File: sargraph2

package info (click to toggle)
sysstat 12.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 4,372 kB
  • sloc: ansic: 30,491; sh: 1,082; tcl: 756; makefile: 561; perl: 257; python: 202
file content (294 lines) | stat: -rwxr-xr-x 7,597 bytes parent folder | download | duplicates (3)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash


set -e


# Our dependencies - bail out if they are not found
SADF=`which sadf`
GNUPLOT=`which gnuplot`


# Default for --sa-file is today's file
# (by default sadf will show today's logs)
SA_FILE=""

# Default for --sa-dir
SA_DIR="/var/log/sysstat"

# Default for --type
GRAPH_TYPE=CPU


GNUPLOT_TERMINAL="pngcairo font \"Sans,8\" size 640,320"
SAFILES_REGEX='/sar?[0-9]{2,8}(\.(Z|gz|bz2|xz|lz|lzo))?$'
GRAPH_TYPES="cpu,mem,io"


print_usage()
{
    echo "
Usage
	sargraph2 [--type $GRAPH_TYPES] [--sa-file INFILE] [--output OUTFILE]

Examples

To plot CPU for today no arguments are needed:
	sargraph2
To plot MEM for 20150330:
	sargraph2 --type mem --sa-file /var/log/sa/sa20150330.gz
To plot I/O for 20150330 to a PNG file:
	sargraph2 --type io --sa-file /var/log/sa/sa20150330.gz --output io.png

"
}


######### OPTION PARSING ###########
parsed_opts=`getopt -o "h" \
    -l help,sa-dir:,sa-file:,output:,type: \
    -- "$@"`
eval set -- "$parsed_opts"

DONE=no
while [ $DONE != yes ]
do
    case $1 in
    -h|--help)
        print_usage
        exit
        shift
        ;;
    --sa-dir)
        shift
        SA_DIR="$1"
        ;;
    --sa-file)
        shift
        SA_FILE="$1"
        ;;
    --output)
        shift
        OUT_FILE="$1"
        ;;
    --type)
        shift
        GRAPH_TYPE=`echo $1 | tr a-z A-Z`
        ;;
    --)
        # End of options
        DONE=yes
        ;;
    *)
        echo Unexpected argument: $1
        exit 1
        ;;
    esac

    shift                      # should shift the last arg or '--' if DONE=yes
done
####################################


# Decompress a file to another, or just copy it if not compressed
decompress_copy()
{
    if [ x$# != x2 ]
    then
        echo "Function decompress_copy requires 2 arguments but got $#: $*"
        return 1
    fi

    if [ -f "$1" ]
    then
        case "$1" in

        *.Z) uncompress -c "$1" > "$2" ;;
        *.gz)  gzip    -dc "$1" > "$2" ;;
        *.bz2) bzip2   -dc "$1" > "$2" ;;
        *.xz)  xz      -dc "$1" > "$2" ;;
        *.lz)  lzip    -dc "$1" > "$2" ;;
        *.lzo) lzop    -dc "$1" > "$2" ;;

        *) cp "$1" "$2"        ;;

        esac
    else
        echo "$1: file not found" 1>&2
        return 1
    fi
}




# TODO send systemd patch
#soupermouf;60;2015-04-01 04:24:00;-1;5.77;0.00;2.10;0.63;0.00;91.50
#soupermouf;26846;2015-04-01 11:51:26;-1;0.00;0.00;0.00;0.00;0.00;0.00
#soupermouf;60;2015-04-01 11:52:26;-1;7.11;0.01;2.69;68.44;0.00;21.75

cpu_gnuplot()                             # %iowait,%user,%nice,%system,%steal
{
    # First make sure that the columns are where we expect them to be
    HEADER=`egrep -v ';LINUX-RESTART|;COM' $2 | head -1 | cut -d ';' -f 5-10`
    EXPECTED="%user;%nice;%system;%iowait;%steal;%idle"
    if [ "$HEADER" != "$EXPECTED" ]
    then
        echo "Headers not in right order: $HEADER" 1>&2
        exit 1
    fi

    cat > $1 <<EOF
set title "sar -u"
set xdata time
set format x "%H:%M"
set ylabel "Percentage"
set yrange [0:100]
set timefmt "%Y-%m-%d %H:%M:%S"
set datafile separator ";"
plot \
    "$2" using 3:(\$9+\$7+\$6+\$5+\$8) title "%iowait" with filledcurves x1 linecolor rgb "light-gray", \
    "$2" using 3:(\$9+\$7+\$6+\$5)     title "%user"   with filledcurves x1 linecolor rgb "dark-green", \
    "$2" using 3:(\$9+\$7+\$6)         title "%nice"   with filledcurves x1 linecolor rgb "greenyellow", \
    "$2" using 3:(\$9+\$7)             title "%system" with filledcurves x1 linecolor rgb "red", \
    "$2" using 3:(\$9)                 title "%steal"  with filledcurves x1 linecolor rgb "purple"
EOF
}


mem_gnuplot()                    # free,used,buffers,cached / %commit,%swpused
{
    # First make sure that the columns are where we expect them to be
    HEADER=`egrep -v ';LINUX-RESTART|;COM' $2 | head -1 | cut -d ';' -f 4-10`
    EXPECTED="kbmemfree;kbmemused;%memused;kbbuffers;kbcached;kbcommit;%commit"
    if [ "$HEADER" != "$EXPECTED" ]
    then
        echo "Headers not in right order: $HEADER" 1>&2
        exit 1
    fi
    HEADER=`egrep -v ';LINUX-RESTART|;COM' $3 | head -1 | cut -d ';' -f 4-6`
    EXPECTED="kbswpfree;kbswpused;%swpused"
    if [ "$HEADER" != "$EXPECTED" ]
    then
        echo "Headers not in right order: $HEADER" 1>&2
        exit 1
    fi

    cat > $1 <<EOF
set title "sar -r"
set xdata time
set format x "%H:%M"
set ylabel "Memory Usage in MB"
set yrange [0:]
set y2label "Percentage"
set y2range [0:]
set y2tics
set timefmt "%Y-%m-%d %H:%M:%S"
set datafile separator ";"
set key on bottom center opaque
plot \
    "$2" using 3:((\$5+\$4)/1024)     title "free"    with filledcurves x1 linecolor rgb "light-gray", \
    "$2" using 3:((\$5)/1024)         title "cached"  with filledcurves x1 linecolor rgb "dark-green", \
    "$2" using 3:((\$5-\$8)/1024)     title "buffers" with filledcurves x1 linecolor rgb "greenyellow", \
    "$2" using 3:((\$5-\$8-\$7)/1024) title "used"    with filledcurves x1 linecolor rgb "red", \
    \
    "$2" using 3:(\$10)    title "%commit"  with lines linecolor rgb "blue"   linewidth 2 axes x1y2, \
    "$3" using 3:(\$6)     title "%swpused" with lines linecolor rgb "dark-magenta" linewidth 2 axes x1y2
EOF
}


io_gnuplot()                                      # %KBr/s,KBw/s / rIOPS,wIOPS
{
    # First make sure that the columns are where we expect them to be
    HEADER=`egrep -v ';LINUX-RESTART|;COM' $2 | head -1 | cut -d ';' -f 4-8`
    EXPECTED="tps;rtps;wtps;bread/s;bwrtn/s"
    if [ "$HEADER" != "$EXPECTED" ]
    then
        echo "Headers not in right order: $HEADER" 1>&2
        exit 1
    fi

    cat > $1 <<EOF
set title "sar -b"
set xdata time
set format x "%H:%M"
set ylabel "MB/s"
set yrange [0:]
set y2label "IOPS"
set y2range [0:]
set y2tics
set timefmt "%Y-%m-%d %H:%M:%S"
set datafile separator ";"
plot \
    "$2" using 3:((\$8+\$7)/2/1024) title "rMB/s" with filledcurves x1 linecolor rgb "dark-green", \
    "$2" using 3:((\$8)/2/1024)     title "wMB/s" with filledcurves x1 linecolor rgb "dark-red", \
    \
    "$2" using 3:(\$4) title "rIOPS"  with lines linecolor rgb "light-green"   linewidth 2 axes x1y2, \
    "$2" using 3:(\$6) title "wIOPS"  with lines linecolor rgb "light-red" linewidth 2 axes x1y2
EOF
}




if [ x"$OUT_FILE" = x ]
then
    # no --output passed, INTERACTIVE mode
    GNUPLOT_OPTS="-persist"
else
    GNUPLOT_OPTS="-e 'set terminal $GNUPLOT_TERMINAL; set output \"$OUT_FILE\"'"
fi

if [ x"$SA_FILE" != x ]
then
    # Decompress the SA file if it's compressed
    PLAIN_SA_FILE=`mktemp`
    decompress_copy "$SA_FILE" $PLAIN_SA_FILE
else
    # If the user passed no parameter
    PLAIN_SA_FILE=""
fi
unset SA_FILE

DATAFILE=`mktemp`
DATAFILE2=`mktemp`
GNUPLOTFILE=`mktemp`

case "$GRAPH_TYPE" in
"CPU")
    $SADF -t -d -C $PLAIN_SA_FILE -- -u   > $DATAFILE
    cpu_gnuplot $GNUPLOTFILE $DATAFILE
    ;;
"MEM")
    $SADF -t -d -C $PLAIN_SA_FILE -- -r   > $DATAFILE
    $SADF -t -d -C $PLAIN_SA_FILE -- -S   > $DATAFILE2
    mem_gnuplot $GNUPLOTFILE $DATAFILE $DATAFILE2
    ;;
"IO")
    $SADF -t -d -C $PLAIN_SA_FILE -- -b   > $DATAFILE
    io_gnuplot $GNUPLOTFILE $DATAFILE
    ;;
*)
    echo "Unknown graph type, supported types are: $GRAPH_TYPES" 1>&2
    exit 1
esac

eval $GNUPLOT $GNUPLOT_OPTS $GNUPLOTFILE


[ -f "$PLAIN_SA_FILE" ]        && rm $PLAIN_SA_FILE
[ -f "$GNUPLOTFILE" ]          && rm $GNUPLOTFILE
[ -f "$DATAFILE" ]             && rm $DATAFILE
[ -f "$DATAFILE2" ]            && rm $DATAFILE2


exit


# Local Variables:
# sh-basic-offset: 4
# indent-tabs-mode: nil
# sh-indent-for-case-label: 0
# sh-indent-for-case-alt: +
# End: