Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

servicefailure.py

From Randomness wiki
Revision as of 13:49, 12 September 2024 by Derg (talk | contribs) (Derg moved page Servicefailure.py to servicefailure.py without leaving a redirect: Misspelled title)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
#!/usr/bin/python3
import subprocess
import requests
import sys
import os


service = sys.argv[1]
proc = subprocess.Popen("hostname", stdout=subprocess.PIPE, shell=True)
hostname = proc.communicate()[0].decode("utf-8").strip()
status = f"Service {service} failed on {hostname}"
print(status)

try:
    import redis

    r = redis.Redis()
    r.publish("boterrorlocal", status)
except Exception as e:
    print(e)

proc = subprocess.Popen(
    f"systemctl --user -M pi@ status {service}", stdout=subprocess.PIPE, shell=True
)
output = proc.communicate()[0].decode("utf-8").strip()

logo = "https://cdn.discordapp.com/attachments/1170631155905744896/1175721829906788472/systemd-logomark.png"
user = "systemd"

hook = "https://discord.com/api/webhooks/1175604004059877426/[REDACTED]"
color = 0xFF0000
o = dict(
    embeds=[dict(color=color, title=status, description=f"```rb\n{output}\n```")],
    username=user,
    avatar_url=logo,
)
d = requests.post(hook, params=dict(wait="true"), json=o)
print(d.text)