Edits history of script submission #23959 for ' Acknowledge event (zabbix)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Acknowledge event
     * 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.
     */
    export async function main(
      auth: RT.Zabbix,
      event_ids: string[],
      close: boolean | undefined,
      acknowledge: boolean | undefined,
      message: string | undefined,
      new_severity:
        | "not_classified"
        | "information"
        | "warning"
        | "average"
        | "high"
        | "disaster"
        | undefined,
      suppress: boolean | undefined,
      suppress_until: string | undefined,
      unsuppress: boolean | undefined
    ) {
      // A suppress_until on its own means suppress until then.
      const doSuppress = suppress || !!suppress_until
      if (doSuppress && unsuppress) {
        throw new Error("Set either suppress or unsuppress, not both")
      }
      const severities = [
        "not_classified",
        "information",
        "warning",
        "average",
        "high",
        "disaster",
      ]
    
      let action = 0
      const params: { [key: string]: any } = { eventids: event_ids }
      if (close) {
        action |= 1
      }
      if (acknowledge) {
        action |= 2
      }
      if (message) {
        action |= 4
        params.message = message
      }
      if (new_severity) {
        const severity = severities.indexOf(new_severity)
        if (severity < 0) {
          throw new Error(`Invalid new_severity: ${new_severity}`)
        }
        action |= 8
        params.severity = severity
      }
      if (doSuppress) {
        action |= 32
        params.suppress_until = 0
        if (suppress_until) {
          const until = Math.floor(new Date(suppress_until).getTime() / 1000)
          if (Number.isNaN(until)) {
            throw new Error(`Invalid suppress_until: ${suppress_until}`)
          }
          params.suppress_until = until
        }
      }
      if (unsuppress) {
        action |= 64
      }
      if (action === 0) {
        throw new Error("Select at least one action or set a message")
      }
      params.action = action
    
      return await call(auth, "event.acknowledge", params)
    }
    
    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