#!/usr/bin/env bash set -e -o errexit set -o pipefail if [[ "${DEBUG}" == 'true' ]]; then set -o xtrace else export DEBUG= fi set -o nounset isNixOS() { if [ -f /etc/NIXOS ]; then echo 1 else echo 0 fi } assertIsNixOS() { MESSAGE="${1:-Program vyžaduje prostedie Linux distribúcie NixOS.}" if [ "x$(isNixOS)" == "x0" ]; then echo "ERROR: $MESSAGE" >/dev/stderr exit 1 fi } assertIsSupportedNixOS() { REQUIRED_VER="${1:-22.05}" assertIsNixOS source /etc/os-release if [ "${VERSION_ID}" != "${REQUIRED_VER}" ]; then echo "ERROR: Vyžaduje sa verzia NixOS ${REQUIRED_VER}. Aktuálne máš spustenú verziu ${VERSION_ID}." >/dev/stderr exit 1 fi } isRoot() { if [ $UID != 0 ]; then echo 0 else echo 1 fi } assertIsRoot() { MESSAGE="${1:-Program vyžaduje práva super používateľa. Použite príkaz 'sudo -i'.}" if [ "x$(isRoot)" == "x0" ]; then echo "ERROR: $MESSAGE" >/dev/stderr exit 1 fi } isNixInstaller() { if grep -q "imports = \[ /dev/stderr exit 1 fi } assertIsNotNixInstaller() { MESSAGE="${1:-Program nie je možné spustiť v prostredí NixOS inštalačného média.}" if [ "x$(isNixInstaller)" != "x0" ]; then echo "ERROR: $MESSAGE" >/dev/stderr exit 1 fi } detectVga() { local _vga local _vga_length local _VIDEO_DRIVER # adapted from https://github.com/helmuthdu/aui/blob/master/sharedfuncs _vga=`nix-shell -p pciutils --run 'lspci | grep VGA | tr "[:upper:]" "[:lower:]"'` _vga_length=`nix-shell -p pciutils --run 'lspci | grep VGA | wc -l'` if [[ -n $(hostnamectl | grep VirtualBox) ]]; then _VIDEO_DRIVER="virtualbox" elif [[ $_vga_length -eq 2 ]] && [[ -n $(echo ${_vga} | grep "nvidia") || -f /sys/kernel/debug/dri/0/vbios.rom ]]; then _VIDEO_DRIVER="bumblebee" elif [[ -n $(echo ${_vga} | grep "nvidia") || -f /sys/kernel/debug/dri/0/vbios.rom ]]; then _VIDEO_DRIVER="nvidia" # _VIDEO_DRIVER="nouveau" elif [[ -n $(echo ${_vga} | grep "advanced micro devices") || -f /sys/kernel/debug/dri/0/radeon_pm_info || -f /sys/kernel/debug/dri/0/radeon_sa_info ]]; then _VIDEO_DRIVER="ati" elif [[ -n $(echo ${_vga} | grep "intel corporation") || -f /sys/kernel/debug/dri/0/i915_capabilities ]]; then _VIDEO_DRIVER="intel" else _VIDEO_DRIVER="vesa" fi echo $_VIDEO_DRIVER } label () { echo ">>>> $1 <<<<" } collectDiag() { date +"%Y-%m-%dT%H:%M:%S%:z" x="$(isNixInstaller)" echo "isNixInstaller=$x" label "BIOS" echo $([ -d /sys/firmware/efi/efivars ] && echo "UEFI" || echo "Legacy") label "/etc/os-release" cat /etc/os-release label "hostnamectl" hostnamectl label "uname" uname -a # label "dmidecode" # nix-shell -p dmidecode --run "dmidecode" label "detectVga" detectVga label "ip.me" curl ip.me label "ip addr" ip -j addr label "parted" nix-shell -p parted --run "parted -l -m" label "mount" mount -l label "lsblk" lsblk -abDJp -T -o name,kname,label,uuid,fstype,model,vendor,serial,size,PARTTYPE,PARTLABEL,PARTUUID,PARTFLAGS label "lspci" nix-shell -p pciutils --run 'lspci -mnn' label "lshw" nix-shell -p lshw --run "lshw -json -quiet" label "dmesg" dmesg # label "journalctl" # journalctl -b -x -o json-pretty } askForPassword() { read -p "Zadaj heslo ak chceš diagnostiku zdielať s niekým iným ako správcami stránky linux.curo.sk?" -s -r passwd /dev/stderr if [ -z "${passwd}" ]; then echo "-" else echo "${passwd}" fi fi } encrypt() { local FILE="${1}" local PASSWD="${2}" if [ -z "${PASSWD}" ]; then echo "ERROR: po" > /dev/stderr exit 1 fi if [ "${PASSWD}" != "-" ]; then # echo "šifrujem súbor s údajmi..." zip -q -P "${PASSWD}" "${FILE}.zip" "${FILE}" echo "${DIAG_FILE}.zip" else echo "${FILE}" fi } upload() { local FILE="${1}" echo "odosielam súbor ${FILE} na server..." curl -LX PUT linux.curo.sk --data-binary @"${FILE}" echo "" } export UPLOAD_PASSWORD="" echo "kontrola prostredia..." assertIsNixOS assertIsRoot # vytvorme si temporárny súbor s informáciami o HW DIAG_FILE=/tmp/diag.log echo "prebieha diagnostika..." collectDiag > $DIAG_FILE if [ -z "${UPLOAD_PASSWORD}" ]; then UPLOAD_PASSWORD="$(askAboutUpload)" fi if [ ! -z "$UPLOAD_PASSWORD" ]; then UPLOAD_FILE=$(encrypt "$DIAG_FILE" "$UPLOAD_PASSWORD") echo "" upload "$UPLOAD_FILE" if [ "${UPLOAD_PASSWORD}" != "-" ]; then echo "Použité heslo: ${UPLOAD_PASSWORD}" fi fi echo "" echo "diagnostické informácie môžes prezrieť príkazom: less ${DIAG_FILE}" echo "koniec."