1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package main
- import (
- "bytes"
- "flag"
- "fmt"
- "github.com/oz123/gupta"
- "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
- }
- func main() {
- var freq int
- var partition, netInterface string
- var load, memory, cpu bool
- flag.IntVar(&freq, "frequency", 5, "Frequency of polling in secods")
- flag.IntVar(&freq, "f", 5, "Frequency of polling in secods")
- 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()
- fmt.Println(gupta.Hello())
- r, _ := os.Open("/proc/stat")
- m, _ := os.Open("/proc/meminfo")
- 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())
- m.Seek(0, 0)
- }
- }
|