-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_bandwidth.sh
executable file
·123 lines (107 loc) · 2.85 KB
/
report_bandwidth.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
#!/bin/bash
##############################################################
# Copyright 2019 Daniel Grant
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################
##############################################################
# report_bandwidth.sh
#
# Script that, using vnstat, reports the daily, weekly or
# monthly bandwidth usage for a given server.
#
# Author: Daniel Grant
# Version: 1.0.1
##############################################################
# Configuration
MODE=daily
TO_EMAIL=
CMD_VNSTAT=/usr/bin/vnstat
CMD_SENDMAIL=/usr/sbin/sendmail
log() {
if [ "$1" = "ERROR" ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [ERROR] $2"
else
echo "[$(date +"%Y-%m-%d %H:%M:%S")] [INFO] $2"
fi
}
log_info() {
log "INFO" "$1"
}
log_error() {
log "ERROR" "$1"
}
check_var() {
if [ -z "$2" ]; then
log_error "$1 is not set"
exit 1
else
log_info "Got $1 = $2"
fi
}
check_cmd() {
if [ -x "$2" ]; then
log_info "Got $1 = $2"
else
log_error "$2 does not exist"
exit 1
fi
}
check_file() {
if [ -w "$2" ]; then
log_info "Got $1 = $2"
else
log_error "$2 does not exist or is not writeable"
exit 1
fi
}
# Verify and log the configuration
check_var "TO_EMAIL" $TO_EMAIL
check_cmd "CMD_VNSTAT" $CMD_VNSTAT
check_cmd "CMD_SENDMAIL" $CMD_SENDMAIL
# Create, verify and log the temporary file
OUTFILE=$(mktemp)
check_file "temporary file" "$OUTFILE"
# Determine the vnstat command and parameters
if [ "$MODE" = "daily" ]; then
VNSTAT_PARAMS="--days --locale en_GB.utf8"
else
if [ "$MODE" = "weekly" ]; then
VNSTAT_PARAMS="--weeks"
else
if [ "$MODE" = "monthly" ]; then
VNSTAT_PARAMS="--months"
else
log "ERROR" "Invalid MODE, must be one of 'daily', 'weekly' or 'monthly'"
exit 1
fi
fi
fi
# Execute vnstat and process the output
log_info "Executing: $CMD_VNSTAT $VNSTAT_PARAMS"
eval "$CMD_VNSTAT $VNSTAT_PARAMS" > "$OUTFILE"
# Dispatch the email
if [ -s "$OUTFILE" ]; then
log_info "Emailing report to: $TO_EMAIL"
(
echo "Subject: [bandwidth report] $(hostname -f) - $MODE report"
echo "To: $TO_EMAIL"
echo ""
cat "$OUTFILE"
) | $CMD_SENDMAIL $TO_EMAIL
else
log_error "Temporary file '$OUTFILE' does not exist or is empty"
fi
# Delete the temporary file
log_info "Deleting temporary file '$OUTFILE'"
rm -f "$OUTFILE"
exit 0