Invalid JSON response from cPanel API - json

I am using the cPanel UAPI but for some reason I am not receiving a valid JSON response with the additional curly braces and other quotations. I have tried different commands and on different servers and it's the same thing so I'm sure it's something simple I'm missing. Here is the doc I am following: https://api.docs.cpanel.net/openapi/cpanel/operation/query/
I've worked with other JSON APIs and I've never seen this before so is it cPanel specific and how would I parse this?:
---
apiversion: 3
func: query
module: Bandwidth
result:
data:
example.com:
"1572580800": '2978386611'
"1575176400": '3017940650'
"1577854800": '1272949062'
"1580533200": '1305954799'
"1583038800": '1072552621'
"1585713600": '1080945766'
"1588305600": '1188271342'
"1590984000": '1209022420'
"1593576000": 825711372
"1596254400": 771882286
"1598932800": '1126013108'
"1601524800": '1739977967'
"1604203200": '1225180686'
"1606798800": '1260268241'
"1609477200": '1503779339'
"1612155600": 985363356
"1614574800": 289737623
subdomain.example.com:
"1572580800": 4054128
"1575176400": 4187711
"1577854800": 83997477
"1580533200": 239566187
"1583038800": 413164665
"1585713600": 247957953
"1588305600": 239197568
"1590984000": 237783617
"1593576000": 304554763
"1596254400": 255965489
"1598932800": 251708858
"1601524800": 264273039
"1604203200": 138803673
"1606798800": 121939910
"1609477200": 68686277
"1612155600": 74725743
"1614574800": 19763737
UNKNOWN:
"1572580800": 13288313
"1575176400": 6326053
"1577854800": 17962542
"1580533200": 9835755
"1583038800": 22566655
"1598932800": 25137467
"1604203200": 31783967
"1606798800": 28088240
"1609477200": 22907243
"1612155600": 21413429
"1614574800": 2085610
subdomain1.example.com:
"1604203200": 2107896
"1606798800": 2516570
"1609477200": 3454393
"1612155600": 3216589
"1614574800": 795065
subdomain2.example.com:
"1580533200": 557880
"1583038800": 440351
"1585713600": 183146
"1588305600": 1047856
"1590984000": 415107
"1593576000": 342764
"1596254400": 5887618
"1598932800": 429651
errors: ~
messages: ~
metadata: {}
status: 1
warnings: ~ ```

cPanel got back to me and all I needed to do was to add --output=jsonpretty at the end.

Related

I can't use Get Value From Json with JsonPAth parameter in Robot Framework - always have a KeyError

When I try to use "Get Value From Json" on a Json with specific JsonPath , I always have KeyError
even web simple example doesn't work for me...
When I try that code :
Library JSONLibrary
*** Test Cases ***
Example
${tmp} = Convert String To JSON {"foo": {"bar": [1,2,3]}}
Log to console tmp: ${tmp}
${values_test}= Get Value From Json ${tmp} $.foo.bar
Log to console values_test: ${values_test}
I always have this kind of Errors and log :
tmp: {'foo': {'bar': [1, 2, 3]}}
...
Resolving variable '${tmp['$.foo.bar']}' failed: KeyError: '$.foo.bar'
Can somebody Help me please ?
it is really basic example by the way and community always says that it works like that in comments..

flatten json with logstash ruby

I have a json log that expands to this:
JSON:
|-host : hostname
|-httpRequest
|-httpVersion : HTTP/1.1
|-headers
|-0
|-name: X-Forwarded-For
|-value: 1.1.1.1
|-1
|-name: X-Forwarded-Prot
|-value: https
|-2
|-name: X-Forwarded-Port
|-value: 443
|-httpMethod: post
|-action: allow
etc..
I would like to reformat it like this:
JSON:
|-host : hostname
|-httpRequest
|-httpVersion : HTTP/1.1
|-headers
|-X-Forwarded-For : 1.1.1.1
|-X-Forwarded-Prot : https
|-X-Forwarded-Port : 443
|-httpMethod: post
|-action: allow
Split will just take the last [#] name/value as it overwrites the previous.
I am pretty sure this will need a ruby code block, but I haven't had luck following along with ruby code I have found online for similar scenarios.
I think the main issue here/difference with other article/answers is that it's not just a pure flatten. But need to rearrange the name:value a bit as well.
This seems to do the trick as well:
Newfield
ruby {
code => '
event.get("[#metadata][json][httpRequest][headers]").each do |header|
event.set("[newfield][#{header["name"]}]", header["value"])
end
'
}
Same field
ruby {
code => '
headerHash = {}
event.get("[#metadata][json][httpRequest][headers]").each do |header|
headerHash[header["name"]] = header["value"]
end
event.set("[#metadata][json][httpRequest][headers]", headerHash)
'
}

Parse JSON with missing fields using cjson Lua module in Openresty

I am trying to parse a json payload sent via a POST request to a NGINX/Openresty location. To do so, I combined Openresty's content_by_lua_block with its cjson module like this:
# other locations above
location /test {
content_by_lua_block {
ngx.req.read_body()
local data_string = ngx.req.get_body_data()
local cjson = require "cjson.safe"
local json = cjson.decode(data_string)
local endpoint_name = json['endpoint']['name']
local payload = json['payload']
local source_address = json['source_address']
local submit_date = json['submit_date']
ngx.say('Parsed')
}
}
Parsing sample data containing all required fields works as expected. A correct JSON object could look like this:
{
"payload": "the payload here",
"submit_date": "2018-08-17 16:31:51",
},
"endpoint": {
"name": "name of the endpoint here"
},
"source_address": "source address here",
}
However, a user might POST a differently formatted JSON object to the location. Assume a simple JSON document like
{
"username": "JohnDoe",
"password": "password123"
}
not containing the desired fields/keys.
According to the cjson module docs, using cjson (without its safe mode) will raise an error if invalid data is encountered. To prevent any errors being raised, I decided to use its safe mode by importing cjson.safe. This should return nil for invalid data and provide the error message instead of raising the error:
The cjson module will throw an error during JSON conversion if any invalid data is encountered. [...]
The cjson.safe module behaves identically to the cjson module, except when errors are encountered during JSON conversion. On error, the cjson_safe.encode and cjson_safe.decode functions will return nil followed by the error message.
However, I do not encounter any different error handling behavior in my case and the following traceback is shown in Openresty's error.log file:
2021/04/30 20:33:16 [error] 6176#6176: *176 lua entry thread aborted: runtime error: content_by_lua(samplesite:50):16: attempt to index field 'endpoint' (a nil value)
Which in turn results in an Internal Server Error:
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>openresty</center>
</body>
</html>
I think a workaround might be writing a dedicated function for parsing the JSON data and calling it with pcall() to catch any errors. However, this would make the safe mode kind of useless. What am I missing here?
Your “simple JSON document” is a valid JSON document. The error you are facing is not related to cjson, it's a standard Lua error:
resty -e 'local t = {foo = 1}; print(t["foo"]); print(t["foo"]["bar"])'
1
ERROR: (command line -e):1: attempt to index field 'foo' (a number value)
stack traceback:
...
“Safeness” of cjson.safe is about parsing of malformed documents:
cjson module raises an error:
resty -e 'print(require("cjson").decode("[1, 2, 3"))'
ERROR: (command line -e):1: Expected comma or array end but found T_END at character 9
stack traceback:
...
cjson.safe returns nil and an error message:
resty -e 'print(require("cjson.safe").decode("[1, 2, 3"))'
nilExpected comma or array end but found T_END at character 9

Postgrex how to define json library

I'm just trying to use Postgrex without any kind of ecto setup, so just the example from the documentation readme.
Here is what my module looks like:
defmodule Receive do
def start(_type, _args) do
{:ok, pid} = Postgrex.start_link(
hostname: "localhost",
username: "john",
# password: "",
database: "property_actions",
extensions: [{Postgrex.Extensions.JSON}]
)
Postgrex.query!(
pid,
"INSERT INTO actions (search_terms) VALUES ($1)",
[
%{foo: 'bar'}
]
)
end
end
when I run the code I get
** (RuntimeError) type `json` can not be handled by the types module Postgrex.DefaultTypes, it must define a `:json` library in its options to support JSON types
Is there something I'm not setting up correctly? From what I've gathered in the documentation, I shouldn't even need to have that extensions line because json is handled by default.
On Postgrex <= 0.13, you need to define your own types:
Postgrex.Types.define(MyApp.PostgrexTypes, [], json: Poison)
and then when starting Postgrex:
Postgrex.start_link(types: MyApp.PostgrexTypes)
On Postgrex >= 0.14 (currently master), it was made easier:
config :postgrex, :json_library, Poison

RabbitMQ Shovel config with Alternate-Exchange

I'm trying to configure the Shovel plugin for RabbitMQ with a list of declarations. I have configured my remote exchange to have an alternate-exchange when I created it.
My problem is that I can't get the config file for shovel to include this argument so RabbitMQ crashes upon startup.
This is what my config looks like:
[
{mnesia, [{dump_log_write_threshold, 100}]},
{rabbit, [{vm_memory_high_watermark, 0.4}]},
{rabbitmq_shovel,
[{shovels,
[{call_stats_shovel,
[{sources, [{broker, "amqp://guest:guest#localhost:5672/test"},
{declarations,
[{'queue.declare', [{queue, <<"incoming">>}, durable]},
{'exchange.declare',[{exchange, <<"my-exchange-topic">>},{type, <<"topic">>},durable]},
{'queue.bind',[{exchange, <<"my-exchange-topic">>},{queue, <<"incoming">>}]}
]}]},
{destinations, [{broker, "amqp://guest:guest#172.16.3.162:5672/blah"},
{declarations,
[
{'queue.declare',[{queue, <<"billing">>},durable]},
{'exchange.declare',[{exchange, <<"my-exchange-topic">>},{type, <<"topic">>},{alternate_exchange, <<"alt">>}, durable]},
{'queue.bind',[{exchange, <<"my-exchange-topic">>},{queue, <<"billing">>},{routing_key, <<"physical">>}]}
]}
]},
{queue, <<"incoming">>},
{ack_mode, no_ack},
{publish_properties, [{delivery_mode, 2}]},
{reconnect_delay, 5}
]}
]
}]
}
].
The problem is on the destination exchange called my-exchange-topic. If I take out the declarations section then the config file works.
This is the error:
=INFO REPORT==== 31-Jul-2012::12:15:25 ===
application: rabbitmq_shovel
exited: {{invalid_shovel_configuration,call_stats_shovel,
{invalid_parameter_value,destinations,
{unknown_fields,'exchange.declare',
[alternate_exchange]}}},
{rabbit_shovel,start,[normal,[]]}}
type: permanent
If I leave the alternate_exchange section out of the declaration I get this error in RabbitMQ web management:
{{shutdown,
{server_initiated_close,406,
<<"PRECONDITION_FAILED - inequivalent arg 'alternate-exchange'for exchange 'my-exchange-topic' in vhost 'blah':
received none but current is the value 'alt' of type 'longstr'">>}},
{gen_server,call,
[<0.473.0>,
{call,
{'exchange.declare',0,<<"my-exchange-topic">>,<<"topic">>,false,
true,false,false,false,[]},
none,<0.444.0>},
infinity]}}
For anyone looking at how to configure exchanges and queues that require additional arguments you do it like this:
{'exchange.declare',[{exchange, <<"my-exchange-topic">>},{type, <<"topic">>}, durable, {arguments, [{<<"alternate-exchange">>, longstr, <<"alternate-exchange">>}]} ]},
you can do a similar thing with queues:
{'queue.declare',[{queue, <<"my-queue">>},durable, {arguments, [{<<"x-dead-letter-exchange">>, longstr, <<"dead-letter-queue">>}]}]}
For clarification of the comment above, in case of an exchange2exchange shovel, the config would be:
{'exchange.declare',[{exchange, <<"my-exchange-topic">>},{type, <<"topic">>}, durable, {arguments, [{<<"alternate-exchange">>, longstr, <<"name-of-your-alternate-exchange">>}]} ]},