This is a translation of the original English documentation page. Help us make it better.

אובייקטי JavaScript נוספים

ופונקציות

אובייקטים מובנים

Zabbix

אובייקט Zabbix מספק אינטראקציה עם הפונקציונליות הפנימית של Zabbix.

שיטה תיאור
log(loglevel, message) כותב <הודעה> לתוך יומן Zabbix באמצעות רמת יומן <loglevel> (ראה פרמטר DebugLevel של קובץ התצורה).

דוגמא:

 Zabbix.log(3, "זהו ערך יומן שנכתב ברמת היומן 'אזהרה'")

אתה יכול להשתמש בכינויים הבאים:

כינוי כינוי ל
console.log(object) Zabbix.log(4, JSON.stringify(object))
console.warn(object) Zabbix.log(3, JSON.stringify(object))
console.error(object) Zabbix.log(2, JSON.stringify(object))

::: שימו לב חשוב הגודל הכולל של כל ההודעות שנרשמו מוגבל ל-8 MB לכל ביצוע סקריפט. :::

שיטה תיאור
sleep(delay) השהיית ביצוע JavaScript ב-delay אלפיות שניות.

דוגמה (עיכוב ביצוע ב-15 שניות):

 Zabbix.sleep(15000)

HttpRequest

This object encapsulates cURL handle allowing to make simple HTTP requests. Errors are thrown as exceptions.

The initialization of multiple HttpRequest objects is limited to 10 per script execution.

Method Description
addHeader(name, value) Adds HTTP header field. This field is used for all following requests until cleared with the clearHeader() method.
The total length of header fields that can be added to a single HttpRequest object is limited to 128 Kbytes (special characters and header names included).
clearHeader() Clears HTTP header. If no header fields are set, HttpRequest will set Content-Type to application/json if the data being posted is JSON-formatted; text/plain otherwise.
connect(url) Sends HTTP CONNECT request to the URL and returns the response.
customRequest(method, url, data) Allows to specify any HTTP method in the first parameter. Sends the method request to the URL with optional data payload and returns the response.
delete(url, data) Sends HTTP DELETE request to the URL with optional data payload and returns the response.
getHeaders(<asArray>) Returns the object of received HTTP header fields.
The asArray parameter may be set to "true" (e.g. getHeaders(true)), "false" or be undefined. If set to "true" the received HTTP header field values will be returned as arrays; this should be used to retrieve the field values of multiple same-name headers.
If not set or set to "false", the received HTTP header field values will be returned as strings.
get(url, data) Sends HTTP GET request to the URL with optional data payload and returns the response.
head(url) Sends HTTP HEAD request to the URL and returns the response.
options(url) Sends HTTP OPTIONS request to the URL and returns the response.
patch(url, data) Sends HTTP PATCH request to the URL with optional data payload and returns the response.
put(url, data) Sends HTTP PUT request to the URL with optional data payload and returns the response.
post(url, data) Sends HTTP POST request to the URL with optional data payload and returns the response.
getStatus() Returns the status code of the last HTTP request.
setProxy(proxy) Sets HTTP proxy to "proxy" value. If this parameter is empty then no proxy is used.
setHttpAuth(bitmask, username, password) Sets enabled HTTP authentication methods (HTTPAUTH_BASIC, HTTPAUTH_DIGEST, HTTPAUTH_NEGOTIATE, HTTPAUTH_NTLM, HTTPAUTH_NONE) in the 'bitmask' parameter.
The HTTPAUTH_NONE flag allows to disable HTTP authentication.
Examples:
request.setHttpAuth(HTTPAUTH_NTLM \| HTTPAUTH_BASIC, username, password)
request.setHttpAuth(HTTPAUTH_NONE)
trace(url, data) Sends HTTP TRACE request to the URL with optional data payload and returns the response.

Example:

try {
           Zabbix.log(4, 'jira webhook script value='+value);
         
           var result = {
               'tags': {
                   'endpoint': 'jira'
               }
           },
           params = JSON.parse(value),
           req = new HttpRequest(),
           fields = {},
           resp;
         
           req.addHeader('Content-Type: application/json');
           req.addHeader('Authorization: Basic '+params.authentication);
         
           fields.summary = params.summary;
           fields.description = params.description;
           fields.project = {"key": params.project_key};
           fields.issuetype = {"id": params.issue_id};
           resp = req.post('https://tsupport.zabbix.lan/rest/api/2/issue/',
               JSON.stringify({"fields": fields})
           );
         
           if (req.getStatus() != 201) {
               throw 'Response code: '+req.getStatus();
           }
         
           resp = JSON.parse(resp);
           result.tags.issue_id = resp.id;
           result.tags.issue_key = resp.key;
       } catch (error) {
           Zabbix.log(4, 'jira issue creation failed json : '+JSON.stringify({"fields": fields}));
           Zabbix.log(4, 'jira issue creation failed : '+error);
         
           result = {};
       }
         
       return JSON.stringify(result);

XML

אובייקט ה-XML מאפשר עיבוד של נתוני XML בפריט ו עיבוד מקדים לגילוי ברמה נמוכה ו-webhooks.

::: שימו לב חשוב על מנת להשתמש באובייקט XML, השרת/פרוקסי חייב להיות הידור עם תמיכה ב-libxml2. :::

שיטה תיאור
XML.query(data, expression) מאחזר תוכן צומת באמצעות XPath. מחזירה null אם הצומת לא נמצא.
ביטוי - ביטוי XPath;
נתונים - נתוני XML כמחרוזת.
XML.toJson(data) ממיר נתונים בפורמט XML ל-JSON.
XML.fromJson(object) ממיר נתונים בפורמט JSON ל-XML.

דוגמא:

קֶלֶט:

 <תפריט>
            <סוג מזון = "ארוחת בוקר">
                <name>שוקולד</name>
                <price>$5.95</price>
                <description></description>
                <calories>650</calories>
            </food>
        </menu>

תְפוּקָה:

{
            "תפריט": {
                "מזון": {
                    "@type": "ארוחת בוקר",
                    "name": "שוקולד",
                    "price": "$5.95",
                    "תיאור": null,
                    "קלוריות": "650"
                }
            }
       }
כללי סדרה

המרת XML ל-JSON תעובד לפי ההנחיות הבאות כללים (עבור המרות JSON ל-XML מוחלים כללים הפוכים):

1. תכונות XML יומרו למפתחות עם שמותיהם עם '@'.

דוגמא:

קֶלֶט:

  <xml foo="FOO">
           <בר>
             <baz>BAZ</baz>
           </bar>
         </xml>

תְפוּקָה:

  {
           "xml": {
             "@foo": "FOO",
             "בר": {
               "baz": "BAZ"
             }
           }
         }

2. אלמנטים סגירה עצמית (<foo/>) יומרו כבעלי ערך 'null'.

דוגמא:

קֶלֶט:

 <xml>
          <foo/>
        </xml>

תְפוּקָה:

{
          "xml": {
            "foo": null
          }
       }

3. מאפיינים ריקים (עם ערך "") יומרו כריקים ערך מחרוזת ('').

דוגמא:

קֶלֶט:

 <xml>
          <foo bar="" />
        </xml>

תְפוּקָה:

{
          "xml": {
            "פו": {
              "@bar": ""
            }
          }
       }

4. מספר צמתים צאצאים עם אותו שם אלמנט יומרו מפתח בודד בעל מערך ערכים כערך שלו.

דוגמא:

קֶלֶט:

 <xml>
          <foo>BAR</foo>
          <foo>BAZ</foo>
          <foo>QUX</foo>
        </xml>

תְפוּקָה:

{
          "xml": {
            "foo": ["BAR", "BAZ", "QUX"]
          }
       }

5. אם לרכיב טקסט אין תכונות ואין ילדים, הוא יהיה הומר כמחרוזת.

דוגמא:

קֶלֶט:

 <xml>
            <foo>BAZ</foo>
        </xml>

תְפוּקָה:

{
          "xml": {
            "foo": "BAZ"
           }
       }

6. אם לרכיב טקסט אין ילדים, אבל יש לו תכונות: תוכן טקסט יומר לרכיב עם המפתח '#text' ותוכן בתור ערך; המאפיינים יומרו כמתואר בסידרה חוק מספר 1.

דוגמא:

קֶלֶט:

 <xml>
          <foo bar="BAR">
            BAZ
          </foo>
        </xml>

תְפוּקָה:

{
          "xml": {
            "פו": {
              "@bar": "BAR",
              "#text": "BAZ"
            }
          }
       }

פונקציות JavaScript גלובליות

פונקציות JavaScript גלובליות נוספות יושמו עם Duktape:

  • btoa(data) - מקודד את הנתונים למחרוזת base64
  • atob(base64_string) - מפענח מחרוזת base64
נסה {
            b64 = btoa("utf8 מחרוזת");
            utf8 = atob(b64);
       }
       לתפוס (שגיאה) {
            החזר {'error.name': error.name, 'error.message': error.message}
       }
  • md5(נתונים) - מחשב את ה-hash MD5 של הנתונים

  • sha256(data) - מחשב את ה-hash SHA256 של הנתונים

  • hmac('<hash type>',key,data) - מחזיר HMAC hash כמחרוזת בפורמט hex; סוגי hash MD5 ו-SHA256 נתמכים; פרמטרי מפתח ונתונים תומכים בנתונים בינאריים. דוגמאות:

    • hmac('md5',key,data)
    • hmac('sha256',key,data)
  • sign(hash,key,data) - מחזיר חתימה מחושבת (חתימת RSA עם SHA-256) כמחרוזת, כאשר:
    hash - מותר רק 'sha256', אחרת נזרקת שגיאה;
    מפתח - המפתח הפרטי. זה צריך להתאים לתקן PKCS#1 או PKCS#8. ניתן לספק את המפתח בצורות שונות:

    • עם רווחים במקום שורות חדשות;
    • עם ''ים עם אסקייפ או ללא בריחה במקום שורות חדשות;
    • ללא שורות חדשות כמחרוזת בשורה אחת;
    • כמחרוזת בפורמט JSON.

    ניתן לטעון את המפתח גם ממאקרו/מאקרו סודי/כספת של המשתמש.

    נתונים - הנתונים שיחתמו. זה יכול להיות מחרוזת (נתמכים גם נתונים בינאריים) או מאגר (Uint8Array/ArrayBuffer).
    OpenSSL או GnuTLS משמשים לחישוב החתימות. אם Zabbix נבנתה ללא אף אחת מספריות ההצפנה הללו, תיגרם שגיאה ('חסרה ספריית OpenSSL או GnuTLS').
    פונקציה זו נתמכת מאז Zabbix 6.0.15.