예제 #1
0
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
}
예제 #2
0
func updateCPUTimes() {
	ct, err = cpu.CPUTimes(false)
	log.Printf("updateCPUTimes(): %v", spew.Sdump(ct))
	if err != nil {
		log.Fatal(err)
	}
}
예제 #3
0
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
}
예제 #4
0
func CPUTimeStat_f() ([]CPUTimeStat_j, error) {
	cpu, _ := cpu.CPUTimes(true)
	timestamp := time.Now().Unix()

	var ret []CPUTimeStat_j
	var info CPUTimeStat_j
	for _, v := range cpu {
		var total float64 = v.User + v.System + v.Idle + v.Nice + v.Iowait + v.Irq + v.Softirq
		info = CPUTimeStat_j{
			CPU:       v.CPU,
			User:      v.User / total * 100,
			System:    v.System / total * 100,
			Idle:      v.Idle / total * 100,
			Nice:      v.Nice / total * 100,
			Iowait:    v.Iowait / total * 100,
			Irq:       v.Irq / total * 100,
			Softirq:   v.Softirq / total * 100,
			Steal:     v.Steal,
			Guest:     v.Guest,
			GuestNice: v.GuestNice,
			Stolen:    v.Stolen,
			Timestamp: timestamp,
		}
		ret = append(ret, info)
	}
	return ret, nil
}
예제 #5
0
파일: ps.go 프로젝트: zooplus/telegraf
func (s *systemPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
	var cpuTimes []cpu.CPUTimesStat
	if perCPU {
		if perCPUTimes, err := cpu.CPUTimes(true); err == nil {
			cpuTimes = append(cpuTimes, perCPUTimes...)
		} else {
			return nil, err
		}
	}
	if totalCPU {
		if totalCPUTimes, err := cpu.CPUTimes(false); err == nil {
			cpuTimes = append(cpuTimes, totalCPUTimes...)
		} else {
			return nil, err
		}
	}
	return cpuTimes, nil
}
예제 #6
0
파일: cpu.go 프로젝트: simudream/resourced
// Run gathers gopsutil_cpu.CPUTimes data.
func (c *CpuStat) Run() error {
	data, err := gopsutil_cpu.CPUTimes(true)
	if err != nil {
		return err
	}

	c.Data["CpuStat"] = data
	return nil
}
예제 #7
0
파일: status.go 프로젝트: chansuke/fuji
// Get returns CPU status by Message.
// Note: currently, only one CPU status is retrieved.
func (c CPUStatus) Get() []message.Message {
	ret := []message.Message{}

	cpuTimes, err := cpu.CPUTimes(false)
	if err == nil {
		for _, t := range c.CpuTimes {
			msg := message.Message{
				Sender:     "status",
				Type:       "status",
				BrokerName: c.BrokerName,
			}
			var body string
			switch t {
			case "user":
				body = strconv.Itoa(int(cpuTimes[0].User))
			case "system":
				body = strconv.Itoa(int(cpuTimes[0].System))
			case "idle":
				body = strconv.Itoa(int(cpuTimes[0].Idle))
			case "nice":
				body = strconv.Itoa(int(cpuTimes[0].Nice))
			case "iowait":
				body = strconv.Itoa(int(cpuTimes[0].Iowait))
			case "irq":
				body = strconv.Itoa(int(cpuTimes[0].Irq))
			case "softirq":
				body = strconv.Itoa(int(cpuTimes[0].Softirq))
			case "guest":
				body = strconv.Itoa(int(cpuTimes[0].Guest))
			}

			msg.Body = []byte(body)
			topic, err := genTopic(c.GatewayName, "cpu", "cpu_times", t)
			if err != nil {
				log.Errorf("invalid topic, %s/%s/%s/%s", c.GatewayName, "cpu", "cpu_times", t)
				continue
			}
			msg.Topic = topic

			ret = append(ret, msg)
		}

	} else {
		log.Warnf("cpu get err, %v", err)
	}

	return ret
}
func getCPUTimesMetricTypes() ([]plugin.PluginMetricType, error) {
	//passing true to CPUTimes indicates per CPU
	//CPUTimes does not currently work on OSX https://github.com/shirou/gopsutil/issues/31
	mts := make([]plugin.PluginMetricType, 0)
	if runtime.GOOS != "darwin" {
		c, err := cpu.CPUTimes(true)
		if err != nil {
			return nil, err
		}
		for _, i := range c {
			for _, label := range cpuLabels {
				mts = append(mts, plugin.PluginMetricType{Namespace_: []string{"intel", "psutil", i.CPU, label}})
			}
		}
	}
	return mts, nil
}
func cpuTimes(ns []string) (*plugin.PluginMetricType, error) {
	cpus, err := cpu.CPUTimes(true)
	if err != nil {
		return nil, err
	}

	for _, cpu := range cpus {
		switch {
		case regexp.MustCompile(`^/intel/psutil/cpu.*/user`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.User,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/system`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.System,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/idle`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.Idle,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/nice`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.Nice,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/iowait`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.Iowait,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/irq`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.Irq,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/softirq`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.Softirq,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/steal`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.Steal,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/guest`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.Guest,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/guest_nice`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.GuestNice,
			}, nil
		case regexp.MustCompile(`^/intel/psutil/cpu.*/stolen`).MatchString(joinNamespace(ns)):
			return &plugin.PluginMetricType{
				Namespace_: ns,
				Data_:      cpu.Stolen,
			}, nil
		}

	}

	return nil, fmt.Errorf("Unknown error processing %v", ns)
}