goprocstat/goprocstat.go

38 lines
744 B
Go
Raw Normal View History

2019-05-06 16:11:38 +02:00
package main
import (
"flag"
"fmt"
"github.com/shirou/gopsutil/process"
"os"
2019-05-06 16:11:38 +02:00
"time"
)
func main() {
flagPID := flag.Int("p", 1, "PID for process to monitor")
flagNUM := flag.Int("n", -1, "Stop after n times of displaying stat")
2019-05-06 16:11:38 +02:00
flag.Parse()
p, _ := process.NewProcess(int32(*flagPID))
v, err := p.Times()
name, _ := p.Name()
2019-05-07 14:50:03 +02:00
cnt := 0
2019-05-06 16:11:38 +02:00
for {
2019-05-07 14:50:03 +02:00
if *flagNUM >= 0 && cnt >= *flagNUM {
os.Exit(0)
}
2019-05-06 16:11:38 +02:00
if err == nil {
cpuUsage, _ := p.CPUPercent()
fmt.Printf("%d %s cpu: %3.2f%% user: %-3.2f system: %0.2f iowait: %0.2f irq: %0.2f softirq: %0.2f\n",
time.Now().Unix(), name, cpuUsage, v.User, v.System, v.Iowait, v.Irq, v.Softirq)
2019-05-06 16:11:38 +02:00
} else {
fmt.Println(err)
}
time.Sleep(time.Second)
cnt++
2019-05-06 16:11:38 +02:00
}
}