MedAziz11/Uptime-Kuma-Web-API

Unable to push many monitors at once

hillout opened this issue · 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:

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

@Morishiri Hi, what exact version did you use?

@hillout I used 1.21.3