Select elements from json array based on 2 criteria - json

I am fetching the list of issue comments on a specific PR with the following curl command
curl -sH "Accept: application/vnd.github+json" -H "Authorization: token XXXXXXXXXX" https://api.github.com/repos/org/repo/issues/12/comments
The response format is according to the documentation as follows:
[
{
"id": 1,
"node_id": "MDEyOklzc3VlQ29tbWVudDE=",
"url": "https://api.github.com/repos/octocat/Hello-World/issues/comments/1",
"html_url": "https://github.com/octocat/Hello-World/issues/1347#issuecomment-1",
"body": "Me too",
"user": {
"login": "octocat",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2011-04-14T16:00:49Z",
"updated_at": "2011-04-14T16:00:49Z",
"issue_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347",
"author_association": "COLLABORATOR"
}
]
I know that I can get the last comment's author's username by piping the output to jq '.[-1].user.login'
How can I get from ALL the comments matching user.login=foobar the most recent one?

select is the function that filters by criteria.
Use with map to retain the array structure
jq 'map(select(.user.login == "foobar"))'
Or iterate and get just the matching items
jq '.[] | select(.user.login == "foobar")'

Related

Parse json response from curl in github action

I have the curl request as shown below and also attached the response of curl request from the response how can I fetch the value which is under "status.azure.resource_name" and store it in some variable as I'm new to GitHub action facing some challenges if this is some programming api I could have resolved it
Request:
curl --location --request PUT $URL \
--header "$AUTH_HEADER" \
--header 'Content-Type: application/json' \
--data-raw "$PAYLOAD"
Response:
{
"labels": {},
"spec": {
"mysql": {
"version": "8.0",
"sku": {
"name": "GP_Gen5_4"
},
"storage_profile": {
"storage_mb": 5120
}
},
"key_vault": {
"access_policies": [
{
"name": "test",
"type": "group",
"project": null
}
]
}
},
"type": "azure-mysql",
"name": "mysql",
"id": "1234",
"created_at": "2012-03-04T10:00:05+00:00",
"updated_at": "2012-03-04T10:00:05+00:00",
"project": {
"id": "ae3dfa99",
"name": "Test",
"url": "www.google.com",
"geography": "in"
},
"links": {
"key_vault": {
"endpoint": {
"url": "",
"description": "test.",
"display_name": "test"
},
"azure_portal": {
"url": "test",
"description": "Link to the resource in the Azure portal.",
"display_name": "Key Vault Azure Portal"
}
},
"azure_portal": {
"url": "test",
"description": "Link to the resource in the Azure portal.",
"display_name": "Azure Portal"
},
"endpoint": {
"url": "test",
"description": "Azure resource endpoint.",
"display_name": "test"
}
},
"url": "test",
"tags": {
"cost_center_id": "471000",
"customer": "internal",
"product_group": "internal",
"environment_type": "test",
"budget_category": "",
"team": ""
},
"spiffe_id": "test",
"status": {
"ready": false,
"state": "reconciling",
"deployment": {
"steps": {}
},
"azure": {
"resource_name": "test",
"id": null,
"subscription_id": "test",
"resource_group": "test"
},
"key_vault": {
"access_policies": []
}
}
}
There are multiple ways to achieve that.
You can use directly jq and read variable in bash step.
You can use one of existing open source actions from marketplace:
- name: get nested property
id: format_script
uses: notiz-dev/github-action-json-property#release
with:
path: 'yourjson.json'
prop_path: 'status.azure.resource_name'
- run: echo ${{steps.format_script.outputs.prop}}
Download just one property from whole JSON if you don't need a file itself:
- uses: senmu/download-json-property-action#v1.0.0
with:
url: 'https://httpbin.org/json'
property_path: status.azure.resource_name

Orion-LD issues no notification send to subscription

Update 2
The issues might be the use of the the Orion-LD (1.0.0), when i use the 0.8.0 all the example is working.
https://github.com/kosgobakis/orion-ldSubcription
My setup is:
The Things Network (LoraWAN)
IoT Agent for LoraWAN supporting TTN and NGSI-LD GitHub
Orion-LD
Quantumleap + cratedb
Minimal web server to provide JSONLD
Provision of the sensor to the IoT Agent
curl -iL -X POST \
localhost:4041/iot/devices \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /' \
-d '{
"devices": [
{
"device_id": "eui-70b3d57ed004a537",
"entity_name": "urn:ngsi-ld:TemperatureSensor:temperaturesensor001",
"entity_type": "LoraDevice",
"timezone": "Europe/Athens",
"attributes": [
{
"object_id": "temperature_3",
"name": "temperatureCh3",
"type": "Float",
"metadata": {"unitCode": {"type": "Property","value": "CEL"}}
},
{
"object_id": "temperature_5",
"name": "temperatureCh5",
"type": "Float",
"metadata": {"unitCode": {"type": "Property","value": "CEL"}}
}
],
"internal_attributes": {
"lorawan": {
"application_server": {
"host": "eu1.cloud.thethings.network",
"username": "XXXX#ttn",
"password": "XXXX",
"provider": "TTN"
},
"dev_eui": "XXXXX",
"app_eui": "XXXXXX",
"application_id": "XXXXXX#ttn",
"application_key": "XXXXXX",
"data_model": "application_server"
}
},
"protocol": "LORAJSON"
}
]
}'
I successfully receive data from the TTN Network (Simulation of sensor at TTN->IoTAgent->Orion-LD)
curl -X GET 'http://localhost:1026/ngsi-ld/v1/entities/?type=LoraDevice' \
-H 'fiware-service: openiot' \
-H 'fiware-servicepath: /' \
-H 'Link: <http://context/ngsi-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
-H 'Accept: application/json' |jq .
[
{
"id": "urn:ngsi-ld:TemperatureSensor:temperaturesensor001",
"type": "LoraDevice",
"temperatureCh3": {
"type": "Property",
"value": 27.2,
"observedAt": "2022-01-04T18:54:14.447Z",
"unitCode": "CEL"
},
"temperatureCh5": {
"type": "Property",
"value": 25.5,
"observedAt": "2022-01-04T18:54:14.447Z",
"unitCode": "CEL"
}
}
]
I use the following curl to create a new subscription to the Orion-LD
curl -L -X POST 'http://localhost:1026/ngsi-ld/v1/subscriptions/' \
-H 'Content-Type: application/ld+json' \
-H 'NGSILD-Tenant: openiot' \
--data-raw '{
"description": "Notify me of all changes",
"type": "Subscription",
"entities": [{"type": "LoraDevice"}],
"notification": {
"attributes": ["temperatureCh3"],
"format": "normalized",
"endpoint": {
"uri": "http://quantumleap:8668/v2/notify",
"accept": "application/json"
}
},
"#context": "http://context/ngsi-context.jsonld"
}'
and verify that i have create the subscription
curl -X GET 'http://localhost:1026/ngsi-ld/v1/subscriptions/' \
-H 'NGSILD-Tenant: openiot'|jq .
{
"id": "urn:ngsi-ld:Subscription:61d53fc5fb688acae04cd92f",
"type": "Subscription",
"description": "Notify me of all changes",
"entities": [
{
"type": "LoraDevice"
}
],
"notification": {
"attributes": [
"temperatureCh3"
],
"format": "normalized",
"endpoint": {
"uri": "http://quantumleap:8668/v2/notify",
"accept": "application/json"
}
},
"#context": "http://context/ngsi-context.jsonld"
}
]
Send new measurement (Orion-LD log: Servicing NGSI-LD request 006: POST /ngsi-ld/v1/entityOperations/upsert/), check that the Orion-LD has the new data, but no notification send to the quantumleap
[
{
"id": "urn:ngsi-ld:TemperatureSensor:temperaturesensor001",
"type": "LoraDevice",
"temperatureCh3": {
"type": "Property",
"value": 27.3,
"observedAt": "2022-01-05T06:53:17.572Z",
"unitCode": "CEL"
},
"temperatureCh5": {
"type": "Property",
"value": 25.5,
"observedAt": "2022-01-05T06:53:17.572Z",
"unitCode": "CEL"
}
}
]
I have spend some time changing the subscription request but with no success.
Is something wrong with my subscription request?
Using MongoDB Compass here is the context of the DB
orion-openiot.entities
{
"_id": {
"id": "urn:ngsi-ld:TemperatureSensor:temperaturesensor001",
"type": "https://ngsi-ld-tutorials.readthedocs.io/en/latest/datamodels.html#loraSensors",
"servicePath": "/"
},
"attrNames": ["https://ngsi-ld-tutorials.readthedocs.io/en/latest/datamodels.html#temperature1", "https://ngsi-ld-tutorials.readthedocs.io/en/latest/datamodels.html#temperature2"],
"attrs": {
"https://ngsi-ld-tutorials=readthedocs=io/en/latest/datamodels=html#temperature1": {
"value": 27.3,
"type": "Property",
"md": {
"unitCode": {
"value": "CEL"
},
"observedAt": {
"value": 1641365597.572
}
},
"mdNames": ["unitCode", "observedAt"],
"creDate": 1641365539.6579854,
"modDate": 1641365597.5791261
},
"https://ngsi-ld-tutorials=readthedocs=io/en/latest/datamodels=html#temperature2": {
"value": 25.5,
"type": "Property",
"md": {
"unitCode": {
"value": "CEL"
},
"observedAt": {
"value": 1641365597.572
}
},
"mdNames": ["unitCode", "observedAt"],
"creDate": 1641365539.6579854,
"modDate": 1641365597.5791261
}
},
"creDate": 1641365441.9127274,
"modDate": 1641365597.5791261,
"lastCorrelator": ""
}
orion-openiot.csubs
{
"_id": "urn:ngsi-ld:Subscription:61d53fc5fb688acae04cd92f",
"expiration": 2147483647,
"reference": "http://quantumleap:8668/v2/notify",
"custom": false,
"mimeType": "application/json",
"throttling": 0,
"servicePath": "/",
"description": "Notify me of all changes",
"status": "active",
"entities": [{
"id": "",
"isPattern": "",
"type": "https://ngsi-ld-tutorials.readthedocs.io/en/latest/datamodels.html#loraSensors",
"isTypePattern": false
}],
"attrs": ["https://ngsi-ld-tutorials.readthedocs.io/en/latest/datamodels.html#temperature1"],
"metadata": [],
"blacklist": false,
"ldContext": "http://context/ngsi-context.jsonld",
"createdAt": 1641365445.068897,
"modifiedAt": 1641365445.068897,
"conditions": [],
"expression": {
"q": "",
"mq": "",
"geometry": "",
"coords": "",
"georel": "",
"geoproperty": ""
},
"format": "normalized"
}
Update 1
I run the latest version of the IoTAgent
{"libVersion":"2.18.0-next","port":"4041","baseRoot":"/"}
and a minimal web server to provide JSONLD (http://context/xxxx.jsonld)
When created a minimal jsonld file like you suggested.
The IoT Agent send the following POST to Orion-LD
{
"url": "http://orion:1026/ngsi-ld/v1/entityOperations/upsert/?options=update",
"method": "POST",
"headers": {
"fiware-service": "openiot",
"fiware-servicepath": "/",
"Content-Type": "application/ld+json",
"NGSILD-Tenant": "openiot",
"NGSILD-Path": "/"
},
"json": [
{
"#context": "http://context/minimal.jsonld",
"temperatureCh3": {
"type": "Property",
"value": 436.8,
"unitCode": "CEL",
"observedAt": "2022-01-07T15:24:28.273Z"
},
"temperatureCh5": {
"type": "Property",
"value": 25.5,
"unitCode": "CEL",
"observedAt": "2022-01-07T15:24:28.273Z"
},
"id": "urn:ngsi-ld:TemperatureSensor:temperaturesensor001",
"type": "LoraDevice"
}
]
}
When i run
curl -G -X GET 'http://localhost:1026/ngsi-ld/v1/entities/urn:ngsi-ld:TemperatureSensor:temperaturesensor001' \
-H 'fiware-service: openiot' \
-H 'Link: <http://context/minimal.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
-H 'Accept: application/ld+json' | jq .
{
"#context": "http://context/minimal.jsonld",
"id": "urn:ngsi-ld:TemperatureSensor:temperaturesensor001",
"type": "LoraDevice",
"temperatureCh3": {
"value": 436.8,
"type": "Property",
"unitCode": "CEL",
"observedAt": "2022-01-07T15:24:28.273Z"
},
"temperatureCh5": {
"value": 25.5,
"type": "Property",
"unitCode": "CEL",
"observedAt": "2022-01-07T15:24:28.273Z"
}
}
And when i do not supply the correct jsonld file
curl -L -X GET \
'http://localhost:1026/ngsi-ld/v1/entities/urn:ngsi-ld:TemperatureSensor:temperaturesensor001' \
-H 'fiware-service: openiot' \
-H 'Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.4.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
-H 'Accept: application/ld+json'| jq .
i get the full URI
{
"#context": "https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.4.jsonld",
"id": "urn:ngsi-ld:TemperatureSensor:temperaturesensor001",
"type": "https://example.com/LoraDevice",
"https://example.com/ch3": {
"value": 436.8,
"type": "Property",
"unitCode": "CEL",
"observedAt": "2022-01-07T15:24:28.273Z"
},
"https://example.com/ch5": {
"value": 25.5,
"type": "Property",
"unitCode": "CEL",
"observedAt": "2022-01-07T15:24:28.273Z"
}
}
The first thing to check is whether the IoT Agent version you are using is supporting NGSI-LD:
curl -X GET \
'http://localhost:4041/iot/about'
Should return:
{
"libVersion": "2.6.0-next",
"port": "4041",
"baseRoot": "/",
"version": "1.6.0-next"
}
The lib-version needs to be 2.15.0 or higher to expose NGSI-LD support. If necessary you can rebuild a latest from the Dockerfile in the IoTagent-LoRaWAN repo to make sure you have all the latest bug fixes from the IoT Agent node lib.
But I think the missing link here is the local #context file you need to provide to supply NGSI-LD data (and indeed any JSON-LD data). This file defines your world in terms of IRIs.
Your LoRaWAN IoT Agent config.js will need an entry to some file on a public server like:
{
contextBroker: {
host: '192.168.1.1',
port: '1026',
ngsiVersion: 'ld',
jsonLdContext: 'http://path/to/your/context.json-ld'
},
Or IOTA_CB_NGSI_VERSION and IOTA_JSON_LD_CONTEXT supplied by environment variables. The file path/to/your/context.json-ld will need to hold key-value entries like:
{
"#context": {
"type": "#type",
"id": "#id",
"ngsi-ld": "https://uri.etsi.org/ngsi-ld/",
"example": "https://example.com/",
"LoraDevice": "example:LoraDevice",
"temperatureCh3": "https://example.com/ch3",
"temperatureCh5": "https://example.com/ch5"
}
}
i.e. one entry for each entity type and one entry for each attribute.
It looks like your #context file is referring to https://fiware.github.io/data-models/context.jsonld which doesn't hold IRI values for your new property names.
When you are provisioning the subscription:
curl -X GET 'http://localhost:1026/ngsi-ld/v1/subscriptions/' \
-H 'NGSILD-Tenant: openiot'|jq .
{
"id": "urn:ngsi-ld:Subscription:61d53fc5fb688acae04cd92f",
"type": "Subscription",
"description": "Notify me of all changes",
"entities": [
{
"type": "LoraDevice"
}
],
"notification": {
"attributes": [
"temperatureCh3"
],
"format": "normalized",
"endpoint": {
"uri": "http://quantumleap:8668/v2/notify",
"accept": "application/json"
}
},
"#context": "http://path/to/your/context.json-ld"
}
]
The attribute names defined in http://path/to/your/context.json-ld are expanded to IRIs. If no matching entry is found then the default context https://uri.etsi.org/ngsi-ld/default-context/ is used (as defined by the #vocab element) , which is not what you want. Define temperatureCh3 and LoraDevice properly and the subscription will work.
As a check, try retrieving an entity without supplying any of your terms in the #context
curl -L -X GET \
'http://localhost:1026/ngsi-ld/v1/entities/urn:ngsi-ld:TemperatureSensor:temperaturesensor001' \
-H 'Link: <https://uri.etsi.org/ngsi-ld/v1/ngsi-ld-core-context-v1.4.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
-H 'Accept: application/ld+json'
All attributes (which are not defined in the core #context) should now return using IRIs - if short names are returned, then the relevant mapping is missing.
I tried to reproduce the problem, pushed a new functest to Orion-LD's develop branch for this purpose:
https://github.com/FIWARE/context.Orion-LD/pull/983
I'm unable to reproduce, so, could you please take a look at the functest I implemented (free from iot-agents, QLs, etc - just the broker, a notification receiver, and curl commands) and propose changes. See if we're able to reproduce the error. Cause if not, if I can't reproduce, then I can't fix ...
Here you can see the functest: https://github.com/FIWARE/context.Orion-LD/pull/983/files#diff-921fc6ab850278cc281e7662f6170be2e3474f95a800000779c1dd9c6c377dd6R1

JOLT remove all but first two fields

Trying to use JOLT within Nifi to remove some fields from an API call, but having trouble getting it to work. Below is what is returned from the GitHub API:
[
{
"login": "sample name",
"id": "000001",
"node_id": "ID#",
"avatar_url": "RETURNED_URL",
"gravatar_id": "",
"url": "RETURNED_URL",
"html_url": "RETURNED_URL",
"followers_url": "RETURNED_URL",
"following_url": "RETURNED_URL",
"gists_url": "RETURNED_URL",
"starred_url": "RETURNED_URL",
"subscriptions_url": "RETURNED_URL",
"organizations_url": "RETURNED_URL",
"repos_url": "RETURNED_URL",
"events_url": "RETURNED_URL",
"received_events_url": "RETURNED_URL",
"type": "MEMBER_TYPE",
"site_admin": false
},
...
}
]
This is the JOLT Spec I have that is raising errors:
[
{
"operation": "remove",
"spec": {
"node_id":"",
"avatar_url":"",
"gravatar_id":"",
"url":"",
"html_url":"",
"followers_url": "",
"following_url": "",
"gists_url": "",
"starred_url": "",
"subscriptions_url": "",
"organizations_url": "",
"repos_url": "",
"events_url": "",
"received_events_url": "",
"type": "User",
"site_admin": false
}
}
]
There are about 70 results returned and i need it to do this for each one. None of them have a higher level key, so I thought it was that, but everything I've tried seems to throw a " is invalid because Specification not valid for the selected transformation
Instead or removing long list of fields, you can just pick the fields you need. In your case you can use Jolt Spec like this:
[
{
"operation": "shift",
"spec": {
"*": {
"login": "[&1].login",
"id": "[&1].id"
}
}
}
]
You can also use this jolt demo site to check yourself. This is how i checked it:

Remove comma from between JSON events

I was hoping to get Splunk to break up the content of the pages into events, but it's unable to. I'm trying to sed the comma that's in between the events out, but it's not going well. This is a portion of the json coming in
"last_updated":"2017-02-28T17:56:19Z"},{"id":588699,"name":null,...
and this is the sed line I'm trying
sed -e "s/},{/}+{/" -e "s/}[^}]*$/}/" secunia.txt | tr "+" "\n"
I've put it outside my for loop in the script that barmar helped with, but it's not pulling out the ,. What am I missing?
Here is some of the data:
{"id":588699,"name":null,"status":{"id":2963,"name":"Handled"},"priority":{"id":2873,"name":"Urgent"},"queue":{"id":2144,"name":"Default"},"description":null,"assigned_to":{"id":4120,"username":"user4#company.com"},"asset_list":{"id":4777,"name":"Info Security Threat_Splunk"},"advisory":{"id":199003,"advisory_identifier":"SA74447","title":"Blue Coat Security Analytics Multiple Vulnerabilities","released":"2016-12-21T15:24:53Z","modified_date":"2016-12-21T15:24:53Z","criticality":2,"criticality_description":"Highly critical","solution_status":4,"solution_status_description":"Partial Fix","where":1,"where_description":"From remote","cvss_score":10.0,"cvss_vector":"(AV:N/AC:L/Au:N/C:C/I:C/A:C/E:U/RL:TF/RC:C)","type":0,"is_zero_day":false},"created":"2016-12-21T15:33:09Z","pretty_id":79,"custom_score":null,"last_updated":"2016-12-21T15:40:28Z"},{"id":584252,"name":null,"status":{"id":2963,"name":"Handled"},"priority":{"id":2873,"name":"Urgent"},"queue":{"id":2144,"name":"Default"},"description":null,"assigned_to":{"id":4118,"username":"user3#company.com"},"asset_list":{"id":4657,"name":"PSS Middleware Environment"},"advisory":{"id":195840,"advisory_identifier":"SA73221","title":"Oracle Solaris Multiple Third Party Components Multiple Vulnerabilities","released":"2016-10-19T14:20:02Z","modified_date":"2016-12-19T14:42:30Z","criticality":2,"criticality_description":"Highly critical","solution_status":2,"solution_status_description":"Vendor Patched","where":1,"where_description":"From remote","cvss_score":10.0,"cvss_vector":"(AV:N/AC:L/Au:N/C:C/I:C/A:C/E:U/RL:OF/RC:C)","type":0,"is_zero_day":false},"created":"2016-12-20T13:43:24Z","pretty_id":76,"custom_score":null,"last_updated":"2017-01-11T19:47:09Z"}
Try this command -
sed -e "s/,//g" -e "s/}{/}\n{/" -e "s/}[^}]*$/}/" f
Replacing the event separator works here, this assumes it does not occur elsewhere in the input though. For example:
sed 's/},{/}\n{/' secunia.txt | jq -s .
Or with portable sed:
sed 's/},{/}\
{/' secunia.txt | jq -s .
Output:
[
{
"id": 588699,
"name": null,
"status": {
"id": 2963,
"name": "Handled"
},
"priority": {
"id": 2873,
"name": "Urgent"
},
"queue": {
"id": 2144,
"name": "Default"
},
"description": null,
"assigned_to": {
"id": 4120,
"username": "user4#company.com"
},
"asset_list": {
"id": 4777,
"name": "Info Security Threat_Splunk"
},
"advisory": {
"id": 199003,
"advisory_identifier": "SA74447",
"title": "Blue Coat Security Analytics Multiple Vulnerabilities",
"released": "2016-12-21T15:24:53Z",
"modified_date": "2016-12-21T15:24:53Z",
"criticality": 2,
"criticality_description": "Highly critical",
"solution_status": 4,
"solution_status_description": "Partial Fix",
"where": 1,
"where_description": "From remote",
"cvss_score": 10,
"cvss_vector": "(AV:N/AC:L/Au:N/C:C/I:C/A:C/E:U/RL:TF/RC:C)",
"type": 0,
"is_zero_day": false
},
"created": "2016-12-21T15:33:09Z",
"pretty_id": 79,
"custom_score": null,
"last_updated": "2016-12-21T15:40:28Z"
},
{
"id": 584252,
"name": null,
"status": {
"id": 2963,
"name": "Handled"
},
"priority": {
"id": 2873,
"name": "Urgent"
},
"queue": {
"id": 2144,
"name": "Default"
},
"description": null,
"assigned_to": {
"id": 4118,
"username": "user3#company.com"
},
"asset_list": {
"id": 4657,
"name": "PSS Middleware Environment"
},
"advisory": {
"id": 195840,
"advisory_identifier": "SA73221",
"title": "Oracle Solaris Multiple Third Party Components Multiple Vulnerabilities",
"released": "2016-10-19T14:20:02Z",
"modified_date": "2016-12-19T14:42:30Z",
"criticality": 2,
"criticality_description": "Highly critical",
"solution_status": 2,
"solution_status_description": "Vendor Patched",
"where": 1,
"where_description": "From remote",
"cvss_score": 10,
"cvss_vector": "(AV:N/AC:L/Au:N/C:C/I:C/A:C/E:U/RL:OF/RC:C)",
"type": 0,
"is_zero_day": false
},
"created": "2016-12-20T13:43:24Z",
"pretty_id": 76,
"custom_score": null,
"last_updated": "2017-01-11T19:47:09Z"
}
]

Is it possible to execute system.run[] with Zabbix 3.0 JSON-RPC API?

I'm trying to remotely stop/start services using systemctl inside Zabbix's system.run[] request/item but it doesn't seem to work.
I'm using Zabbix 3.0 JSON-RPC API and my JSON looks like this:
{
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"filter": {
"host": "host-name",
"key_": "system.run[sudo systemctl stop nginx.service]"
}
},
"id": 1,
"auth": "my-token"
}
Result:
{"jsonrpc":"2.0","result":[],"id":1}
But I'm not too sure about validity of this request because all the information I've seen on system.run[] so far was related to zabbix_get. Is it even possible to execute system.run[] this way? What am I doing wrong?
This is obviously just filtering items but I have no idea how to replicate what zabbix_get does using Zabbix JSON-RPC API. There is no information I could find about this.
This works well for gathering data, tho:
{
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"filter": {
"host": "host-name",
"key_": "vm.memory.size[used]"
}
},
"id": 1,
"auth": "my-token"
}
Result:
{
"jsonrpc": "2.0",
"result": [
{
"itemid": "455",
"type": "0",
"snmp_community": "",
"snmp_oid": "",
"hostid": "12241",
"name": "Used memory",
"key_": "vm.memory.size[used]",
"delay": "60",
"history": "90",
"trends": "365",
"status": "0",
"value_type": "3",
"trapper_hosts": "",
"units": "B",
"multiplier": "0",
"delta": "0",
"snmpv3_securityname": "",
"snmpv3_securitylevel": "0",
"snmpv3_authpassphrase": "",
"snmpv3_privpassphrase": "",
"formula": "1",
"error": "",
"lastlogsize": "0",
"logtimefmt": "",
"templateid": "106",
"valuemapid": "0",
"delay_flex": "",
"params": "",
"ipmi_sensor": "",
"data_type": "0",
"authtype": "0",
"username": "",
"password": "",
"publickey": "",
"privatekey": "",
"mtime": "0",
"flags": "0",
"interfaceid": "2",
"port": "",
"description": "",
"inventory_link": "0",
"lifetime": "30",
"snmpv3_authprotocol": "0",
"snmpv3_privprotocol": "0",
"state": "0",
"snmpv3_contextname": "",
"evaltype": "0",
"lastclock": "1466142275",
"lastns": "142277413",
"lastvalue": "3971121455",
"prevvalue": "3971001230"
}
],
"id": 1
}
If someone managed to execute system.run[] using JSON-RPC API, please, share your solution.
Thank you.
No, there seem to be a few things wrong. First, the Zabbix API is JSON-RPC (not REST). Second, the item.get method is primarily used to get item configuration from the server.
To request item values from an agent (and this is how remote commands are implemented with the system.run item key), you can use the already mentioned zabbix_get:
$ zabbix_get -s host-name -k "system.run[sudo systemctl stop nginx.service]"
Note that when you say "This works well for gathering data", you are not telling Zabbix to collect data at that point - it just returns you some data that is already in the database. In the case of remote commands, the best you could get would be "1" that indicates that last time this remote command was sent to the agent successfully.