#!/bin/bash # This ist a small script which collect system information of a Linux PC and print it as json # copy this script to /usr/local/bin and make it executable # created by matthias of Angestöpselt e.V. # created at 2022-02-17 # modified at 2024-08-25 # Script muss mit sudo Rechten ausgeführt werden if [ "$EUID" -ne 0 ] then echo "Please run as root" exit fi install_if_not_exist() { if /usr/bin/dpkg -s "$1" &>/dev/null; then PKG_EXIST=$(/usr/bin/dpkg -s "$1" | grep "install ok installed") if [[ -n "$PKG_EXIST" ]]; then return fi fi /usr/bin/apt-get install --no-install-recommends --yes "$1" } install_if_not_exist jq install_if_not_exist jc # Werte aus dmidecode auslesen dmidecode_output=$(jc dmidecode) type=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Chassis Information") | .values.type') manufacturer=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "System Information") | .values.manufacturer') product_name=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "System Information") | .values.product_name') serial_number=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "System Information") | .values.serial_number') family=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "System Information") | .values.family') memory_size=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Memory Device" and .values.bank_locator == "BANK 0") | .values.size') memory_type=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Memory Device" and .values.bank_locator == "BANK 0") | .values.type') cpu_family=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.family') cpu_manufacturer=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.manufacturer') cpu_version_raw=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.version') cpu_core_count=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.core_count') cpu_thread_count=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.thread_count') # Formatiere Variablen für eine schöne Ausgabe # Im Moment ist es nur auf einem Intel Laptop getestet mit einem Intel Core i5 cpu_version=$(echo "${cpu_version_raw%" CPU"*}") # Laptop spezifischen Werte, Batterie, Display battery_dir="/sys/class/power_supply/BAT0" # Überprüfen, ob das Verzeichnis existiert (d.h. ob eine Batterie vorhanden ist) if [ -d "$battery_dir" ]; then charge_full_design=$(cat "$battery_dir/charge_full_design") charge_full=$(cat "$battery_dir/charge_full") # Bestimme auch Display Größe und Auflösung sofern ein Akku vorhanden ist display_resolution=$(xdpyinfo | awk '/dimensions/ {print $2}') display_size_raw=$(xrandr | awk '/ connected/{print sqrt( ($(NF-2)/10)^2 + ($NF/10)^2 )/2.54}' | head -n 1) display_size=$(LC_ALL=C /usr/bin/printf "%.*f\n" 2 $display_size_raw) # Sicherstellen, dass beide Werte numerisch sind if [[ "$charge_full_design" =~ ^[0-9]+$ ]] && [[ "$charge_full" =~ ^[0-9]+$ ]]; then if [ "$charge_full_design" -ne 0 ]; then bat_capacity=$((100 * charge_full / charge_full_design)) fi fi else bat_capacity="no_battery" display_resolution="n/a" display_size="n/a" fi # Erstelle das JSON-Objekt jq -n \ --arg type "$type" \ --arg manufacturer "$manufacturer" \ --arg product_name "$product_name" \ --arg serial_number "$serial_number" \ --arg family "$family" \ --arg memory_size "$memory_size" \ --arg memory_type "$memory_type" \ --arg cpu_family "$cpu_family" \ --arg cpu_manufacturer "$cpu_manufacturer" \ --arg cpu_version "$cpu_version" \ --arg cpu_core_count "$cpu_core_count" \ --arg cpu_thread_count "$cpu_thread_count" \ --arg bat_capacity "$bat_capacity" \ --arg display_resolution "$display_resolution" \ --arg display_size "$display_size" \ '{ type: $type, manufacturer: $manufacturer, product_name: $product_name, serial_number: $serial_number, family: $family, memory_size: $memory_size, memory_type: $memory_type, cpu_family: $cpu_family, cpu_manufacturer: $cpu_manufacturer, cpu_version: $cpu_version, cpu_core_count: $cpu_core_count, cpu_thread_count: $cpu_thread_count, bat_capacity: $bat_capacity, display_resolution: $display_resolution, display_size: $display_size }'