Exemple #1
0
func postData(datagram *sflow.Datagram, log *helper.LogFile){
    for i:=0; uint32(i) < datagram.NumSamples; i++ {
    // currently we only care about counter sample
        sample := datagram.Samples[i].(*sflow.CounterSample)
        records := sample.GetRecords()
        for _, record := range records {
            //fmt.Println(reflect.ValueOf(record))
            counter_record := record.(sflow.GenericInterfaceCounters)
            data := map[string] string{
                "uuid": strconv.FormatUint(uint64(counter_record.Index),10),
                "host": HOSTNAME,
                "inDiscard": strconv.FormatUint(uint64(counter_record.InDiscards), 10),
                "inError": strconv.FormatUint(uint64(counter_record.InErrors), 10),
                "inBps": strconv.FormatUint(counter_record.InOctets, 10),
                "inPps": strconv.FormatUint(uint64(counter_record.InUnicastPackets), 10),
                "outDiscard": strconv.FormatUint(uint64(counter_record.OutDiscards), 10),
                "outError": strconv.FormatUint(uint64(counter_record.OutErrors), 10),
                "outBps": strconv.FormatUint(counter_record.OutOctets, 10),
                "outPps": strconv.FormatUint(uint64(counter_record.OutUnicastPackets), 10),
            }
            err := notifier.YunhaiPost(data, log)
            if err != nil {
                msg := fmt.Sprintf("Error posting counter record: %v ", counter_record)
                log.LogErr(msg, err)
            }

        }
    }
}
Exemple #2
0
func YunhaiPost(data map[string]string, log *helper.LogFile) (err error) {
	client := &http.Client{}
	json_data, err := json.Marshal(data)
	if err != nil {
		fmt.Println("json err:", err)
		return err
	}
	body := bytes.NewBuffer([]byte(json_data))
	req, err := http.NewRequest("POST",
		"http://net-api.yunhai.baidu.com:8855/api/v1/flows/summary/",
		body)
	req.SetBasicAuth("neutron-api-user", "neutron-api-secret")

	resp, err := client.Do(req)
	if err != nil {
		msg := fmt.Sprintf("error when posting data: %v ", data)
		log.LogErr(msg, err)
		fmt.Printf("Error: %s", err)
		return err
	}
	msg := fmt.Sprintf("Post data: %v ", data)
	log.LogMsg(msg)
	log.LogMsg(resp.Status)
	//fmt.Println(resp)
	return nil
}