-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_graphs
executable file
·173 lines (140 loc) · 5.29 KB
/
update_graphs
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
#!/bin/sh
# Copyright (c) 2022 Ashlen <[email protected]>
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# update graphs with rrdtool(1)
err() {
printf '%s\n' "$*" >&2
exit 1
}
usage() {
err "${PROGRAM_NAME} [-s] [type] [period] [rrddir] [savedir]
-s: swap colors for allowed incoming and outgoing traffic (by default,
incoming traffic is green and outgoing traffic is blue). These defaults
work for a router, but are inappropriate for a public-facing server that
sends out data more than it receives it.
[type] can be: pf
[period] can be: day week month
Note that [period] can also take a number of hours as an integer.
[rrddir] is where the rrd files are found.
[savedir] is where the graphs are saved.
${PROGRAM_NAME} is meant to be a frontend for rrdtool(1) for rrd files
maintained by symux(1). ${PROGRAM_NAME} assumes that the rrd files in
rrddir for a particular type are named the same way that the 'datadir'
statement in symux.conf would name them."
}
# Restrict PERIOD length as to avoid too large an integer. 99999 hours is
# roughly 11 years which is more than enough for my use case.
check_period_length() {
[ ${#PERIOD} -gt 5 ] || err 'PERIOD is restricted to five characters or less.'
}
gen_pf_graphs() {
GRAPHS='pf-bits
pf-passed-packets
pf-dropped-packets
pf-state-inserts-removals
pf-state-searches'
[ "${SWAPCOLORS}" = 'yes' ] && INCOLOR="${BLUE}" OUTCOLOR="${GREEN}"
: "${INCOLOR:=${GREEN}}"
: "${OUTCOLOR:=${BLUE}}"
for graphname in ${GRAPHS}; do
BASE_CMD="rrdtool graph "${SAVEDIR}/${graphname}-${PERIOD}.png" -s -${HOURS}hours"
case "${graphname}" in
'pf-bits')
REST_OF_CMD="-t 'pf bits in/out (IPv4)' \
--vertical-label 'bits/s' \
'DEF:bytes_v4_in=${RRDDIR}/${TYPE}.rrd:bytes_v4_in:MAX' \
'DEF:bytes_v4_out=${RRDDIR}/${TYPE}.rrd:bytes_v4_out:MAX' \
'CDEF:bps_in=bytes_v4_in,8,*' \
'CDEF:bps_out=bytes_v4_out,8,*' \
'AREA:bps_in${INCOLOR}:in' \
'AREA:bps_out${OUTCOLOR}:out'"
;;
'pf-passed-packets')
REST_OF_CMD="-t 'pf passed packets in/out (IPv4)' \
--vertical-label 'packets/s' \
'DEF:packets_v4_in_pass=${RRDDIR}/${TYPE}.rrd:packets_v4_in_pass:MAX' \
'DEF:packets_v4_out_pass=${RRDDIR}/${TYPE}.rrd:packets_v4_out_pass:MAX' \
'AREA:packets_v4_in_pass${INCOLOR}:in' \
'AREA:packets_v4_out_pass${OUTCOLOR}:out'"
;;
'pf-dropped-packets')
REST_OF_CMD="-t 'pf dropped packets in/out (IPv4)' \
--vertical-label 'packets/s' \
'DEF:packets_v4_in_drop=${RRDDIR}/${TYPE}.rrd:packets_v4_in_drop:MAX' \
'DEF:packets_v4_out_drop=${RRDDIR}/${TYPE}.rrd:packets_v4_out_drop:MAX' \
'AREA:packets_v4_in_drop${RED}:in' \
'AREA:packets_v4_out_drop${YELLOW}:out'"
;;
'pf-state-inserts-removals')
REST_OF_CMD="-t 'pf state inserts/removals' \
--vertical-label 'states/s' \
'DEF:states_inserts=${RRDDIR}/${TYPE}.rrd:states_inserts:MAX' \
'DEF:states_removals=${RRDDIR}/${TYPE}.rrd:states_removals:MAX' \
'AREA:states_inserts${GREEN}:inserts' \
'AREA:states_removals${RED}:removals'"
;;
'pf-state-searches')
REST_OF_CMD="-t 'pf state searches' \
--vertical-label 'states/s' \
'DEF:states_searches=${RRDDIR}/${TYPE}.rrd:states_searches:MAX' \
'AREA:states_searches${RED}:searches'"
;;
*) err 'This should never happen.' ;;
esac
eval "${BASE_CMD} ${REST_OF_CMD}"
done
}
command -v 'rrdtool' > /dev/null 2>&1 \
|| err "rrdtool not found in PATH or not executable."
while getopts s opt; do
case "${opt}" in
's') SWAPCOLORS='yes' ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
readonly PROGRAM_NAME="${0##*/}"
[ -z "$*" ] && usage
readonly TYPE="$1"
readonly PERIOD="$2"
readonly RRDDIR="$3"
readonly SAVEDIR="$4"
case "${TYPE}" in
'pf') : ;;
*) err "\"${TYPE}\" isn't a valid type of statistic to graph." ;;
esac
# shellcheck disable=SC2086
case "${PERIOD}" in
'day') readonly HOURS=24 ;;
'week') readonly HOURS=168 ;;
'month') readonly HOURS=672 ;;
[0-9]*)
check_period_length
readonly HOURS=${PERIOD}
;;
*) err "\"${PERIOD}\" isn't a valid time period for graphing." ;;
esac
[ -n "${RRDDIR}" ] || err "${PROGRAM_NAME} needs a directory containing rrd files."
[ -d "${RRDDIR}" ] || err "\"${RRDDIR}\" isn't a directory."
[ -r "${RRDDIR}" ] || err "\"${RRDDIR}\" isn't readable."
[ -n "${SAVEDIR}" ] || err "${PROGRAM_NAME} needs a directory to save graphs in."
[ -d "${SAVEDIR}" ] || err "\"${SAVEDIR}\" isn't a directory."
[ -w "${SAVEDIR}" ] || err "\"${SAVEDIR}\" isn't writable."
[ -f "${RRDDIR}/${TYPE}.rrd" ] || err "\"${RRDDIR}/${TYPE}.rrd\" isn't a file."
[ -r "${RRDDIR}/${TYPE}.rrd" ] || err "\"${RRDDIR}/${TYPE}.rrd\" isn't readable."
readonly RED='#FF0000'
readonly GREEN='#00FF00'
readonly YELLOW='#FFFF00'
readonly BLUE='#0000FF'
case "${TYPE}" in
'pf') gen_pf_graphs "$1" "$2" "$3" "$4" ;;
esac