//native
/**
* List maintenances
* List maintenance windows (maintenance.get) by exact name, a case-insensitive name substring, host IDs or host group IDs, with their hosts, host groups and time periods. The Zabbix API has no offset: has_more tells you the limit cut the result, so narrow the filters or raise the limit.
*/
export async function main(
auth: RT.Zabbix,
name: string | undefined,
name_search: string | undefined,
host_ids: string[] | undefined,
group_ids: string[] | undefined,
limit: number | undefined
) {
const max = Math.max(1, limit || 100)
const params: { [key: string]: any } = {
output: "extend",
selectHosts: ["hostid", "host", "name"],
selectHostGroups: ["groupid", "name"],
selectTimeperiods: "extend",
sortfield: "maintenanceid",
limit: max + 1,
}
if (name) {
params.filter = { name }
}
if (name_search) {
params.search = { name: name_search }
}
if (host_ids?.length) {
params.hostids = host_ids
}
if (group_ids?.length) {
params.groupids = group_ids
}
const maintenances = await call(auth, "maintenance.get", params)
return {
maintenances: maintenances.slice(0, max),
has_more: maintenances.length > max,
}
}
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