forked from gcoop-libre/freeipa-sssd-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipa-sss-plt
executable file
·283 lines (213 loc) · 7.48 KB
/
ipa-sss-plt
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
#!/bin/bash
# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
# Copyright (C) 2021 Osiris Alejandro Gomez <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# shellcheck disable=SC1090
# shellcheck disable=SC1091
# shellcheck disable=SC2001
[[ -n "$BASH_DEBUG" ]] && set -x
DIR_BIN="$(dirname "$0")" && source "$DIR_BIN/ipa-src-cfg"
function usage()
{
cat << EOF
## \`$BIN\` Plot SynLog between hours range
### Usage:
\`\`\`bash
$BIN [YYYY-MM-DD] [X_START] [X_END]
\`\`\`
### Description
Plot SynLog Stats:
- LDAP Simultaneous Connections
- KRB5 Simultaneous Connections
- Average Host
- Total SYNs
- SYNs > 20s
- SYNs < 20s
- No-SYNs
### Example:
\`\`\`bash
$BIN 2022-01-01 07:00 19:00
\`\`\`
See full example output in [\`ipa-sss-plt.md\`](examples/ipa-sss-plt.md)
and [\`ipa-sss-plt.pdf\`](examples/ipa-sss-plt.pdf)
EOF
exit 0
}
[[ "$1" =~ ^[-]+(h|help) ]] && usage
function defaults()
{
DATE="$(date +%F)"
HOUR="$(date +'%k' | tr -d ' ')"
NEXT="$((HOUR+1))"
[[ -n "$UMASK" ]] || UMASK='0022'
[[ -n "$XSTART" ]] || XSTART='07:00'
[[ -n "$PP" ]] || PP="1"
XEND="$NEXT:00"
}
function params()
{
[[ -z "$1" ]] || DATE="$1"
[[ -z "$2" ]] || XSTART="$2"
[[ -z "$3" ]] || XEND="$3"
[[ -z "$4" ]] || PP="$4"
XS="${XSTART//:/}"
XE="${XEND//:/}"
SEC_PLT="$DIR_PLT/$DATE-seconds-vs-total-vs-ips-$XS-$XE.plt"
SEC_PNG="$DIR_PNG/$DATE-seconds-vs-total-vs-ips-$XS-$XE.png"
SEC_ERR="$DIR_PLT/$DATE-seconds-vs-total-vs-ips-$XS-$XE.err"
SEC_DAT="$DIR_DAT/$DATE-seconds.dat"
SEC_TOT="$DIR_DAT/$DATE-total.dat"
TOTAL_DAT="$DIR_DAT/$DATE-total.dat"
TOTAL="$(awk 'BEGIN {S=0} {S+=$2} END {print S}' "$TOTAL_DAT")"
}
function validate()
{
[[ -z "$PLT_KEY_COLOR" ]] && cfg 'PLT_KEY_COLOR'
[[ -z "$PLT_KEY_FONT" ]] && cfg 'PLT_KEY_FONT'
[[ -z "$PLT_TITLE" ]] && cfg 'PLT_TITLE'
[[ -z "$PLT_TITLE_FONT" ]] && cfg 'PLT_TITLE_FONT'
[[ -z "$PLT_TITLE_LT" ]] && cfg 'PLT_TITLE_LT'
[[ -z "$PLT_X_FONT" ]] && cfg 'PLT_X_FONT'
[[ -z "$PLT_X_LABEL" ]] && cfg 'PLT_X_LABEL'
[[ -z "$PLT_Y_FONT" ]] && cfg 'PLT_Y_FONT'
[[ -z "$PLT_Y_LABEL" ]] && cfg 'PLT_Y_LABEL'
[[ -z "$DIR_PLT" ]] && cfg 'IPA_PLT'
[[ -z "$DIR_PNG" ]] && cfg 'IPA_PNG'
[[ -z "$IPA_PLT" ]] && cfg 'IPA_PLT'
[[ -e "$SEC_DAT" ]] || die "NOT FOUND FILE $SEC_DAT"
[[ -e "$SEC_TOT" ]] || die "NOT FOUND FILE $SEC_TOT"
[[ -d "$DIR_PLT" ]] || die "NOT FOUND DIR $DIR_PLT"
[[ -d "$DIR_PNG" ]] || die "NOT FOUND DIR $DIR_PNG"
}
file2max()
{
[[ -e "$1" ]] && exit
sort -nr -k2 "$1" | head -1 | cut -d ' ' -f2
}
file2labels()
{
local MIN
local VAL
local COLOR
local PP=1
local XY='/1'
local ROTATE='center'
COLOR='#ffffff'
[[ -e "$1" ]] || return
[[ -z "$2" ]] || MIN="$2"
[[ -z "$3" ]] || COLOR="$3"
[[ -z "$4" ]] || PP="$4"
[[ -z "$5" ]] || XY="$5"
[[ -z "$6" ]] || ROTATE="$6"
LAST="${Y%%"."*}"
DS="$(date -d "$XS" +%s)"
DE="$(date -d "$XE" +%s)"
while read -r X Y
do
DX="$(date -d "$X" +%s)"
[[ "$DX" -lt "$DS" ]] && continue
[[ "$DX" -gt "$DE" ]] && continue
VAL="${Y%%"."*}"
VAL="$(echo "$VAL" | grep -Eo "[0-9\.]+")"
YY="$(echo "$Y" | grep -Eo '[0-9\.]+')"
[[ "$XY" != '/1' ]] && YY="$(echo "$Y$XY" | bc)"
[[ "$VAL" -lt "$MIN" ]] && continue
P="$(echo "$VAL*$PP" | bc)"
I="${P%%"."*}"
TMAX="$((LAST+I))"
TMIN="$((LAST-I))"
LAST="$VAL"
LABEL=$(cat << EOF
set label sprintf(" %s",'$Y') at '$X',$YY+3 $ROTATE font "Inconsolata,10" textcolor rgb '$COLOR'
EOF
)
if [[ "$PP" = '1' ]]
then
echo "$LABEL" >> "$SEC_PLT"
else
if [[ "$VAL" -ge "$TMIN" ]]
then
if [[ "$VAL" -lt "$TMAX" ]]
then
echo "$LABEL" >> "$SEC_PLT"
fi
fi
fi
done < "$1"
}
function plot()
{
# FIXME ROUND + CEIL
# FIXME MAX=$(file2max "$DATE-seconds.dat")
HEAD=$(cat << EOF
load '$IPA_PLT'
set key inside font '$PLT_KEY_FONT' textcolor rgb '#$PLT_KEY_COLOR'
set xdata time;set timefmt '%H:%M';set format x '%H:%M'
set xlabel "$PLT_X_LABEL" font "$PLT_X_FONT" tc lt 5
set ylabel "$PLT_Y_LABEL" font "$PLT_Y_FONT" tc lt 5
set title "$PLT_TITLE\n$DATE | $TOTAL syn | $XSTART - $XEND" font "$PLT_TITLE_FONT" tc lt $PLT_TITLE_LT
set key on
set yrange [-10:100]
set xrange ["$XSTART":"$XEND"]
EOF
)
PLOT=$(cat << EOF
plot '$DIR_DAT/$DATE-crn-syw.dat' u 1:2 w p ls 30 pt 11 ps 2 ti 'crn s20', \
'$DIR_DAT/$DATE-crn-syn.dat' u 1:2 w p ls 30 pt 10 ps 2 ti 'crn syn', \
'$DIR_DAT/$DATE-crn-now.dat' u 1:2 w p ls 32 pt 11 ps 2 ti 'crn n20', \
'$DIR_DAT/$DATE-crn-nos.dat' u 1:2 w p ls 32 pt 10 ps 2 ti 'crn nsy', \
'$DIR_DAT/$DATE-ivr-syw.dat' u 1:2 w p ls 34 pt 11 ps 2 ti 'ivr s20', \
'$DIR_DAT/$DATE-ivr-syn.dat' u 1:2 w p ls 34 pt 10 ps 2 ti 'ivr syn', \
'$DIR_DAT/$DATE-ivr-now.dat' u 1:2 w p ls 36 pt 11 ps 2 ti 'ivr n20', \
'$DIR_DAT/$DATE-ivr-nos.dat' u 1:2 w p ls 36 pt 10 ps 2 ti 'ivr nsy', \
'$DIR_DAT/$DATE-aid-syw.dat' u 1:2 w p ls 38 pt 11 ps 2 ti 'aid s20', \
'$DIR_DAT/$DATE-aid-syn.dat' u 1:2 w p ls 38 pt 10 ps 2 ti 'aid syn', \
'$DIR_DAT/$DATE-aid-now.dat' u 1:2 w p ls 39 pt 11 ps 2 ti 'aid n20', \
'$DIR_DAT/$DATE-aid-nos.dat' u 1:2 w p ls 39 pt 10 ps 2 ti 'aid nsy', \
'$DIR_DAT/$DATE-syn-err.dat' u 1:2 w p ls 5 pt 19 ps 2 ti 'syn err', \
'$DIR_DAT/$DATE-total.dat' u 1:(\$2*10) w p ls 3 pt 6 ps 2 ti 'total syn', \
'$DIR_DAT/$DATE-ldap.dat' u 1:(\$2/100) w l ls 20 ti 'ldap', \
'$DIR_DAT/$DATE-ldas.dat' u 1:(\$2/100) w l ls 21 ti 'ldas', \
'$DIR_DAT/$DATE-krb5.dat' u 1:(\$2/100) w l ls 22 ti 'krb5', \
'$DIR_DAT/$DATE-av01.dat' u 1:(\$2*10) w l ls 23 ti 'avg1', \
'$DIR_DAT/$DATE-lsof.dat' u 1:(\$2/10000) w l ls 24 ti 'lsof'
EOF
)
echo "$HEAD" | sed 's/ \+$//g' > "$SEC_PLT"
file2labels "$DIR_DAT/$DATE-crn-syw.dat" 20 "#8dd3c7" 1
file2labels "$DIR_DAT/$DATE-aid-syw.dat" 20 "#bc80bd" 1
file2labels "$DIR_DAT/$DATE-ivr-syw.dat" 20 "#80b1d3" 1
file2labels "$DIR_DAT/$DATE-syn-err.dat" 1 "#ff0000" 1
file2labels "$DIR_DAT/$DATE-av01.dat" 8 "#33a02c" 1 "*10"
file2labels "$DIR_DAT/$DATE-krb5.dat" 1900 "#b2df8a" "$PP" "/100"
file2labels "$DIR_DAT/$DATE-total.dat" 2 "#0080ff" 1 "*10"
if [[ "$XEND" != '19:00' ]]
then
file2labels "$DIR_DAT/$DATE-ldap.dat" 1900 "#a6cee3" "$PP" "/100" "rotate left"
fi
file2labels "$DIR_DAT/$DATE-total.dat" 5 "#0080ff" 1
echo "$PLOT" | sed 's/ \+$//g' | sed 's/, /, \\\n/g' >>"$SEC_PLT"
gnuplot "$SEC_PLT" > "$SEC_PNG" 2>"$SEC_ERR" && echo "$SEC_PNG"
}
function main()
{
config
defaults
umask "$UMASK"
params "$@"
validate
plot
}
main "$@"