-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathrrd.sh
executable file
·211 lines (182 loc) · 6.26 KB
/
rrd.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
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
#!/usr/bin/env bash
#####
# This script generates and updates an rrdtool database
# of CPU and drive temperatures. It calls 'temps-rrd-format.sh'
# to actually get the data in a format it can use.
# It writes the data files to the same directory it
# runs from.
#
# Author: Seren Thompson
# Date: 2017-09-19
# Website: https://github.com/seren/freenas-temperature-graphing
#####
# # display expanded values
# set -o xtrace
# # quit on errors
# set -o errexit
# # error on unset variables
# set -o nounset
######################################
# Script variables
######################################
RRDTOOL=/usr/local/bin/rrdtool
RRDSCRIPTVERSION=1.0
#######################################
# Usage message
func_usage () {
echo '
This script generates and updates an rrdtool database
of CPU and drive temperatures. It calls "temps-rrd-format.sh"
to actually get the data in a format it can use.
It writes the data files to the same directory it
runs from.
Usage '"$0"' [-v] [-d] [-h] [--platform "esxi"] output-filename
-v | --verbose Enables verbose output
-d | --debug Outputs each line of the script as it executes (turns on xtrace)
-h | --help Displays this message
Options for ESXi:
--platform "esxi" Indicates that we will use ESXi tools to retrieve CPU temps
--ipmitool_username <USERNAME> Required: Username to use when connecting to BMC
--ipmitool_address <BMC_ADDRESS> Required: BMC ip address to connect to
Note: The filename must be in the following format: temps-Xmin.rdd
where X is the minute interval between readings.
ex: "temps-10min.rrd" would contain readings every 10 minutes
Example:
'"$0"' /mnt/mainpool/temperatures/temps-5min.rrd
'
echo "Script version: ${RRDSCRIPTVERSION}"
}
# Process command line args
args=''
help=
verbose=
debug=
datafile=
PLATFORM=default
while [ $# -gt 0 ]; do
case $1 in
-h|--help) help=1; shift 1 ;;
-v|--verbose) verbose=1; shift 1 ;;
-d|--debug) debug=1; shift 1 ;;
--platform) PLATFORM=$2; shift 1; shift 1 ;;
--ipmitool_username) USERNAME=$2; shift 1; shift 1 ;;
--ipmitool_address) BMC_ADDRESS=$2; shift 1; shift 1 ;;
-*)
echo "$0: Unrecognized option: $1 (try --help)" >&2
exit 1
;;
*)
if [ -n "$datafile" ]; then
echo "You can only specify one output-filename. You gave these:"
echo "${datafile}"
echo "$1"
exit 1
else
datafile=$1
shift 1
fi
;;
esac
done
if [ -n "$debug" ]; then
set -o xtrace
verbose=1
fi
[ -n "$help" ] && func_usage && exit 0
[ -n "$verbose" ] && echo "Script version: ${RRDSCRIPTVERSION}"
case "${PLATFORM}" in
esxi)
[ -n "$verbose" ] && echo "Platform is set to '${PLATFORM}'. Username is '${USERNAME} and ip is '${BMC_ADDRESS}'"
[ -z "$USERNAME" ] && echo "You need to to provide --ipmitool_username with an argument" && exit 1
[ -z "$BMC_ADDRESS" ] && echo "You need to to provide --ipmitool_address with an argument" && exit 1
args="${args} --platform ${PLATFORM} --ipmitool_username ${USERNAME} --ipmitool_address ${BMC_ADDRESS}"
;;
default)
args=''
;;
*)
echo "Unrecognized platform: '${PLATFORM}'"
exit 1
;;
esac
# Check we're root
if [ "$(id -u)" != "0" ]; then
echo "Error: this script needs to be run as root (for smartctl). Try 'sudo $0 $1'"
exit 1
fi
# Check that we were supplied a db filename
if [ -z "${datafile}" ]; then
echo "Error: you need to give an output filename as an argument."
echo
func_usage
exit 1
fi
# Debugging info
[ -n "$verbose" ] && echo "Rrdtool database filename: ${datafile}"
# Get current working directory
CWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
[ -n "$verbose" ] && echo "Current working directory is: ${CWD}"
# Load common functions (temperature retrieval, device enumeration, etc)
# shellcheck source=./rrd-lib.sh
. "${CWD}/rrd-lib.sh"
# If the rrdtool database exists, make sure it's writable. Otherwise create it
if [ -e "${datafile}" ]; then
func_test_writable "${datafile}" || exit 1
else
[ -n "$verbose" ] && echo "Rrdtool database doesn't exist. Creating it."
get_devices
# Calculate the sampling interval from the filename
interval=$(echo "${datafile}" | sed 's/.*temps-\(.*\)min.rrd/\1/') # extract minute number
if [ -z $interval ]; then
echo "Couldn't find a minute number in filename '${datafile}' (should in format: temps-5min.rrd)."
exit 1
fi
timespan=$((interval * 60))
doubletimespan=$((timespan * 2))
[ -n "$verbose" ] && echo "Sampling every ${timespan} seconds"
# Generate the arguments for db creation for each cpu and drive
rrdarg=
for (( i=0; i < numcpus; i++ )); do
rrdarg="${rrdarg} DS:cpu${i}:GAUGE:${doubletimespan}:0:150"
done
for i in ${drivedevs}; do
rrdarg="${rrdarg} DS:${i}:GAUGE:${doubletimespan}:0:100"
done
echo "Creating rrdtool db file: ${datafile}"
echo "Rrdtool arguments: ${rrdarg}"
echo ${RRDTOOL} create ${datafile} --step ${timespan} ${rrdarg} RRA:MAX:0.5:1:3000
${RRDTOOL} create ${datafile} --step ${timespan} ${rrdarg} RRA:MAX:0.5:1:3000
if ! [ $? == 0 ]; then
echo "ERROR: Couldn't initialize ${datafile}. Running diagnostics..."
func_debug_setup
exit 1
else
[ -n "$verbose" ] && echo "Initialized datafile"
exit 0
fi
fi
tempdir=${TMPDIR,/tmp}
tempfile=$(mktemp ${tempdir}/$$.rrd.output.XXXXXXX)
tempfile=/tmp/$$.temp.rrd.output
tempargs=""
# If we run temps-rrd-format.sh in verbose mode, we can't capture the output
# in a variable. So if verbose is set, we run it twice, once for the user to
# see and once for the script to grab the output.
[ -n "$verbose" ] && echo "Running script: '${CWD}/temps-rrd-format.sh -v -o ${tempfile} ${args}'"
[ -n "$verbose" ] && echo ""
[ -n "$verbose" ] && tempargs=-v
"${CWD}/temps-rrd-format.sh" ${tempargs} -o ${tempfile} ${args}
data=$(cat ${tempfile})
rm $tempfile
[ -n "$verbose" ] && echo "Data: ${data}"
[ -n "$verbose" ] && echo ""
[ -n "$verbose" ] && echo "Updating the db: '${RRDTOOL} update ${datafile} N:${data}'"
${RRDTOOL} update ${datafile} N:${data}
if ! [ $? == 0 ]; then
echo "ERROR: Couldn't update ${datafile} with the data provided. Running diagnostics..."
func_debug_setup
exit 1
else
[ -n "$verbose" ] && echo "Added data"
exit 0
fi