Provide additional device information via API

As a user that runs two Pi-hole instances on two separate devices with keepalived for failover (Type of user),
I would like to know the current active Pi-hole instance (Goal),
so I can send notifications in Home-Assistant when failover takes place (Reason)

Problem

  • I use a floating IP as DNS address that gets switched over to the slave Pi-hole instance in case something is wrong with the master Pi-hole instance
  • I want a notification in case the failover takes place
  • However, there is no Pi-hole API method that tells me on which device Pi-hole is running

Proposed Change

$ curl -X GET 'http://<floating-ip>/admin/api.php?device&auth=<auth-token>
{
"Hostname": "raspberrypi-master",
...
"LAN addr": "192.168.xxx.xx"
}
# failover takes place
$ curl -X GET 'http://<floating-ip>/admin/api.php?device&auth=<auth-token>
{
"Hostname": "raspberrypi-slave",
...
"LAN addr": "192.168.xxx.yy"
}

Details & Background

  • The service keepalived only supports email notifications. I don't want to use email since I receive all other network-related notifications via Telegram from Home-Assistant (HA). Thus, I want to use the Telegram integration in HA to send notifications to my devices when the failover takes place.
  • Current 'workaround' is to resolve the hostname via host <floating-ip> with a command line sensor in HA

You can run the command host pi.hole and the server IP will be in the reply.

host pi.hole
pi.hole has address 192.168.0.155

Thanks for the quick reply! I already have a similar solution for the goal I'm trying to achieve:

- platform: command_line
  name: Active Pihole
  # Floating DNS address
  command: "host <floating-ip> | grep -Eo 'raspberrypi[-0-9]+?'"
  • Do you think adding information about the device to the API will be a benefit for other users? Why should the chronometer information only be available from the command line?
  • Now that I have a working regex I'm fine. From the start, however, it would have been simpler for me to just call an API endpoint.

Edit:

  • Looks like the solution does not work yet. In case the <floating-ip> is on the slave, I receive the following
# On master where also HA is running
host 192.168.xxx.100
** Host 100.xxx.168.192.in-addr.arpa: not found: 3 (NXDOMAIN)
  • I would still like to request the feature mentioned in the first post. As this is not a support thread, I will only post my final workaround once I accomplished my goal.

In my opinion - no.

Because if you have the web GUI available, that shows you more information and in a graphical format.

If the information is available from any client with a single host command, why does it need to be added to the API?

As I see it, you are knocking on the wrong door here.

Pi-hole is not involved in the failover at all.

Whatever software you are using to handle failover, that is the source of the information you seek to be notified of. I would recommend checking your failover software's documentation and forums on how they support notifications and how to configure.

Detailed information about DNS queries is also available via the API although there is a nice graphical representation in the GUI. Following your argument, you could even argue that there is no need for an API since all information is available via the GUI.

In my case, the host command does not work when the floating IP switches to the slave (see the previous post). This is probably due to an invalid configuration of the device or network/router and specific to me. However, access to the Pi-hole hostname via the API would have worked for me.

But, I see your point. In general, the host command is a solution to the problem. Thanks for pointing this out. It does not work - for whatever reason (hosts file, resolve.conf, etc.) - in my case. Thus, I found another solution to my problem (see below).

I suggest closing this feature request as I found another solution to my problem.

As the software keepalived only supports email notifications, I first came up with the hostname solution via the Pi-hole API. This is what this thread is about. I then tried a solution via the host command as jfb pointed out. Thought hasn't worked out for me.

Finally, I stumbled across a hook in keepalived which triggers when the failover takes place. I now use this hook to call a webhook on Home-Assistant. Just as you said :+1:

Thanks for helping me out. I think we can close this now. Stay healthy!

Below are two Pi-hole instances with this time dig for lookup (nice for script):

pi@noads:~ $ dig +short pi.hole
10.0.0.2

pi@phb5:~ $ dig +short pi.hole
10.0.0.4

Even if the virtual IP does a failover, the response will correspond with the active node.

EDIT:
You can specify the virtual IP like below for scripting:

dig +short pi.hole @<VIRTUAL_IP>

And to get name of the active node:

dig +short -x $(dig +short pi.hole @<VIRTUAL_IP>)