0

Create maintenance

by
Published today

Create a one-time maintenance window on hosts or host groups.

Script zabbix Verified

The script

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

3
/**
4
 * Create maintenance
5
 * Create a one-time maintenance window (maintenance.create) on host IDs and/or host group IDs, starting at start_time (ISO 8601, default now) for duration_minutes (at least 5; Zabbix rounds times down to the minute). Data keeps being collected (problems are suppressed) unless without_data_collection is set. Names must be unique: include the flow job ID, for example, so list_maintenances can find it again. Returns the maintenanceid.
6
 */
7
export async function main(
8
  auth: RT.Zabbix,
9
  name: string,
10
  duration_minutes: number,
11
  host_ids: string[] | undefined,
12
  group_ids: string[] | undefined,
13
  start_time: string | undefined,
14
  without_data_collection: boolean | undefined,
15
  description: string | undefined
16
) {
17
  if (!host_ids?.length && !group_ids?.length) {
18
    throw new Error("Provide at least one host ID or host group ID")
19
  }
20
  // Zabbix rounds times down to the minute: start on the minute and round the
21
  // end up, so the window covers at least the requested duration.
22
  const requested = start_time ? new Date(start_time).getTime() : Date.now()
23
  const start = Math.floor(requested / 60000) * 60
24
  if (Number.isNaN(start)) {
25
    throw new Error(`Invalid start_time: ${start_time}`)
26
  }
27
  const end = Math.ceil((requested + duration_minutes * 60000) / 60000) * 60
28
  const period = end - start
29

30
  const params: { [key: string]: any } = {
31
    name,
32
    maintenance_type: without_data_collection ? 1 : 0,
33
    active_since: start,
34
    active_till: start + period,
35
    timeperiods: [{ timeperiod_type: 0, start_date: start, period }],
36
    description: description || undefined,
37
  }
38
  if (host_ids?.length) {
39
    params.hosts = host_ids.map((hostid) => ({ hostid }))
40
  }
41
  if (group_ids?.length) {
42
    params.groups = group_ids.map((groupid) => ({ groupid }))
43
  }
44

45
  const result = await call(auth, "maintenance.create", params)
46
  return { maintenanceid: result.maintenanceids[0] }
47
}
48

49
async function call(auth: RT.Zabbix, method: string, params: any) {
50
  const response = await fetch(
51
    `${auth.base_url.replace(/\/+$/, "").replace(/\/api_jsonrpc\.php$/, "")}/api_jsonrpc.php`,
52
    {
53
      method: "POST",
54
      headers: {
55
        Authorization: `Bearer ${auth.token}`,
56
        "Content-Type": "application/json-rpc",
57
        Accept: "application/json",
58
      },
59
      body: JSON.stringify({ jsonrpc: "2.0", method, params, id: 1 }),
60
    }
61
  )
62

63
  if (!response.ok) {
64
    throw new Error(`${response.status} ${await response.text()}`)
65
  }
66

67
  const text = await response.text()
68
  let data: any
69
  try {
70
    data = JSON.parse(text)
71
  } catch {
72
    throw new Error(
73
      `Not a JSON-RPC response (check base_url): ${text.slice(0, 300)}`
74
    )
75
  }
76
  if (data.error) {
77
    throw new Error(
78
      `${data.error.code} ${data.error.message} ${data.error.data ?? ""}`
79
    )
80
  }
81
  return data.result
82
}
83