示例#1
0
func (p *Process) Exe() (string, error) {
	query := fmt.Sprintf("ProcessId = %d", p.Pid)
	lines, err := common.GetWmic("process", "where", query, "get", "ExecutablePath")
	if err != nil {
		return "", err
	}
	if len(lines) == 0 {
		return "", fmt.Errorf("could not get ExecutablePath")
	}
	return lines[0][1], nil
}
示例#2
0
func BootTime() (uint64, error) {
	lines, err := common.GetWmic("os", "get", "LastBootUpTime")
	if err != nil {
		return 0, err
	}
	if len(lines) == 0 || len(lines[0]) != 2 {
		return 0, fmt.Errorf("could not get LastBootUpTime")
	}
	format := "20060102150405"
	t, err := time.Parse(format, strings.Split(lines[0][1], ".")[0])
	if err != nil {
		return 0, err
	}
	now := time.Now()
	return uint64(now.Sub(t).Seconds()), nil
}
示例#3
0
func (p *Process) NumThreads() (int32, error) {
	query := fmt.Sprintf("ProcessId = %d", p.Pid)
	lines, err := common.GetWmic("process", "where", query, "get", "ThreadCount")
	if err != nil {
		return 0, err
	}
	if len(lines) == 0 {
		return 0, fmt.Errorf("could not get command line")
	}
	count, err := strconv.Atoi(lines[0][1])
	if err != nil {
		return 0, err
	}

	return int32(count), nil
}
示例#4
0
// Get processes
func processes() ([]*Process, error) {
	lines, err := common.GetWmic("process", "get", "processid")
	if err != nil {
		return nil, err
	}
	var results []*Process
	for _, l := range lines {
		pid, err := strconv.Atoi(l[1])
		if err != nil {
			continue
		}
		p, err := NewProcess(int32(pid))
		if err != nil {
			continue
		}
		results = append(results, p)
	}

	return results, nil
}
示例#5
0
func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
	ret := []float64{}

	lines, err := common.GetWmic("cpu", "get", "loadpercentage")
	if err != nil {
		return ret, err
	}
	for _, l := range lines {
		if len(l) < 2 {
			continue
		}
		p, err := strconv.Atoi(l[1])
		if err != nil {
			p = 0
		}
		// but windows can only get one percent.
		ret = append(ret, float64(p)/100.0)
	}
	return ret, nil
}
示例#6
0
func CPUInfo() ([]CPUInfoStat, error) {
	var ret []CPUInfoStat
	lines, err := common.GetWmic("cpu", "get", "Family,L2CacheSize,Manufacturer,Name,NumberOfLogicalProcessors,ProcessorId,Stepping")
	if err != nil {
		return ret, err
	}
	for i, t := range lines {
		cache, err := strconv.Atoi(t[2])
		if err != nil {
			cache = 0
		}
		cores, err := strconv.Atoi(t[5])
		if err != nil {
			cores = 0
		}
		stepping := 0
		if len(t) > 7 {
			stepping, err = strconv.Atoi(t[6])
			if err != nil {
				stepping = 0
			}
		}
		cpu := CPUInfoStat{
			CPU:        int32(i),
			Family:     t[1],
			CacheSize:  int32(cache),
			VendorID:   t[3],
			ModelName:  t[4],
			Cores:      int32(cores),
			PhysicalID: t[6],
			Stepping:   int32(stepping),
			Flags:      []string{},
		}
		ret = append(ret, cpu)
	}
	return ret, nil
}