-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchromeos-disk-metrics
executable file
·160 lines (143 loc) · 4.85 KB
/
chromeos-disk-metrics
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
#!/bin/sh -u
# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
. /usr/share/misc/storage-info-common.sh
. /usr/share/misc/shflags
DEFINE_boolean 'test' "${FLAGS_FALSE}" "For unit testing."
# Parse command line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
# This file is used for userfeedback as well.
STORAGE_INFO_FILE="/var/log/storage_info.txt"
INVALID="-1"
# Report disk health from SMART (S.M.A.R.T.) parameters.
# usage:
# report_smart_metrics <uncorrectable-errors> <transfer-errors>
# <percentage-used> <available-reserved-space> <sectors-written>
# <sectors-read>
report_smart_metrics() {
if [ -n "$1" ]; then
if [ "$1" -ne "${INVALID}" ]; then
metrics_client Platform.SmartUncorrectableErrors "$1" 1 1000000 20
fi
fi
if [ -n "$2" ]; then
if [ "$2" -ne "${INVALID}" ]; then
metrics_client Platform.SmartTransferErrors "$2" 1 1000000 20
fi
fi
if [ -n "$3" ]; then
if [ "$3" -ne "${INVALID}" ]; then
metrics_client Platform.SATA.PercentageUsed "$3" 0 255 50
fi
fi
if [ -n "$4" ]; then
if [ "$4" -ne "${INVALID}" ]; then
metrics_client Platform.SATA.AvailableReservedSpace "$4" 0 100 50
fi
fi
if [ -n "$5" ]; then
if [ "$5" -ne "${INVALID}" ]; then
# Logical Sectors Written is the total number of 512-byte sectors written.
# The maximum value set here equates to 100 TiB.
metrics_client Platform.SATA.LogicalSectorsWritten "$5" 0 214748364800 50
fi
fi
if [ -n "$6" ]; then
if [ "$6" -ne "${INVALID}" ]; then
# Logical Sectors Read is the total number of 512-byte sectors read.
# The maximum value set here equates to 100 TiB.
metrics_client Platform.SATA.LogicalSectorsRead "$6" 0 214748364800 50
fi
fi
}
# Sata information.
GET_SMART_INFO='
$2 == "Reported_Uncorrect" { uncorr = $8 }
$2 == "UDMA_CRC_Error_Count" { txfer = $8 }
$6 == "Percentage" && $7 == "Used" { life = $4 }
$2 == "Available_Reservd_Space" { rsrved = $8 }
$6 == "Logical" && $7 == "Sectors" && $8 == "Written" { written = $4 }
$6 == "Logical" && $7 == "Sectors" && $8 == "Read" { read = $4 }
END { print uncorr, txfer, life, rsrved, written, read }
'
sata_disk_metrics() {
# Extract revelevant SMART information from this file, if any.
report_smart_metrics $(awk -v uncorr="${INVALID}" -v txfer="${INVALID}" \
-v life="${INVALID}" -v rsrved="${INVALID}" \
-v written="${INVALID}" -v read="${INVALID}" \
"${GET_SMART_INFO}" "${STORAGE_INFO_FILE}")
}
emmc_disk_metrics() {
local life type
# Extract revelevant eMMC information from this file, if any.
for type in 'A' 'B'; do
life="$(sed -ne "/DEVICE_LIFE_TIME_EST_TYP_${type}/s/.*: \(0x.*\)]$/\1/p" \
"${STORAGE_INFO_FILE}")"
if [ -n "${life}" ]; then
metrics_client -s "Platform.Emmc.LifeUsed.Type${type}" \
"$(printf "%d" "${life}")"
fi
done
}
sindin8de2_disk_metrics() {
# Sandisk SDIN8DE2-*G 4.51 stores eMMC device health in vendor registers:
# 94: MLC Device health.
# 87: SLC Device health.
local offset_A=94
local offset_B=87
local offset life type
if grep -qe "^name.*SEM..G" "${STORAGE_INFO_FILE}"; then
for type in 'A' 'B'; do
eval "offset=\"\${offset_${type}}\""
life="$(sed -ne "/Vendor Specific Fields \[VENDOR_SPECIFIC_FIELD\[${offset}\]\]:/s/.*: \(0x.*\)$/\1/p" \
"${STORAGE_INFO_FILE}")"
if [ -n "${life}" ]; then
metrics_client -s "Platform.Emmc.LifeUsed.Type${type}" \
"$(printf "%d" "${life}")"
fi
done
fi
}
nvme_disk_metrics() {
local used
# Extract revelevant NVMe information from this file, if any.
used="$(sed -ne "/Percentage Used/s/[^0-9]*//p" \
"${STORAGE_INFO_FILE}" | sed -ne "s/%//p")"
if [ -n "${used}" ]; then
metrics_client -s "Platform.Storage.Nvme.PercentageUsed" \
"$(printf "%d" "${used}")"
fi
}
ufs_disk_metrics() {
local life method type
# Extract revelevant UFS information from this file, if any.
for method in 'a' 'b'; do
life="$(sed -ne "/life_time_estimation_${method}/s/.*:\(0x.*\)$/\1/p" \
"${STORAGE_INFO_FILE}")"
if [ -n "${life}" ]; then
type="$(echo "${method}" | sed 's/.*/\u&/')"
metrics_client -s "Platform.Storage.Ufs.LifeUsed.Type${type}" \
"$(printf "%d" "${life}")"
fi
done
}
# Invoke main if not in test mode, otherwise let the test code call.
main() {
if [ "${FLAGS_test}" -eq "${FLAGS_TRUE}" ]; then
return
fi
if [ $# -ne 0 ]; then
flags_help
exit 1
fi
rm -f "${STORAGE_INFO_FILE}"
get_storage_info > "${STORAGE_INFO_FILE}"
sata_disk_metrics
emmc_disk_metrics
sindin8de2_disk_metrics
nvme_disk_metrics
ufs_disk_metrics
}
main "$@"