package pai

import (
	"os/exec"
	"io/ioutil"
	"strings"
	"chain33-pai/pkg/util"
	"github.com/33cn/chain33/common/log"
)

var tlog = log.New("pkg","pai")

func MonitorServer() ([]*Disk,error) {
	//获取硬盘情况
	cmd := exec.Command("df","-T")
	stdout, err := cmd.StdoutPipe()
	err = cmd.Start()
	if err != nil {
		tlog.Info("MonitorServer","df -T start " ,err)
		return nil,err
	}
	content, err := ioutil.ReadAll(stdout)
	if err != nil {
		tlog.Info("MonitorServer","ioutil readd err",err)
		return nil,err
	}
	err = cmd.Wait()
	if err != nil {
		tlog.Info("MonitorServer","df -T end" ,err)
		return nil,err
	}
	tlog.Info("MonitorServer",string(content))
	list := strings.Split(string(content),"\n")
	tempDisk := make([]*Disk,0)
	for _,v := range list {
		c := strings.Contains(v,"/dev/sd")
		if c {
			tempD := &Disk{}
			params := strings.Split(util.DeleteExtraSpace(v)," ")
			if len(params) == 7 {
				tempD.Filesystem = params[0]
				tempD.Type = params[1]
				tempD.SizeK = params[2]
				tempD.Use = params[5]
				tempDisk = append(tempDisk,tempD)
			} else {
				break
			}
		}
	}
	return tempDisk,nil
}
