0

Delete maintenance

by
Published today

Delete maintenance windows by ID.

Script zabbix Verified

The script

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

3
/**
4
 * Delete maintenance
5
 * Delete one or more maintenance windows by maintenanceid (maintenance.delete). Returns the deleted maintenanceids.
6
 */
7
export async function main(auth: RT.Zabbix, maintenance_ids: string[]) {
8
  return await call(auth, "maintenance.delete", maintenance_ids)
9
}
10

11
async function call(auth: RT.Zabbix, method: string, params: any) {
12
  const response = await fetch(
13
    `${auth.base_url.replace(/\/+$/, "").replace(/\/api_jsonrpc\.php$/, "")}/api_jsonrpc.php`,
14
    {
15
      method: "POST",
16
      headers: {
17
        Authorization: `Bearer ${auth.token}`,
18
        "Content-Type": "application/json-rpc",
19
        Accept: "application/json",
20
      },
21
      body: JSON.stringify({ jsonrpc: "2.0", method, params, id: 1 }),
22
    }
23
  )
24

25
  if (!response.ok) {
26
    throw new Error(`${response.status} ${await response.text()}`)
27
  }
28

29
  const text = await response.text()
30
  let data: any
31
  try {
32
    data = JSON.parse(text)
33
  } catch {
34
    throw new Error(
35
      `Not a JSON-RPC response (check base_url): ${text.slice(0, 300)}`
36
    )
37
  }
38
  if (data.error) {
39
    throw new Error(
40
      `${data.error.code} ${data.error.message} ${data.error.data ?? ""}`
41
    )
42
  }
43
  return data.result
44
}
45