-
Notifications
You must be signed in to change notification settings - Fork 3
/
sysinfo2json.sh
105 lines (90 loc) · 3.19 KB
/
sysinfo2json.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
#title: sysinfo2json.sh
#description: Script to generate system information from /proc/cpuinfo, dmidecode, lshw in json format.
#author: Bhavyanshu Parasher (https://bhavyanshu.me)
#usage: sudo bash sysinfo2json.sh
#==============================================================================
getCPUInfo() {
cpufile="/proc/cpuinfo"
start='"Processors" : ['
end=']'
if [ -f "$cpufile" ]; then
count=$(grep -Ec 'processor' $cpufile)
i=1
while [ $i -le $count ]; do
fetch="$(cat "$cpufile" | awk -vi="$i" '/processor/{j++}j==i')"
grep_processor=$(echo "$fetch" | perl -F: -alpe 's/.*:*/"$F[0]":"$F[1]"/' | tr -s '\n' ',' | sed 's/\s\(":"\)\s/":"/g' | sed -zE 's/[[:space:]]+([:"])/\1/g' )
proc="{"${grep_processor::-1}"},"
mid=$mid$proc
i=$((i+1))
done
fi
echo $start${mid::-1}$end
}
getMemInfo() {
start='"MemoryUnits" : ['
end=']'
memInfo=$(dmidecode --type memory | grep 'Memory\|Size\|Type\|Speed\|Manufacturer\|Serial\|Part' | sed "/Memory/d")
count=$(echo "$memInfo" | grep -Ec 'Size')
i=1
while [ $i -le $count ]; do
fetch="$(echo "$memInfo" | awk -vi="$i" '/Size/{j++}j==i')"
grep_processor=$(echo "$fetch" | perl -F: -alpe 's/.*:*/"$F[0]":"$F[1]"/' | tr -s '\n' ',' | sed 's/\s\(":"\)\s/":"/g' | sed -zE 's/[[:space:]]+([:"a-zA-Z0-9])/\1/g' )
proc="{"${grep_processor::-1}"},"
mid=$mid$proc
i=$((i+1))
done
echo $start${mid::-1}$end
}
getDiskInfo() {
start='"Disks" : ['
end=']'
diskInfo=$(sudo lshw -quiet -class disk | sed "/*-/d")
count=$(lshw -quiet -class disk | grep -Ec '*-disk')
i=1
while [ $i -le $count ]; do
fetch="$(echo "$diskInfo" | awk -vi="$i" '/description/{j++}j==i')"
grep_processor=$(echo "$fetch" | perl -F: -alpe 's/.*:*/"$F[0]":"$F[1]"/' | tr -s '\n' ',' | sed 's/\s\(":"\)\s/":"/g' | sed -zE 's/[[:space:]]+([:"a-zA-Z0-9])/\1/g' )
proc="{"${grep_processor::-1}"},"
mid=$mid$proc
i=$((i+1))
done
echo $start${mid::-1}$end
}
getMotherboardInfo() {
start='"Motherboard" : {'
end='}'
mbinfo=$(dmidecode --type baseboard | grep 'Manufacturer\|Name\|Version\|Serial')
grep_processor=$(echo "$mbinfo" | perl -F: -alpe 's/.*:*/"$F[0]":"$F[1]"/' | tr -s '\n' ',' | sed 's/\s\(":"\)\s/":"/g' | sed -zE 's/[[:space:]]+([:"a-zA-Z0-9])/\1/g' )
proc=${grep_processor::-1}
local mid=$mid$proc
echo $start${mid}$end
}
outputFile() {
boardvendor=$(dmidecode -s baseboard-manufacturer)
boardserial=$(dmidecode -s baseboard-serial-number)
echo "$boardvendor-$boardserial.json"
}
getLogDetails() {
echo '"LoggedOn" : "'$(date "+%Y-%m-%d %H:%M:%S")'"'
}
main() {
if [[ $EUID -ne 0 ]]; then
echo "Run this script using sudo/root" 1>&2
exit 1
fi
logdetails=$(getLogDetails)
echo "|>>>>>>>> Generating Motherboard Info..."
motherboard=$(getMotherboardInfo)
echo "|>>>>>>>> Generating Processor Info..."
cpu=$(getCPUInfo)
echo "|>>>>>>>> Generating Memory Unit Info..."
mem=$(getMemInfo)
echo "|>>>>>>>> Generating Disk Info..."
disk=$(getDiskInfo)
echo "|>>>>>>>> Compiling..."
filename=$(outputFile)
echo "{$logdetails,$motherboard,$cpu,$mem,$disk}" > "${filename}"
echo "|>>>>>>>> Done, Generated file -> $filename |"
}
main