Пример #1
0
func CPUInfo() ([]CPUInfoStat, error) {
	var ret []CPUInfoStat
	var dst []Win32_Processor
	q := wmi.CreateQuery(&dst, "")
	err := wmi.Query(q, &dst)
	if err != nil {
		return ret, err
	}
	for i, l := range dst {
		cpu := CPUInfoStat{
			CPU:        int32(i),
			Family:     fmt.Sprintf("%d", l.Family),
			CacheSize:  int32(l.L2CacheSize),
			VendorID:   l.Manufacturer,
			ModelName:  l.Name,
			Cores:      int32(l.NumberOfLogicalProcessors),
			PhysicalID: l.ProcessorId,
			Mhz:        float64(l.MaxClockSpeed),
			Flags:      []string{},
		}
		ret = append(ret, cpu)
	}

	return ret, nil
}
Пример #2
0
func BootTime() (uint64, error) {
	now := time.Now()

	var dst []Win32_OperatingSystem
	q := wmi.CreateQuery(&dst, "")
	err := wmi.Query(q, &dst)
	if err != nil {
		return 0, err
	}
	t := dst[0].LastBootUpTime.Local()
	return uint64(now.Sub(t).Seconds()), nil
}
Пример #3
0
func (p *Process) NumThreads() (int32, error) {
	var dst []Win32_Process
	query := fmt.Sprintf("WHERE ProcessId = %d", p.Pid)
	q := wmi.CreateQuery(&dst, query)
	err := wmi.Query(q, &dst)
	if err != nil {
		return 0, err
	}
	if len(dst) != 1 {
		return 0, fmt.Errorf("could not get ThreadCount")
	}
	return int32(dst[0].ThreadCount), nil
}
Пример #4
0
func (p *Process) Cmdline() (string, error) {
	var dst []Win32_Process
	query := fmt.Sprintf("WHERE ProcessId = %d", p.Pid)
	q := wmi.CreateQuery(&dst, query)
	err := wmi.Query(q, &dst)
	if err != nil {
		return "", err
	}
	if len(dst) != 1 {
		return "", fmt.Errorf("could not get CommandLine")
	}
	return *dst[0].CommandLine, nil
}
Пример #5
0
func (p *Process) Exe() (string, error) {
	var dst []Win32_Process
	query := fmt.Sprintf("WHERE ProcessId = %d", p.Pid)
	q := wmi.CreateQuery(&dst, query)
	err := wmi.Query(q, &dst)
	if err != nil {
		return "", err
	}
	if len(dst) != 1 {
		return "", fmt.Errorf("could not get ExecutablePath")
	}
	return *dst[0].ExecutablePath, nil
}
Пример #6
0
func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
	var ret []float64
	var dst []Win32_Processor
	q := wmi.CreateQuery(&dst, "")
	err := wmi.Query(q, &dst)
	if err != nil {
		return ret, err
	}
	for _, l := range dst {
		// use range but windows can only get one percent.
		ret = append(ret, float64(l.LoadPercentage)/100.0)
	}
	return ret, nil
}
Пример #7
0
// Get processes
func processes() ([]*Process, error) {

	var dst []Win32_Process
	q := wmi.CreateQuery(&dst, "")
	err := wmi.Query(q, &dst)
	if err != nil {
		return []*Process{}, err
	}
	if len(dst) == 0 {
		return []*Process{}, fmt.Errorf("could not get Process")
	}
	results := make([]*Process, 0, len(dst))
	for _, proc := range dst {
		p, err := NewProcess(int32(proc.ProcessId))
		if err != nil {
			continue
		}
		results = append(results, p)
	}

	return results, nil
}