1 Esempi di script webhook

Panoramica

Sebbene Zabbix offra un gran numero di integrazioni webhook disponibili immediatamente, potresti voler creare invece i tuoi webhook personalizzati. Questa sezione fornisce esempi di script webhook personalizzati (utilizzati nel parametro Script). Vedi la sezione webhook per la descrizione degli altri parametri del webhook.

Non utilizzare assegnazioni non dichiarate nel preprocessing JavaScript. Usa var per dichiarare le variabili locali.

Gli eventi di ripristino (sia generati automaticamente sia in seguito a una chiusura manuale) includono i tag degli eventi risolti (compresi i tag ereditati da template, host e trigger). Gli script webhook vengono eseguiti dopo la creazione dell'avviso; i tag restituiti da uno script webhook vengono quindi applicati dopo la creazione iniziale dell'avviso e potrebbero non essere presenti nell'avviso che ha attivato il webhook. Se un'integrazione richiede che i tag prodotti da un webhook siano presenti nella notifica iniziale, recupera i tag dell'evento dal server (ad esempio, utilizzando l'API Event) oppure memorizza i tag prodotti dal webhook in un archivio persistente esterno ed esegui lì la correlazione.

Webhook Jira (personalizzato)

Questo script creerà una issue di JIRA e restituirà alcune informazioni sulla issue creata.

try {
    Zabbix.log(4, '[ Jira webhook ] Started with params: ' + value);

    var result = {
            'tags': {
                'endpoint': 'jira'
            }
        },
        params = JSON.parse(value),
        req = new HttpRequest(),
        fields = {},
        resp;

    if (params.HTTPProxy) {
        req.setProxy(params.HTTPProxy);
    }

    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://jira.example.com/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;

    return JSON.stringify(result);
}
catch (error) {
    Zabbix.log(4, '[ Jira webhook ] Issue creation failed json : ' + JSON.stringify({"fields": fields}));
    Zabbix.log(3, '[ Jira webhook ] issue creation failed : ' + error);

    throw 'Failed with error: ' + error;
}

webhook Slack (personalizzato)

Questo webhook inoltra le notifiche da Zabbix a un canale Slack.

try {
    var params = JSON.parse(value),
        req = new HttpRequest(),
        response;

    if (params.HTTPProxy) {
        req.setProxy(params.HTTPProxy);
    }

    req.addHeader('Content-Type: application/x-www-form-urlencoded');

    Zabbix.log(4, '[ Slack webhook ] Webhook request with value=' + value);

    response = req.post(params.hook_url, 'payload=' + encodeURIComponent(value));
    Zabbix.log(4, '[ Slack webhook ] Responded with code: ' + req.getStatus() + '. Response: ' + response);

    try {
        response = JSON.parse(response);
    }
    catch (error) {
        if (req.getStatus() < 200 || req.getStatus() >= 300) {
            throw 'Request failed with status code ' + req.getStatus();
        }
        else {
            throw 'Request success, but response parsing failed.';
        }
    }

    if (req.getStatus() !== 200 || !response.ok || response.ok === 'false') {
        throw response.error;
    }

    return 'OK';
}
catch (error) {
    Zabbix.log(3, '[ Slack webhook ] Sending failed. Error: ' + error);

    throw 'Failed with error: ' + error;
}