From 8e4a52194cc0a054bc48201c81b39ee0abc3a518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Mon, 8 Nov 2021 10:19:55 +0100 Subject: [PATCH] Move hardware-to-mkd script to hardware repository --- hardware-to-mkd | 149 ------------------------------------------------ 1 file changed, 149 deletions(-) delete mode 100755 hardware-to-mkd diff --git a/hardware-to-mkd b/hardware-to-mkd deleted file mode 100755 index f501e81..0000000 --- a/hardware-to-mkd +++ /dev/null @@ -1,149 +0,0 @@ -#!/bin/sh - -if [ "$(whoami)" != "root" ]; then - echo "This script is intended to run as root. Exiting" >&2 - exit 1 -fi - -for cmd in lshw jq; do - if ! command -v $cmd > /dev/null; then - echo "Please install $cmd. Exiting." >&2 - exit 1 - fi -done - -if [ "$1" = "" ]; then - echo "Usage: export-hardware.sh PATH_TO_OUTPUT" >&2 - exit 1 -fi - -output=$1 -if [ -d "$1" ]; then - echo "$1 is a directory and cannot be used as output. Exiting." >&2 - exit 1 -fi - -# We extract hardware details only once, as it can be somewhat expensive/slow. -echo "Extracting hardware informations.... " -hardware=$(lshw -json) -hostname=$(hostname --short) -components=$(echo "$hardware" | jq -r -c '..|.children?|select(.!=null)|.[]') -echo "--- OK" - -print_server_component () { - class=$(echo "$@"| jq -j -c .class) - if echo "system processor memory power bus" | grep -w -q "$class"; then - # Motherboards are 'bus' class and what **seem** to be consistent (from a - # server + my laptop) 'Motherboard' description. - if [ "$class" = "bus" ] && [ "$(echo "$@"| jq -j -c .description)" != "Motherboard" ]; then - return - fi - - serial=$(echo "$@"| jq -j -c '.serial // "-"') - name='\(.description // .product)' - manufacturer='\(.vendor // "-")' - model='\(.product // "-")' - - case "$class" in - "power") - name="$name (slot: \\(.slot // \"-\"))" - ;; - "system") - # Ignore irrelevant PnP (and probably others) devices. - if [ "$serial" = "-" ]; then - return - fi - ;; - "memory") - # Ignore CPU cache (= memory entries without serial number). - if [ "$serial" = "-" ]; then - return - fi - - # Extract size of RAM sticks. - units=$(echo "$@"| jq -j -c .units) - size=$(echo "$@"| jq -j -c .size) - if [ "$units" = "bytes" ]; then - model="\(.product) ($(((size / 1024 / 1024 / 1024 ))) GB)" - else - model='\(.product) (\(.size) \(.units),)' - fi - ;; - esac - - echo "$@" | jq -j -c ".|\"| $class | $name | $manufacturer | $model | $serial | \n\"" - fi -} - -get_disk_details () { - product=$(echo "$@" | jq -j -c .product) - size=$(echo "$@" | jq -j -c .size) - units=$(echo "$@" | jq -j -c .units) - serial=$(echo "$@" | jq -j -c .serial) - - if [ "$units" = "bytes" ]; then - size=$(((size / 1024 / 1024 / 1024))) - units='GB' - fi - - cat <<- EOF - $product ($serial, $size $units) - EOF -} - -### Output ### - -touch "$output" - -echo "Generating system section... " -cat <<- EOF > "$output" - # Host: $hostname - - Extracted on $(date -I) by the [hardware-to-mkd script](https://code.recycled.cloud/RecycledCloud/tools/src/branch/master/hardware-to-mkd). - - ## System - - | Class | Name | Manufacturer | Model | Serial | - |-------|------|--------------|-------|--------| - $(print_server_component "$hardware") -EOF - -echo "$components" | while read -r component; do -print_server_component "$component" >> "$output" -done -echo "--- OK" - -echo "Generating disks section... " -cat <<- EOF >> "$output" - - ## Disks - - | Disk | - |------| -EOF - echo "$components" | while read -r component; do - class=$(echo "$component"| jq -j -c .class) - if [ "$class" = "disk" ]; then - echo "| $(get_disk_details "$component") |" >> "$output" - fi -done -echo "--- OK" - -echo "Generating NICs section... " -cat <<- EOF >> "$output" - ## NICs - - | Interface | MAC | - |-----------|-----| -EOF - -echo "$components" | while read -r component; do - class=$(echo "$component"| jq -j -c .class) - if [ "$class" = "network" ]; then - serial=$(echo "$component"| jq -j -c '.serial // "-"') - iface=$(ip -j l | jq -r -c ".[] | select(.address == \"$serial\") | .ifname") - echo "| $iface | $serial |" >> "$output" - fi -done - -echo "--- OK"