How do I call ubus's listen method using http requests in jsonrpc format - json-rpc

ubus have call and listen method
for example:
use call:
command line format: ubus call poe restart
jsonrpc format:
{
"jsonrpc": "2.0",
"id": 1,
"method": "call",
"params": [
"dfe453285ef10443532f4949ec992fe7",
"poe",
"restart",
{}
]
}
question:
When the method is 'listen',How should jsonrpc be written?
command line format: ubus listen poe restart
jsonrpc format: ???
please help me. thanks

Related

Web Socket Message Exchange Achitectire

I have web browser application which is connecting to Web Socket endpoint and receive some data from server.
Dy data structure looks like:
channel_1
channel_2
channel_3
channel_4
There could be two approaches, first:
Create separate ws connection for each channel:
wss://myhost/socket/channel_1
wss://myhost/socket/channel_2
wss://myhost/socket/channel_3
wss://myhost/socket/channel_4
Or second approach, single ws connection, but multiple channels type inside JSON:
wss://myhost/socket
{
"channel": "channel_1"
"payload": ...
}
{
"channel": "channel_2"
"payload": ...
}
{
"channel": "channel_3"
"payload": ...
}
Which is better?

How to Send & receive Websocket client Requests, responses in JSON format in PowerShell

With the below PowerShell commands, I am able to open the connection using Websocket.
$mySock = New-Object System.Net.WebSockets.ClientWebSocket
$CT = New-Object System.Threading.CancellationToken($false)
$CTS = New-Object System.Threading.CancellationTokenSource
$mySock.Options.UseDefaultCredentials = $true
$connectTask = $mySock.ConnectAsync($URL, $CTS.Token)
The result as below:
PS C:\PS1> $mySock
Options : System.Net.WebSockets.ClientWebSocketOptions
CloseStatus :
CloseStatusDescription :
SubProtocol :
State : Open
PS C:\PS1>
My question is how do I send a request JSON format & also expect to receive the response in JSON format? Any help/guide would be much appreciated.
The document states as below:
LOGIN Request
When WebSocket connection is opened, the first command to the Streamer Server must be a LOGIN command with the following parameters.
Sample login request:
{
"service": "ADMIN",
"requestid": "1",
"command": "LOGIN",
"account": "your_account",
"source": "your_source_id",
"parameters": {
"token": "027363a5a5acd5",
"version": "1.0",
"credential": "userid%3DMYUSER20%26token%abcd"
}
}
Sample login successful response:
{
"response": [
{
"service": "ADMIN",
"requestid": "1",
"command": "LOGIN",
"timestamp": 1400607506478,
"content": {
"code": 0,
"msg": "02-1"
}
}
]
}
Not sure if you figured it out yet or not. How I did it was to convert the logon request to JSON and encode it in UTF8 and put it into an 1024 bit array segment using example code I found. Then I send it asynchronously back to the target.
$Array = [byte[]] #(,0) * 1024
$Command = [System.Text.Encoding]::UTF8.GetBytes($LoginJSON)
$Send = New-Object System.ArraySegment[byte] -ArgumentList #(,$Command)
$Conn = $WS.SendAsync($Send, [System.Net.WebSockets.WebSocketMessageType]::Text, $True, $CT)
When you receieve the response it must also be an array segement data type.
$Recv = New-Object System.ArraySegment[byte] -ArgumentList #(,$Array)
$Conn = $WS.ReceiveAsync($Recv, $CT)
This line will then get the return
[System.Text.Encoding]::utf8.GetString($Recv.array)
To keep getting all future messages I then created a Do until loop that continues to run until the web socket is no longer open.

Drools stateful session per request

We are trying to use Drool as our rule engine service. What we done till now is listed below
Deployed workbench 7.2.Final
Deployed KIE server 7.2.0.Final
Configured some data objects, rules, deployed the changes to KIE server and we are able to execute the rule using rest API
Most of our requirements satisfied by stateless session (Give a set of data, execute the rule and return the data, that's it) . But using stateless we have to compromise many of the important features provided by Drools stateful session.
So we are trying to use stateful session per request. Which means the session should get disposed as soon as the request end. Also, parallel request should not interfere each other even if the session name is same
We found about container runtime strategy configuration (Workbench > Deploy > {any container} > Process Configuration > Runtime strategy)
But even after configure the container strategy to Per Request, it still behave same as Singleton (the session is not getting disposed after each request)
Few place we read it as, run time strategy only implemented in jBPM
The way we make request to KIE server is shown below
Request: POST {HOST}/kie-server/services/rest/server/containers/instances/TestRequest_1.0.4
{
"lookup": "ab-session", //stateful session
"commands": [
{
"insert": {
"out-identifier": "125",
"object": {
"com.myteam.testrequest.Product": {
"id": "123",
"name": "Hoo Hoo",
"count": 0
}
},
"return-object": "true"
}
},
{
"insert": {
"out-identifier": "126",
"object": {
"com.myteam.testrequest.Product": {
"id": "123",
"name": "Hoo Hoo",
"count": 0
}
},
"return-object": "true"
}
},
{"fire-all-rules": "hf2"}
]
}
We need help in achieving this requirement. Also, please help understand if we done something wrong
In kmodule.xml you may try to add "prototype" scope, because default is "singleton":
<ksession name="SessionName" type="stateful" default="false" clockType="realtime" scope="prototype"/>

Sensu remediation does not work

I have configured the following check:
"cron": {
"command": "check-process.rb -p cron",
"subscribers": [],
"handlers": [
"mailer",
"flowdock",
"remediator"],
"interval": 10,
"occurences": 3,
"refresh": 600,
"standalone": false,
"remediation": {
"light_remediation": {
"occurrences": [1, 2],
"severities": [2]
}
}
},
"light_remediation": {
"command": "touch /tmp/test",
"subscribers": [],
"handlers": ["flowdock"],
"publish": false,
"interval": 10
},
Mailer and flowdock handlers are being executed as expected, so I am receiving e-mails and flowdock notifications when cron service is not running. The problem is that remediator check is not working and I have no idea why. I have used this: https://github.com/nstielau/sensu-community-plugins/blob/remediation/handlers/remediation/sensu.rb
I ran into similar issues but finally managed to get it working with some modifications.
First off, the gotchas:
Each server (client.json.template) needs to subscribe to a channel $HOSTNAME
"subscribers": ["$HOSTNAME"],
You don't have a "trigger_on" section, which is in the code but not the example and you want to set that up to trigger on the $HOSTNAME as well.
my_chek.json.template
"trigger_on": ["$HOSTNAME"]
The remediation checks need to subscribe to $HOSTNAME as well (so you need to template the checks out as well)
"subscribers": ["$HOSTNAME"],
At this point, you should be able to trigger your remediation from the sensu server manually.
Lastly, the example code listed in sensu.rb is broken... The occurrences check needs to be up one level in the loop, and the trigger_on is not inside the remediations section, it's outside.
subscribers = #event['check']['trigger_on'] ? [#event['check']['trigger_on']].flatten : [client]
...
# Check remediations matching the current severity
next unless (conditions["severities"] || []).include?(severity)
remediations_to_trigger << check
end
end
remediations_to_trigger
end
After that, it should work for you.
Oh, and one last gotcha. In your client.json.template
"safe_mode": true
It defaults to false...

Installing Slack Plugin to Sensu NON-enterprise edition

I have Sensu running and followed the instructions the best I could to install the Slack plugin. I'm attempting to just do a "hello-world" to get started, but the documentation seems lacking to me.
I followed the "getting started" with checks:
https://sensuapp.org/docs/0.20/getting-started-with-checks
and everything seems to be in the correct place on the server.
I am attempting to install the following community plugin, but they have a catch-all instruction for all community plugins. There is a json file in the plugin instructions, but doesn't say where to put it...
https://github.com/sensu-plugins/sensu-plugins-slack
Here is what my check_cron.json looks like ( I tried 2 methods, 1 from another source other than Sensu):
{
"checks": {
"cron_checks": {
"handlers": ["default", "slack"],
"command": "/etc/sensu/plugins/check-procs.rb -p cron -C 1 ",
"interval": 60, "subscribers": ["webservers"]
},
"cron": {
"handlers": ["default", "slack"],
"command": "/etc/sensu/plugins/check-procs.rb -p cron",
"subscribers": [
"production",
"webservers",
],
"interval": 60
}
}
}
I have restarted my server after making the changes. I'm assuming that this cron will hit every minute and call the slack notification plugin, but don't know what I'm missing, or where to put the .json doc from the Slack plugin "documentation"
https://github.com/sensu-plugins/sensu-plugins-slack
Any help getting me to the right direction?
You need a handler on the Sensu Server that will fire the request to Slack. Have you created that? If yes, please post it's content.
So I just solved this. benishkey did provide the solution in the link, however, just in case anyone comes across this and the link is broken, I thought I would add the solution.
-github user eugene-chow:
The Slack handler's config need to be named differently. Try the JSON below. I renamed the Slack config for each environment, and then pointed the handler to the respective config with -j config_name
{
"handlers": {
"slack-staging": {
"type": "pipe",
"command": "/usr/local/bin/handler-slack.rb -j slack-staging",
"severites": ["critical", "unknown"]
}
},
"slack-staging": {
"webhook_url": "https://hooks.slack.com/services/...",
"template" : ""
}
}
{
"handlers": {
"slack-production": {
"type": "pipe",
"command": "/usr/local/bin/handler-slack.rb -j slack-production",
"severites": ["critical", "unknown"]
}
},
"slack-production": {
"webhook_url": "https://hooks.slack.com/services/...",
"template" : ""
}
}
I dropped the handler-slack.rb file in with my checks and referenced it from there because it wasn't in my /usr/local/bin/ folder
I was facing the same issue, so the answer is already given but maybe help someone in the future,
First, install sensu slack plugin
/opt/sensu/embedded/bin/gem install sensu-plugins-slack
Then, Create a handler config file
vim /etc/sensu/conf.d/slack-handler.json
handler-slack.rb https://github.com/sensu-plugins/sensu-plugins-slack/blob/master/bin/handler-slack.rb
{
"handlers": {
"slack": {
"type": "pipe",
"command": "/opt/sensu/embedded/bin/handler-slack.rb",
"severites": ["critical", "unknown"]
}
},
"slack": {
"webhook_url": "https://your_webhook.com/abc",
"template" : ""
}
}
I found the answer in the "issues" section in Git
https://github.com/sensu-plugins/sensu-plugins-slack/issues/7