open-falcon自定义push数据

本文介绍了一个使用Java实现的数据推送程序,该程序利用Apache HttpClient和Fastjson库将监测数据发送到指定服务器。推送的数据包括指标名称、时间戳、采样间隔等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依赖的jar包:

 <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.41</version>
    </dependency>

代码实现,是否push成功可以在agent的日志中查看。

package agent;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
public class App
{
    public static void main( String[] args ) throws Exception
    {
        while (true) {
            Test();
            Thread.sleep(5000);
        }
    }
    public static void Test() {
        try {
            HttpClient client = new DefaultHttpClient( );
            HttpPost post = new HttpPost("http://192.168.1.127:1988/v1/push");
            HashMap<String, Object> map = new HashMap<String, Object>();

            map.put("endpoint","ubuntu");
            map.put("metric","testmetric2018.02.06java");
            map.put("timestamp",System.currentTimeMillis()/1000);
            map.put("step", 60);
            map.put("value", 100.0);
            map.put("counterType", "GAUGE");
            map.put("tags","idc=lg,loc=shanghai");

            StringEntity stringEntity = null;
            stringEntity = new StringEntity("["+ JSON.toJSONString(map)+"]");
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType("application/json");
            post.setEntity(stringEntity);

            //debug
            System.out.println(stringEntity);
            System.out.println(JSON.toJSONString(map));
            //debug

            HttpResponse response = client.execute(post);
            if(response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                System.out.println(EntityUtils.toString(entity));
            }
        }catch (Exception e) {
            System.out.println(e);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值