0

Acknowledge event

by
Published today

Close, acknowledge, comment, change severity, suppress or unsuppress problem events.

Script zabbix Verified

The script

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

3
/**
4
 * Acknowledge event
5
 * Update problem events (event.acknowledge): close, acknowledge, add a message (when message is set), change severity, suppress until a time (ISO 8601; empty = indefinitely; setting it implies suppress) or unsuppress. Closing only works on events whose trigger allows manual close, and closing or changing severity needs read-write rights on the trigger. Returns the updated eventids.
6
 */
7
export async function main(
8
  auth: RT.Zabbix,
9
  event_ids: string[],
10
  close: boolean | undefined,
11
  acknowledge: boolean | undefined,
12
  message: string | undefined,
13
  new_severity:
14
    | "not_classified"
15
    | "information"
16
    | "warning"
17
    | "average"
18
    | "high"
19
    | "disaster"
20
    | undefined,
21
  suppress: boolean | undefined,
22
  suppress_until: string | undefined,
23
  unsuppress: boolean | undefined
24
) {
25
  // A suppress_until on its own means suppress until then.
26
  const doSuppress = suppress || !!suppress_until
27
  if (doSuppress && unsuppress) {
28
    throw new Error("Set either suppress or unsuppress, not both")
29
  }
30
  const severities = [
31
    "not_classified",
32
    "information",
33
    "warning",
34
    "average",
35
    "high",
36
    "disaster",
37
  ]
38

39
  let action = 0
40
  const params: { [key: string]: any } = { eventids: event_ids }
41
  if (close) {
42
    action |= 1
43
  }
44
  if (acknowledge) {
45
    action |= 2
46
  }
47
  if (message) {
48
    action |= 4
49
    params.message = message
50
  }
51
  if (new_severity) {
52
    const severity = severities.indexOf(new_severity)
53
    if (severity < 0) {
54
      throw new Error(`Invalid new_severity: ${new_severity}`)
55
    }
56
    action |= 8
57
    params.severity = severity
58
  }
59
  if (doSuppress) {
60
    action |= 32
61
    params.suppress_until = 0
62
    if (suppress_until) {
63
      const until = Math.floor(new Date(suppress_until).getTime() / 1000)
64
      if (Number.isNaN(until)) {
65
        throw new Error(`Invalid suppress_until: ${suppress_until}`)
66
      }
67
      params.suppress_until = until
68
    }
69
  }
70
  if (unsuppress) {
71
    action |= 64
72
  }
73
  if (action === 0) {
74
    throw new Error("Select at least one action or set a message")
75
  }
76
  params.action = action
77

78
  return await call(auth, "event.acknowledge", params)
79
}
80

81
async function call(auth: RT.Zabbix, method: string, params: any) {
82
  const response = await fetch(
83
    `${auth.base_url.replace(/\/+$/, "").replace(/\/api_jsonrpc\.php$/, "")}/api_jsonrpc.php`,
84
    {
85
      method: "POST",
86
      headers: {
87
        Authorization: `Bearer ${auth.token}`,
88
        "Content-Type": "application/json-rpc",
89
        Accept: "application/json",
90
      },
91
      body: JSON.stringify({ jsonrpc: "2.0", method, params, id: 1 }),
92
    }
93
  )
94

95
  if (!response.ok) {
96
    throw new Error(`${response.status} ${await response.text()}`)
97
  }
98

99
  const text = await response.text()
100
  let data: any
101
  try {
102
    data = JSON.parse(text)
103
  } catch {
104
    throw new Error(
105
      `Not a JSON-RPC response (check base_url): ${text.slice(0, 300)}`
106
    )
107
  }
108
  if (data.error) {
109
    throw new Error(
110
      `${data.error.code} ${data.error.message} ${data.error.data ?? ""}`
111
    )
112
  }
113
  return data.result
114
}
115