| #!/bin/sh | |
| # Copyright 2017 The ChromiumOS Authors | |
| # 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 -l "^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_stateful fs_error_count | |
| # shellcheck disable=SC2034 | |
| dev_stateful="$( | |
| get_stateful_df_data | awk '{print $1}' | sed -e 's#^/dev/##')" | |
| if [ -z "${dev_stateful}" ]; then | |
| return | |
| fi | |
| metrics_client -e "Platform.FileSystem.Stateful_RecoveryNeeded" \ | |
| "$(dmesg_matches "EXT4-fs (${dev_stateful}): recovery complete")" 2 | |
| metrics_client -e "Platform.FileSystem.Stateful_FsckNeeded" \ | |
| "$(dmesg_matches \ | |
| "EXT4-fs (${dev_stateful}): warning: mounting fs with errors")" 2 | |
| fs_error_count="$(dumpe2fs -h "/dev/${dev_stateful}" 2>/dev/null | \ | |
| sed -En '/FS Error count/s/.* +([0-9]+)$/\1/p')" | |
| : "${fs_error_count:=0}" | |
| metrics_client "Platform.FileSystem.Stateful_ErrorCount" \ | |
| "${fs_error_count}" 0 100000 20 | |
| } | |
| 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 "$@" |