Esempio n. 1
0
func GetHardwareProfile() (*pb.HardwareProfile, error) {
	v, err := mem.VirtualMemory()
	if err != nil {
		return &pb.HardwareProfile{}, err
	}
	d, err := disk.Partitions(true)
	if err != nil {
		return &pb.HardwareProfile{}, err
	}
	hw := &pb.HardwareProfile{
		Disks:    make([]*pb.Disk, 0),
		Cpus:     int64(runtime.NumCPU()),
		Memtotal: v.Total,
		Memfree:  v.Free,
	}
	for k := range d {
		usage, err := disk.Usage(d[k].Mountpoint)
		if err != nil {
			continue
		}
		entry := &pb.Disk{
			Path:   d[k].Mountpoint,
			Device: d[k].Device,
			Size_:  usage.Total,
			Used:   usage.Used,
		}
		hw.Disks = append(hw.Disks, entry)
	}
	return hw, nil
}
Esempio n. 2
0
// Collect collects stats related to resource usage of a host
func (h *HostStatsCollector) Collect() (*HostStats, error) {
	hs := &HostStats{Timestamp: time.Now().UTC().UnixNano()}
	if memStats, err := mem.VirtualMemory(); err == nil {
		ms := &MemoryStats{
			Total:     memStats.Total,
			Available: memStats.Available,
			Used:      memStats.Used,
			Free:      memStats.Free,
		}
		hs.Memory = ms
	}

	if cpuStats, err := cpu.Times(true); err == nil {
		cs := make([]*CPUStats, len(cpuStats))
		for idx, cpuStat := range cpuStats {
			cs[idx] = &CPUStats{
				CPU:    cpuStat.CPU,
				User:   cpuStat.User,
				System: cpuStat.System,
				Idle:   cpuStat.Idle,
			}
			percentCalculator, ok := h.statsCalculator[cpuStat.CPU]
			if !ok {
				percentCalculator = NewHostCpuStatsCalculator()
				h.statsCalculator[cpuStat.CPU] = percentCalculator
			}
			idle, user, system, total := percentCalculator.Calculate(cpuStat)
			cs[idx].Idle = idle
			cs[idx].System = system
			cs[idx].User = user
			cs[idx].Total = total
		}
		hs.CPU = cs
	}

	if partitions, err := disk.Partitions(false); err == nil {
		var diskStats []*DiskStats
		for _, partition := range partitions {
			if usage, err := disk.Usage(partition.Mountpoint); err == nil {
				ds := DiskStats{
					Device:            partition.Device,
					Mountpoint:        partition.Mountpoint,
					Size:              usage.Total,
					Used:              usage.Used,
					Available:         usage.Free,
					UsedPercent:       usage.UsedPercent,
					InodesUsedPercent: usage.InodesUsedPercent,
				}
				diskStats = append(diskStats, &ds)
			}
		}
		hs.DiskStats = diskStats
	}

	if uptime, err := host.Uptime(); err == nil {
		hs.Uptime = uptime
	}
	return hs, nil
}
Esempio n. 3
0
func ListMountPoint() ([][3]string, error) {
	result := make([][3]string, 0)
	list, err := disk.Partitions(true)
	if err != nil {
		return nil, err
	}
	for _, v := range list {
		result = append(result, [3]string{v.Device, v.Mountpoint, v.Fstype})
	}
	return result, nil
}
Esempio n. 4
0
func (s *systemPS) DiskUsage(
	mountPointFilter []string,
	fstypeExclude []string,
) ([]*disk.UsageStat, error) {
	parts, err := disk.Partitions(true)
	if err != nil {
		return nil, err
	}

	// Make a "set" out of the filter slice
	mountPointFilterSet := make(map[string]bool)
	for _, filter := range mountPointFilter {
		mountPointFilterSet[filter] = true
	}
	fstypeExcludeSet := make(map[string]bool)
	for _, filter := range fstypeExclude {
		fstypeExcludeSet[filter] = true
	}

	var usage []*disk.UsageStat

	for _, p := range parts {
		if len(mountPointFilter) > 0 {
			// If the mount point is not a member of the filter set,
			// don't gather info on it.
			_, ok := mountPointFilterSet[p.Mountpoint]
			if !ok {
				continue
			}
		}
		mountpoint := os.Getenv("HOST_MOUNT_PREFIX") + p.Mountpoint
		if _, err := os.Stat(mountpoint); err == nil {
			du, err := disk.Usage(mountpoint)
			du.Path = p.Mountpoint
			if err != nil {
				return nil, err
			}
			// If the mount point is a member of the exclude set,
			// don't gather info on it.
			_, ok := fstypeExcludeSet[p.Fstype]
			if ok {
				continue
			}
			du.Fstype = p.Fstype
			usage = append(usage, du)
		}
	}

	return usage, nil
}
Esempio n. 5
0
func diskPartitionMetrics() []*types.TimeSeriesData {
	pts, err := disk.Partitions(false)
	if err != nil {
		return nil
	}
	metrics := []*types.TimeSeriesData{}
	ts := time.Now().Unix()
	for _, p := range pts {
		cnt, err := disk.Usage(p.Mountpoint)
		if err != nil {
			continue
		}
		metrics = append(metrics,
			&types.TimeSeriesData{
				Metric:    "disk.free",
				Value:     float64(cnt.Free),
				Cycle:     Cycle,
				Timestamp: ts,
				DataType:  "GAUGE",
				Tags:      map[string]string{"path": cnt.Path, "fstype": cnt.Fstype},
			},
			&types.TimeSeriesData{
				Metric:    "disk.total",
				Value:     float64(cnt.Total),
				Cycle:     Cycle,
				Timestamp: ts,
				DataType:  "GAUGE",
				Tags:      map[string]string{"path": cnt.Path, "fstype": cnt.Fstype},
			},
			&types.TimeSeriesData{
				Metric:    "disk.used",
				Value:     float64(cnt.Used),
				Cycle:     Cycle,
				Timestamp: ts,
				DataType:  "GAUGE",
				Tags:      map[string]string{"path": cnt.Path, "fstype": cnt.Fstype},
			},
			&types.TimeSeriesData{
				Metric:    "disk.usedpercent",
				Value:     cnt.UsedPercent,
				Cycle:     Cycle,
				Timestamp: ts,
				DataType:  "GAUGE",
				Tags:      map[string]string{"path": cnt.Path, "fstype": cnt.Fstype},
			},
			&types.TimeSeriesData{
				Metric:    "disk.inodes.free",
				Value:     float64(cnt.InodesFree),
				Cycle:     Cycle,
				Timestamp: ts,
				DataType:  "GAUGE",
				Tags:      map[string]string{"path": cnt.Path, "fstype": cnt.Fstype},
			},
			&types.TimeSeriesData{
				Metric:    "disk.inodes.total",
				Value:     float64(cnt.InodesTotal),
				Cycle:     Cycle,
				Timestamp: ts,
				DataType:  "GAUGE",
				Tags:      map[string]string{"path": cnt.Path, "fstype": cnt.Fstype},
			},
			&types.TimeSeriesData{
				Metric:    "disk.inodes.used",
				Value:     float64(cnt.InodesUsed),
				Cycle:     Cycle,
				Timestamp: ts,
				DataType:  "GAUGE",
				Tags:      map[string]string{"path": cnt.Path, "fstype": cnt.Fstype},
			},
			&types.TimeSeriesData{
				Metric:    "disk.inodes.usedpercent",
				Value:     float64(cnt.InodesUsedPercent),
				Cycle:     Cycle,
				Timestamp: ts,
				DataType:  "GAUGE",
				Tags:      map[string]string{"path": cnt.Path, "fstype": cnt.Fstype},
			},
		)

	}
	return metrics
}
Esempio n. 6
0
// Collect collects stats related to resource usage of a host
func (h *HostStatsCollector) Collect() (*HostStats, error) {
	hs := &HostStats{Timestamp: time.Now().UTC().UnixNano()}
	memStats, err := mem.VirtualMemory()
	if err != nil {
		return nil, err
	}
	hs.Memory = &MemoryStats{
		Total:     memStats.Total,
		Available: memStats.Available,
		Used:      memStats.Used,
		Free:      memStats.Free,
	}

	ticksConsumed := 0.0
	cpuStats, err := cpu.Times(true)
	if err != nil {
		return nil, err
	}
	cs := make([]*CPUStats, len(cpuStats))
	for idx, cpuStat := range cpuStats {
		percentCalculator, ok := h.statsCalculator[cpuStat.CPU]
		if !ok {
			percentCalculator = NewHostCpuStatsCalculator()
			h.statsCalculator[cpuStat.CPU] = percentCalculator
		}
		idle, user, system, total := percentCalculator.Calculate(cpuStat)
		cs[idx] = &CPUStats{
			CPU:    cpuStat.CPU,
			User:   user,
			System: system,
			Idle:   idle,
			Total:  total,
		}
		ticksConsumed += (total / 100) * (shelpers.TotalTicksAvailable() / float64(len(cpuStats)))
	}
	hs.CPU = cs
	hs.CPUTicksConsumed = ticksConsumed

	partitions, err := disk.Partitions(false)
	if err != nil {
		return nil, err
	}
	var diskStats []*DiskStats
	for _, partition := range partitions {
		usage, err := disk.Usage(partition.Mountpoint)
		if err != nil {
			return nil, err
		}
		ds := DiskStats{
			Device:            partition.Device,
			Mountpoint:        partition.Mountpoint,
			Size:              usage.Total,
			Used:              usage.Used,
			Available:         usage.Free,
			UsedPercent:       usage.UsedPercent,
			InodesUsedPercent: usage.InodesUsedPercent,
		}
		if math.IsNaN(ds.UsedPercent) {
			ds.UsedPercent = 0.0
		}
		if math.IsNaN(ds.InodesUsedPercent) {
			ds.InodesUsedPercent = 0.0
		}
		diskStats = append(diskStats, &ds)
	}
	hs.DiskStats = diskStats

	uptime, err := host.Uptime()
	if err != nil {
		return nil, err
	}
	hs.Uptime = uptime

	return hs, nil
}