boredazfcuk / docker-icloudpd

An Alpine Linux container for the iCloud Photos Downloader command line utility
1.76k stars 167 forks source link

Using with Uptime Kuma? #633

Closed johnny2678 closed 1 month ago

johnny2678 commented 1 month ago

Curious if anyone has this configured to alert Uptime Kuma if new downloads haven't been detected in X days? Or if a reauth is needed?

I can configure Kuma with an alert if a push hasn't been received in X days but unclear to me how to configure the push to send in iCloudpd.

Kind of like the webhook but the Kuma push url doesn't follow the webhook url format.

johnny2678 commented 1 month ago

I've been playing around with this some more. Currently trying to trigger the following webhook: http://192.168.1.11:3001/api/push/Zr0Jk9M2sY?status=up&msg=OK&ping=

These are how I have my variables configured:

webhook_https=false
webhook_id=Zr0Jk9M2sY
webhook_path=/api/push/
webhook_port=3001
webhook_server=192.168.1.11
webhook_body=?status=up&msg=OK&ping=

This is the debug output:

2024-08-31 18:05:59 DEBUG    Webhook server: 192.168.1.11
2024-08-31 18:05:59 DEBUG    Webhook port: 3001
2024-08-31 18:05:59 DEBUG    Webhook path: /api/push/
2024-08-31 18:05:59 DEBUG    Webhook ID: Zr0Jk9M2sY
2024-08-31 18:05:59 DEBUG    Webhook notification URL: http://192.168.1.11:3001/api/push/Zr0Jk9M2sY
2024-08-31 18:05:59 DEBUG    Webhook body keyword: ?status=up&msg=OK&ping=
2024-08-31 18:05:59 DEBUG    Startup notification: Enabled
2024-08-31 18:05:59 INFO     Sending Webhook startup notification
2024-08-31 18:05:59 ERROR    Webhook startup notification failed with http status code: 404 and curl exit code: 0
2024-08-31 18:05:59 ERROR    ***** Please post the above debug log, along with a description of your problem, here: https://github.com/boredazfcuk/docker-icloudpd/issues *****

What needs to change to make this work?

boredazfcuk commented 1 month ago

Webhook requires data to be posted to it in a JSON format.

Looking at how Uptime Kuma works, the client needs to repeatedly hit the Uptime Kuma URL to let it know it's alive:

from https://github.com/louislam/uptime-kuma/blob/master/extra/push-examples/bash-curl/index.sh

#!/bin/bash
# Filename: index.sh
PUSH_URL="https://example.com/api/push/key?status=up&msg=OK&ping="
INTERVAL=60

while true; do
    curl -s -o /dev/null $PUSH_URL
    echo "Pushed!"
    sleep $INTERVAL
done

That hits the server every minute to let it know it's alive.

Notifications in icloudpd are triggered by events. Even if you got it working using Webhook, it would only hit Uptime Kuma each time it syncs and the shortest period you can sync on is 6 hours.

The container uses Docker health checks to make sure everything is OK. If something goes wrong, it will set the status to unhealthy. You can then use this container to restart it when it's in an unhealthy state: https://hub.docker.com/r/willfarrell/autoheal

Or you can check the health status from a command line (you'll need jq installed) with docker inspect icloudpd | jq -r .[].State.Health.Status Then in your monitoring script, you can trigger the curl command to hit your Uptime Kuma URL as long as the health status returned is 'healthy'. I'm not familiar with Uptime Kuma, but I'd guess you could change:

PUSH_URL="https://example.com/api/push/key?status=down&msg=Unhealthy&ping="

before hitting the URL to let it know that the container has failed.

johnny2678 commented 1 month ago

Thanks for responding. You can set Uptime Kuma thresholds to whatever you want. So if I set a threshold at 86401 seconds... as long as icloudpd can ping it once a day it will be considered up.

But thanks for posting the docker inspect command... I can script it up in bash to make this work.

Assuming that a container awaiting re-auth will be in some other status than healthy?

boredazfcuk commented 1 month ago

Once the cookie expires, container will be marked as "unhealthy", I think.

johnny2678 commented 1 month ago

Thanks again for your help.

This is the script I came up with and I have it set to run every minute via cron. Dropping here in case anyone else wants to make this work. Will close.

#!/bin/bash

# Array of container names
containers=("icloudpd-me" "icloudpd-wife")

# Array of corresponding Uptime KUMA URLs
urls=("http://192.168.1.11:3001/api/push/Zr0Jk9M2sY" "http://192.168.1.11:3001/api/push/CrI0N5Xxay")

# Check if the number of containers matches the number of URLs
if [ ${#containers[@]} -ne ${#urls[@]} ]; then
    echo "Error: The number of containers does not match the number of URLs."
    exit 1
fi

# Loop through each container in the array
#for i in "${!containers[@]}"; do
index=0
while [ $index -lt ${#containers[@]} ]; do
    container=${containers[$index]}
    url=${urls[$index]}

    # Run docker inspect command and filter the output for each container
    status=$(docker inspect "$container" | jq -r '.[].State.Health.Status')

    echo "$container: $status"

    # Check if the status is "healthy"
    if [ "$status" = "healthy" ]; then
        curl -s -o /dev/null "${url}?status=up&msg=OK&ping="
        echo "sending healthy ping"
    else
        curl -s -o /dev/null "${url}?status=down&msg=ERROR-CHECK-CONTAINER&ping="
        echo "sending unhealthy ping"
    fi

    ((index++))
done

edit: to get the URLs from Uptime Kuma, add a PUSH monitor with a 90 second heartbeat and set notification to your preference