MedAziz11 / Uptime-Kuma-Web-API

Uptime Kuma REST API
199 stars 42 forks source link

Unable to push many monitors at once #64

Open hillout opened 9 months ago

hillout commented 9 months ago

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:
Morishiri commented 9 months ago

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

hillout commented 9 months ago

@Morishiri Hi, what exact version did you use?

Morishiri commented 9 months ago

@hillout I used 1.21.3