0

I am trying to configure an item in Zabbix to preprocessing some data collected and I need to work out a piece of Javascript code in order to loop through the result spit by Zabbix which is collected by an API CALL and count how many words "ERROR" and "WAITING" (just the ones in capital letter) it were found in the dump file.

Below is a snippet of the file to be looped through:

{"body":[{"entity":{"entityType":"xxx.xxx","id":"JA0483_APSDD285_log-sp2340L-dol-ss",
"name":"log-sp2340L-dol-ss","description":"","modifiedTime":1587753102338,"creationTime":1587769148481,
"displayName":"Log:_Dolomite","version":"1.0.0.669","drillHoleID":"APSDD285","type":"USRLOG",
"mode":"MOSAIC","layout":"STACKEDSECTION","categoryIds":["minerallogs"],"fileFormat":"CSV",
"compressionLevel":"NONE","pixelSize":0.0,"storeSize":7676,"generatedDate":1587753102338,
"depthFrom":0.0,"depthTo":0.0,"dataLength":0.0,"storePath":"ProcessedProducts/",
"entityType":"XXX.XXX"},"size":7676,"progress":3882,"status":"ERROR","message":"Server error response code 409: 
Key JA0483_APSDD285_log-sp2340L-dol-ss already exists in the store. Use update instead","order":0,
"dateAdded":1587769395658,"dateTransferred":1587769443174,"nextRetryDate":1587769443166,"numRetries":1},
{"entity":{"entityType":"XXX.XXX","id":"JA0483_APSDD234_log-sp2350L-chl-fe-10cm","name":"log-sp2350L-chl-fe-10cm",
"description":"","modifiedTime":1587767851726,"creationTime":1587785977841,"displayName":"__Log: Fe-chlorite (2350nm)",
"version":"1.0.0.673","drillHoleID":"APSDD234","type":"USRLOG","mode":"MOSAIC","layout":"DOWNHOLE",
"categoryIds":["minerallogs"],"fileFormat":"CSV","compressionLevel":"NONE","pixelSize":0.0,"storeSize":39078,
"generatedDate":1587767851726,"depthFrom":0.0,"depthTo":0.0,"dataLength":0.0,"storePath":"ProcessedProducts/JA0483",
"entityType":"XXX.XXX"},"size":39078,"progress":3983,"status":"ERROR","message":"Server error response code 409: 
Key JA0483_APSDD234_log-sp2350L-chl-fe-10cm already exists in the store. Use update instead"

The idea of the code is just to count the ERRORs and WAITINGs founds. E.g ERROR = 30, WAITING = 20 etc.

Is this possible? I Don't have experience with JavaScript at all but I do have a good knowledge of Python and Bash but Zabbix does not know these languages to do the preprocessing of a given item. So I am hoping to get some help from the community.

3 Answers3

0

You can use match: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

supposing x is your string variable, total count of error in caps would be:

x.match(/ERROR/g).length
ibrahim tanyalcin
  • 5,643
  • 3
  • 16
  • 22
0

You can set up an HTTP agent item to do the api call and gather the full JSON.

Then you can setup N dependent items (one to count WARN, one to count ERROR and so on) and apply there the preproccesing.

To count occurences of "SOMETHING" in a string in js you could use match function, as describere here on SO:

var temp = "This is a string.";
var count = (temp.match(/is/g) || []).length;
console.log(count);
Simone Zabberoni
  • 2,024
  • 1
  • 10
  • 15
  • Hey @Simone Zabberoni Nice suggestion. Thank you so much for sharing your knowledge. I will try to use this match function and let you know if it worked. :) Cheers, PeterF – Peter Franca May 01 '20 at 07:11
-1

I got this working with the below code.

var numError = 0
if(value){
    var json = JSON.parse(value)
    for(var ii =0; ii < json.length; ii++){
        var msg = json[ii]
        if(msg.status === "ERROR"){
           numError += 1
        }
    }
}
return numError

Thanks for the help anyway guys. :)

  • This is actually more verbose and complex than the other two answers, also it accounts for "ERROR" only, if you want to extract the number of "WARNS" you have to duplicate the items... it's quite unoptimized. – Simone Zabberoni May 05 '20 at 18:50