Exemple #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
}
Exemple #2
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, " "))
	}
}