package main import ( "bytes" "encoding/json" "flag" "fmt" "github.com/oz123/gupta" "log" "os" "time" ) func readProcStatLine(r *os.File) string { buf := make([]byte, 80) r.ReadAt(buf, 0) idx := bytes.Index(buf, []byte("cpu0")) line := string(buf[:idx]) r.Seek(0, 0) return line } var Usage = func() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) flag.PrintDefaults() } func main() { var interval int var partition, netInterface string var load, memory, cpu bool writer := os.Stdout enc := json.NewEncoder(writer) 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") flag.StringVar(&partition, "p", "", "Poll disk usage (partition)") flag.StringVar(&netInterface, "n", "", "Network usage (interface)") 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 { report := gupta.GuptaReport{Time: time.Now()} 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 } if err := enc.Encode(&report); err != nil { log.Println(err) } r.Seek(0, 0) m.Seek(0, 0) } }