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, " ")) } }
func TestProcStateUsername(t *testing.T) { proc := sigar.ProcState{} err := proc.Get(os.Getpid()) if err != nil { t.Fatal(err) } user, err := user.Current() if err != nil { t.Fatal(err) } if user.Username != proc.Username { t.Fatalf("Usernames don't match, expected %s, but got %s", user.Username, proc.Username) } }
// newProcess creates a new Process object based on the state information. func newProcess(pid int) (*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) } proc := Process{ Pid: pid, Ppid: state.Ppid, Name: state.Name, State: getProcState(byte(state.State)), Username: state.Username, Ctime: time.Now(), } return &proc, nil }
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 }
func TestLinuxProcState(t *testing.T) { setUp(t) defer tearDown(t) var procNames = []string{ "cron", "a very long process name", } for _, n := range procNames { func() { pid := rand.Int() pidDir := filepath.Join(procd, strconv.Itoa(pid)) err := os.Mkdir(pidDir, 0755) if err != nil { t.Fatal(err) } defer os.RemoveAll(pidDir) pidStatFile := filepath.Join(pidDir, "stat") writePidStats(pid, n, pidStatFile) if err != nil { t.Fatal(err) } pidStatusFile := filepath.Join(pidDir, "status") uid := 123456789 writePidStatus(n, pid, uid, pidStatusFile) if err != nil { t.Fatal(err) } state := sigar.ProcState{} if assert.NoError(t, state.Get(pid)) { assert.Equal(t, n, state.Name) assert.Equal(t, 2, state.Pgid) assert.Equal(t, strconv.Itoa(uid), state.Username) } }() } }
func TestDarwinProcState(t *testing.T) { setUp(t) defer tearDown(t) state := sigar.ProcState{} usr, err := user.Current() if err == nil && usr.Username == "root" { if assert.NoError(t, state.Get(1)) { ppid, _ := strconv.Atoi(procinfo["ppid"]) pgid, _ := strconv.Atoi(procinfo["pgid"]) assert.Equal(t, procinfo["name"], state.Name) assert.Equal(t, ppid, state.Ppid) assert.Equal(t, pgid, state.Pgid) assert.Equal(t, 1, state.Pgid) assert.Equal(t, 0, state.Ppid) } } else { fmt.Println("Skipping ProcState test; run as root to test") } }
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 }
BeforeEach(func() { var err error procd, err = ioutil.TempDir("", "sigarTests") Expect(err).ToNot(HaveOccurred()) sigar.Procd = procd }) AfterEach(func() { sigar.Procd = "/proc" }) Describe("ProcState", func() { var ( pid int pidDir string pidStatFile string state sigar.ProcState ) BeforeEach(func() { pid = rand.Int() pidDir = filepath.Join(procd, strconv.Itoa(pid)) err := os.Mkdir(pidDir, 0755) Expect(err).ToNot(HaveOccurred()) pidStatFile = filepath.Join(pidDir, "stat") state = sigar.ProcState{} }) It("Knows the process name", func() { writePidStats(pid, "cron", pidStatFile) state.Get(pid)
}) }) Describe("Disk", func() { It("gets the total disk space", func() { usage := sigar.FileSystemUsage{} err := usage.Get(os.TempDir()) Ω(err).ShouldNot(HaveOccurred()) Ω(usage.Total).Should(BeNumerically(">", 0)) }) }) Describe("Process", func() { It("gets the current process user name", func() { proc := sigar.ProcState{} err := proc.Get(os.Getpid()) user, usererr := user.Current() Ω(err).ShouldNot(HaveOccurred()) Ω(usererr).ShouldNot(HaveOccurred()) Ω(proc.Username).Should(Equal(user.Username)) }) }) }) func TestProcArgs(t *testing.T) { args := sigar.ProcArgs{} err := args.Get(os.Getpid()) if err != nil { t.Fatal(err)