#!/bin/bash
HOSTNAME=`hostname`
IP=`ip link show | grep -v lo | grep UP | awk {'print $2'} | tr -d ":" | awk '{system("ip addr show dev "$1)}'  | grep inet | grep -v inet6 | awk {'print $2'} | awk -F "/" {'print $1'}`

#Hardware and BIOS
MANUFACTURER=`dmidecode -t 2 | grep Manufacturer | sed -e 's/Manufacturer://g' | sed -e 's/^[ \t]*//'`
PRODUCT=`dmidecode -t 2 | grep "Product Name:" | sed -e "s/Product Name://g" |  sed -e 's/^[ \t]*//'`
SERIAL=`dmidecode -t 2 | grep "Serial Number:" | sed -e "s/Serial Number://g" |  sed -e 's/^[ \t]*//'`
VERSION=`dmidecode -t 0 | grep "Version:" | sed -e "s/Version://g" |  sed -e 's/^[ \t]*//'`

#CPU Detection
hwinfo --short --cpu | tail -n +2 | sed -e 's/^[ \t]*//'  | head -n 1 | awk -F "," {'print $1'} >  /tmp/cpu
CPU=`cat /tmp/cpu`
CORES=`lscpu | grep Core | awk {'print $4'}`
SOCKETS=`lscpu | grep Socket | awk {'print $2'}`

#Drives Detection
hwinfo --short --disk | tail -n +2 | awk {'print $1'} | awk '{system("smartctl --all "$1)}' | grep -E "Device Model|Model Number" | awk -F ":" {'print $2'} | sed -e 's/^[ \t]*//' > /tmp/disk_model
hwinfo --short --disk | tail -n +2 | awk {'print $1'} | awk '{system("smartctl --all "$1)}' | grep "Serial" | awk -F ":" {'print $2'} | sed -e 's/^[ \t]*//' > /tmp/disk_serial
hwinfo --short --disk | tail -n +2 | awk {'print $1'} | awk '{system("echo \"unit GB \n p\" | parted "$1 " 2>/dev/null | grep "$1 "| grep -v Using")}' > /tmp/disk_capacity.tmp
cat /tmp/disk_capacity.tmp | awk {'print $3'} > /tmp/disk_capacity
hwinfo --short --disk | tail -n +2 | awk {'print $1'} | awk '{system("hdparm -Tt "$1" | grep buffered")}' | awk {'print $11 " MB/sec"'} > /tmp/disk_speed

#Model + Serial + Speed
paste -d "|" /tmp/disk_model /tmp/disk_serial /tmp/disk_speed /tmp/disk_capacity > /tmp/drives
DRIVES=`cat /tmp/drives | awk -F "|" '{i+1; print "\"Drive " i++ "\": { Model:\"" $1"\",\"Serial\":\""$2"\",\"Speed\":\""$3"\",\"Capacity\":\""$4"\"},"}'`
#Memory
dmidecode --type 17  | grep "Size:" | grep -v " Size" | awk -F ":" {'print $2'} | sed -e 's/^[ \t]*//' > /tmp/memory_slots
cat /tmp/memory_slots | awk {'print $1'} | grep -v "No" | paste -s -d+ | bc > /tmp/ram_amount
RAM=`cat /tmp/ram_amount`
SLOTS=`cat /tmp/memory_slots | awk '{i+1; print "\"Slot " i++ "\":\"" $0"\","}'`

#SMART logs
hwinfo --short --disk | tail -n +2 | awk {'print $1'} | awk '{system("smartctl --all "$1)}' | sed -E 's/([^\]|^)"/\1\\"/g' > /tmp/smart
SMART=`cat /tmp/smart | base64 -w 0`

#DMIDECODE
DMIDECODE=`dmidecode | base64 -w 0`

#Prepare JSON
cat << EOF
{
    "host": {
        "label": "$HOSTNAME",
	"ip": "$IP",
        "hardware": {
            "bios": {
              "Manufacturer":"$MANUFACTURER",
	      "Serial Number":"$SERIAL",
	      "Product":"$PRODUCT",
	      "Version":"$VERSION"
	    },
            "cpu": {
               "model":"$CPU",
               "cores":"$CORES",
               "sockets":"$SOCKETS"
            },
            "memory": {
	      "amount": "$RAM GB",
              "slots": {
		$SLOTS
	      }
            },
            "drives": {
		$DRIVES
            },
            "smart": "$SMART",
            "dmidecode": "$DMIDECODE"
        }
    }
}
EOF
