Solving practical tasks in Zabbix using JavaScript



Tikhon Uskov , engineer of the Zabbix integration team



Zabbix is โ€‹โ€‹a customizable platform that is used to monitor any kind of data. From the earliest versions of Zabbix, monitoring administrators had the ability to run various scripts through Actions to check on target hosts. At the same time, launching scripts led to a number of difficulties, including such as the need to support scripts, deliver them to communication nodes and proxies, as well as support for different versions.



JavaScript for Zabbix



2019 Zabbix 4.2 JavaScript. , - , Zabbix , , Zabbix , Zabbix JavaScript. , Zabbix 3.4, .



Zabbix 4.4, JavaScript, โ€” Webhook, Zabbix .



JavaScript Duktape



JavaScript Duktape? :



  • Lua โ€“ Lua 5.1
  • Lua โ€“ LuaJIT
  • Javascript โ€“ Duktape
  • Javascript โ€“ JerryScript
  • Embedded Python
  • Embedded Perl


, , , . JavaScript Duktape.





performance testing



Duktape:



โ€” ECMAScript E5/E5.1

โ€” Zabbix Duktape:



  • Zabbix.log() โ€” Zabbix Server , , , Webhook .
  • CurlHttpRequest() โ€” HTTP- , Webhook.
  • atob() btoa() โ€” Base64.


. Duktape ACME. Zabbix 2015 . , .



JavaScript



JavaScript : , .



, .



, -, . โ€” , , , .



. , , , , valueOf toString. valueOf toString. valueOf , .



valueOF. , toString. toString , valueOf , . toString, , . .



, 'obj' toString,



`var obj = { toString() { return "200" }}` 


toString , :



`obj + 1 // '2001'` 

`obj + 'a' // โ€˜200a'`


toString, , .



`var obj = { toString() { return 200 }}` 

`obj + 1 // '2001'`


, , , .



`obj + 'a' // โ€˜200a'`


JavaScript.



toString , 1.





, 3, 4.



(==) toString . , . (===).







. .



, , Webhook , , , .



Webhook Media



2019 2020 Zabbix Webhooks ยซ ยป, Zabbix.









Preprocessing



  • JavaScript , Zabbix .
  • Zabbix JavaScript, - , value ( , ).
  • , return.
  • .
  • , . 10 10 .




. - 10 , 1 ยซยป Zabbix. JavaScript (dummy items), .



zabbix_js:



`zabbix_js -s *script-file -p *input-param* [-l log-level] [-t timeout]`

`zabbix_js -s script-file -i input-file [-l log-level] [-t timeout]`

`zabbix_js -h`

`zabbix_js -V`




1



.



: .



, . โ€” (), .



:



  • .
  • ยซยป , , . , .


, , ( โ€” ).



, , , 30 , 29 Zabbix ยซยป, . . , .



โ€” JavaScript, :



`return (value - 32) * 5 / 9;`


, , .





`return (parseInt(value) + parseInt("{$EXAMPLE.MACRO}"));`


, , , - , , , value . , .





`return (value + "{$EXAMPLE.MACRO}");`


, . parseInt(), , parseFloat(), , number, .



2



.



: "Feb 12 12:33:56 2022 GMT".



ECMAScript5 Date.parse() ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ). MMM DD YYYY HH:mm:ss ZZ



: , . Duktape.



:



  • , ( โ€” , ).



  • value split. , , , 0, . split(0) โ€” , split(1) โ€” , split(2) โ€” . . .





`var split = value.split(' '),`


  • ( ) ( 0 11). , ( 1). , , . slice() โ€” , ( ).


`MONTHS_LIST = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],`

`month_index = ('0' + (MONTHS_LIST.indexOf(split[0]) + 1)).slice(-2),`


  • ISO .


`ISOdate = split[3] + '-' + month_index + '-' + split[1] + 'T' + split[2],`


โ€” 1970 - . , Zabbix {Date} {Time}, .



  • JavaScript Unix Timestamp , .


`now = Date.now();`


  • , Zabbix.


`return parseInt((Date.parse(ISOdate) - now) / 1000);`


'last<' , , , , . , , .



. parseInt() return, , , . parseFloat() .






All Articles