通過DMIDECODE讀取裝置硬體資訊並輸出檔案
一、dmidecode簡介
dmidecode允許你在Linux系統下獲取有關硬體方面的資訊。dmidecode遵循SMBIOS/DMI標準,其輸出的資訊包括BIOS、系統、主機板、處理器、記憶體、快取等等。
DMI(Desktop Management Interface,DMI)就是幫助收集電腦系統資訊的管理系統,DMI資訊的收集必須在嚴格遵照SMBIOS規範的前提下進行。SMBIOS(System Management BIOS)是主機板或系統製造者以標準格式顯示產品管理資訊所需遵循的統一規範。SMBIOS和DMI是由行業指導機構Desktop Management Task Force(DMTF)起草的開放性的技術標準,其中DMI設計適用於任何的平臺和作業系統。
DMI充當了管理工具和系統層之間介面的角色。它建立了標準的可管理系統更加方便了電腦廠商和使用者對系統的瞭解。DMI的主要組成部分是Management Information Format(MIF)資料庫。這個資料庫包括了所有有關電腦系統和配件的資訊。通過DMI,使用者可以獲取序列號、電腦廠商、串列埠資訊以及其它系統配件資訊。
二、dmidecode的作用
dmidecode的作用是將DMI資料庫中的資訊解碼,以可讀的文字方式顯示。由於DMI資訊可以人為修改,因此裡面的資訊不一定是系統準確的資訊。
三、dmidecode命令用法
不帶選項執行dmidecode通常會輸出所有的硬體資訊。dmidecode有個很有用的選項-t,可以指定型別輸出相關資訊。
Usage: dmidecode [OPTIONS]
Options are:
-d:(default:/dev/mem)從裝置檔案讀取資訊,輸出內容與不加引數標準輸出相同。
-h:顯示幫助資訊。
-s:只顯示指定DMI字串的資訊。(string)
-t:只顯示指定條目的資訊。(type)
-u:顯示未解碼的原始條目內容。
-- dump-bin FILE: Dump the DMI data to a binary file.
-- from-dump FILE: Read the DMI data from a binary file.
1. 最簡單的的顯示全部dmi資訊:
[[email protected] ~]# dmidecode|wc -l 506 2.顯示指定型別的資訊: 通常我只想檢視某型別,比如CPU,記憶體或者磁碟的資訊而不是全部的。這可以使用-t(–type TYPE)來指定資訊型別:[[email protected] ~]# dmidecode -t 0,4 dmidecode到底支援哪些type? 這些可以在man dmidecode裡面看到:
Type Information
--------------------------------------------
0 BIOS
1 System
2 Baseboard
3 Chassis
4 Processor
5 Memory Controller
6 Memory Module
7 Cache
8 Port Connector
9 System Slots
10 On Board Devices
11 OEM Strings
12 System Configuration Options
13 BIOS Language
14 Group Associations
15 System Event Log
16 Physical Memory Array
17 Memory Device
18 32-bit Memory Error
19 Memory Array Mapped Address
20 Memory Device Mapped Address
21 Built-in Pointing Device
22 Portable Battery
23 System Reset
24 Hardware Security
25 System Power Controls
26 Voltage Probe
27 Cooling Device
28 Temperature Probe
29 Electrical Current Probe
30 Out-of-band Remote Access
31 Boot Integrity Services
32 System Boot
33 64-bit Memory Error
34 Management Device
35 Management Device Component
36 Management Device Threshold Data
37 Memory Channel
38 IPMI Device
39 Power Supply
40 Additional Information
41 Onboard Devices Extended Information
42 Management Controller Host Interface
4.通過關鍵字檢視資訊: 比如只想檢視序列號,可以使用:
[[email protected] ~]# dmidecode -s system-serial-number
-s (–string keyword)支援的keyword包括:
bios-vendor,bios-version,bios-release-date,
system-manufacturer,system-product-name,system-version,system-serial-number,
baseboard-manu-facturer,baseboard-product-name,baseboard-version,baseboard-serial-number,baseboard-asset-tag,
chassis-manufacturer,chas-sis-version,chassis-serial-number,chassis-asset-tag,
processor-manufacturer,processor-version.
四、例項
檢視當前記憶體和支援的最大記憶體,硬碟 Linux下,可以使用free或者檢視meminfo來獲得當前的實體記憶體:
[[email protected] ~]# free
total used free shared buffers cached
Mem: 16256156 302412 15953744 208 5788 71236
-/+ buffers/cache: 225388 16030768
Swap: 8126460 0 8126460
[[email protected] ~]# free -m
total used free shared buffers cached
Mem: 15875 295 15580 0 5 69
-/+ buffers/cache: 219 15655
Swap: 7935 0 7935
1、檢視記憶體槽數、哪個槽位插了記憶體,大小是多少
[[email protected] ~]# dmidecode |grep -A5 'Memory Device' |grep Size
Size: No Module Installed
Size: No Module Installed
Size: 16384 MB
Size: No Module Installed
Range Size: 16 GB
2、檢視最大支援記憶體數
[[email protected] ~]# dmidecode |grep 'Maximum Capacity'
Maximum Capacity: 64 GB
3、檢視槽位上記憶體的速率,沒插就是unknown。
[[email protected] ~]# dmidecode |grep -A16 'Memory Device' |grep 'Speed'
Speed: Unknown
Speed: Unknown
Speed: 2400 MHz
Speed: Unknown
4、檢視伺服器硬碟資訊
[[email protected] ~]# cat /proc/scsi/scsi
Attached devices:
Host: scsi3 Channel: 00 Id: 00 Lun: 00
Vendor: ATA Model: ST1000DM010-2EP1 Rev: CC43
Type: Direct-Access ANSI SCSI revision: 05
五、一個檢視基本硬體資訊的shell指令碼
#!/bin/bash
echo "IP:"
ifconfig |grep "inet addr"|grep -v 127.0.0.1|awk '{print $2}'|awk -F ':' '{print $2}'
echo "Product Name:"
dmidecode |grep Name
echo "CPU Info:"
dmidecode |grep -i cpu|grep -i version|awk -F ':' '{print $2}'
echo "Disk Info:"
parted -l|grep 'Disk /dev/sd'|awk -F ',' '{print " ",$1}'
echo "Network Info:"
lspci |grep Ethernet
echo "Memory Info:"
dmidecode|grep -A5 "Memory Device"|grep Size|grep -v No
echo "Memory number:"`dmidecode|grep -A5 "Memory Device"|grep Size|grep -v No|wc -l`
echo "HDD:"
cat /proc/scsi/scsi |grep Model
echo "HDD-hd:"
hdparm -i /dev/sda /dev/sdb /dev/sdc /dev/sdd |grep Model
六、將shell的輸出資訊儲存到檔案
[[email protected] ~]# sh test.sh 2>&1 | tee sn.log
其中sh test.sh:表示要執行的shell腳步;0,1,2:在linux分別表示標準輸入、標準輸出和標準錯誤資訊輸出。
1,輸入輸出重定向之:'<' and '>'
'<' and '>'分別用來支援linux中的輸入輸出重定向,其中'<'支援輸入重定向,'>'支援輸出重定向。
1. '<':重定向輸入
sh test.sh < hadoop-hadoop-jobtracker-brix-00.out,將hadoop-hadoop-jobtracker-brix-00.out的內容作為test.sh的輸入
2. '>':將內容全域性覆蓋式的加入檔案,相當於刪除該檔案並重新建立該檔案,再寫入的效果
ls * > test.txt ,將ls * 的所有資訊輸出到檔案test.txt中
3. '>!':如果存在則覆蓋
4. '>&':執行時螢幕上所產生的任何資訊寫入指定的檔案中
5. '>>':追加到檔案中
6. '>>&':螢幕上的資訊追加到檔案中
2,標準輸入輸出
在 Linux 系統中:標準輸入(stdin)預設為鍵盤輸入;標準輸出(stdout)預設為螢幕輸出;標準錯誤輸出(stderr)預設也是輸出到螢幕(上面的 std 表示 standard)。在 BASH 中使用這些概念時一般將標準輸出表示為 1,將標準錯誤輸出表示為 2。下面我們舉例來說明如何使用他們,特別是標準輸出和標準錯誤輸出。
3,tee命令
tee指令會從標準輸入裝置讀取資料,將其內容輸出到標準輸出裝置,同時儲存成檔案。
[[email protected] ~]# tee --help
Usage: tee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.
-a, --append append to the given FILEs, do not overwrite
-i, --ignore-interrupts ignore interrupt signals
--help display this help and exit
--version output version information and exit
If a FILE is -, copy again to standard output.