refactor!: Cleanup zabbix api access and remove unused classes

This commit is contained in:
Andreas Hilbig 2026-01-07 18:11:47 +01:00
parent a89c3eeea7
commit da86c726db
9 changed files with 246 additions and 624 deletions

View file

@ -1,11 +1,5 @@
import {ZabbixAPI} from "./zabbix-api.js";
import {ApiError, SortOrder, StorageItemType} from "../schema/generated/graphql.js";
import {ZabbixCreateOrUpdateStorageItemRequest} from "./zabbix-items.js";
import {ZabbixForceCacheReloadRequest} from "./zabbix-script.js";
import {logger} from "../logging/logger.js";
import {ApiErrorCode} from "../model/model_enum_values.js";
import {SortOrder, StorageItemType} from "../schema/generated/graphql.js";
import {ParsedArgs, ZabbixParams, ZabbixRequest, ZabbixResult} from "./zabbix-request.js";
import {sleep} from "../common_utils";
export interface ZabbixValue {
key?: string,
@ -56,73 +50,3 @@ export class ZabbixQueryHistoryRequest extends ZabbixRequest<ZabbixExportValue[]
}
}
}
export interface ZabbixHistoryPushResult {
response: string,
data: { itemid: string, error?: string[] | ApiError }[],
error?: ApiError | string[]
}
export class ZabbixHistoryPushRequest extends ZabbixRequest<ZabbixHistoryPushResult> {
constructor(authToken?: string | null, cookie?: string) {
super("history.push", authToken, cookie);
}
}
export class ZabbixStoreObjectInItemHistoryRequest extends ZabbixRequest<ZabbixHistoryPushResult> {
// After creating an item or host zabbix needs some time before the created object can be referenced in other
// operations - the reason is the config-cache. In case of having ZBX_CACHEUPDATEFREQUENCY=1 (seconds) set within the
// Zabbix - config the delay of 1 second will be sufficient
private static readonly ZABBIX_DELAY_UNTIL_CONFIG_CHANGED: number = 0
public itemid: number | undefined
constructor(authToken?: string | null, cookie?: string) {
super("history.push.jsonobject", authToken, cookie);
}
async prepare(zabbixAPI: ZabbixAPI, args?: ParsedArgs): Promise<any> {
// Create or update zabbix Item
let success = false;
this.itemid = Number(args?.getParam("itemid"))
let timeoutForValueUpdate = this.itemid ? 0 : ZabbixStoreObjectInItemHistoryRequest.ZABBIX_DELAY_UNTIL_CONFIG_CHANGED;
// Create or update controlprogram - item
let result: {
"itemids": string[]
} | undefined = await new ZabbixCreateOrUpdateStorageItemRequest(
this.itemid ? "item.update.storeiteminhistory" : "item.create.storeiteminhistory",
this.authToken, this.cookie).executeRequestThrowError(zabbixAPI, args)
// logger.debug(`Create/update item itemid=${this.itemid}, hostid=${this.zabbixHostId} lead to result=`, JSON.stringify(result));
if (result && result.hasOwnProperty("itemids") && result.itemids.length > 0) {
this.itemid = Number(result.itemids[0]);
let scriptExecResult =
await new ZabbixForceCacheReloadRequest(this.authToken, this.cookie).executeRequestThrowError(zabbixAPI)
if (scriptExecResult.response != "success") {
logger.error(`cache reload not successful: ${scriptExecResult.value}`)
}
await sleep(timeoutForValueUpdate).promise
}
if (!this.itemid) {
this.prepResult = {
error: {
message: "Unable to create/update item",
code: ApiErrorCode.ZABBIX_NO_ITEM_PUSH_ITEM,
path: this.path,
args: args,
}
}
}
}
createZabbixParams(args?: ParsedArgs): ZabbixParams {
return {
itemid: this.itemid,
value: JSON.stringify(args?.getParam("value"))
}
}
}