Ad Widget

Collapse

Script updates to Close alert

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teddy
    Senior Member
    • Dec 2017
    • 234

    #1

    Script updates to Close alert

    Коллеги.
    Может кому будет полезно. Давно существует скрипт на баз API с помощью котоого можно закрывть алерты.
    Это скрипт начиная с версии Zabbix 7.2 начал выдавать что-то типа такого
    "cannot execute script: Cannot close event 108871877: API error: Invalid request.; Invalid parameter "/": unexpected parameter "auth"".
    Поэтому в текст внесена небольшая правка и новая версия кода ниже..

    Code:
    try {
        var req = new HttpRequest(),
            params = JSON.parse(value),
            eventid = params.eventid,
            token = params.token,
            url = params.url,
            body,
            resp;
    
        Zabbix.log(4, '[Closing problem] starts: eventid=' + eventid + '", url="' + url + '".');
    
        //Validation
        if (typeof eventid !== 'string' || eventid.trim() === '') {
            throw 'The eventid must be defined in macro "{EVENT.ID}".';
        }
    
        if (typeof token !== 'string' || token.trim() === '') {
            throw 'The token must be defined in macro "{$Z_API_TOKEN}".';
        }
    
        if (typeof url !== 'string' || !(url.startsWith('http://') || url.startsWith('https://'))) {
            throw 'The URL (starting with "http://" or "https://") must be defined in macro "{$Z_API_PHP}".';
        }
    
        if (typeof params.HTTPProxy === 'string' && params.HTTPProxy.trim() !== '') {
            req.SetProxy(params.HTTPProxy);
        }
    
        //Processing logic
        req.addHeader('Authorization: Bearer ' + token.trim());
        req.addHeader('Content-Type: application/json');
        body = {
            'jsonrpc': '2.0',
            'method': 'event.acknowledge',
            'params': {
                'eventids': eventid.trim(),
                'action': 1,
                'message': 'Problem auto-closed.'
            },
            'id': 1
        };
        resp = req.post(url.trim(),
            JSON.stringify(body)
        );
    
        if (req.getStatus() != 200) {
            throw 'Response code: ' + req.getStatus();
        }
    
        Zabbix.log(4, '[Closing problem] returned: ' + resp);
        resp = JSON.parse(resp);
        if ('error' in resp) {
            throw ('API error: ' + resp.error.message + '; ' + resp.error.data);
        }
    
    } catch (error) {
        Zabbix.log(3, '[Closing problem] cannot close event "' + eventid + '": ' + error);
        throw 'Cannot close event ' + eventid + ': ' + error;
    }
    
    return 'OK';
Working...