Beispiel #1
0
// getDetails fills in CPU, memory, and command line details for the process
func (proc *Process) getDetails(cmdline string) error {

	mem := sigar.ProcMem{}
	if err := mem.Get(proc.Pid); err != nil {
		return fmt.Errorf("error getting process mem for pid=%d: %v", proc.Pid, err)
	}
	proc.Mem = &ProcMemStat{
		Size:  mem.Size,
		Rss:   mem.Resident,
		Share: mem.Share,
	}

	cpu := sigar.ProcTime{}
	if err := cpu.Get(proc.Pid); err != nil {
		return fmt.Errorf("error getting process cpu time for pid=%d: %v", proc.Pid, err)
	}
	proc.Cpu = &ProcCpuTime{
		Start:  cpu.FormatStartTime(),
		Total:  cpu.Total,
		User:   cpu.User,
		System: cpu.Sys,
	}

	if cmdline == "" {
		args := sigar.ProcArgs{}
		if err := args.Get(proc.Pid); err != nil {
			return fmt.Errorf("error getting process arguments for pid=%d: %v", proc.Pid, err)
		}
		proc.CmdLine = strings.Join(args.List, " ")
	} else {
		proc.CmdLine = cmdline
	}

	return nil
}
Beispiel #2
0
// getDetails fills in CPU, memory, FD usage, and command line details for the process.
func (proc *Process) getDetails(cmdline string) error {

	proc.Mem = sigar.ProcMem{}
	if err := proc.Mem.Get(proc.Pid); err != nil {
		return fmt.Errorf("error getting process mem for pid=%d: %v", proc.Pid, err)
	}

	proc.Cpu = sigar.ProcTime{}
	if err := proc.Cpu.Get(proc.Pid); err != nil {
		return fmt.Errorf("error getting process cpu time for pid=%d: %v", proc.Pid, err)
	}

	if cmdline == "" {
		args := sigar.ProcArgs{}
		if err := args.Get(proc.Pid); err != nil {
			return fmt.Errorf("error getting process arguments for pid=%d: %v", proc.Pid, err)
		}
		proc.CmdLine = strings.Join(args.List, " ")
	} else {
		proc.CmdLine = cmdline
	}

	if fd, err := getProcFDUsage(proc.Pid); err != nil {
		return fmt.Errorf("error getting process file descriptor usage for pid=%d: %v", proc.Pid, err)
	} else if fd != nil {
		proc.FD = *fd
	}

	return nil
}
Beispiel #3
0
func main() {
	pids := gosigar.ProcList{}
	pids.Get()

	// ps -eo pid,ppid,stime,time,rss,user,state,command
	fmt.Print("  PID  PPID STIME     TIME    RSS USER            S COMMAND\n")

	for _, pid := range pids.List {
		state := gosigar.ProcState{}
		mem := gosigar.ProcMem{}
		time := gosigar.ProcTime{}
		args := gosigar.ProcArgs{}

		if err := state.Get(pid); err != nil {
			continue
		}
		if err := mem.Get(pid); err != nil {
			continue
		}
		if err := time.Get(pid); err != nil {
			continue
		}
		if err := args.Get(pid); err != nil {
			continue
		}

		fmt.Printf("%5d %5d %s %s %6d %-15s %c %s\n",
			pid, state.Ppid,
			time.FormatStartTime(), time.FormatTotal(),
			mem.Resident/1024, state.Username, state.State,
			strings.Join(args.List, " "))
	}
}
Beispiel #4
0
func TestProcArgsUnknown(t *testing.T) {
	args := sigar.ProcArgs{}
	err := args.Get(math.MaxInt32)
	if err == nil {
		t.Fatal("Expected process not found")
	}

	if !strings.Contains(err.Error(), "Process not found") {
		t.Fatal("Expected error containing 'Process not found'")
	}
}
Beispiel #5
0
func TestProcArgs(t *testing.T) {
	args := sigar.ProcArgs{}
	err := args.Get(os.Getpid())
	if err != nil {
		t.Fatal(err)
	}

	if len(args.List) == 0 {
		t.Fatalf("Expected at least one arg")
	}
}
Beispiel #6
0
func GetProcess(pid int) (*Process, error) {

	state := sigar.ProcState{}
	mem := sigar.ProcMem{}
	cpu := sigar.ProcTime{}
	args := sigar.ProcArgs{}

	err := state.Get(pid)
	if err != nil {
		return nil, fmt.Errorf("Error getting state info: %v", err)
	}

	err = mem.Get(pid)
	if err != nil {
		return nil, fmt.Errorf("Error getting mem info: %v", err)
	}

	err = cpu.Get(pid)
	if err != nil {
		return nil, fmt.Errorf("Error getting cpu info: %v", err)
	}

	err = args.Get(pid)
	if err != nil {
		return nil, fmt.Errorf("Error getting command line: %v", err)
	}

	cmdLine := strings.Join(args.List, " ")

	proc := Process{
		Pid:      pid,
		Ppid:     state.Ppid,
		Name:     state.Name,
		State:    getProcState(byte(state.State)),
		Username: state.Username,
		CmdLine:  cmdLine,
		Mem:      mem,
		Cpu:      cpu,
	}
	proc.ctime = time.Now()

	return &proc, nil
}
Beispiel #7
0
func GetProcess(pid int, cmdline string) (*Process, error) {
	state := sigar.ProcState{}
	if err := state.Get(pid); err != nil {
		return nil, fmt.Errorf("error getting process state for pid=%d: %v", pid, err)
	}

	mem := sigar.ProcMem{}
	if err := mem.Get(pid); err != nil {
		return nil, fmt.Errorf("error getting process mem for pid=%d: %v", pid, err)
	}

	cpu := sigar.ProcTime{}
	if err := cpu.Get(pid); err != nil {
		return nil, fmt.Errorf("error getting process cpu time for pid=%d: %v", pid, err)
	}

	if cmdline == "" {
		args := sigar.ProcArgs{}
		if err := args.Get(pid); err != nil {
			return nil, fmt.Errorf("error getting process arguments for pid=%d: %v", pid, err)
		}
		cmdline = strings.Join(args.List, " ")
	}

	proc := Process{
		Pid:      pid,
		Ppid:     state.Ppid,
		Name:     state.Name,
		State:    getProcState(byte(state.State)),
		Username: state.Username,
		CmdLine:  cmdline,
		Mem:      mem,
		Cpu:      cpu,
		ctime:    time.Now(),
	}

	return &proc, nil
}