本文整理匯總了Golang中github.com/elastic/gosigar.ProcState類的典型用法代碼示例。如果您正苦於以下問題:Golang ProcState類的具體用法?Golang ProcState怎麽用?Golang ProcState使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ProcState類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
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, " "))
}
}
示例2: TestProcStateUsername
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)
}
}
示例3: GetProcess
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
}
示例4: TestLinuxProcState
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)
}
}()
}
}
示例5: TestDarwinProcState
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")
}
}
示例6: GetProcess
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
}
示例7:
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)
示例8: TestProcArgs
})
})
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)