Uptime-Kuma-Web-API icon indicating copy to clipboard operation
Uptime-Kuma-Web-API copied to clipboard

Unable to push many monitors at once

Open hillout opened this issue 1 year ago • 3 comments

Hi, thanks for your project.

I made a script which is creating many monitors at once, but max that I get is 6 successful, the rest of them which fails get reply with json: {"detail":"'version'"}, logs are saying: [7] [CRITICAL] 'version'. I tried to increase pauses to 30 sec between requests, but no luck.

Here is my script:

#!/bin/bash

UPTIME_USER="admin"
UPTIME_API_PASSWORD="admin"
UPTIME_URL="http://localhost:8000"
TOKEN_FILE=./token

# Putting the list of desired monitors
MONITOR_TYPE="ping"
project_id_array=(
    "server01.domain.org"
    "server02.domain.org"
    "server03.domain.org"
    "server04.domain.org"
    "server05.domain.org"
    "server06.domain.org"
    "server07.domain.org"
    "server08.domain.org"
)

# Check the file with token
if [ -f "${TOKEN_FILE}" ]; then
    # If exists, read from it
    TOKEN=$(cat "${TOKEN_FILE}")
else
    # If there is no file, get a new token..
    TOKEN=$(curl -s -L -X POST -H "Content-Type: application/x-www-form-urlencoded" \
        --data "username=${UPTIME_USER}&password=${UPTIME_API_PASSWORD}" \
        "${UPTIME_URL}/login/access-token" | jq -r '.access_token')

    # ..and save it to token file
    echo "${TOKEN}" > "${TOKEN_FILE}"
fi


# Cycle for adding monitors
for project_id in "${project_id_array[@]}"; do
    echo "Create monitor in Uptime Kuma for ${project_id}"

    RESULT=$(curl -s -X POST -H "Content-Type: application/json" \
        -H "Authorization: Bearer ${TOKEN}" \
        -d '{"name": "'${project_id}'",
            "pathName": "'${project_id}'",
            "hostname": "'${project_id}'",
            "maxretries": 3,
            "active": true,
            "forceInactive": false,
            "type": "'${MONITOR_TYPE}'",
            "timeout": 0,
            "interval": 60,
            "retryInterval": 60,
            "resendInterval": 0,
            "packetSize": 56
            }' \
        "${UPTIME_URL}/monitors")

    # Check if token is valid, if no - delete the token file and ask user to run the script again
    if [[ $RESULT == *"invalid credentials"* ]]; then
        echo "Invalid Credentials"
        rm -f "${TOKEN_FILE}"
        echo "Please run the script again"
        break
    fi

    # If token is OK, show monitor ID
    monitor_id=$(echo "$RESULT" | jq -r '.monitorID')
    echo "Monitor ID: ${monitor_id}"
# Making a pause between next requests
    sleep 3
done

Deployment:

version: "3.9"
services:
  kuma:
    container_name: uptime-kuma
    image: louislam/uptime-kuma:latest
    ports:
      - "3001:3001"
    restart: always
    volumes:
      - uptime-kuma:/app/data

  api:
    container_name: backend
    image: medaziz11/uptimekuma_restapi
    volumes:
      - api:/db
    restart: always
    environment:
      - KUMA_SERVER=http://uptime-kuma:3001
      - KUMA_USERNAME=admin
      - KUMA_PASSWORD=Cdewsxzaq2002
      - ADMIN_PASSWORD=admin
      - ACCESS_TOKEN_EXPIRATION=60
    depends_on:
      - kuma
    ports:
      - "8000:8000"
volumes:
  uptime-kuma:
  api:

hillout avatar Dec 05 '23 16:12 hillout

I deployed older kuma version, then it works. Seems version in error message is a hint for incompatible API.

Morishiri avatar Dec 20 '23 13:12 Morishiri

@Morishiri Hi, what exact version did you use?

hillout avatar Dec 20 '23 16:12 hillout

@hillout I used 1.21.3

Morishiri avatar Dec 21 '23 08:12 Morishiri

@hillout Found this as a working solution. https://github.com/MedAziz11/Uptime-Kuma-Web-API/pull/59#issuecomment-2178609697

w00dst0ck avatar Oct 16 '24 14:10 w00dst0ck