|
@@ -20,12 +20,18 @@ func readProcStatLine(r *os.File) string {
|
|
|
return line
|
|
|
}
|
|
|
|
|
|
+var Usage = func() {
|
|
|
+ fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
|
|
|
+
|
|
|
+ flag.PrintDefaults()
|
|
|
+}
|
|
|
+
|
|
|
func main() {
|
|
|
- var freq int
|
|
|
+ var interval int
|
|
|
var partition, netInterface string
|
|
|
var load, memory, cpu bool
|
|
|
|
|
|
- flag.IntVar(&freq, "i", 5, "polling inteval")
|
|
|
+ flag.IntVar(&interval, "i", 5, "polling inteval")
|
|
|
flag.BoolVar(&memory, "m", false, "Poll memory")
|
|
|
flag.BoolVar(&load, "l", false, "Poll load")
|
|
|
flag.BoolVar(&cpu, "c", false, "poll CPU usage")
|
|
@@ -34,25 +40,35 @@ func main() {
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
+ if load == false && memory == false && cpu == false {
|
|
|
+ Usage()
|
|
|
+ os.Exit(1)
|
|
|
+ }
|
|
|
fmt.Println(gupta.Hello())
|
|
|
r, _ := os.Open("/proc/stat")
|
|
|
m, _ := os.Open("/proc/meminfo")
|
|
|
+ defer r.Close()
|
|
|
+ defer m.Close()
|
|
|
|
|
|
for true {
|
|
|
- line := readProcStatLine(r)
|
|
|
- time.Sleep(time.Second)
|
|
|
- lineNew := readProcStatLine(r)
|
|
|
- cpustat := gupta.NewCPUStat(line, lineNew)
|
|
|
- fmt.Printf("CPU usage %.2f%%\n", cpustat.CPUUsage())
|
|
|
- mem := gupta.NewMemory(m)
|
|
|
- //fmt.Printf("Memory free %d\n", mem.Total())
|
|
|
report := gupta.GuptaReport{}
|
|
|
- report.MemoryUsage = mem
|
|
|
+ line := readProcStatLine(r)
|
|
|
+ time.Sleep(time.Duration(interval) * time.Second)
|
|
|
+ if cpu {
|
|
|
+ lineNew := readProcStatLine(r)
|
|
|
+ cpustat := gupta.NewCPUStat(line, lineNew)
|
|
|
+ report.CPUUsage = &cpustat
|
|
|
+ }
|
|
|
+ if memory {
|
|
|
+ mem := gupta.NewMemory(m)
|
|
|
+ report.MemoryUsage = &mem
|
|
|
+ }
|
|
|
writer := os.Stdout
|
|
|
enc := json.NewEncoder(writer)
|
|
|
if err := enc.Encode(&report); err != nil {
|
|
|
log.Println(err)
|
|
|
}
|
|
|
+ r.Seek(0, 0)
|
|
|
m.Seek(0, 0)
|
|
|
}
|
|
|
}
|