In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to deal with text subtotals with golang". In daily operation, I believe many people have doubts about how to deal with text subtotals with golang. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to deal with text subtotals with golang". Next, please follow the editor to study!
Very simple solution, the original text format is more, see laugh. Env
Linux 64bit
Go version go1.7.4 linux/amd64
Physical machine
Smartctl 2013-07-26 r3841 [x86_64-linux-3.10.0-327.36.2.el7.x86_64] (local build)
Task
Use the smartctl command to collect hard disk information on the physical machine. The hard disk information required is as follows:
Date: '2016-02-09, string
Serial_number: MJ0351YNG9Z0XA, string
Model: Hitachi HDS5C3030ALA630, string
Capacity_bytes: 3000592982016, string
Normalize: 100, int
Raw: 32296, string
The above information can be obtained through smartctl with relevant parameters, but the format is very primitive, so we need to extract the really needed information according to the requirements.
For example, normalize and raw are hard disk attribute information, which can be obtained through smartctl-A device:
➜~ sudo smartctl-A / dev/sdasmartctl 6.2 2013-07-26 r3841 [x86_64-linux-3.10.0-327.36.2.el7.x86_64] (local build) Copyright (C) 2002-13, Bruce Allen, Christian Franke Www.smartmontools.org=== START OF READ SMART DATA SECTION = SMART Attributes Data Structure revision number: 10Vendor Specific SMART Attributes with Thresholds:ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE 1 Raw_Read_Error_Rate 0x000f 118099006 Pre-fail Always-199795992 3 Spin_Up_Time 0x0003 097097000 Pre-fail Always-0 4 Start_Stop_Count 0x0032 100100020 Old_age Always-1495Reallocated_Sector_Ct 0x0033 100100010 Pre-fail Always-07 Seek_Error_Rate 0x000f 077060030 Pre-fail Always-54764573 9 Power_On_Hours 0x0032 089089000 Old_age Always-9792 10 Spin_Retry_Count 0x0013 100100097 Pre-fail Always-012 Power_Cycle_Count 0x0032 100100020 Old_age Always-149183 Runtime_Bad_Block 0x0032 100100000 Old_age Always-0184 End-to-End_Error 0x0032 100100099 Old_age Always-0187 Reported_Uncorrect 0x0032 100100000 Old_age Always-0188 Command_Timeout 0x0032 100,099,000 Old_age Always-22 2189 High_Fly_Writes 0x003a 100,000,000 Old_age Always-0190 Airflow_Temperature_Cel 0x0022 060 055 045 Old_age Always-40 (Min/Max 26 G-Sense_Error_Rate 0x0032 40) 191 G-Sense_Error_Rate 0x0032 100 100000 Old_age Always-0192 Power-Off_Retract_Count 0x0032 100 100000 Old_age Always-25193 Load_Cycle_Count 0x0032 098 098000 Old_age Always-4708194 Temperature_Celsius 0x0022 040 045000 Old_age Always-40 (06000) 197 Current_Pending_Sector 0x0012 100 100 000 Old_age Always-0198 Offline_Uncorrectable 0x0010 100 100000 Old_age Offline-0199 UDMA_CRC_Error_Count 0x003e 200 200000 Old_age Always-0240 Head_Flying_Hours 0x0000 100 253000 Old_age Offline-9049h+51m+35.226s241 Total_LBAs_Written 0x0000 100 253000 Old_age Offline-4749106878242 Total_LBAs_Read 0x0000 100 253000 Old_age Offline-126575500815
Static information such as manufacturers can be obtained through sudo smartctl-I device:
➜~ sudo smartctl-I / dev/sdasmartctl 2013-07-26 r3841 [x86_64-linux-3.10.0-327.36.2.el7.x86_64] (local build) Copyright (C) 2002-13, Bruce Allen, Christian Franke, www.smartmontools.org=== START OF INFORMATION SECTION = Model Family: Seagate Barracuda 7200.14 (AF) Device Model: ST1000DM003-1ER162Serial Number: W4Y3Z28ALU WWN Device Id: 5 000c50 08a2c464eFirmware Version: CC46User Capacity: 1000204886016 bytes [1.00 TB] Sector Sizes: 512 bytes logical 4096 bytes physicalRotation Rate: 7200 rpmDevice is: In smartctl database [for details use:-P show] ATA Version is: ACS-2, ACS-3 T13 Gb/s revision 3bSATA Version is: SATA 3.1,6.0 Gb/s (current: 6.0 Gb/s) Local Time is: Tue Feb 7 17:53:34 2017 CST== > WARNING: A firmware update for this drive is available See the following Seagate web pages: http://knowledge.seagate.com/articles/en_US/FAQ/207931enhttp://knowledge.seagate.com/articles/en_US/FAQ/223651enSMART support is: Available-device has SMART capability.SMART support is: Enabled
How can we extract the data from it?
The method that comes to mind
Regular expression
Strings (golang stdlib): handle strings, split, delete, etc.
Strconv (golang stdlib): handles string conversion.
Os/exec: execute smartctl script to get output
Matters needing attention
For the formatted text, filter out the unwanted data according to the format as far as possible, and retain only the needed data.
Lines: = "data1\ ndata2\ nneeded1\ nneeded2\ nneeded: = strings.Split (lines,")
For segmentation, strings.Fields () is usually better than strings.Split ().
Package mainimport ("strings"fmt") func main () {lines: = "194 Temperature_Celsius 0x0022 037 045000 Old_age Always-37 (06000)" useSplit (lines) useFields (lines)} func useSplit (lines string) {line: = strings.Split (lines) ") fmt.Println (line) fmt.Println (len (line))} func useFields (lines string) {line: = strings.Fields (lines) fmt.Println (line) fmt.Println (len (line))} / * output ➜gist go run main.go [194 Temperature_Celsius 0x0022 037 045000 Old_age Always-37 (06000)] 42 [194 Temperature_Celsius 0x0022 037 045000 Old_age Always -37 (0 6000)] 15 years /
Exec acquires the output information of bash script with a\ nnewline character at the end by default. Remember to dispose of it:
Package mainimport ("os/exec"os"fmt") func main () {devices: = getDevices () fmt.Println (devices) fmt.Println (len (devices))} func getDevices () [] string {ret, err: = exec.Command ("/ sbin/smartctl", "--scan"). Output () if err! = nil {log.Println ("Execute / sbin/smartctl-- scan error:% v") Err) os.Exit (- 1)} linest: = strings.Split (string (ret), "\ n") lines: = linest [: len (linest)-1] / / remove last extra line with output of bash command var devList = [] string {} for _, line: = range lines {device: = strings.Fields (line) [0] devList = append (devList Device)} return devList} / * remove extra lineage [/ dev/sda] 1not remove extra line [/ dev/sda] 2customers / so far The study on "how to handle text subtotals with golang" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.