blob: dbefe196f3eddbba6824dc2442f81491bc332537 [file] [log] [blame]
#!/bin/sh
# 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/shflags
DEFINE_boolean 'test' "${FLAGS_FALSE}" "For unit testing."
# Parse command line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
set -e
if [ "${FLAGS_test}" -eq "${FLAGS_FALSE}" ]; then
. /usr/share/cros/disk_utils.sh
else
. ./disk_utils.sh
fi
dmesg_matches() {
if dmesg | grep -q "$1"; then
echo 1
else
echo 0
fi
}
gather_battery_errors() {
# The metrics library requires a max value of 2 rather than 1
# (http://crbug.com/338015).
metrics_client -e Platform.BatteryAbsent "$(
dmesg_matches "ACPI: Battery Slot.*absent")" 2
}
gather_wifi_error() {
local count
count=$(grep "^DEVTYPE=wlan" /sys/class/net/*/uevent | wc -l)
if [ "${count}" -gt 9 ]; then
count=9
fi
metrics_client -e Platform.WiFiDeviceCount "${count}" 10
}
gather_fs_error() {
# Gather filesystem errors.
local dev dev_name fs_error_count
local DEV_Stateful
local DEV_EncStateful
DEV_Stateful="$(
get_stateful_df_data | awk '{print $1}' | sed -e 's#^/dev/##')"
if [ -e /dev/mapper/encstateful ] || \
[ "${FLAGS_test}" -eq "${FLAGS_TRUE}" ]; then
DEV_EncStateful="$(printf "dm-%d" \
"$(stat -c "%T" /dev/mapper/encstateful)")"
else
DEV_EncStateful=""
fi
for dev in "Stateful" "EncStateful"; do
eval "dev_name=\"\${DEV_${dev}}\""
if [ -z "${dev_name}" ]; then
continue
fi
metrics_client -e "Platform.FileSystem.${dev}_RecoveryNeeded" \
"$(dmesg_matches "EXT4-fs (${dev_name}): recovery complete")" 2
metrics_client -e "Platform.FileSystem.${dev}_FsckNeeded" \
"$(dmesg_matches \
"EXT4-fs (${dev_name}): warning: mounting fs with errors")" 2
fs_error_count="$(dumpe2fs -h "/dev/$dev_name" 2>/dev/null | \
sed -En '/FS Error count/s/.* +([0-9]+)$/\1/p')"
: "${fs_error_count:=0}"
metrics_client "Platform.FileSystem.${dev}_ErrorCount" \
"${fs_error_count}" 0 100000 20
done
}
gather_errors() {
gather_battery_errors
# The wifi devices may take some time to appear.
sleep 10
gather_wifi_error
gather_fs_error
}
main() {
if [ "${FLAGS_test}" -eq "${FLAGS_TRUE}" ]; then
return
fi
if [ $# -ne 0 ]; then
flags_help
exit 1
fi
gather_errors
}
main "$@"