Beispiel #1
0
func (this *Server) output(data []byte, outChan chan *common.Packet) error {
	var err error

	m := make(map[string]interface{})
	err = json.Unmarshal(data, &m)
	if err != nil {
		return err
	}

	host, exists := m["host"]
	if !exists {
		return fmt.Errorf("host not exists")
	}
	hostname, ok := host.(string)
	if !ok {
		return fmt.Errorf("host is not a string")
	}

	//time, exists := m["time"]
	//if !exists {
	//      fmt.Printf("json unmarshal from %s failed, time not exists", s)
	//      return nil
	//}

	datas, exists := m["data"]
	if !exists {
		return fmt.Errorf("data not exists")
	}
	items, ok := datas.([]interface{})
	if !ok {
		return fmt.Errorf("data is not []interface")
	}
	length := len(items)
	if length < 1 {
		return fmt.Errorf("data item length < 1")
	}

	for i := 0; i < length; i++ {
		item, ok := items[i].([]interface{})
		if !ok {
			return fmt.Errorf("data item is not []interface{}")
		}
		item_id, ok := item[0].(string)
		if !ok {
			return fmt.Errorf("item id is not a string")
		}
		value, ok := item[1].(string)
		if !ok {
			return fmt.Errorf("value is not a string")
		}
		itemId, _ := strconv.Atoi(item_id)
		val, _ := strconv.ParseFloat(value, 64)

		//insert into out channel
		select {
		case outChan <- common.NewPacket(hostname, itemId, val):
			this.logger.Debug("Insert into packet channel success, hostname:%s, item_id:%s, value:%s", hostname, item_id, value)
		default:
			this.logger.Error("Insert into packet channel timeout, channel length:%d too more, drop packet: hostname:%s, item id:%s, value:%s", len(outChan), hostname, item_id, value)
			common.Alarm("packet_channel_timeout")
			time.Sleep(time.Second)
			return fmt.Errorf("channel timeout")
		}
	}

	this.logger.Info("Put %d alarm packets into packet channel", length)
	return nil
}
Beispiel #2
0
func (this *Server) output(data []byte, outChan chan *common.Packet) error {
	var err error

	m := make(map[string]interface{})
	err = json.Unmarshal(data, &m)
	if err != nil {
		return err
	}

	host, exists := m["host"]
	if !exists {
		return fmt.Errorf("host not exists")
	}
	hostname, ok := host.(string)
	if !ok {
		return fmt.Errorf("host is not a string, type:%v, host:%v", reflect.TypeOf(host), host)
	}

	//agenttime localtime
	agenttime, exists := m["time"]
	if !exists {
		return fmt.Errorf("time not exists")
	}
	agenttime_s, ok := agenttime.(string)
	if !ok {
		return fmt.Errorf("time is not a string, type:%v, time:%v", reflect.TypeOf(agenttime), agenttime)
	}
	agent_time, _ := strconv.ParseInt(agenttime_s, 10, 64)

	datas, exists := m["data"]
	if !exists {
		return fmt.Errorf("data not exists")
	}
	items, ok := datas.([]interface{})
	if !ok {
		return fmt.Errorf("data is not array: []interface, type:%v, data:%v", reflect.TypeOf(datas), datas)
	}
	length := len(items)
	if length < 1 {
		return fmt.Errorf("data items length < 1")
	}

	p := common.NewPacket(hostname, agent_time)
	var itemName string
	for i := 0; i < length; i++ {
		//item:  [item_id, value, val_type]
		item, ok := items[i].([]interface{})
		if !ok {
			return fmt.Errorf("data item is not array []interface, type:%v, item:%v", reflect.TypeOf(items[i]), items[i])
		}
		if len(item) < 3 {
			return fmt.Errorf("data item length < 3, item:%v", item)
		}
		item_id, ok := item[0].(string)
		if !ok {
			return fmt.Errorf("item id is not string, type:%v, id:%v", reflect.TypeOf(item[0]), item[0])
		}
		itemId, _ := strconv.Atoi(item_id)
		value, ok := item[1].(string)
		if !ok {
			return fmt.Errorf("value is not string, value:%v", item[1])
		}

		if this.itemBlacklist != nil {
			if _, ok = this.itemBlacklist[itemId]; ok {
				this.logger.Debug("item id:%d is in item blacklist, drop", itemId)
				continue
			}
		}

		if this.itemWhitelist != nil {
			if itemName, ok = this.itemWhitelist[itemId]; !ok {
				this.logger.Debug("item id:%d not in item whitelist, drop", itemId)
				continue
			}
		}

		p.AddItem(itemId, value, itemName)
	}

	if p.ItemNum < 1 {
		this.logger.Debug("packet item num < 1, hostname:%s, time:%s", hostname, agenttime_s)
		return nil
	}

	//insert into output channel
	select {
	case outChan <- p:
		this.logger.Debug("Insert into packet channel success: Hostname:%s, time:%s", hostname, agenttime_s)
	default:
		this.logger.Error("Insert into packet channel failed, channel length:%d too more, drop packet: %v", len(outChan), p)
		common.Alarm("packet_channel_block")
		time.Sleep(time.Second)
		return fmt.Errorf("packet channel block")
	}

	this.logger.Info("Put %d packet items into packet channel", p.ItemNum)

	return nil
}