Ad Widget

Collapse

Removing auto-registered hosts

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nibbles
    Junior Member
    • Mar 2023
    • 4

    #1

    Removing auto-registered hosts

    I've trawled the forums and Google and come up short for a solution to what i thought would be a straight forward request.

    I have a bunch of hosts that get created and auto registered to Zabbix. That all works fine.
    These provision and destruction of systems are customer driven. So they can come online and get destroyed at any time. The customer doesn't have the ability to reboot or shutdown the machine, if they are offline for 7 days its because they have been deleted. So i simply want to clean up the Zabbix remnants of that host.

    My dashboard currently just displays the dead systems as triggered items "Zabbix agent is not available".

    Anyone able to offer a solution that enables me to get rid of these systems. I've located the Alerts > Actions > Discovery Actions section, but this only seems to apply to systems added via discovery rather than auto-registration.
  • Answer selected by nibbles at 28-03-2023, 18:26.
    nibbles
    Junior Member
    • Mar 2023
    • 4

    In the interest of passing on knowledge to the community i created the following webhook script to remove the host, and then used a trigger to call it if the agent was offline for 72 hours.

    Code:
    try {
        Zabbix.log(4, '[ Remove host webhook ] Started with params: ' + value);
        var result = {
            },
            params = JSON.parse(value),
            req = new HttpRequest(),
            data = {},
            resp;
        req.addHeader('Content-Type: application/json');
        req.addHeader('Authorization: Bearer ' + params.authentication);
    
        data.jsonrpc = "2.0";
        data.method = "host.delete";
        data.params = [params.hostid];
        data.id = "1";
    
        Zabbix.log(4, '[ Remove host webhook ] Data : ' + JSON.stringify(data));
    
        resp = req.post('https://<YOUR_ZABBIX_URL>/api_jsonrpc.php',
            JSON.stringify(data)
        );
        if (req.getStatus() != 200) {
            throw 'Response code: ' + req.getStatus();
        }
        resp = JSON.parse(resp);
        result = resp;
        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);
        throw 'Failed with error: ' + error;
    }​

    Comment

    • cyber
      Senior Member
      Zabbix Certified SpecialistZabbix Certified Professional
      • Dec 2006
      • 4807

      #2
      Add another trigger, "agent not available for 7days" and add action to it, what deletes the host over API.

      Comment

      • nibbles
        Junior Member
        • Mar 2023
        • 4

        #3
        Originally posted by cyber
        Add another trigger, "agent not available for 7days" and add action to it, what deletes the host over API.
        Unless I'm being dense, how do i add an API call action to a trigger? The only options i see when in the Actions > Trigger menu are to send a message. Running Zabbix server 6.4

        Comment

        • nibbles
          Junior Member
          • Mar 2023
          • 4

          #4
          Edit.. Never mind, the action only becomes available to select once a script action has been created in the scripts menu.

          Comment

          • nibbles
            Junior Member
            • Mar 2023
            • 4

            #5
            In the interest of passing on knowledge to the community i created the following webhook script to remove the host, and then used a trigger to call it if the agent was offline for 72 hours.

            Code:
            try {
                Zabbix.log(4, '[ Remove host webhook ] Started with params: ' + value);
                var result = {
                    },
                    params = JSON.parse(value),
                    req = new HttpRequest(),
                    data = {},
                    resp;
                req.addHeader('Content-Type: application/json');
                req.addHeader('Authorization: Bearer ' + params.authentication);
            
                data.jsonrpc = "2.0";
                data.method = "host.delete";
                data.params = [params.hostid];
                data.id = "1";
            
                Zabbix.log(4, '[ Remove host webhook ] Data : ' + JSON.stringify(data));
            
                resp = req.post('https://<YOUR_ZABBIX_URL>/api_jsonrpc.php',
                    JSON.stringify(data)
                );
                if (req.getStatus() != 200) {
                    throw 'Response code: ' + req.getStatus();
                }
                resp = JSON.parse(resp);
                result = resp;
                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);
                throw 'Failed with error: ' + error;
            }​

            Comment

            • jeeva
              Junior Member
              • May 2021
              • 4

              #6
              Great, I've been also search for this for ages, there was a video made by Official Zabbix on Youtube but it was a bit dated.

              Update: where do i set the parameter for "7 days" :?
              Last edited by jeeva; 03-04-2023, 12:30.

              Comment


              • cristiancruz1609
                cristiancruz1609 commented
                Editing a comment
                Hi everyone, can you explain how Edit parameter in Trigger condition, I only see Types condition like "Time period" and I don't know how configurate that trigger
            • cyber
              Senior Member
              Zabbix Certified SpecialistZabbix Certified Professional
              • Dec 2006
              • 4807

              #7
              Originally posted by jeeva
              Update: where do i set the parameter for "7 days" :?
              In trigger conditions...

              Comment

              • SOLARIS
                Junior Member
                • Mar 2019
                • 2

                #8
                Originally posted by nibbles
                In the interest of passing on knowledge to the community i created the following webhook script to remove the host, and then used a trigger to call it if the agent was offline for 72 hours.
                Hi, could you please also share media type details? After many tries I still not able to get this working always getting a message:
                Code:
                {
                "jsonrpc": "2.0",
                "error": {
                "code": -32602,
                "message": "Invalid params.",
                "data": "Session terminated, re-login, please."
                },
                "id": "1"
                }​

                Comment

                • SOLARIS
                  Junior Member
                  • Mar 2019
                  • 2

                  #9
                  I managed to configure that. Requirements are next:
                  • you must obtain API token in Users > API tokens
                  • create Media type as webhook with parameters "authentication" (put API token here) and "hostid" (put {HOST.ID} here).

                  Click image for larger version

Name:	image.png
Views:	2872
Size:	40.0 KB
ID:	466062

                  Comment

                  • donikatz
                    Junior Member
                    • Jul 2023
                    • 2

                    #10
                    Thanks, exactly what I'd been looking for!

                    In case this helps any other noobs, I couldn't get it to trigger until I found in docs that you always need to associate a media type with a user for it to trigger.
                    Last edited by donikatz; 01-08-2023, 20:22.

                    Comment

                    • damiancancela
                      Junior Member
                      • May 2020
                      • 14

                      #11
                      Originally posted by nibbles
                      In the interest of passing on knowledge to the community i created the following webhook script to remove the host, and then used a trigger to call it if the agent was offline for 72 hours.

                      Code:
                      try {
                      Zabbix.log(4, '[ Remove host webhook ] Started with params: ' + value);
                      var result = {
                      },
                      params = JSON.parse(value),
                      req = new HttpRequest(),
                      data = {},
                      resp;
                      req.addHeader('Content-Type: application/json');
                      req.addHeader('Authorization: Bearer ' + params.authentication);
                      
                      data.jsonrpc = "2.0";
                      data.method = "host.delete";
                      data.params = [params.hostid];
                      data.id = "1";
                      
                      Zabbix.log(4, '[ Remove host webhook ] Data : ' + JSON.stringify(data));
                      
                      resp = req.post('https://<YOUR_ZABBIX_URL>/api_jsonrpc.php',
                      JSON.stringify(data)
                      );
                      if (req.getStatus() != 200) {
                      throw 'Response code: ' + req.getStatus();
                      }
                      resp = JSON.parse(resp);
                      result = resp;
                      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);
                      throw 'Failed with error: ' + error;
                      }​
                      Hello, can you share how the Trigger Action it looks? I'm not seeing how to setup that part, thanks!

                      Comment

                      • dimir
                        Zabbix developer
                        • Apr 2011
                        • 1080

                        #12
                        Related new change in Zabbix: the autoreg_host table will now be cleared from unreferenced records: https://support.zabbix.com/browse/ZBX-15210

                        Comment

                        • Duck501
                          Junior Member
                          • Jan 2024
                          • 1

                          #13
                          Originally posted by donikatz
                          Thanks, exactly what I'd been looking for!

                          In case this helps any other noobs, I couldn't get it to trigger until I found in docs that you always need to associate a media type with a user for it to trigger.
                          Hi! How exactly did you get this to work - I've been trying to setup something very similar - the only difference being that I want to delete the hosts after 30 days. However I just can't seem to work out how to actually get it to work - I have set up the script as a media type and added the script in there - added the api token and hostid statement as well.
                          However I always get an error when I test this - the trigger works fine but once the media type is triggered it just keeps saying "Status: failed. Info: No message defined for media type."

                          I would really appreciate someone who can help me in this - I mean it just has to be possible to automatically remove hosts, right?

                          Comment

                          • shafuq
                            Junior Member
                            • Oct 2024
                            • 17

                            #14
                            Hi guys. I was in the same boat, trying to find a proper and practical way to delete hosts that were added via "Autoregistration" and were offline for X amount of days. This post is one of the few if not only answer I found on the net. I'm also pretty new to Zabbix and haven't delved into API's and such so I tried mingling a bit hoping for a simpler solution. And I think I came up with an alternative way using a discovery rule/action. I've only done limited testing and the solution below seems to be working fine.


                            Solution:


                            * I have a bunch of windows/linux clients added via autoregistration. I'm not going into that part as it's out of scope. Basically they all have "Windows/Linux by Zabbix agent active" templates linked.


                            * Discovery Rule > New discovery rule named "Target_network" >
                            ------ Server/Proxy: (pick which ever one you use)
                            ------ IP range: 192.168.1.2-254 (add IP address range of your target host IP's. could be their dhcp range. you may add multiple if you want.)
                            ------ Update interval: 2h
                            ------ Maximum concurrent checks per type: unlimited
                            ------ Checks: ICMP ping
                            ------ Device uniqueness criteria: IP address
                            ------ Host name: DNS name
                            ------ Visible name: Host name
                            ------ Enabled: check


                            * Discovery actions > New action named "Remove Host" >
                            Action: A and B and C
                            ---A--- Discovery rule equals "Target_network"
                            ---B--- Discovery status equals "Down"
                            ---C--- Uptime/Downtime is greater than or equals "86400" (minutes = 60 days)
                            Operations:
                            ------- Remove host


                            That's it! But please note that I have purposely added the target_network discovery rule using ping instead of zabbix_agent. If you do any discovery action like adding discovered host to a host group or whatnot the discovery rule will directly add the host to the system. So don't do that. Keep it simple. I've done a lot of testing and the way I described (discover via ping > check down time > delete host) seems the only way it deletes autoregistered hosts WITHOUT adding/modifying new ones to the system.

                            If any of you reads this and gives it a shot please share your experience. If there is something about it not working as intended at least it will help the rest of us to know beforehand.

                            Comment

                            Working...