package main

import (
	"github.com/mackerelio/go-osstat/memory"
	"github.com/mackerelio/go-osstat/loadavg"
	"github.com/mackerelio/go-osstat/network"
	"github.com/mackerelio/go-osstat/uptime"
	"github.com/mackerelio/go-osstat/cpu"
	"github.com/hako/durafmt"

	"fmt"
	"os"
	"amuz.es/src/infra/goutils/misc"
)

func a() {
	memory, err := memory.Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		return
	}
	//Total
	//Used
	//Buffers
	//Cached
	//Free
	//Active
	//Inactive
	//SwapTotal
	//SwapUsed
	//SwapCached
	//SwapFree uint64
	fmt.Println("memory total: ", misc.FileSizeIEC(memory.Total))
	fmt.Println("memory active: ", misc.FileSizeIEC(memory.Active))
	fmt.Println("memory cached: ", misc.FileSizeIEC(memory.Cached))
	fmt.Println("memory free: ", misc.FileSizeIEC(memory.Free))
	fmt.Println("memory inactive: ", misc.FileSizeIEC(memory.Inactive))
	fmt.Println("memory swapFree: ", misc.FileSizeIEC(memory.SwapFree))
	fmt.Println("memory swapTotal: ", misc.FileSizeIEC(memory.SwapTotal))
	fmt.Println("memory swapUsed: ", misc.FileSizeIEC(memory.SwapUsed))
	fmt.Println("memory used: ", misc.FileSizeIEC(memory.Used))

}

func b() {
	load, err := loadavg.Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		return
	}
	fmt.Printf("load Loadavg1: %f \n", load.Loadavg1)
	fmt.Printf("load Loadavg5: %f \n", load.Loadavg5)
	fmt.Printf("load Loadavg15: %f \n", load.Loadavg15)

}

func c() {
	netios, err := network.Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		return
	}
	for _, netio := range netios {
		fmt.Println("netio name: ", netio.Name)
		fmt.Println("netio rxBytes: ", misc.FileSizeIEC(netio.RxBytes))
		fmt.Println("netio txBytes: ", misc.FileSizeIEC(netio.TxBytes))

	}

}

func d() {
	ut, err := uptime.Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		return
	}
	fmt.Printf("uptime: %s\n", durafmt.Parse(ut).String())

}

func e() {
	ct, err := cpu.Get()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		return
	}
	fmt.Printf("cpu idle: %d\n", ct.Idle)
	fmt.Printf("cpu nice: %d\n", ct.Nice)
	fmt.Printf("cpu system: %d\n", ct.System)
	fmt.Printf("cpu total: %d\n", ct.Total)
	fmt.Printf("cpu user: %d\n", ct.User)

	fmt.Printf("cpu idle: %f%%\n", float64(ct.Idle*100)/float64(ct.Total))
	fmt.Printf("cpu nice: %f%%\n", float64(ct.Nice*100)/float64(ct.Total))
	fmt.Printf("cpu system: %f%%\n", float64(ct.System*100)/float64(ct.Total))
	fmt.Printf("cpu user: %f%%\n", float64(ct.User*100)/float64(ct.Total))
}

func f() {
	fmt.Fprintf(os.Stderr, "%s\n", "not supported")
}
