Backup Server API access

Hi,

I'm trying to access the PBS api through a little script.

  • If I access them from the browser, I can see all of the data form all of the three api nodes
  • If I access them from the script, I have null responses from two of them (see next)
  • I'm using a user/password on the browser, apitoken in the script (apitoken added to the same user)
  • I gave Audit permissions, at root path, both at the user and at the apitoken.
  • The apitoken works as I can scrape one of the api nodes, I should see the others or get authorization errors, not null data.

Script:

Code:
import { useTranslation } from "next-i18next";


import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";


export default function Component({ service }) {
  const { t } = useTranslation();


  const { widget } = service;


  const { data: datastoreData, error: datastoreError } = useWidgetAPI(widget, "status/datastore-usage");
  const { data: tasksData, error: tasksError } = useWidgetAPI(widget, "nodes/localhost/tasks");
  const { data: hostData, error: hostError } = useWidgetAPI(widget, "nodes/localhost/status");


  if (datastoreError || tasksError || hostError) {
    const finalError = tasksError ?? datastoreError ?? hostError;
    return <Container service={service} error={finalError} />;
  }


  if (!datastoreData || !tasksData || !hostData) {
    return (
      <Container service={service}>
        <Block label="proxmoxbackupserver.datastore_usage" />
        <Block label="proxmoxbackupserver.failed_tasks_24h" />
        <Block label="proxmoxbackupserver.cpu_usage" />
        <Block label="proxmoxbackupserver.memory_usage" />
      </Container>
    );
  }


  const datastoreUsage = datastoreData.data[0].used / datastoreData.data[0].total * 100;
  const cpuUsage = hostData.data.cpu * 100;
  const memoryUsage = hostData.data.memory.used / hostData.data.memory.total * 100;
  const failedTasks = tasksData.total >= 100 ? "99+" : tasksData.total;


  return (
    <Container service={service}>
      <Block label="proxmoxbackupserver.datastore_usage" value={t("common.percent", { value: datastoreUsage })} />
      <Block label="proxmoxbackupserver.failed_tasks_24h" value={failedTasks} />
      <Block label="proxmoxbackupserver.cpu_usage" value={t("common.percent", { value: cpuUsage })} />
      <Block label="proxmoxbackupserver.memory_usage" value={t("common.percent", { value: memoryUsage })} />
    </Container>
  );
}

I get the following results (from the Chrome DevTool on the page where the script is running):

Code:
request:
type=proxmoxbackupserver&group=Management&service=Proxmox+Backup+Server&endpoint=status%2Fdatastore-usage
response:
{"data":[]}

Code:
request:
type=proxmoxbackupserver&group=Management&service=Proxmox+Backup+Server&endpoint=nodes%2Flocalhost%2Ftasks
response:
{"total":0,"data":[]}

Code:
request:
type=proxmoxbackupserver&group=Management&service=Proxmox+Backup+Server&endpoint=nodes%2Flocalhost%2Fstatus
response:
{
    "data": {
        "cpu": 0.0036681950812839093,
        "cpuinfo": {
            "cpus": 12,
            "model": "Intel(R) Core(TM) i7-8700T CPU @ 2.40GHz",
            "sockets": 1
        },
        "info": {
            "fingerprint": "fingerprint"
        },
        "kversion": "Linux 6.2.16-10-pve #1 SMP PREEMPT_DYNAMIC PMX 6.2.16-10 (2023-08-18T11:42Z)",
        "loadavg": [
            0.07,
            0.09,
            0.02
        ],
        "memory": {
            "free": 23020175360,
            "total": 25019404288,
            "used": 1999228928
        },
        "root": {
            "avail": 222679269376,
            "total": 232441511936,
            "used": 9762242560
        },
        "swap": {
            "free": 0,
            "total": 0,
            "used": 0
        },
        "uptime": 109568,
        "wait": 0.0
    }
}
 
I'm also trying using postman, but always get:

Code:
authentication failed - no authentication credentials provided.

When trying to access with code:

Code:
curl --location 'https://pbs1.domain.com:8007/api2/json/status/datastore-usage' \
--header 'Authorization: PVEAPIToken=user@pbs!token-id=token-secret' \
--data ''

What's wrong?
 
Hi, with PBSAPIToken now the error is:

authentication failed - invalid token name

Code:
curl --location 'https://pbs1.domain.com:8007/api2/json/status/datastore-usage' \
--header 'Authorization: PBSAPIToken=user@pbs!token-id=token-secret'
make sure to have the correct format for the token PBSAPIToken=user@realm!tokenname:tokensecret.

Edit: fix incorrect separator
 
Last edited:
  • Like
Reactions: Urbaman
Ok, now it seems to work on all of the three targets, thank you very much.

Now to the first point: can't understand why the script gets null data from two of the targets, if targets and permisisons work. Is there some situation in which a null data can be fetched instead of the full data?
 
Well, I suspect (haven't looked at the code) this is probably due to missing permissions for that user/token on that API endpoint. Did you test this using curl as well, using the correct token?
 
Hi,

I am using the same api/token used in postman (all three targets work) for all of the targets in the script, and in the script only one target (nodes/localhost/status) work fetching data, while the others seem to work (no auth problems received), just null data fetched.
Well, if the API response in Postman contains data for all 3 PBS hosts, using the same token as you use in the script, then I suspect there might be an issue with how your script parses the response.
 
  • Like
Reactions: Urbaman
Ok,

Furhter debugging showed that probably the user had conflicting permissions (Audit on / and DatastoreBackup on /datastore/Backups) and probably they somehow conflicted in the way the script calls the apis.

Using a new dedicated user with just Audit on / permissions made te script work.

Thank you very much for your support!
 
  • Like
Reactions: Chris

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!