Significantly improve script

This commit is contained in:
Loïc Guégan 2024-07-05 16:15:28 +02:00
parent afecf5a06d
commit 35c6d6cbea
2 changed files with 165 additions and 92 deletions

View file

@ -9,11 +9,8 @@ Run a dump:
Run a dump on a remote machine using ssh:
> cat sysdump.sh | ssh user@host /bin/bash > dump.json
Explore a dump (require `jq`):
> ./sysdump.sh dump.json
Explore specific entries of a dump:
> ./sysdump.sh dump.json uname timezone
To explore a dump (require `jq`), see help as follow:
> ./sysdump.sh -h
# Useful entries
Important note: Depending on the system, some entries may not be available.
@ -33,10 +30,6 @@ Important note: Depending on the system, some entries may not be available.
# Notes
- Some commands require root permissions.
If `sysdump.sh` is not run as root, these commands will not be executed.
- List available entries:
> cat dump.json |grep -o ".*:"
- List available command versions:
> cat dump.json |grep -o "cmd_.*_version"
- For more infos on what is actually dump see `sysdump.sh`
If `sysdump.sh` is not run as root, these commands will not be executed and the entries
will not be reported in the dump file.

View file

@ -29,32 +29,7 @@ dump() {
[ "$USE_COMMA" -eq 0 ] && echo "\"${entry}\": \"${value}\""
}
# Read mode
if [ $# -gt 0 ]
then
ensure jq
file=$1
[ ! -f "$file" ] && { echo >&2 "Dump \"$file\" not found, abort..."; exit 1; }
if [ $# -gt 1 ]
then
shift
for entry in $@
do
echo "====================> $entry"
cat "$file"|jq -r ".[\"${entry}\"]"|base64 -d
done
exit 0
fi
while IFS= read -r entry; do
echo "====================> $entry"
cat "$file"|jq -r ".[\"${entry}\"]"|base64 -d
done <<< "$(cat "$file"|jq -r 'keys[]')"
exit 0
fi
# Check commands exist
ensure base64
sysdump() {
# Start dump
echo "{"
USE_COMMA=1
@ -131,4 +106,109 @@ dump "fdisk" safecmdroot fdisk -l
USE_COMMA=0
dump "dmesg" safecmdroot dmesg
echo "}"
}
# Parse arguments
POSITIONAL_ARGS=()
ACTION="dump"
while [[ $# -gt 0 ]]; do
case $1 in
-l|--list-entries)
ACTION="list"
shift
;;
-s|--summarize)
ACTION="summarize"
shift # past value
;;
-p|--parse)
ACTION="parse"
shift # past value
;;
-h|--help)
echo "Usage: $0 [OPTION] [DUMP_FILE] [ENTRIES]"
echo " -l, --list-entries: Show available entries from a dump file"
echo " -p, --parse: Parse the content of a dump file"
echo " Example 1: $0 -p dump.json"
echo " Example 2: $0 -p dump.json uname uptime"
echo " -s, --summarize: Summarize a dump file"
echo " -h, --help: Show this help"
exit 0
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
# Check requirements
ensure base64
[ "$ACTION" == "dump" ] && [ $# -ne 0 ] && { echo "I do not understand the following: $@"; exit 1; }
[ "$ACTION" != "dump" ] && [ $# -eq 0 ] && { echo "Missing dump file path"; exit 1; }
[ "$ACTION" != "dump" ] && [ ! -f "$1" ] && { echo "File \"$1\" not found"; exit 1; }
# Do dump
[ "$ACTION" == "dump" ] && { sysdump; exit 0; }
# List entries
[ "$ACTION" == "list" ] && { ensure jq; cat "$1"|jq -r "keys[]"; exit 0; }
# Parse dump file
if [ "$ACTION" == "parse" ]
then
ensure jq
file=$1
if [ $# -gt 1 ]
then
shift
for entry in $@
do
echo "====================> $entry"
cat "$file"|jq -r ".[\"${entry}\"]"|base64 -d
done
exit 0
fi
while IFS= read -r entry; do
echo "====================> $entry"
cat "$file"|jq -r ".[\"${entry}\"]"|base64 -d
done <<< "$(cat "$file"|jq -r 'keys[]')"
exit 0
fi
# Summarize dump file
if [ "$ACTION" == "summarize" ]
then
ensure jq
file=$1
OS_RELEASE=$(cat "$file"|jq -r '.["/etc/os-release"]'|base64 -d)
CPU_INFO=$(cat "$file"|jq -r '.["/proc/cpuinfo"]'|base64 -d)
PING_SUCCESS_COUNT=$(cat "$file"|jq -r '.["ping"]'|base64 -d|grep "packet loss"|cut -d, -f 2|awk '{print $1+0}')
# Extract infos
INFO_OS_NAME=$(echo "$OS_RELEASE"|grep "^NAME=" | cut -d'"' -f 2)
INFO_OS_VERSION=$(echo "$OS_RELEASE"|grep "^VERSION=" | cut -d'"' -f 2)
INFO_CPU_MODEL=$(echo "$CPU_INFO"|grep "model name" | cut -d':' -f 2|uniq|awk '{$1=$1};1')
INFO_CPU_CORE_N_PHY=$(echo "$CPU_INFO"|grep ^cpu\\scores /proc/cpuinfo | uniq | awk '{print $4}')
INFO_CPU_CORE_N_VIRT=$(echo "$CPU_INFO"|grep -c ^processor /proc/cpuinfo)
[ "$INFO_CPU_CORE_N_PHY" -eq "$INFO_CPU_CORE_N_VIRT" ] && INFO_CPU_HYPERTHREADING="off" || INFO_CPU_HYPERTHREADING="on"
[ $PING_SUCCESS_COUNT -gt 0 ] && INFO_OTHER_NETWORK="on" || INFO_OTHER_NETWORK="off"
# Print Information
echo "====> OS <===="
echo "OS Name: ${INFO_OS_NAME}"
echo "OS Version: ${INFO_OS_VERSION}"
echo
echo "====> CPU <===="
echo "Model: ${INFO_CPU_MODEL}"
echo "Physical Core Count: ${INFO_CPU_CORE_N_PHY}"
echo "Logical Core Count: ${INFO_CPU_CORE_N_VIRT}"
echo "Hyperthreading State: ${INFO_CPU_HYPERTHREADING}"
echo
echo "====> Other informations <===="
echo "Network State: ${INFO_OTHER_NETWORK}"
fi