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

ping: add a new block which ping an host and display a colored pill #531

Open
wants to merge 2 commits 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
34 changes: 34 additions & 0 deletions ping/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# `ping`

Display a green or red pill according to the ability to reach the
configured `ping_target` target host, or the `8.8.8.8` default target.

![](ping-ok.png)
![](ping-ko.png)
![](ping-error.png)

# Dependencies

- `python3`
- `ping`
- `fonts-noto-color-emoji`

# Config
## Output with pango markup available
```INI
[my-external-ip]
command=$SCRIPT_DIR/my-external-ip
markup=pango
interval=60
color=#aaffaa
# external_ip_provider=ip.yunohost.org
```

## Classic output
```INI
[my-external-ip]
command=$SCRIPT_DIR/my-external-ip
interval=60
color=#aaffaa
# external_ip_provider=ipv4.icanhazeip.com
```
18 changes: 18 additions & 0 deletions ping/i3blocks.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Standard ping Internet 8.8.8.8
[ping]
command=$SCRIPT_DIR/ping
interval=10
# markup=pango
# ping_target=8.8.8.8
# color=#aaffaa
# color_error=#ff0000

# ping LAN host named Nas
[ping]
command=$SCRIPT_DIR/ping
interval=10
# markup=pango
label=Nas
ping_target=192.168.1.234
# color=#aaffaa
# color_error=#ff0000
72 changes: 72 additions & 0 deletions ping/ping
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python3
#
# Copyright (C) 2024 Gregory David <dev@groolot.net>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import subprocess
import sys

CONFIG: dict = dict(os.environ)
LABEL: str = CONFIG.get("label", "")
PANGO_MARKUP: bool = CONFIG.get("markup", "") == "pango"
PING_TARGET: str = CONFIG.get("ping_target", "8.8.8.8")
DEFAULT_COLOR: str = CONFIG.get("color", "#ffffff")
COLOR_ERROR: str = CONFIG.get("color_error", "#ff0000")


def print_format(
long_text: str,
short_text: str,
color: str = DEFAULT_COLOR,
pango: bool = PANGO_MARKUP,
):
if pango:
print(f"<span color='{color}'>{': ' if LABEL else ''}{long_text}</span>")
print(f"<span color='{color}'>{short_text}</span>")
else:
print(f"{': ' if LABEL else ''}{long_text}")
print(short_text)
print(color)


def ping_block():
try:
ping_command = "ping"
subprocess.run(
[ping_command, "-n", "-w", "1", "-c", "1", PING_TARGET],
universal_newlines=True,
check=True,
capture_output=True,
)
print_format(long_text="🟢", short_text="🟢")
return 0
except FileNotFoundError:
print_format(
long_text=f"{ping_command} command not found",
short_text=" ",
)
return 33
except subprocess.CalledProcessError:
print_format(
long_text="🔴 unreachable",
short_text="🔴",
color=COLOR_ERROR,
)
return 0


if __name__ == "__main__":
sys.exit(ping_block())
Binary file added ping/ping-error.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 ping/ping-ko.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 ping/ping-ok.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.