curl 如何传 JSON参数 给opentsdb的 api/query?

时间:2022-11-26 19:08:34
我使用以下命令是成功的:
curl -d start=1h-ago  -d m="avg:proc.loadavg.10m{host=*}" -d ascii= --get  http://10.4.22.187:4242/api/query
但是我想传JSON对象时失败了:
curl -d '{"start":1h-ago,"m":[{"aggregator":avg,"metric":proc.loadavg.10m,"tags":{"host":"*",}}] }' http://10.4.22.187:4242/api/query
这个问题怎么fix啊?或者大神给我一个传JSON对象给 api/query的例子吧。

4 个解决方案

#1


这个json格式有问题
修改后的json

{
    "start": "1h-ago",
    "m": [
        {
            "aggregator": "avg",
            "metric": "proc.loadavg.10m",
            "tags": {
                "host": "*"
            }
        }
    ]
}

在线验证json格式
http://www.bejson.com/

#2


引用 1 楼 shixitong 的回复:
这个json格式有问题
修改后的json

{
    "start": "1h-ago",
    "m": [
        {
            "aggregator": "avg",
            "metric": "proc.loadavg.10m",
            "tags": {
                "host": "*"
            }
        }
    ]
}

在线验证json格式
http://www.bejson.com/



我使用curl -d '{"start":"1h-ago","m":[{"aggregator":"avg","metric":"proc.loadavg.10m","tags":{"host":"*"}}]}' http://10.4.22.187:4242/api/query 还是出错了,出现一大串错误信息:

{"error":{"code":400,"message":"Unable to parse the given JSON","details":"com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"m\" (class net.opentsdb.core.TSQuery), not marked as ignorable (10 known properties: , \"globalAnnotations\", \"start\", \"noAnnotations\", \"queries\", \"padding\", \"msResolution\", \"timezone\", \"end\", \"options\", \"showTSUIDs\"])\n at [Source: java.io.StringReader@6c32a839; line: 1, column: 24] (through reference chain: net.opentsdb.core.TSQuery[\"m\"])","trace":"net.opentsdb.tsd.BadRequestException: Unable to parse the given JSON\n\tat net.opentsdb.tsd.HttpJsonSerializer.parseQueryV1(HttpJsonSerializer.java:205) ~[tsdb-2.0.0.jar:]\n\tat net.opentsdb.tsd.QueryRpc.execute(QueryRpc.java:73) ~[tsdb-2.0.0.jar:]\n\tat net.opentsdb.tsd.RpcHandler.handleHttpQuery(RpcHandler.java:255) [tsdb-2.0.0.jar:]\n\tat net.opentsdb.tsd.RpcHandler.messageReceived(RpcHandler.java:163) [tsdb-2.0.0.jar:]\n\tat org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:536) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:435) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.SimpleChannelHandler.messageReceived(SimpleChannelHandler.java:142) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88) [netty-3.9.1.Final.jar:na]\n\tat net.opentsdb.tsd.ConnectionManager.handleUpstream(ConnectionManager.java:87) [tsdb-2.0.0.jar:]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) [netty-3.9.1.Final.jar:na]\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_60]\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_60]\n\tat java.lang.Thread.run(Thread.java:745) [na:1.7.0_60]\nCaused by: java.lang.IllegalArgumentException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"m\" (class net.opentsdb.core.TSQuery), not marked as ignorable (10 known properties: , \"globalAnnotations\", \"start\", \"noAnnotations\", \"queries\", \"padding\", \"msResolution\", \"timezone\", \"end\", \"options\", \"showTSUIDs\"])\n at [Source: java.io.StringReader@6c32a839; line: 1, column: 24] (through reference chain: net.opentsdb.core.TSQuery[\"m\"])\n\tat net.opentsdb.utils.JSON.parseToObject(JSON.java:112) ~[tsdb-2.0.0.jar:]\n\tat net.opentsdb.tsd.HttpJsonSerializer.parseQueryV1(HttpJsonSerializer.java:203) ~[tsdb-2.0.0.jar:]\n\t... 37 common frames omitted\nCaused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"m\" (class net.opentsdb.core.TSQuery), not marked as ignorable (10 known properties: , \"globalAnnotations\", \"start\", \"noAnnotations\", \"queries\", \"padding\", \"msResolution\", \"timezone\", \"end\", \"options\", \"showTSUIDs\"])\n at [Source: java.io.StringReader@6c32a839; line: 1, column: 24] (through reference chain: net.opentsdb.core.TSQuery[\"m\"])\n\tat com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:579) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:672) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:906) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:328) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2793) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1943) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat net.opentsdb.utils.JSON.parseToObject(JSON.java:108) ~[tsdb-2.0.0.jar:]\n\t... 38 common frames omitted\n"}}

我简直要崩溃了。

#3


看错误提示还是json有问题,换个最简单的json试试呢,比如:{"name":"1111"}

#4


是你的json数据组成方式,不对,具体可以参考 http://opentsdb.net/docs/build/html/api_http/put.html
下面我写的一个 
curl -d '{"start": 1460451066,"queries": [{"aggregator": "sum","metric": "mysql.bytes_sent","tags": {"host": "*"}}]}' http://127.0.0.1:4242/api/query

#1


这个json格式有问题
修改后的json

{
    "start": "1h-ago",
    "m": [
        {
            "aggregator": "avg",
            "metric": "proc.loadavg.10m",
            "tags": {
                "host": "*"
            }
        }
    ]
}

在线验证json格式
http://www.bejson.com/

#2


引用 1 楼 shixitong 的回复:
这个json格式有问题
修改后的json

{
    "start": "1h-ago",
    "m": [
        {
            "aggregator": "avg",
            "metric": "proc.loadavg.10m",
            "tags": {
                "host": "*"
            }
        }
    ]
}

在线验证json格式
http://www.bejson.com/



我使用curl -d '{"start":"1h-ago","m":[{"aggregator":"avg","metric":"proc.loadavg.10m","tags":{"host":"*"}}]}' http://10.4.22.187:4242/api/query 还是出错了,出现一大串错误信息:

{"error":{"code":400,"message":"Unable to parse the given JSON","details":"com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"m\" (class net.opentsdb.core.TSQuery), not marked as ignorable (10 known properties: , \"globalAnnotations\", \"start\", \"noAnnotations\", \"queries\", \"padding\", \"msResolution\", \"timezone\", \"end\", \"options\", \"showTSUIDs\"])\n at [Source: java.io.StringReader@6c32a839; line: 1, column: 24] (through reference chain: net.opentsdb.core.TSQuery[\"m\"])","trace":"net.opentsdb.tsd.BadRequestException: Unable to parse the given JSON\n\tat net.opentsdb.tsd.HttpJsonSerializer.parseQueryV1(HttpJsonSerializer.java:205) ~[tsdb-2.0.0.jar:]\n\tat net.opentsdb.tsd.QueryRpc.execute(QueryRpc.java:73) ~[tsdb-2.0.0.jar:]\n\tat net.opentsdb.tsd.RpcHandler.handleHttpQuery(RpcHandler.java:255) [tsdb-2.0.0.jar:]\n\tat net.opentsdb.tsd.RpcHandler.messageReceived(RpcHandler.java:163) [tsdb-2.0.0.jar:]\n\tat org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:536) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:435) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.SimpleChannelHandler.messageReceived(SimpleChannelHandler.java:142) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:88) [netty-3.9.1.Final.jar:na]\n\tat net.opentsdb.tsd.ConnectionManager.handleUpstream(ConnectionManager.java:87) [tsdb-2.0.0.jar:]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) [netty-3.9.1.Final.jar:na]\n\tat org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42) [netty-3.9.1.Final.jar:na]\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_60]\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_60]\n\tat java.lang.Thread.run(Thread.java:745) [na:1.7.0_60]\nCaused by: java.lang.IllegalArgumentException: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"m\" (class net.opentsdb.core.TSQuery), not marked as ignorable (10 known properties: , \"globalAnnotations\", \"start\", \"noAnnotations\", \"queries\", \"padding\", \"msResolution\", \"timezone\", \"end\", \"options\", \"showTSUIDs\"])\n at [Source: java.io.StringReader@6c32a839; line: 1, column: 24] (through reference chain: net.opentsdb.core.TSQuery[\"m\"])\n\tat net.opentsdb.utils.JSON.parseToObject(JSON.java:112) ~[tsdb-2.0.0.jar:]\n\tat net.opentsdb.tsd.HttpJsonSerializer.parseQueryV1(HttpJsonSerializer.java:203) ~[tsdb-2.0.0.jar:]\n\t... 37 common frames omitted\nCaused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field \"m\" (class net.opentsdb.core.TSQuery), not marked as ignorable (10 known properties: , \"globalAnnotations\", \"start\", \"noAnnotations\", \"queries\", \"padding\", \"msResolution\", \"timezone\", \"end\", \"options\", \"showTSUIDs\"])\n at [Source: java.io.StringReader@6c32a839; line: 1, column: 24] (through reference chain: net.opentsdb.core.TSQuery[\"m\"])\n\tat com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:79) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:579) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:672) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:906) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:328) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2793) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1943) ~[jackson-databind-2.1.5.jar:2.1.5]\n\tat net.opentsdb.utils.JSON.parseToObject(JSON.java:108) ~[tsdb-2.0.0.jar:]\n\t... 38 common frames omitted\n"}}

我简直要崩溃了。

#3


看错误提示还是json有问题,换个最简单的json试试呢,比如:{"name":"1111"}

#4


是你的json数据组成方式,不对,具体可以参考 http://opentsdb.net/docs/build/html/api_http/put.html
下面我写的一个 
curl -d '{"start": 1460451066,"queries": [{"aggregator": "sum","metric": "mysql.bytes_sent","tags": {"host": "*"}}]}' http://127.0.0.1:4242/api/query