0

Get system metrics

by
Published today

Get a Network Controller system metric: CPU, disk, disk IO, memory or uptime.

Script ericsson_ep5g Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 hours ago
1
//native
2

3
/**
4
 * Get system metrics
5
 * Get a Network Controller system metric: cpu, disk, diskIO and memory (averaged over the last 5 minutes), their *Usage variants (time series over a timespan, default 1h), or uptime (days). Returns one entry per Network Controller, or only node_id's when set. The timespan only applies to the *Usage metrics.
6
 */
7
export async function main(
8
  auth: RT.EricssonEp5g,
9
  metric:
10
    | "cpu"
11
    | "cpuUsage"
12
    | "disk"
13
    | "diskUsage"
14
    | "diskIO"
15
    | "diskIOUsage"
16
    | "memory"
17
    | "memoryUsage"
18
    | "uptime",
19
  node_id: string | undefined,
20
  timespan: "1h" | "4h" | "8h" | "24h" | "72h" | "168h" | "336h" | undefined
21
) {
22
  const base = auth.base_url.replace(/\/+$/, "")
23
  const url = new URL(
24
    `${base}/v1/organization/${encodeURIComponent(auth.organization)}/site/${encodeURIComponent(auth.site)}/system/metrics/${metric}`
25
  )
26
  if (node_id != null && node_id !== "") {
27
    url.searchParams.append("nodeId", node_id)
28
  }
29
  if (metric.endsWith("Usage") && timespan) {
30
    url.searchParams.append("timespan", timespan)
31
  }
32

33
  const response = await fetch(url, {
34
    headers: {
35
      "X-Api-Key": auth.api_key,
36
      ...(auth.user_role ? { "X-User-Role": auth.user_role } : {}),
37
      Accept: "application/json",
38
    },
39
  })
40

41
  if (!response.ok) {
42
    throw new Error(`${response.status} ${await response.text()}`)
43
  }
44

45
  return await response.json()
46
}
47