From 934ab849a3ec91702d39b1c72d18872e7672cf4c Mon Sep 17 00:00:00 2001 From: Sthope Date: Tue, 8 Aug 2023 20:02:29 +0200 Subject: [PATCH] Add custom-cmds-in-ubuntu/bin_examples/system_info.sh --- .../bin_examples/system_info.sh | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 custom-cmds-in-ubuntu/bin_examples/system_info.sh diff --git a/custom-cmds-in-ubuntu/bin_examples/system_info.sh b/custom-cmds-in-ubuntu/bin_examples/system_info.sh new file mode 100644 index 0000000..f8b5a7b --- /dev/null +++ b/custom-cmds-in-ubuntu/bin_examples/system_info.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +# Log file for errors, warnings, and output +log_file="system_info.log" + +# Redirect both stdout and stderr to the log file +exec > >(tee -a "$log_file") 2>&1 + +if ! command -v dmidecode &> /dev/null; then + echo "dmidecode is not installed. Please install it." >&2 + exit 1 +fi + +# Color escape codes +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Define a divider line +divider="----------------------------------------------------------------------------------------------------------------------------------------------------------------------------" + +# Function to print section header +print_section_header() { + echo "$divider" + echo -e "${YELLOW}$1${NC}" + echo "$divider" +} + +# Function to convert bytes to human-readable sizes +human_readable_size() { + num=$1 + unit=("B" "KB" "MB" "GB" "TB") + index=0 + + while [ $num -ge 1024 ] && [ $index -lt 4 ]; do + num=$((num / 1024)) + index=$((index + 1)) + done + + echo "$num ${unit[$index]}" +} + +# Get system information +hostname=$(hostname) +kernel=$(uname -r) +cpu=$(cat /proc/cpuinfo | grep "model name" | head -n 1 | cut -d ':' -f 2 | xargs) +memory=$(free -b | grep "Mem:" | awk '{print $2}') +disk=$(df -h / | awk 'NR==2{print $2}') +uptime=$(uptime -p) +ip_address=$(ip route get 1.1.1.1 | grep -oP 'src \K\S+') +motherboard_brand=$(sudo dmidecode -s baseboard-manufacturer) +motherboard=$(sudo dmidecode -s baseboard-product-name) +gpu=$(lspci | grep -i vga | cut -d ":" -f3) +audio=$(lspci | grep -i audio | cut -d ":" -f3) + +# Create a timestamp for the filename +# d=$(date +"%Y-%m-%d") +# t=$(date +"%H-%M-%S") +# filename="${hostname}_${d}_${t}.log" +filename="${hostname}.log" + +# Get the number of CPU cores +cpu_cores=$(nproc) + +# Write information to the file +echo "System Information" > "$filename" +print_section_header "System Information" +echo "Hostname: $hostname" >> "$filename" +echo "IP Address: $ip_address" >> "$filename" +echo "Uptime: $uptime" >> "$filename" +echo "Kernel Version: $kernel" >> "$filename" +echo "CPU: $cpu ($cpu_cores cores)" >> "$filename" # Display CPU cores here +echo "Memory: $(human_readable_size $memory)" >> "$filename" +echo "Disk Space: $disk" >> "$filename" +echo "Motherboard: $motherboard_brand $motherboard" >> "$filename" +echo "GPU:$gpu" >> "$filename" +echo "Audio:$audio" >> "$filename" +echo "$divider" >> "$filename" + +# System Health Check +print_section_header "System Health Check" +cpu_usage=$(top -n 1 -b | grep "%Cpu" | awk '{print $2}') +memory_usage=$(free -m | awk 'NR==2{print $3}') +disk_usage=$(df -h / | awk 'NR==2{print $5}') +echo "CPU Usage: $cpu_usage%" >> "$filename" +echo "Memory Usage: $memory_usage MB" >> "$filename" +echo "Disk Usage: $disk_usage" >> "$filename" +echo "$divider" >> "$filename" + +# Installed Packages +print_section_header "Installed Packages" +installed_packages=$(dpkg -l) # or rpm -qa for Red Hat-based systems +echo "Package Name | Version" >> "$filename" +echo "$installed_packages" | awk 'NR>5{print}' >> "$filename" # Skip header lines +echo "$divider" >> "$filename" + +echo -e "${GREEN}System information saved to $filename${NC}"