1
0
Fork 0
mirror of https://github.com/roles-ansible/ansible_role_sway.git synced 2024-08-16 13:49:49 +02:00

Create custom ping module to detect internet

This commit is contained in:
L3D 2024-07-31 16:11:08 +02:00
parent a4a8a53ba5
commit 926c74b6e2
Signed by: l3d
GPG key ID: CD08445BFF4313D1
5 changed files with 68 additions and 0 deletions

View file

@ -27,6 +27,7 @@ sway__waybar_modules_right:
- "custom/weather"
- "pulseaudio"
- "network"
- "custom/ping"
- "cpu"
- "memory"
- "disk"

View file

@ -27,6 +27,16 @@
group: "{{ swayusr.user }}"
backup: true
- name: Deploy ping script
become: true
ansible.builtin.template:
src: 'templates/ping.py.j2'
dest: "{{ swayusr.home }}/.config/waybar/ping.py"
mode: '0750'
owner: "{{ swayusr.user }}"
group: "{{ swayusr.user }}"
backup: true
- name: Deploy waybar stylesheet
become: true
ansible.builtin.template:

43
templates/ping.py.j2 Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python
"""
ping waybar widget
{{ ansible_managed }}
"""
import subprocess
import json
import sys
def check_internet():
"""
Try to ping a host in the internet,
then return true or false
"""
try:
subprocess.check_call(['ping', '-c', '2', 'c3woc.de'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return True
except subprocess.CalledProcessError:
return False
def main():
"""
parsing internet status and create output
"""
internet_status = check_internet()
if internet_status:
status = {
"text": "Online",
"tooltip": "Internet connection is active",
"class": "online"
}
else:
status = {
"text": "Offline",
"tooltip": "No internet connection",
"class": "offline"
}
print(json.dumps(status))
sys.stdout.flush()
if __name__ == "__main__":
main()

View file

@ -280,3 +280,11 @@ label:focus {
#scratchpad.empty {
background-color: transparent;
}
#custom-ping.connected {
color: #00FF00; /* Grün für verbunden */
}
#custom-ping.disconnected {
color: #FF0000; /* Rot für nicht verbunden */
}

View file

@ -166,6 +166,12 @@
"format": "",
"on-click": "wlogout",
"tooltip": false
},
"custom/ping": {
"format": "{}",
"exec": "{{ swayusr.home }}/.config/waybar/ping.py",
"interval": 60,
"return-type": "json"
}
}