Ad Widget

Collapse

automatic tasks to remove hosts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JulBz
    Junior Member
    • Nov 2024
    • 4

    #1

    automatic tasks to remove hosts

    Hello,

    I would like to know if we can use automatic tasks to remove hosts that have not been available for 7 days. And how to do it?

    I found this link : https://www.zabbix.com/forum/zabbix-...gistered-hosts , I am getting the error SyntaxError: invalid token (line 33) even though I have generated and copied the API token correctly

    I may be testing wrong..

    Thanks
    Last edited by JulBz; 17-12-2024, 12:58.
  • jhboricua
    Senior Member
    • Dec 2021
    • 113

    #2
    I know this is an old thread but I was getting the same error and the cause was a hidden zero-width space in my code. Once I removed it (thanks to Zabbix support for identifying this) I was able to execute the webhook. Also, not sure about Zabbix 5 or 6, but in Zabbix 7 it DOESN'T need to be a mediatype webhook. I created mine (Zabbix 7.0) as a webhook script under Alerts -> Scripts.

    Here's my version of the webhook to auto remove hosts:

    Code:
    try {
        Zabbix.log(4, '[ Remove host webhook ] Started with params: ' + value);
    
        var body,
            params = JSON.parse(value),
            request = new HttpRequest(),
            response,
            result = {};
        
        request.addHeader('Content-Type: application/json-rpc');
        request.addHeader('Authorization: Bearer ' + params.token);
    
        body = {
            "jsonrpc": "2.0",
            "method": "host.delete",
            "params": [params.hostid],
            "id": 1
        };
    
        Zabbix.log(4, '[ Remove host webhook ] Data : ' + JSON.stringify(body));
    
        response = request.post('https://my_zabbix_url/api_jsonrpc.php', JSON.stringify(body));
    
        if (request.getStatus() != 200) {
            throw new Error('Response code: ' + request.getStatus());
        }
    
        response = JSON.parse(response);
        result = response;
        Zabbix.log(4, '[ Remove host webhook ] Result : ' + JSON.stringify(result));
        return JSON.stringify(result);
    } catch (error) {
        Zabbix.log(3, '[ Remove host webhook ] Host removal failed : ' + error.message);
        throw new Error('Failed with error: ' + error.message);
    }
    Give it the proper parameters for token and hostid. Then set your trigger action to execute it.

    Comment

    Working...