0

Get problems

by
Published today

List problems on hosts or host groups with severity, acknowledged and suppressed state.

Script zabbix Verified

The script

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

3
/**
4
 * Get problems
5
 * List trigger problems (problem.get) on host IDs or host group IDs, with an optional minimum severity and creation time window (ISO 8601), including suppressed and acknowledged state. Unresolved only unless include_recently_resolved. Pages by event ID: pass next_eventid_from back as eventid_from to fetch the next page.
6
 */
7
export async function main(
8
  auth: RT.Zabbix,
9
  host_ids: string[] | undefined,
10
  group_ids: string[] | undefined,
11
  min_severity:
12
    | "not_classified"
13
    | "information"
14
    | "warning"
15
    | "average"
16
    | "high"
17
    | "disaster"
18
    | undefined,
19
  time_from: string | undefined,
20
  time_till: string | undefined,
21
  include_recently_resolved: boolean | undefined,
22
  limit: number | undefined,
23
  eventid_from: string | undefined
24
) {
25
  const max = Math.max(1, limit || 100)
26
  const severities = [
27
    "not_classified",
28
    "information",
29
    "warning",
30
    "average",
31
    "high",
32
    "disaster",
33
  ]
34
  const params: { [key: string]: any } = {
35
    output: [
36
      "eventid",
37
      "objectid",
38
      "name",
39
      "severity",
40
      "clock",
41
      "r_eventid",
42
      "acknowledged",
43
      "suppressed",
44
    ],
45
    selectSuppressionData: "extend",
46
    sortfield: "eventid",
47
    sortorder: "ASC",
48
    limit: max + 1,
49
  }
50
  if (host_ids?.length) {
51
    params.hostids = host_ids
52
  }
53
  if (group_ids?.length) {
54
    params.groupids = group_ids
55
  }
56
  if (min_severity) {
57
    const min = severities.indexOf(min_severity)
58
    if (min < 0) {
59
      throw new Error(`Invalid min_severity: ${min_severity}`)
60
    }
61
    params.severities = [0, 1, 2, 3, 4, 5].filter((s) => s >= min)
62
  }
63
  if (time_from) {
64
    params.time_from = toUnix(time_from)
65
  }
66
  if (time_till) {
67
    params.time_till = toUnix(time_till)
68
  }
69
  if (include_recently_resolved) {
70
    params.recent = true
71
  }
72
  if (eventid_from) {
73
    params.eventid_from = eventid_from
74
  }
75

76
  const problems = await call(auth, "problem.get", params)
77
  return {
78
    problems: problems.slice(0, max),
79
    next_eventid_from: problems.length > max ? problems[max].eventid : null,
80
  }
81
}
82

83
function toUnix(value: string) {
84
  const seconds = Math.floor(new Date(value).getTime() / 1000)
85
  if (Number.isNaN(seconds)) {
86
    throw new Error(`Invalid date: ${value}`)
87
  }
88
  return seconds
89
}
90

91
async function call(auth: RT.Zabbix, method: string, params: any) {
92
  const response = await fetch(
93
    `${auth.base_url.replace(/\/+$/, "").replace(/\/api_jsonrpc\.php$/, "")}/api_jsonrpc.php`,
94
    {
95
      method: "POST",
96
      headers: {
97
        Authorization: `Bearer ${auth.token}`,
98
        "Content-Type": "application/json-rpc",
99
        Accept: "application/json",
100
      },
101
      body: JSON.stringify({ jsonrpc: "2.0", method, params, id: 1 }),
102
    }
103
  )
104

105
  if (!response.ok) {
106
    throw new Error(`${response.status} ${await response.text()}`)
107
  }
108

109
  const text = await response.text()
110
  let data: any
111
  try {
112
    data = JSON.parse(text)
113
  } catch {
114
    throw new Error(
115
      `Not a JSON-RPC response (check base_url): ${text.slice(0, 300)}`
116
    )
117
  }
118
  if (data.error) {
119
    throw new Error(
120
      `${data.error.code} ${data.error.message} ${data.error.data ?? ""}`
121
    )
122
  }
123
  return data.result
124
}
125