Created
August 13, 2018 12:52
-
-
Save chrigl/aa2d68f96869e474125ee83313cf24dc to your computer and use it in GitHub Desktop.
There is some code on my computer, I wrote but not remember why.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "io/ioutil" | |
| "log" | |
| "os/exec" | |
| "path" | |
| "github.com/shirou/gopsutil/cpu" | |
| "github.com/sirupsen/logrus" | |
| ) | |
| var ( | |
| SysfsPath = "/sys/devices/virtual/dmi/id" | |
| ) | |
| type BlockdeviceInfo struct { | |
| Name string `json:"name"` | |
| Kname string `json:"kname"` | |
| MajMin string `json:"maj:min"` | |
| Fstype string `json:"fstype"` | |
| Mountpoint string `json:"mountpoint"` | |
| Label string `json:"label"` | |
| UUID string `json:"uuid"` | |
| Parttype string `json:"parttype"` | |
| Partlabel string `json:"partlabel"` | |
| Partuuid string `json:"partuuid"` | |
| Partflags string `json:"partflags"` | |
| Ra uint64 `json:"ra,string"` | |
| Ro uint8 `json:"ro,string"` | |
| Rm uint8 `json:"rm,string"` | |
| Hotplug uint8 `json:"hotplug,string"` | |
| Model string `json:"model"` | |
| Serial string `json:"serial"` | |
| Size uint64 `json:"size,string"` | |
| State string `json:"state"` | |
| Owner string `json:"owner"` | |
| Group string `json:"group"` | |
| Mode string `json:"mode"` | |
| Alignment uint64 `json:"alignment,string"` | |
| MinIo uint64 `json:"min-io,string"` | |
| OptIo uint64 `json:"opt-io,string"` | |
| PhySec uint64 `json:"phy-sec,string"` | |
| LogSec uint64 `json:"log-sec,string"` | |
| Rota uint8 `json:"rota,string"` | |
| Sched string `json:"sched"` | |
| RqSize uint64 `json:"rq-size,string"` | |
| Type string `json:"type"` | |
| DiscAln uint64 `json:"disc-aln,string"` | |
| DiscGran uint64 `json:"disc-gran,string"` | |
| DiscMax uint64 `json:"disc-max,string"` | |
| DiscZero uint8 `json:"disc-zero,string"` | |
| Wsame uint64 `json:"wsame,string"` | |
| Wwn string `json:"wwn"` | |
| Rand uint8 `json:"rand,string"` | |
| Pkname string `json:"pkname"` | |
| Hctl string `json:"hctl"` | |
| Tran string `json:"tran"` | |
| Subsystems string `json:"subsystems"` | |
| Rev string `json:"rev"` | |
| Vendor string `json:"vendor"` | |
| Zoned string `json:"zoned"` | |
| Children []BlockdeviceInfo `json:"children"` | |
| } | |
| type Blockdevices struct { | |
| Blockdevices []BlockdeviceInfo `json:"blockdevices"` | |
| } | |
| type DMI struct { | |
| Bios struct { | |
| Date string `json:"date"` | |
| Vendor string `json:"vendor"` | |
| Version string `json:"version"` | |
| } `json:"bios"` | |
| Board struct { | |
| AssetTag string `json:"asset_tag"` | |
| Name string `json:"name"` | |
| Serial string `json:"serial"` | |
| Vendor string `json:"vendor"` | |
| Version string `json:"version"` | |
| } `json:"board"` | |
| Chassis struct { | |
| AssetTag string `json:"asset_tag"` | |
| Serial string `json:"serial"` | |
| Type string `json:"type"` | |
| Vendor string `json:"vendor"` | |
| Version string `json:"version"` | |
| } `json:"chassis"` | |
| Product struct { | |
| Family string `json:"family"` | |
| Name string `json:"name"` | |
| Serial string `json:"serial"` | |
| UUID string `json:"uuid"` | |
| Version string `json:"version"` | |
| } `json:"product"` | |
| log *logrus.Logger `json:"-"` | |
| } | |
| type Inventory struct { | |
| CPUInfo []cpu.InfoStat `json:"cpuinfo"` | |
| Blockdevices []BlockdeviceInfo `json:"blockdevices"` | |
| DMIInfo DMI `json:"dmi"` | |
| } | |
| func (i *Inventory) collectCPUInfo() { | |
| c, err := cpu.Info() | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| for _, info := range c { | |
| fmt.Printf("%s\n", info.ModelName) | |
| } | |
| } | |
| func (i *Inventory) collectBlockdevices() error { | |
| cmd := exec.Command("lsblk", "-JOpb") | |
| stdin, err := cmd.StdoutPipe() | |
| if err != nil { | |
| return err | |
| } | |
| defer stdin.Close() | |
| err = cmd.Start() | |
| if err != nil { | |
| return err | |
| } | |
| dec := json.NewDecoder(stdin) | |
| var blockdevices Blockdevices | |
| err = dec.Decode(&blockdevices) | |
| if err != nil { | |
| return err | |
| } | |
| err = cmd.Wait() | |
| if err != nil { | |
| return err | |
| } | |
| i.Blockdevices = blockdevices.Blockdevices | |
| return nil | |
| } | |
| func (d *DMI) collect() error { | |
| if d == nil { | |
| return nil | |
| } | |
| readData := func(field *string, filename string) { | |
| bs, err := ioutil.ReadFile(path.Join(SysfsPath, filename)) | |
| if err != nil { | |
| d.log.Warnf("Unable to collect %s: %v", filename, err) | |
| } else { | |
| *field = string(bs) | |
| } | |
| } | |
| // BIOS | |
| readData(&d.Bios.Date, "bios_date") | |
| readData(&d.Bios.Vendor, "bios_vendor") | |
| readData(&d.Bios.Version, "bios_version") | |
| // Board | |
| readData(&d.Board.AssetTag, "board_asset_tag") | |
| readData(&d.Board.Name, "board_name") | |
| readData(&d.Board.Serial, "board_serial") | |
| bs, err := ioutil.ReadFile(path.Join(SysfsPath, "board_vendor")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Board.Vendor = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "board_version")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Board.Version = string(bs) | |
| // Chassis | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "chassis_asset_tag")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Chassis.AssetTag = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "chassis_serial")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Chassis.Serial = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "chassis_type")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Chassis.Type = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "chassis_vendor")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Chassis.Vendor = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "chassis_version")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Chassis.Version = string(bs) | |
| // Product | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "product_family")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Product.Family = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "product_name")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Product.Name = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "product_serial")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Product.Serial = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "product_uuid")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Product.UUID = string(bs) | |
| bs, err = ioutil.ReadFile(path.Join(SysfsPath, "product_version")) | |
| if err != nil { | |
| return err | |
| } | |
| d.Product.Version = string(bs) | |
| return nil | |
| } | |
| func main() { | |
| inventory := Inventory{} | |
| log := logrus.New() | |
| inventory.DMIInfo.log = log | |
| inventory.collectCPUInfo() | |
| err := inventory.collectBlockdevices() | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Printf("%+v\n", inventory.Blockdevices) | |
| err = inventory.DMIInfo.collect() | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| fmt.Printf("%+v\n", inventory.DMIInfo) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment