-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathacpiStats.sh
271 lines (216 loc) · 8.89 KB
/
acpiStats.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
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
#!/bin/sh
#############################################################################
# Script aimed at computing how much time the NAS spent in each respective
# ACPI state
#
# Usage: acpiStats.sh [-p W0,W3,W5]
#
# -p W0: typical power consumption in mW while in ACPI state S0
# W3: typical power consumption in mW while in ACPI state S3
# W5: typical power consumption in mW while in ACPI state S5
#
# Author: fritz from NAS4Free forum
#
#############################################################################
# Initialization of the script name
readonly SCRIPT_NAME=`basename $0` # The name of this file
# set script path as working directory
cd "`dirname $0`"
# Import required scripts
. "config.sh"
. "common/commonLogFcts.sh"
. "common/commonMailFcts.sh"
. "common/commonLockFcts.sh"
# Initialization of the constants
readonly START_TIMESTAMP=`$BIN_DATE +"%s"`
readonly LOGFILE="$CFG_LOG_FOLDER/$SCRIPT_NAME.log"
readonly TMPFILE="$CFG_TMP_FOLDER/$SCRIPT_NAME.$$.stat.tmp"
readonly TMPFILE_ARGS="$CFG_TMP_FOLDER/$SCRIPT_NAME.$$.args.tmp"
readonly ACPI_STATE_LOGFILE="$CFG_LOG_FOLDER/acpi.log"
# Set variables corresponding to the input parameters
ARGUMENTS="$@"
# Initialization of constants
readonly S_IN_WEEK=604800
readonly S_IN_MONTH=2592000
readonly S_IN_YEAR=31536000
# Initialisation of the default values for the arguments of the script
I_COMPUTE_CONSUMPTION="0"
I_W_S0="0" # Power consumed by the NAS in S0 (in mW)
I_W_S3="0" # Power consumed by the NAS in S3 (in mW)
I_W_S5="0" # Power consumed by the NAS in S5 (in mW)
I_W_UNTRACKED="0" # Assumed Power consumed by the NAS when the ACPI state was not tracked (in mW)
##################################
# Check script input parameters
#
# Params: all parameters of the shell script
# return : 1 if an error occured, 0 otherwise
##################################
parseInputParams() {
local regex_pow regex_pows opt
regex_pow="([1-9][0-9]*)"
regex_pows="$regex_pow[,]$regex_pow[,]$regex_pow"
# parse the optional arguments
while getopts ":p:" opt; do
case $opt in
p)
if ! echo "$OPTARG" | grep -E "^$regex_pows$" >/dev/null; then
echo "Invalid parameter \"$OPTARG\" for option: -p. Should be \"pS0,pS3,pS5\", were pSx are integer"
return 1
fi
I_COMPUTE_CONSUMPTION="1"
I_W_S0=`echo "$OPTARG" | cut -f1 -d,`
I_W_S3=`echo "$OPTARG" | cut -f2 -d,`
I_W_S5=`echo "$OPTARG" | cut -f3 -d,`
;;
\?)
echo "Invalid option: -$OPTARG"
return 1 ;;
:)
echo "Option -$OPTARG requires an argument"
return 1 ;;
esac
done
# Remove the optional arguments parsed above.
shift $((OPTIND-1))
# Check if the number of mandatory parameters
# provided is as expected
if [ "$#" -ne "0" ]; then
echo "No mandatory arguments should be provided"
return 1
fi
return 0
}
##################################
# Compute ACPI state stastistics
#
# Param 1: Log file containing the data to analyze
# Param 2: String to be search for in the 3rd column of the log file
# Return : The percentage of time the NAS spent in the state described by the string
##################################
compute_stat() {
local log_file pattern date_format duration duration_tot current_line \
nbLinesInFile current_date current_ts current_pattern old_ts old_pattern \
log_entry deltat
log_file="$1"
pattern="$2"
date_format=`ts_format`
duration="0"
duration_tot="0"
current_line="1"
nbLinesInFile=`wc -l "$log_file" | awk ' { print $1 } '` # find number of lines
# Itterate the file to compute the time spent by the NAS
# in the state described by the pattern
while [ "$current_line" -le "$nbLinesInFile" ]
do
old_ts="$current_ts"
old_pattern="$current_pattern"
log_entry=`sed "$current_line"!d "$log_file"`
# get line data from log file
current_date=`echo "$log_entry" | cut -f1`
current_pattern=`echo "$log_entry" | cut -f3`
current_ts=`$BIN_DATE -j -f "$date_format" "$current_date" +%s`
# compute duration for pattern
if [ "$current_line" -gt "1" ]; then
deltat=$(($current_ts-$old_ts))
duration_tot=$(($duration_tot+$deltat))
if [ "$old_pattern" = "$pattern" ]; then
duration=$(($duration+$deltat))
fi
fi
current_line=$(($current_line+1))
done
# Take into account the time between the last
# timestamp and now
old_ts="$current_ts"
current_ts=`$BIN_DATE +%s`
deltat=$(($current_ts-$old_ts))
duration_tot=$(($duration_tot+$deltat))
if [ "$current_pattern" = "$pattern" ]; then
duration=$(($duration+$deltat))
fi
echo $(($duration*100/$duration_tot))
}
##################################
# Log the statistics for the file
# provided as parameter
#
# param 1: file name (inkl. path)
# return : 1 if an error occured, 0 otherwise
##################################
log_stats() {
local S0p S3p S5p file percentage_tot
file=$1
# compute statistics
S0p=`compute_stat "$file" "S0"`
S3p=`compute_stat "$file" "S3"`
S5p=`compute_stat "$file" "S5"`
UNTRACKEDp=`compute_stat "$file" "UNTRACKED"`
if [ "$I_COMPUTE_CONSUMPTION" -eq "1" ]; then
printf 'S0 (%3s W) (Working) : %3s percent\n' "$(($I_W_S0/1000))" "$S0p" | log_info "$LOGFILE"
printf 'S3 (%3s W) (Suspend to RAM) : %3s percent\n' "$(($I_W_S3/1000))" "$S3p" | log_info "$LOGFILE"
printf 'S5 (%3s W) (Soft off) : %3s percent\n' "$(($I_W_S5/1000))" "$S5p" | log_info "$LOGFILE"
printf 'UNKNOWN (%3s W) (Soft off) : %3s percent\n' "$(($I_W_UNTRACKED/1000))" "$UNTRACKEDp" | log_info "$LOGFILE"
W_average=$((($I_W_S0*$S0p+$I_W_S3*$S3p+$I_W_S5*$S5p+$I_W_UNTRACKED*$UNTRACKEDp)/100/1000))
log_info "$LOGFILE" "Average power comsumption: $W_average W"
else
printf 'S0 (Working) : %3s percent\n' "$S0p" | log_info "$LOGFILE"
printf 'S3 (Suspend to RAM): %3s percent\n' "$S3p" | log_info "$LOGFILE"
printf 'S5 (Soft off) : %3s percent\n' "$S5p" | log_info "$LOGFILE"
printf 'UNKNOWN : %3s percent\n' "$UNTRACKEDp" | log_info "$LOGFILE"
fi
# consistency check
percentage_tot=$(($S0p+$S3p+$S5p+$UNTRACKEDp))
if [ $percentage_tot -lt "97" ] || [ $percentage_tot -gt "103" ]; then
log_warning "$LOGFILE" "The sum of the percentages is not equal to 100, but equals $percentage_tot"
return 1
fi
return 0
}
##################################
# Main
##################################
main() {
local oldest_acpi_ts
log_info "$LOGFILE" "Starting computation of ACPI statistics"
oldest_acpi_ts=`get_log_oldest_ts "$ACPI_STATE_LOGFILE"`
if [ "$?" -ne "0" ]; then
log_error "$LOGFILE" "Could not read \"$ACPI_STATE_LOGFILE\" that should contain the acpi states history"
return 1
fi
if [ $oldest_acpi_ts -le `$BIN_DATE -j -v -"$S_IN_WEEK"S +%s` ]; then
get_log_entries "$ACPI_STATE_LOGFILE" "$S_IN_WEEK" > $TMPFILE
log_info "$LOGFILE" "ACPI Statistics for the last week:"
if ! log_stats $TMPFILE; then return 1; fi
fi
if [ $oldest_acpi_ts -le `$BIN_DATE -j -v -"$S_IN_MONTH"S +%s` ]; then
get_log_entries "$ACPI_STATE_LOGFILE" "$S_IN_MONTH" > $TMPFILE
log_info "$LOGFILE" "ACPI Statistics for the last month:"
if ! log_stats $TMPFILE; then return 1; fi
fi
if [ $oldest_acpi_ts -le `$BIN_DATE -j -v -"$S_IN_YEAR"S +%s` ]; then
get_log_entries "$ACPI_STATE_LOGFILE" "$S_IN_YEAR" > $TMPFILE
log_info "$LOGFILE" "ACPI Statistics for the last year:"
if ! log_stats $TMPFILE; then return 1; fi
fi
cat "$ACPI_STATE_LOGFILE" > $TMPFILE
log_info "$LOGFILE" "ACPI Statistics since the beginning of the log (`$BIN_DATE -j -f %s $oldest_acpi_ts`):"
if ! log_stats $TMPFILE; then return 1; fi
# Delete the temporary file
$BIN_RM -f "$TMPFILE"
return 0
}
# Parse and validate the input parameters
if ! parseInputParams $ARGUMENTS > "$TMPFILE_ARGS"; then
log_info "$LOGFILE" "-------------------------------------"
cat "$TMPFILE_ARGS" | log_error "$LOGFILE"
get_log_entries_ts "$LOGFILE" "$START_TIMESTAMP" | sendMail "$SCRIPT_NAME : Invalid arguments"
else
log_info "$LOGFILE" "-------------------------------------"
cat "$TMPFILE_ARGS" | log_info "$LOGFILE"
# run script if possible (lock not existing)
run_main "$LOGFILE" "$SCRIPT_NAME"
# in case of error, send mail with extract of log file
[ "$?" -eq "2" ] && get_log_entries_ts "$LOGFILE" "$START_TIMESTAMP" | sendMail "$SCRIPT_NAME : issue occured during execution"
fi
$BIN_RM "$TMPFILE_ARGS"
exit 0