Example #1
0
// 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
}
Example #2
0
func (m MemoryStatus) Get() []message.Message {
	ret := []message.Message{}

	vmem, err := mem.VirtualMemory()
	if err == nil {
		for _, t := range m.VirtualMemory {
			msg := message.Message{
				Sender:     "status",
				Type:       "status",
				BrokerName: m.BrokerName,
			}
			var body string
			switch t {
			case "total":
				body = strconv.Itoa(int(vmem.Total))
			case "available":
				body = strconv.Itoa(int(vmem.Available))
			case "percent":
				body = fmt.Sprintf("%v", vmem.UsedPercent)
			case "used":
				body = strconv.Itoa(int(vmem.Used))
			case "free":
				body = strconv.Itoa(int(vmem.Free))
			}
			msg.Body = []byte(body)
			topic, err := genTopic(m.GatewayName, "memory", "virtual_memory", t)
			if err != nil {
				log.Errorf("invalid topic, %s/%s/%s/%s", m.GatewayName, "memory", "virtual_memory", t)
				continue
			}
			msg.Topic = topic

			ret = append(ret, msg)
		}
	} else {
		log.Warnf("virtual_memory get error, %v", err)
	}
	return ret
}