Ejemplo n.º 1
0
func (f *SystemFacts) getBlockDevices(wg *sync.WaitGroup) {
	defer wg.Done()

	d, err := os.Open("/sys/block")
	if err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}
	defer d.Close()

	files, err := d.Readdir(0)
	if err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	bdMap := make(BlockDevices)
	f.mu.Lock()
	defer f.mu.Unlock()

	for _, fi := range files {
		path := fmt.Sprintf("/sys/block/%s/device", fi.Name())
		if _, err := os.Stat(path); !os.IsNotExist(err) {
			bd := BlockDevice{}
			bd.Device = fi.Name()

			sizePath := fmt.Sprintf("/sys/block/%s/size", fi.Name())
			size, err := facts.ReadFileAndReturnValue(sizePath)
			if err != nil {
				if c.Debug {
					log.Println(err)
				}
				return
			}

			bd.Size, _ = strconv.ParseUint(size, 10, 64)

			vendorPath := fmt.Sprintf("/sys/block/%s/device/vendor", fi.Name())
			if bd.Vendor, err = facts.ReadFileAndReturnValue(vendorPath); err != nil {
				if c.Debug {
					log.Println(err)
				}
				return
			}

			statPath := fmt.Sprintf("/sys/block/%s/stat", fi.Name())
			sf, err := os.Open(statPath)
			if err != nil {
				if c.Debug {
					log.Println(err)
				}
			}
			defer sf.Close()

			scanner := bufio.NewScanner(sf)
			for scanner.Scan() {
				columns := strings.Fields(scanner.Text())
				if len(columns) == 11 {
					bd.ReadIOs, _ = strconv.ParseUint(columns[0], 10, 64)
					bd.ReadMerges, _ = strconv.ParseUint(columns[1], 10, 64)
					bd.ReadSectors, _ = strconv.ParseUint(columns[2], 10, 64)
					bd.ReadTicks, _ = strconv.ParseUint(columns[3], 10, 64)
					bd.WriteIOs, _ = strconv.ParseUint(columns[4], 10, 64)
					bd.WriteMerges, _ = strconv.ParseUint(columns[5], 10, 64)
					bd.WriteSectors, _ = strconv.ParseUint(columns[6], 10, 64)
					bd.WriteTicks, _ = strconv.ParseUint(columns[7], 10, 64)
					bd.InFlight, _ = strconv.ParseUint(columns[8], 10, 64)
					bd.IOTicks, _ = strconv.ParseUint(columns[9], 10, 64)
					bd.TimeInQueue, _ = strconv.ParseUint(columns[10], 10, 64)
				}
			}
			bdMap[bd.Device] = bd
		}
	}

	f.BlockDevices = bdMap

	return
}
Ejemplo n.º 2
0
func (f *SystemFacts) getDMI(wg *sync.WaitGroup) {
	defer wg.Done()
	f.mu.Lock()
	defer f.mu.Unlock()

	var err error
	if f.DMI.BIOSDate, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/bios_date"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.BIOSVendor, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/bios_vendor"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.BIOSVersion, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/bios_version"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.ChassisAssetTag, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/chassis_asset_tag"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.ChassisSerial, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/chassis_serial"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.ChassisVendor, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/chassis_vendor"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.ChassisVersion, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/chassis_version"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.ProductName, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/product_name"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.ProductSerial, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/product_serial"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.ProductUUID, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/product_uuid"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.ProductVersion, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/product_version"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	if f.DMI.SysVendor, err = facts.ReadFileAndReturnValue("/sys/class/dmi/id/sys_vendor"); err != nil {
		if c.Debug {
			log.Println(err)
		}
		return
	}

	return
}