func main() {
	v, _ := mem.VirtualMemory()

	// almost every return value is a struct
	fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)

	// convert to JSON. String() is also implemented
	fmt.Println(v)

	h, err := host.HostInfo()
	if err != nil {
		fmt.Println("err:", err)
	} else {

		fmt.Printf("hostname %v", h)
	}

	c, err := cpu.CPUInfo()
	if err != nil {
		fmt.Println("err:", err)
	}
	for _, v := range c {
		fmt.Printf("cpu info %v \n ", v)
	}

}
Exemple #2
0
func updateCPUInfo() {
	ci, err = cpu.CPUInfo()
	log.Printf("updateCPUInfo(): %v", spew.Sdump(ci))
	if err != nil {
		log.Fatal(err)
	}
}
func initCPUInfo(monitorData *monitoringData) error {
	info, err := cpu.CPUInfo()

	if info != nil {
		monitorData.CPUInfo = info

		monitorData.CPUModelName = monitorData.CPUInfo[0].ModelName

		t, err := cpu.CPUTimes(true)

		if t != nil {
			monitorData.CPUTime = t

			globalTime, err := cpu.CPUTimes(false)

			if globalTime != nil {
				monitorData.GlobalCPUTime = globalTime

				var percentWaitGroup sync.WaitGroup

				percentWaitGroup.Add(2)

				go func(monitorData *monitoringData, wg *sync.WaitGroup) {
					defer wg.Done()
					cpuPercent, _ := cpu.CPUPercent(monitorData.UpdatePeriod, true)

					monitorData.CPUPercent = cpuPercent
				}(monitorData, &percentWaitGroup)

				go func(monitorData *monitoringData, wg *sync.WaitGroup) {
					defer wg.Done()
					globalCpuPercent, _ := cpu.CPUPercent(monitorData.UpdatePeriod, false)

					monitorData.GlobalCPUPercent = globalCpuPercent[0]
				}(monitorData, &percentWaitGroup)

				percentWaitGroup.Wait()

				cpuCounts, err := cpu.CPUCounts(false)

				monitorData.CPUCounts = cpuCounts

				logicalCpuCounts, err := cpu.CPUCounts(false)

				monitorData.LogicalCPUCounts = logicalCpuCounts

				return err
			}

			return err
		}

		return err
	}

	return err
}
Exemple #4
0
func (c *CpuInfo) Run() error {
	data, err := gopsutil_cpu.CPUInfo()
	if err != nil {
		return err
	}

	c.Data["CpuInfo"] = data
	return nil
}
Exemple #5
0
func main() {
	v, _ := mem.VirtualMemory()
	c, _ := cpu.CPUInfo()
	// almost every return value is a struct
	fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
	for _, v := range c {
		fmt.Printf("Percentage: %f\n", v.Mhz)
	}
	// convert to JSON. String() is also implemented
}
func startNode(masterHost, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	masterUrl := url.URL{Scheme: "tcp", Host: masterHost}
	ip := getPrivateIP()

	// Try to get new "respondent" socket
	if sock, err = respondent.NewSocket(); err != nil {
		utils.Die("Can't get new respondent socket: %s", err.Error())
	}
	defer sock.Close()

	sock.AddTransport(tcp.NewTransport())

	// Connect to master
	if err = sock.Dial(masterUrl.String()); err != nil {
		utils.Die("Can't dial on respondent socket: %s", err.Error())
	}

	// Wait for a survey request and send responses
	for {
		if msg, err = sock.Recv(); err != nil {
			utils.Die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("Client(%s): Received \"%s\" survey request\n", name, string(msg))

		var loadAvg *load.LoadAvgStat
		if loadAvg, err = load.LoadAvg(); err != nil {
			utils.Die("Cannot get load average: %s", err.Error())
		}

		var cpuInfo []cpu.CPUInfoStat
		if cpuInfo, err = cpu.CPUInfo(); err != nil {
			utils.Die("Cannot get CPU info: %s", err.Error())
		}
		fmt.Printf("CPU INFO len: %d\n", len(cpuInfo))

		// Get the normalized CPU load
		avg := loadAvg.Load1
		cores := int32(0)
		for _, info := range cpuInfo {
			fmt.Printf("Inner Cores: %d\n", info.Cores)
			cores += info.Cores
		}
		fmt.Printf("Load avg: %f\n", avg)
		fmt.Printf("Cores: %d\n", cores)
		avg = avg / float64(cores)

		fmt.Printf("Client(%s): Sending survey response\n", name)
		if err = sock.Send([]byte(fmt.Sprintf("%s,%f", ip, avg))); err != nil {
			utils.Die("Cannot send: %s", err.Error())
		}
	}
}
Exemple #7
0
func (f *CPUFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
	cpuInfo, err := cpu.CPUInfo()
	if err != nil {
		f.logger.Println("[WARN] Error reading CPU information:", err)
		return false, err
	}

	var numCores int32
	var mhz float64
	var modelName string

	// Assume all CPUs found have same Model. Log if not.
	// If CPUInfo() returns nil above, this loop is still safe
	for _, c := range cpuInfo {
		numCores += c.Cores
		mhz += c.Mhz

		if modelName != "" && modelName != c.ModelName {
			f.logger.Println("[WARN] Found different model names in the same CPU information. Recording last found")
		}
		modelName = c.ModelName
	}
	// Get average CPU frequency
	mhz /= float64(len(cpuInfo))

	if mhz > 0 {
		node.Attributes["cpu.frequency"] = fmt.Sprintf("%.6f", mhz)
	}

	if numCores > 0 {
		node.Attributes["cpu.numcores"] = fmt.Sprintf("%d", numCores)
	}

	if mhz > 0 && numCores > 0 {
		tc := float64(numCores) * mhz
		node.Attributes["cpu.totalcompute"] = fmt.Sprintf("%.6f", tc)

		if node.Resources == nil {
			node.Resources = &structs.Resources{}
		}

		node.Resources.CPU = int(tc)
	}

	if modelName != "" {
		node.Attributes["cpu.modelname"] = modelName
	}

	return true, nil
}
func jsonCPUInfo() []byte {
	type CpuInfo struct {
		Info  []cpu.CPUInfoStat
		Times []cpu.CPUTimesStat
		Time  []cpu.CPUTimesStat
	}
	c := &CpuInfo{}
	CPUTime, _ := cpu.CPUTimes(false)
	CPUTimes, _ := cpu.CPUTimes(true)
	CPUInfo, _ := cpu.CPUInfo()
	c.Info = CPUInfo
	c.Time = CPUTime
	c.Times = CPUTimes
	jsonPartitions, _ := json.Marshal(c)
	return jsonPartitions
}
Exemple #9
0
	hateoas.ServiceUse(api.JSON),
	hateoas.AddResource(root, cpuRsc, loadRsc, procRsc),
)

var root = hateoas.NewResource(
	hateoas.Path("/"),
	hateoas.AddLink("cpus", cpuRsc),
	hateoas.AddLink("load", loadRsc),
	hateoas.AddLink("processes", procRsc),
	hateoas.HEAD(mohttp.EmptyBodyHandler),
)

var cpuRsc = hateoas.NewResource(
	hateoas.Path("/cpus"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return cpu.CPUInfo()
	})),
)

var loadRsc = hateoas.NewResource(
	hateoas.Path("/load"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return load.LoadAvg()
	})),
)

var procRsc = hateoas.NewResource(
	hateoas.Path("/processes"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return process.Pids()
	})),
Exemple #10
0
func main() {

	// DISK INFO

	fmt.Print("\n----Disk Info ----\n\n")
	diskInfo, err := disk.DiskPartitions(true)
	if err != nil {
		panic(err)
	}
	log.Printf("[DEBUG] %#v\n\n", diskInfo)

	for _, diskn := range diskInfo {
		fmt.Printf("%s %s\n", diskn.Device, diskn.Mountpoint)
	}

	// CPU INFO

	fmt.Print("\n----CPU Info ----\n\n")
	cpuInfo, err := cpu.CPUInfo()
	if err != nil {
		panic(err)
	}
	log.Printf("[DEBUG] %#v\n\n", cpuInfo)

	var totalCores int32
	var frequency float64
	var totalCompute float64
	var flags string

	for _, cpu := range cpuInfo {
		totalCores += cpu.Cores
		if frequency == 0 {
			frequency = cpu.Mhz
		} else {
			if frequency != cpu.Mhz {
				log.Printf("A CPU shows inconsistent frequency with other CPUs: baseline %f, abberant %f", frequency, frequency)
			}
		}
		flags = strings.Join(cpu.Flags, " ")
	}
	totalCompute = frequency * float64(totalCores)
	fmt.Printf("%s %d\n", "cpu.totalcores", totalCores)
	fmt.Printf("%s %d\n", "cpu.numcpus", len(cpuInfo))
	fmt.Printf("%s %f\n", "cpu.frequency", frequency)
	fmt.Printf("%s %f\n", "cpu.totalcompute", totalCompute)
	fmt.Printf("%s %s\n", "cpu.flags", flags)

	// MEMORY INFO

	fmt.Print("\n----Memory Info ----\n\n")
	memInfo, err := mem.VirtualMemory()
	if err != nil {
		panic(err)
	}
	log.Printf("[DEBUG] %#v\n\n", memInfo)

	fmt.Printf("%s %d\n", "memory.totalbytes", memInfo.Total)

	// HOST INFO

	fmt.Print("\n----Host Info ----\n\n")
	hostInfo, err := host.HostInfo()
	if err != nil {
		panic(err)
	}

	log.Printf("[DEBUG] %#v\n\n", hostInfo)

	fmt.Printf("%s %s\n", "hostInfo.Hostname       ", hostInfo.Hostname)
	fmt.Printf("%s %s\n", "hostInfo.OS             ", hostInfo.OS)
	fmt.Printf("%s %s\n", "hostInfo.Platform       ", hostInfo.Platform)
	fmt.Printf("%s %s\n", "hostInfo.PlatformVersion", hostInfo.PlatformVersion)
	fmt.Printf("%s %s\n", "hostInfo.PlatformFamily ", hostInfo.PlatformFamily)
}
func json_cpuinfo(w http.ResponseWriter, r *http.Request) {
	cpuinfo, _ := cpu.CPUInfo()
	b, _ := json.Marshal(cpuinfo)
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, string(b))
}