neues info Script

Übersichtlicher und nicht verwendete Infos aus dem Script entfernt
This commit is contained in:
Matthias Hemmerich 2024-08-25 09:32:58 +00:00
parent d651f714b6
commit 088dbd46c1

View file

@ -1,231 +1,108 @@
#!/bin/bash #!/bin/bash
# This ist a small script which collect system information of a Linux PC and print it as json # This ist a small script which collect system information of a Linux PC and print it as json
# created by matthias of angestöpselt e.V. # copy this script to /usr/local/bin and make it executable
# created by matthias of Angestöpselt e.V.
# created at 2022-02-17 # created at 2022-02-17
# modified at 2024-08-25
version=1.0 # Script muss mit sudo Rechten ausgeführt werden
charity=angestoepselt if [ "$EUID" -ne 0 ]
os=Linux then echo "Please run as root"
exit
read_uname() {
read -ra uname <<< "$(uname -srm)"
kernel_name="${uname[0]}"
kernel_version="${uname[1]}"
kernel_machine="${uname[2]}"
}
get_memory() {
memory="$(free -g | awk '/Mem:/ {printf "%i GiB", $2}')"
}
get_cpu() {
# Get CPU name.
cpu_file="/proc/cpuinfo"
cpu="$(awk -F '\\s*: | @' \
'/model name|Hardware|Processor|^cpu model|chip type|^cpu type/ {
cpu=$2; if ($1 == "Hardware") exit } END { print cpu }' "$cpu_file")"
# Remove un-needed patterns from cpu output.
cpu="${cpu//(TM)}"
cpu="${cpu//(tm)}"
cpu="${cpu//(R)}"
cpu="${cpu//(r)}"
cpu="${cpu//CPU}"
cpu="${cpu//Processor}"
cpu="${cpu//Dual-Core}"
cpu="${cpu//Quad-Core}"
cpu="${cpu//Six-Core}"
cpu="${cpu//Eight-Core}"
cpu="${cpu//[1-9][0-9]-Core}"
cpu="${cpu//[0-9]-Core}"
cpu="${cpu//, * Compute Cores}"
cpu="${cpu//Core / }"
cpu="${cpu//(\"AuthenticAMD\"*)}"
cpu="${cpu//with Radeon * Graphics}"
cpu="${cpu//, altivec supported}"
cpu="${cpu//FPU*}"
cpu="${cpu//Chip Revision*}"
cpu="${cpu//Technologies, Inc}"
cpu="${cpu//Core2/Core 2}"
# Remove CPU brand from the output.
cpu="${cpu/AMD}"
cpu="${cpu/Intel}"
cpu="${cpu/Core? Duo}"
cpu="${cpu/Qualcomm}"
cpu="${cpu//[[:space:]]}"
}
get_disk() {
type -p df &>/dev/null ||
{ err "Disk requires 'df' to function. Install 'df' to get disk info."; return; }
df_version=$(df --version 2>&1)
# Create an array called 'disks' where each element is a separate line from
# df's output. We then unset the first element which removes the column titles.
IFS=$'\n' read -d "" -ra disks <<< "$(df "${df_flags[@]}" "${disk_show[@]:-/}")"
unset "disks[0]"
# Stop here if 'df' fails to print disk info.
[[ ${disks[*]} ]] || {
err "Disk: df failed to print the disks, make sure the disk_show array is set properly."
return
}
for disk in "${disks[@]}"; do
# Create a second array and make each element split at whitespace this time.
IFS=" " read -ra disk_info <<< "$disk"
disk=$((disk_info[${#disk_info[@]} - 5] / 1024/ 1024))G
done
}
get_model() {
if [[ -d /system/app/ && -d /system/priv-app ]]; then
model="$(getprop ro.product.brand) $(getprop ro.product.model)"
elif [[ -f /sys/devices/virtual/dmi/id/board_vendor ||
-f /sys/devices/virtual/dmi/id/board_name ]]; then
model=$(< /sys/devices/virtual/dmi/id/board_vendor)
model+=" $(< /sys/devices/virtual/dmi/id/board_name)"
elif [[ -f /sys/devices/virtual/dmi/id/product_name ||
-f /sys/devices/virtual/dmi/id/product_version ]]; then
model=$(< /sys/devices/virtual/dmi/id/product_name)
model+=" $(< /sys/devices/virtual/dmi/id/product_version)"
elif [[ -f /sys/firmware/devicetree/base/model ]]; then
model=$(< /sys/firmware/devicetree/base/model)
elif [[ -f /tmp/sysinfo/model ]]; then
model=$(< /tmp/sysinfo/model)
fi
# Remove dummy OEM info.
model=${model//To be filled by O.E.M.}
model=${model//To Be Filled*}
model=${model//OEM*}
model=${model//Not Applicable}
model=${model//System Product Name}
model=${model//System Version}
model=${model//Undefined}
model=${model//Default string}
model=${model//Not Specified}
model=${model//Type1ProductConfigId}
model=${model//INVALID}
model=${model//All Series}
model=${model//<2F>}
case $model in
"Standard PC"*) model="KVM/QEMU (${model})" ;;
OpenBSD*) model="vmm ($model)" ;;
esac
}
get_vendor() {
sys_vendor=$(cat /sys/devices/virtual/dmi/id/sys_vendor)
}
get_distro() {
source /etc/os-release
distro="${NAME} ${VERSION_ID}"
}
get_serial() {
[ `id -u` -eq 0 ] && serialno=$(sudo cat /sys/devices/virtual/dmi/id/product_serial)
}
get_category() {
category=$(hostnamectl | grep Chassis | cut -c 21-)
}
get_odd() {
[[ $(lsblk --include 11 -n) ]] && odd="1" || odd="0"
}
get_mac() {
mac=$(ip link | sed -n "/BROADCAST.*state UP/{n;p}" | tail -1 | tr -s " " | cut -d" " -f3)
}
get_anydesk_id() {
dpkg-query -l "anydesk" &>/dev/null && anydesk_status="TRUE" || anydesk_status="FALSE"
if [ "$anydesk_status" == "TRUE" ]; then
# fetching the id of anydesk consumes most of the time when calling the script. Room for improvement
andydesk_id=$(/usr/bin/anydesk --get-id)
fi
}
get_battery() {
for bat in /sys/class/power_supply/*; do
if [[ $bat =~ BAT ]]; then
source $bat/uevent
energy_full_design="$(< "${bat}/energy_full_design")"
energy_full="$(< "${bat}/energy_full")"
battery=`expr ${energy_full} \* 100 / ${energy_full_design}`%
fi
done
}
get_resolution() {
resolution=$(xdpyinfo | awk '/dimensions/ {print $2}')
}
get_display() {
display=`xrandr | awk '/ connected/{print sqrt( ($(NF-2)/10)^2 + ($NF/10)^2 )/2.54}' | cut -c -2 | head -n 1`
}
get_desktopmanager() {
echo $XDG_CURRENT_DESKTOP
}
# fetch informations
read_uname
get_memory
get_cpu
get_disk
get_model
get_vendor
get_distro
get_serial
get_category
get_odd
get_mac
get_desktopmanager
get_anydesk_id
# fetch additional laptop infos
if [[ $category == "laptop" ]]; then
get_battery
get_resolution
get_display
fi fi
# print summary in json 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"
}
printf '{\n' install_if_not_exist jq
printf ' %s\n' "\"Charity\": \"${charity}\"", install_if_not_exist jc
printf ' %s\n' "\"Distro\": \"${distro}\"",
printf ' %s\n' "\"Vendor\": \"${sys_vendor}\"", # Werte aus dmidecode auslesen
printf ' %s\n' "\"Model\": \"${model}\"", dmidecode_output=$(jc dmidecode)
printf ' %s\n' "\"Serial\": \"${serialno}\"",
printf ' %s\n' "\"Platform\": \"${category}\"", type=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Chassis Information") | .values.type')
printf ' %s\n' "\"CPU\": \"${cpu}\"", manufacturer=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "System Information") | .values.manufacturer')
printf ' %s\n' "\"Memory\": \"${memory}\"", product_name=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "System Information") | .values.product_name')
printf ' %s\n' "\"HDD\": \"${disk}\"", serial_number=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "System Information") | .values.serial_number')
printf ' %s\n' "\"HW_ADDRESS\": \"${mac}\"", family=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "System Information") | .values.family')
printf ' %s\n' "\"Anydesk_ID\": \"${andydesk_id}\"", memory_size=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Memory Device" and .values.bank_locator == "BANK 0") | .values.size')
printf ' %s\n' "\"Resolution\": \"${resolution}\"", memory_type=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Memory Device" and .values.bank_locator == "BANK 0") | .values.type')
printf ' %s\n' "\"Displaysize\": \"${display}\"", cpu_family=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.family')
printf ' %s\n' "\"Battery_life\": \"${battery}\"", cpu_manufacturer=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.manufacturer')
printf ' %s\n' "\"Optical_Drive\": \"${odd}\"", cpu_version_raw=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.version')
printf ' %s\n' "\"Version\": \"${version}\"" cpu_core_count=$(echo "$dmidecode_output" | jq -r '.[] | select(.description == "Processor Information") | .values.core_count')
printf '}\n' 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
}'