forked from seren/freenas-temperature-graphing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrd.sh
executable file
·162 lines (137 loc) · 4.76 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
#!/usr/local/bin/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-04-24
# 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
RRDTOOL=/usr/local/bin/rrdtool
SCRIPTVERSION="1.0"
# When this script is executed as a FreeNAS cron job, it is executed with $PWD
# set to /root. Because it can be installed in any arbitrary directory, we
# must use dirname to find what to include in $PATH. Unless make or pkgng can
# be used to install freenas-temperature-graphing, we must live with this
# unfortunate hack.
PATH="$PATH:$(dirname $0)"
source rrd-lib.sh
# Helpful 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] 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
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
'
}
# Process command line args
help=
verbose=
debug=
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 ;;
-*) echo "$0: Unrecognized option: $1 (try --help)" >&2; exit 1 ;;
*) datafile=$1; shift 1; break ;;
esac
done
if [ -n "$debug" ]; then
set -o xtrace
verbose=1
fi
[ -n "$help" ] && func_usage && exit 0
# 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}"
# 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
[ -n "$verbose" ] && echo "Running script: '${CWD}/temps-rrd-format.sh'"
[ -n "$verbose" ] && echo ""
[ -n "$verbose" ] && echo ""
[ -n "$verbose" ] && "${CWD}/temps-rrd-format.sh" "$@"
[ -n "$verbose" ] && echo ""
[ -n "$verbose" ] && echo ""
[ -n "$verbose" ] && echo "(running script again non-verbosely)"
data=`${CWD}/temps-rrd-format.sh`
[ -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