Skip to content

Instantly share code, notes, and snippets.

@rmueck
Last active August 19, 2023 08:56
Show Gist options
  • Save rmueck/415f1a271a0fc483491a15e401b32d77 to your computer and use it in GitHub Desktop.
Save rmueck/415f1a271a0fc483491a15e401b32d77 to your computer and use it in GitHub Desktop.
Shoutcast monitoring for connected source using monit and M/monit
#!/usr/bin/python3
import requests
import xml.etree.ElementTree as ET
import subprocess
import sys
import datetime
import time
# Do not start sc_cast if in blackout timeframe - DSL connection is not up during this frame
def blackout_execution(start_time, end_time):
current_time = datetime.datetime.now().time()
return not (start_time <= current_time <= end_time)
# Define the time range when execution is not allowedA
# Check FritzBox events to adjust!
start_time = datetime.time(2, 10, 0) # 03:10:00 (03:10 AM)
end_time = datetime.time(2, 15, 0) # 07:00:00 (03:15 AM)
# Check if execution is allowed at the current time
if blackout_execution(start_time, end_time):
print("Execution is allowed - DSL supposed to be up!.")
# Put your code here that should be executed outside the disallowed timeframe.
else:
print("Execution is not allowed at this time (DSL Blackout). We sleep for 180s")
# Optionally, you can put some code here to handle this case, such as waiting or exiting the program.
time.sleep(180) # Sleep for 3 minutes so DSL should be up on RadioDJ system.
# sys.exit(2)
def is_execution_allowed(disallowed_weekday, start_time, end_time):
current_time = datetime.datetime.now().time()
current_weekday = datetime.datetime.today().weekday()
return not (current_weekday == disallowed_weekday and start_time <= current_time <= end_time)
# Define the time range when execution is not allowed Primary used for ALPHA'S CRAZY SOUNDS on Thursday
start_time = datetime.time(19, 59, 00) # 19:55:00 (07:55 PM)
end_time = datetime.time(20, 45, 0) # 20:45:00 (08:45 PM)
# Define the disallowed weekday (0 = Monday, 6 = Sunday)
# 0=monday,1=tuesday,2=wednesday,3=thusrday,4=friday,5=saturday, 6=sunday
disallowed_weekday = 5 # Do not allow execution on Thursday (change this as needed)
# Check if execution is allowed at the current time and weekday
if is_execution_allowed(disallowed_weekday, start_time, end_time):
print("Execution is allowed - checking for connected source.")
# Put your code here that should be executed outside the disallowed timeframe and weekday.
else:
print("Execution is not allowed at this time or weekday.")
# Optionally, you can put some code here to handle this case, such as waiting or exiting the program.
sys.exit(1)
# Example usage
host = "localhost"
# ports = [7000, 8000, 9000] # List of ports to monitor
ports = [8000] # List of ports to monitor
backup_started = False
stream_status_retrieved = False
ret_source = 0
def is_server_reachable(host, port):
url = f"http://{host}:{port}/7.html"
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
return True
except requests.exceptions.RequestException:
pass
return False
def get_stream_status(host, port):
url = f"http://{host}:{port}/statistics"
response = requests.get(url)
if response.status_code == 200:
root = ET.fromstring(response.text)
stream_status_element = root.find(".//STREAMSTATUS")
if stream_status_element is not None:
stream_status_value = stream_status_element.text
return stream_status_value
return None
def start_backup_stream(port):
config_file = f"/usr/local/etc/sc_trans2-{port}.conf"
command = ["sudo", "-u", "nobody", "sc_trans2", "daemon", config_file]
subprocess.Popen(command)
for port in ports:
if not is_server_reachable(host, port):
print(f"Error: The server {host}:{port} is not responding.")
continue
stream_status = get_stream_status(host, port)
if stream_status is not None:
stream_status_retrieved = True
if stream_status == "1":
backup_started = True
print(f"Stream on port {port} is currently active and a source is connected.")
ret_source = 0
elif stream_status == "0":
print(f"Stream on port {port} is up but no source is connected. Starting backup stream.")
start_backup_stream(port)
backup_started = True
ret_source = int(port) + int(ret_source)
print(f"return cod e: {ret_source} ")
if stream_status_retrieved and not backup_started:
sys.exit(1)
print(f"Exit code: {ret_source} ")
# sys.exit(0)
sys.exit(ret_source)
group radio
check program sc_source with path /usr/local/bin/sc-mon
with timeout 240 seconds
# We started a sc_cast instance - this will be reported!
if content = "Starting backup stream" then alert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment