blob: 47ec6d3977e3b81b40ce3188094cf4cea492dec4 [file] [log] [blame]
#!/bin/bash
# Copyright 2019 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Run, inspect and debug
### generate a report of error conditions
## usage: fx triage [--config <config-file>]
## [--select <select-string>]
## [--data <path-to-snapshot>]
## Without 'select':
## Processes "fx snapshot" output looking for problems specified in config
## files. If no snapshot output is specified (no '--data') it runs a new
## "fx snapshot" and uses its "inspect.json" file.
## With 'select':
## Generates selectors from the snapshot, filtered by select-string.
## --config, --tag, --exclude-tag options are ignored.
##
## --config <config-file-or-dir> Path to config file or dir
## --data <snapshot-or-dir> Path to snapshot.zip or uncompressed dir
## --tag <tag> Adds an action tag to include
## --exclude-tag <tag> Adds an action tag to exclude
## --select <string> Generates selectors filtered by <string>
## --test (deprecated) Run self-tests (only)
##
## 0 or more --config paths may be given. Path is relative to CWD, or absolute.
## If path is a directory, path/*.triage will be loaded.
## If 0 paths are given, config defaults to
## /src/diagnostics/config/triage/*.triage.
##
## 0 or more --tag values may be given. If tags are provided, only actions with
## matching tags will be included.
##
## 0 or more --exclude-tag values may be given. If exclude tags are provided,
## actions with matching tags will be excluded. This value is ignored if
## any --tag values are included.
##
## If no --tag values or --exclude-tag values are included then all of the
## actions will be included.
##
## 0 or 1 --data paths may be given.
## Path must point to a snapshot.zip or a directory containing an unpacked snapshot.zip.
## For this path, 'fx triage' will analyze all supported filetypes.
## Currently, the only supported filetype is inspect.json.
## Future versions will add support for different files.
## If no --data is given,`fx snapshot` will be invoked and analyzed.
##
## 0 or more --select strings may be given. --select changes the
## operation of the program: it generates all selectors for all
## data in the snapshot and then filters them (via grep) through
## all the --select strings.
set -e
trap 'echo "Triage found a problem!"' ERR
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"\
/../lib/image_build_vars.sh
# Defaults.
config_paths=( )
testit=""
data_paths=( )
tags=( )
exclude_tags=( )
flags=""
select_filters=( )
# Flag parsing.
while [[ "$1" =~ ^- ]]; do
case "$1" in
-h|--help)
fx-command-help
exit 0
;;
--config)
shift
config_paths+=( "$1" )
;;
--data)
shift
data_paths+=( "$1" )
;;
--select)
shift
select_filters+=( "$1" )
;;
--tag)
shift
tags+=( "$1" )
;;
--exclude-tag)
shift
exclude_tags+=( "$1" )
;;
--test)
testit="yes"
;;
*)
echo Bad option "$1"
echo
fx-command-help
exit 1
esac
shift
done
triage_exe="${HOST_OUT_DIR}/../host-tools/triage"
diag_tool_exe="${HOST_OUT_DIR}/../host-tools/diag_tool"
if [[ -n "$testit" ]]; then
echo "[ERROR] fx triage --test is deprecated!"
echo "please use 'fx test triage_lib_test' instead"
exit
fi
if [ -z "${data_paths}" ]; then
temp_dir=$(mktemp -d)
trap 'rm -rf "${temp_dir}"' EXIT
fx-command-run snapshot --output-directory "${temp_dir}" > /dev/null
unzip "${temp_dir}/snapshot.zip" -d "${temp_dir}" > /dev/null
data_paths+="${temp_dir}"
fi
unzipped_data_dirs=( )
for data_path in "${data_paths[@]}"
do
if [[ -d "${data_path}" ]] ; then
# This should be an unzipped snapshot.zip; pass it to the Triage
# executable.
# Triage will complain if snapshot.zip files aren't in the directory.
unzipped_data_dirs+=( $data_path )
else
# This should be a .zip file. unzip will complain if not. Unzip it and
# pass its contents to the Triage executable.
temp_dir=$(mktemp -d)
trap 'rm -rf "${temp_dir}"' EXIT
unzip "${data_path}" -d "${temp_dir}" > /dev/null
unzipped_data_dirs+=( $temp_dir )
fi
done
if [ -z "${select_filters}" ]; then
if (( !"${#config_paths[@]}" )); then
config_paths=( "${FUCHSIA_DIR}/src/diagnostics/config/triage/"
"${FUCHSIA_DIR}/src/diagnostics/config/triage/detect/" )
fi
config_files=( )
for config_path in "${config_paths[@]}"; do
if [[ -d "${config_path}" ]] ; then
config_files+=( "${config_path}"/*.triage )
else
config_files+=( "${config_path}" )
fi
done
for config_file in "${config_files[@]}"
do
flags="${flags} --config ${config_file}"
done
for tag in "${tags[@]}"
do
flags="${flags} --tag ${tag}"
done
for data_path in "${unzipped_data_dirs[@]}"
do
flags="${flags} --data ${data_path}"
done
for tag in "${exclude_tags[@]}"
do
flags="${flags} --exclude-tag ${tag}"
done
if [[ ! -f "$triage_exe" ]]; then
echo "Building Triage..."
fx-command-run build triage
fi
$triage_exe ${flags}
else
temp_dir=$(mktemp -d)
trap 'rm -rf "${temp_dir}"' EXIT
for data_path in "${unzipped_data_dirs[@]}"
do
$diag_tool_exe --snapshot "$data_path/inspect.json" generate "$temp_dir/diag_out"
cat "$temp_dir/diag_out" >> "$temp_dir/diag_all"
echo >> "$temp_dir/diag_all" # diag_tool output has no final newline
done
for filter in "${select_filters[@]}"
do
# Note this repeatedly filters diag_all by writing to a temp file and
# moving it back into place.
cat "$temp_dir/diag_all" | grep "$filter" > "$temp_dir/diag_filtered"
mv "$temp_dir/diag_filtered" "$temp_dir/diag_all"
done
cat "$temp_dir/diag_all" | sort | uniq | sed s/^/INSPECT:/
fi