Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pihole-status #454

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Is this your first time here? You should definitely take a look at these scripts
[![info-healthchecks.io](polybar-scripts/info-healthchecks.io/screenshots/1.png)](polybar-scripts/info-healthchecks.io/)
[![network-huawei-modem](polybar-scripts/network-huawei-modem/screenshots/1.png)](polybar-scripts/network-huawei-modem/)
[![info-timezone](polybar-scripts/info-timezone/screenshots/1.gif)](polybar-scripts/info-timezone/)
[![pihole-status](polybar-scripts/pihole-status/screenshots/1.png)](polybar-scripts/pihole-status/)
[![pihole-status](polybar-scripts/pihole-status/screenshots/2.png)](polybar-scripts/pihole-status/)


## See also these other user repositories:
Expand Down
25 changes: 25 additions & 0 deletions polybar-scripts/pihole-status/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Script: pihole-status

A script to remotely disable/enable Pi-Hole's blocking. Displays the current status and toggle status when clicked.

Depends on `curl` and `jq`.

## Module

Update the `pihole-status.sh` with your Pi-Hole instance's hostname and API key. You may also customize the on/off label, refresh time, as well as the amount of time to disable Pi-Hole for (by default, 300 seconds).

```ini
[module/pihole-status]
type = custom/script
exec = ~/polybar-scripts/pihole-status.sh
tail = true
click-left = kill -USR1 %pid%
```

## How this script works

This script uses `tail` to keep a long-running instance of the application, which Polybar uses the last outputted line as label. It fetches the new status with curl every 10 seconds (configurable) and outputs the on or off label based on the current status read without the JSON response with jq.

When the script is clicked, a `USR1` signal is sent to it with `kill`, and it runs the `toggle`. This updates the status with curl and prints the current status, and restarts the refresh loop.

This design was required to have both auto-refreshing and updating on click.
58 changes: 58 additions & 0 deletions polybar-scripts/pihole-status/pihole-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh

ENDPOINT="http://pi.hole/admin/api.php" # Pi-Hole API endpoint. Change host
API_TOKEN="" # Get from /admin/settings.php?tab=api

DISABLE_TIME="300" # In seconds. Set to 0 to disable Pi-Hole forever
REFRESH_INTERVAL="10" # In seconds. Set to "infinity" to disable refreshing

# Uses Nerd Fonts for icon and lemonbar tags for coloring
ON_LABEL="󰷱"
OFF_LABEL="%{F#f00}󰷱%{F-}"


get_status() {
curl -s "$ENDPOINT?status&auth=$API_TOKEN" | jq -r '.status'
}

sleep_pid=0

toggle() {
status="$(get_status)"

case "$status" in
enabled)
curl -s "$ENDPOINT?disable=$DISABLE_TIME&auth=$API_TOKEN" > /dev/null
echo "$OFF_LABEL"
sleep 0.1 # some time for write to finish before refetch
;;
disabled)
curl -s "$ENDPOINT?enable&auth=$API_TOKEN" > /dev/null
echo "$ON_LABEL"
sleep 0.1 # some time for write to finish before refetch
;;
esac

if [ "$sleep_pid" -ne 0 ]; then
kill $sleep_pid >/dev/null 2>&1
fi
}

trap "toggle" USR1

while true; do
status="$(get_status)"

case "$status" in
enabled)
echo "$ON_LABEL"
;;
disabled)
echo "$OFF_LABEL"
;;
esac

sleep "$REFRESH_INTERVAL" &
sleep_pid=$!
wait
done
Binary file added polybar-scripts/pihole-status/screenshots/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added polybar-scripts/pihole-status/screenshots/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading