//native
/**
* Get problems
* 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.
*/
export async function main(
auth: RT.Zabbix,
host_ids: string[] | undefined,
group_ids: string[] | undefined,
min_severity:
| "not_classified"
| "information"
| "warning"
| "average"
| "high"
| "disaster"
| undefined,
time_from: string | undefined,
time_till: string | undefined,
include_recently_resolved: boolean | undefined,
limit: number | undefined,
eventid_from: string | undefined
) {
const max = Math.max(1, limit || 100)
const severities = [
"not_classified",
"information",
"warning",
"average",
"high",
"disaster",
]
const params: { [key: string]: any } = {
output: [
"eventid",
"objectid",
"name",
"severity",
"clock",
"r_eventid",
"acknowledged",
"suppressed",
],
selectSuppressionData: "extend",
sortfield: "eventid",
sortorder: "ASC",
limit: max + 1,
}
if (host_ids?.length) {
params.hostids = host_ids
}
if (group_ids?.length) {
params.groupids = group_ids
}
if (min_severity) {
const min = severities.indexOf(min_severity)
if (min < 0) {
throw new Error(`Invalid min_severity: ${min_severity}`)
}
params.severities = [0, 1, 2, 3, 4, 5].filter((s) => s >= min)
}
if (time_from) {
params.time_from = toUnix(time_from)
}
if (time_till) {
params.time_till = toUnix(time_till)
}
if (include_recently_resolved) {
params.recent = true
}
if (eventid_from) {
params.eventid_from = eventid_from
}
const problems = await call(auth, "problem.get", params)
return {
problems: problems.slice(0, max),
next_eventid_from: problems.length > max ? problems[max].eventid : null,
}
}
function toUnix(value: string) {
const seconds = Math.floor(new Date(value).getTime() / 1000)
if (Number.isNaN(seconds)) {
throw new Error(`Invalid date: ${value}`)
}
return seconds
}
async function call(auth: RT.Zabbix, method: string, params: any) {
const response = await fetch(
`${auth.base_url.replace(/\/+$/, "").replace(/\/api_jsonrpc\.php$/, "")}/api_jsonrpc.php`,
{
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json-rpc",
Accept: "application/json",
},
body: JSON.stringify({ jsonrpc: "2.0", method, params, id: 1 }),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const text = await response.text()
let data: any
try {
data = JSON.parse(text)
} catch {
throw new Error(
`Not a JSON-RPC response (check base_url): ${text.slice(0, 300)}`
)
}
if (data.error) {
throw new Error(
`${data.error.code} ${data.error.message} ${data.error.data ?? ""}`
)
}
return data.result
}
Submitted by hugo989 3 hours ago