Skip to main content

monitoring

import subprocess
import requests

def run_ssh_command():
    # SSH command
    ssh_command = "ssh -p 54231 monitoring@netvm1.shiny.space"
    
    # Run the SSH command and capture the output
    try:
        result = subprocess.check_output(ssh_command, shell=True, text=True, stderr=subprocess.STDOUT)
        # Filter out unwanted parts of the output
        filtered_output = [line.strip() for line in result.split('\n') if line.strip() and "PTY allocation request failed" not in line and "Connection to" not in line and "Pseudo-terminal will not be allocated" not in line]
        return filtered_output

    except subprocess.CalledProcessError as e:
        return [f"Error: {e.output.strip()}"]

def send_result_to_ntfy(result):
    # Ntfy.sh endpoint
    ntfy_url = "https://ntfy.sh/shinyspace"
    
    # Check if the first line is "OK"
    if result and result[0].strip() == "OK":
        print("SSH command succeeded. Sending nothing to ntfy.sh.")
    else:
        print("SSH command failed or the first line is not 'OK'. Sending error to ntfy.sh.")
        payload = {"error": result}
        # POST request with the result or error
        response = requests.post(ntfy_url, data=payload)
    
        # Check the response status
        if response.status_code == 200:
            print("Result sent successfully to ntfy.sh")
        else:
            print(f"Failed to send result. Status code: {response.status_code}")

if __name__ == "__main__":
    # Run SSH command
    ssh_result = run_ssh_command()
    
    # Send result or error to ntfy.sh
    send_result_to_ntfy(ssh_result)