Zabbix pre-processing item get item within an array with javascript - api

I don't have any experience with Javascript so I'm reaching out for the community to accomplish the below.
I have a few API calls configured on Zabbix which are working just fine. The thing is, not all of the results within an item is importante for me so I need to grab just what I want out of the array.
I'm using the pre-processing option on Zabbix to grab what I'm after but I can't get the code correct.
For instance, the below is one of the results Zabbix is getting.
[
{
"batteryLife": "15 minutes",
"communityString": "public",
"instanceId": "260596.1",
"instanceName": "UPS-01",
"ipAddress": "10.1.100.44",
"modelNumber": "GXT4-10000RT230",
"name": "UPS-01",
"objectType": "ScUps",
"scName": "pth-pf-04",
"scSerialNumber": 260596,
"serialNumber": "unknown",
"status": "Up",
"statusDescription": "Online",
"type": "Liebert"
}
]
How Can I Use the pre-processing to grab just the "ipAddress" value for instance?
Thanks for the help.
PeteF

You can avoid JavaScript preprocessing where you can use JSONPath preprocessing, see https://www.zabbix.com/documentation/current/manual/config/items/preprocessing/jsonpath_functionality
In your case:
$[0].ipAddress
A useful tool for JSONPath is http://jsonpath.com/

In case someone have the same doubt as I was having here is how I got it working to return the "ipAdress" value
var json = JSON.parse(value);
return json.ipAddress;
In case there are more than one dictionary inside of an array.
var json = JSON.parse(value);
return json[0].ipAddress;

Related

Matching multiple response in a single scenario outline

I want to match multiple response of an API. Please find below Scenario Outline.
Background:
* def kittens = read('../sample.json')
Scenario Outline: Create Test1
Given url url
And request <Users>
When method POST
Then status 200
And match response.success.name == <expectedName>
And match response.success.contact.mobile == <expectedMobile>
Examples:
|Users|expectedName|expectedMobile|
|kittens.User1|'Micheal'|'123456'|
|kittens.User2|'Steve'|'998877'|
In the above case, I am able to match for 2 fields but I want to validate for more fields, but it is increasing pile of code that I do not want.
Multiple response of an API:
"success": {
"name": "Micheal",
"addr": "Tesla road",
"contact": {
"mobile": 123456,
"phone": 4422356
}
}
"success": {
"name": "Steve",
"addr": "Karen Road",
"contact": {
"mobile": 998877,
"phone": 244344
}
}
I am looking for minimizing the lines of code.
Can you please tell me another way where I can load entire response into expected and then I will traverse in the example section?
Please help me. Thank you !!
I strongly recommend you don't do this, and the reasons are explained in detail here: https://stackoverflow.com/a/54126724/143475
Also note instead of going field-by-field, you can use the whole JSON in the Examples column, or even pull from a file, see examples.feature.
You will actually end up with a much more "increasing pile of code" if you go down this path in my sincere opinion.

In Scrapy, download files nested below the to of a yielded item dict

To download files in Scrapy, one adds the key 'fileurls' to the yielded item dict with a value of the urls to download. But my files are nested somewhere below the top level of the yielded dict. An item looks like this:
{
"title": "foo",
"files": {
"drawings": [
{
"caption": "bar",
"fileurl": "http://foo.com/foo/foo.pdf"
},
{
"caption": "second floor",
"fileurl": "http://foo.com/foo/bar.pdf"
}
],
"photos": [
{
"caption": "bar",
"fileurl": "http://foo.com/foo/baz.pdf"
}
]
}
}
Ideally, I'd like each file downloaded and have scrapy add its "file" element next to the "fileurl". But this does not seem to work automatically.
How can I achieve this? The current version of Scrapy is 1.6.0.
To do something like this, you will need to make your own subclass of scrapy's FilesPipeline.
To make the downloading happen, you'll need a custom get_media_requests method, which should get the URLs from your item and return an iterable of requests which will be used to download the files.
After that, you'll also need to modify the item_completed and/or the file_downloaded method to store the result in the exact way you want.
If you need more details than what's provided in the docs, take a look at the source and see how the existing pipeline works.

Google Vision Text Detection returns too much unnecesary data

When using Google Vision to run text detection on a menu, the response from their API is way too large and returns way too much data that I don't need. I just want the text from the menu, not all the coordinates that come with the response. I can't find anything about narrowing down the response in any documentation i've read. Does someone know how to specify what fields get returned in the response?
Heres my request:
POST: https://vision.googleapis.com/v1/images:annotate?key=<MY_KEY>
BODY:
{
"requests": [
{
"image": {
"content": "...base64-encoded-image-content..."
},
"features": [
{
"type": "TEXT_DETECTION"
}
]
}
]
}
I figured it out. I could not find any documentation on how to do this, I had to just guess for like half an hour. If someone knows of any documentation on this let me know.
Anyway you can use the "fields" parameter to narrow down the response like so:
POST: https://vision.googleapis.com/v1/images:annotate?key=<MY_KEY>&fields=responses.fullTextAnnotation.text
This will only return the menu text from the Google Vision text detection API

I am facing issue for selenium webdriver script for Goibibo site city selection using xpath/css selector

While selecting city from site using xpath/css selector facing issue let me know solution
List<WebElement> frm=driver.findElements(By.cssSelector("html body div#gi_midcontent.col-md-12.col-sm-12.col-xs-12.pad0 div#gi_mid_in div#viewContainer div#flights-home-view.homeContainer.col-md-12.col-sm-12.col-xs-12.myPropDisplay div.homeContainerInner div#searchWidgetNew.blueBg.homeWidgetWrap.posRel form#gi_search div#searchWidgetCommon div.formWrap.padT15 div#source_st.fl_shAutosgBox.col-md-5.col-sm-5.col-xs-12.autoSuggestBox.marginB10 input#gi_source_st.form-control.inputTxtLarge.fromTxt.posRel"));
Couple of things.
Go through the selenium document here.http://www.seleniumhq.org/docs/03_webdriver.jsp
Read on how to use different locator strategy to identity particular locator.
Don't ask the question just because something is not working,spend some time on that.
Visit this page to understand how to ask question so that you can get answers.
The reason I have mentioned above point is you css locator looks pathetic, also the locator you are trying to identify has both name and id, and if you now how to use locator you could have figure that out.
The selector specified in the problem mixed id and class. However, taking a look at the website, the suggested city list is not offered as a group of input elements but fetched from AJAX.
Sending GET query with Postman tool on this URL and you can get JSON data with a list of objects. The property of json["data"]["r"][index]["xtr"]["cn"] seems a string of "CityName". This API does not require authentication so it can be fetched with Java HttpClient, too.
https://voyager.goibibo.com/api/v1/flights_search/find_node_by_name_v2/?search_query=hint&limit=10
The sample result:
{
"data": {
"r": [
{
"s_sc": 5055001,
"xtr": {
"cn": "Beica",
"cid": "1918715907401915921",
"cc": "ET",
"dis_type": "ldis",
"ar": 0,
"eid": "7717797727439208389",
"cnt_n": "Ethiopia",
"cnt_id": "1871100673297067773",
"dis": 0
},
"iata": "BEI",
"n": "Beica Airport",
"mt": "Beica Airport ### Beica",
"t": "airport",
"_id": "7717797727439208389",
"ct": {
"_id": "1918715907401915921",
"n": "Beica"
}
},
...
]
}
}
Please replace "hint" with your query string, for example, a letter to start with. Also take the note that (page) limit could not be greater than 50 in one query.

How can I use REST url as data in Vega-lite

I have this REST API that returns tabular data in the following way:
{"data": [{"el1": 8, "el2": 9}, {"el1": 3, "el2": 4}]}
I would like to use el1 and el2 in a Vega-lite chart. How should I refer to the elements in the array?
From the documentation here:
(JSON only) The JSON property containing the desired data. This parameter can be used when the loaded JSON file may have surrounding structure or meta-data. For example
"property": "values.features"
is equivalent to retrieving json.values.features from the loaded JSON object.
It seems that you can try to specify the "property" property (punny, eh) on the format. Something like this:
"data": {
"url": <your url here>,
"format": {
"type": "json",
"property": "data"
},
}
Disclaimer: I haven't actually tested this but it looks to be supported (: