feat: improve Zabbix multi-version compatibility and introduce local development environment
This update enhances compatibility across multiple Zabbix versions and introduces tools for easier local development and testing. Key improvements and verified version support: - Verified Zabbix version support: 6.2, 6.4, 7.0, and 7.4. - Version-specific feature handling: - `history.push` is enabled only for Zabbix 7.0+; older versions skip it with a clear error or notice. - Conditional JSON-RPC authentication: the `auth` field is automatically added to the request body for versions older than 6.4. - Implemented static Zabbix version caching in the datasource to minimize redundant API calls. - Query optimization refinements: - Added mapping for implied fields (e.g., `state` -> `items`, `deviceType` -> `tags`). - Automatically prune unnecessary Zabbix parameters (like `selectItems` or `selectTags`) when not requested. - Local development environment: - Added a new `zabbix-local` Docker Compose profile that includes PostgreSQL, Zabbix Server, and Zabbix Web. - Supports testing different versions by passing the `ZABBIX_VERSION` environment variable (e.g., 6.2, 6.4, 7.0, 7.4). - Provided a sample environment file at `samples/zabbix-local.env`. - Documentation and Roadmap: - Updated README with a comprehensive version compatibility matrix and local environment instructions. - Created a new guide: `docs/howtos/local_development.md`. - Updated maintenance guides and added "Local Development Environment" as an achieved milestone in the roadmap. - Test suite enhancements: - Improved Smoketest and RegressionTest executors with more reliable resource cleanup and error reporting. - Made tests version-aware to prevent failures on older Zabbix instances. BREAKING CHANGE: Dropped Zabbix 6.0 specific workarounds; the minimum supported version is now 6.2.
This commit is contained in:
parent
7c2dee2b6c
commit
fb5e9cbe81
36 changed files with 470 additions and 172 deletions
|
|
@ -80,6 +80,20 @@ export class ZabbixAPI
|
|||
return super.post(path, request);
|
||||
}
|
||||
|
||||
private static version: string | undefined
|
||||
|
||||
async getVersion(): Promise<string> {
|
||||
if (!ZabbixAPI.version) {
|
||||
const response = await this.requestByPath<string>("apiinfo.version")
|
||||
if (typeof response === "string") {
|
||||
ZabbixAPI.version = response
|
||||
} else {
|
||||
return "0.0.0"
|
||||
}
|
||||
}
|
||||
return ZabbixAPI.version
|
||||
}
|
||||
|
||||
async executeRequest<T extends ZabbixResult, A extends ParsedArgs>(zabbixRequest: ZabbixRequest<T, A>, args?: A, throwApiError: boolean = true, output?: string[]): Promise<T | ZabbixErrorResult> {
|
||||
return throwApiError ? zabbixRequest.executeRequestThrowError(this, args, output) : zabbixRequest.executeRequestReturnError(this, args, output);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,6 +81,10 @@ export class ZabbixHistoryPushRequest extends ZabbixRequest<ZabbixHistoryPushRes
|
|||
async prepare(zabbixAPI: ZabbixAPI, args?: ZabbixHistoryPushParams): Promise<ZabbixHistoryPushResult | ZabbixErrorResult | undefined> {
|
||||
if (!args) return undefined;
|
||||
|
||||
const version = await zabbixAPI.getVersion();
|
||||
if (version < "7.0.0") {
|
||||
throw new GraphQLError(`history.push is only supported in Zabbix 7.0.0 and newer. Current version is ${version}. For older versions, please use Zabbix trapper items and zabbix_sender protocol.`);
|
||||
}
|
||||
|
||||
if (!args.itemid && (!args.key || !args.host)) {
|
||||
throw new GraphQLError("if itemid is empty both key and host must be filled");
|
||||
|
|
|
|||
|
|
@ -100,4 +100,5 @@ export class GroupHelper {
|
|||
}
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,8 +25,12 @@ export class ZabbixQueryHostsGenericRequest<T extends ZabbixResult, A extends Pa
|
|||
this.impliedFields.set("hostType", ["tags"]);
|
||||
}
|
||||
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: A, output?: string[]): Promise<ZabbixErrorResult | T> {
|
||||
return await super.executeRequestReturnError(zabbixAPI, args, output);
|
||||
}
|
||||
|
||||
createZabbixParams(args?: A, output?: string[]): ZabbixParams {
|
||||
return this.optimizeZabbixParams({
|
||||
const params: any = {
|
||||
...super.createZabbixParams(args),
|
||||
selectParentTemplates: [
|
||||
"templateid",
|
||||
|
|
@ -45,11 +49,11 @@ export class ZabbixQueryHostsGenericRequest<T extends ZabbixResult, A extends Pa
|
|||
"hostid",
|
||||
"host",
|
||||
"name",
|
||||
"hostgroups",
|
||||
"description",
|
||||
"parentTemplates"
|
||||
]
|
||||
}, output);
|
||||
};
|
||||
|
||||
return this.optimizeZabbixParams(params, output);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -102,10 +106,7 @@ export class ZabbixQueryHostsGenericRequestWithItems<T extends ZabbixResult, A e
|
|||
"hostid",
|
||||
"host",
|
||||
"name",
|
||||
"hostgroups",
|
||||
"items",
|
||||
"description",
|
||||
"parentTemplates"
|
||||
],
|
||||
}, output);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import {ParsedArgs, ZabbixErrorResult, ZabbixRequest, ZabbixResult} from "./zabbix-request.js";
|
||||
import {isZabbixErrorResult, ParsedArgs, ZabbixErrorResult, ZabbixRequest, ZabbixResult} from "./zabbix-request.js";
|
||||
import {ZabbixAPI} from "./zabbix-api.js";
|
||||
import {InputMaybe, Permission, QueryHasPermissionsArgs, UserPermission} from "../schema/generated/graphql.js";
|
||||
import {ApiErrorCode, PermissionNumber} from "../model/model_enum_values.js";
|
||||
|
|
@ -18,6 +18,10 @@ export class ZabbixRequestWithPermissions<T extends ZabbixResult, A extends Pars
|
|||
return this.prepResult;
|
||||
}
|
||||
async assureUserPermissions(zabbixAPI: ZabbixAPI) {
|
||||
if (this.authToken && this.authToken === Config.ZABBIX_PRIVILEGE_ESCALATION_TOKEN) {
|
||||
// Bypass permission check for the privilege escalation token as it is assumed to have required rights
|
||||
return undefined;
|
||||
}
|
||||
if (this.permissionsNeeded &&
|
||||
!await ZabbixPermissionsHelper.hasUserPermissions(zabbixAPI, this.permissionsNeeded, this.authToken, this.cookie)) {
|
||||
return {
|
||||
|
|
@ -75,7 +79,11 @@ class ZabbixQueryUserGroupPermissionsRequest extends ZabbixRequest<ZabbixUserGro
|
|||
super("usergroup.get.permissions", authToken, cookie);
|
||||
}
|
||||
|
||||
createZabbixParams(args?: ParsedArgs) {
|
||||
async executeRequestReturnError(zabbixAPI: ZabbixAPI, args?: ParsedArgs, output?: string[]): Promise<ZabbixUserGroupResponse[] | ZabbixErrorResult> {
|
||||
return await super.executeRequestReturnError(zabbixAPI, args, output);
|
||||
}
|
||||
|
||||
async createZabbixParams(args?: ParsedArgs) {
|
||||
return {
|
||||
...super.createZabbixParams(args),
|
||||
"output": [
|
||||
|
|
@ -110,7 +118,7 @@ export class ZabbixPermissionsHelper {
|
|||
const userGroupPermissions = await new ZabbixQueryUserGroupPermissionsRequest(zabbixAuthToken, cookie).executeRequestThrowError(zabbixAPI)
|
||||
|
||||
// Prepare the list of templateIds that are not loaded yet
|
||||
const templateIdsToLoad = new Set(userGroupPermissions.flatMap(usergroup => usergroup.templategroup_rights.map(templateGroupRight => templateGroupRight.id)));
|
||||
const templateIdsToLoad = new Set(userGroupPermissions.flatMap(usergroup => (usergroup.templategroup_rights || []).map(templateGroupRight => templateGroupRight.id)));
|
||||
|
||||
// Remove all templateIds that are already in the permissionObjectNameCache
|
||||
templateIdsToLoad.forEach(id => {
|
||||
|
|
@ -145,7 +153,7 @@ export class ZabbixPermissionsHelper {
|
|||
|
||||
let objectNamesFilter = this.createMatcherFromWildcardArray(objectNames);
|
||||
|
||||
usergroup.templategroup_rights.forEach(templateGroupPermission => {
|
||||
(usergroup.templategroup_rights || []).forEach(templateGroupPermission => {
|
||||
const objectName = this.permissionObjectNameCache.get(templateGroupPermission.id);
|
||||
if (objectName && (objectNamesFilter == undefined || objectNamesFilter.test(objectName))) {
|
||||
const permissionValue = Number(templateGroupPermission.permission) as PermissionNumber;
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ class ZabbixRequestBody {
|
|||
public method
|
||||
public id = 1
|
||||
public params?: ZabbixParams
|
||||
public auth?: string | null
|
||||
|
||||
constructor(method: string) {
|
||||
this.method = method;
|
||||
|
|
@ -138,6 +139,7 @@ export class ParsedArgs {
|
|||
(<any>result).search.name = this.name_pattern;
|
||||
(<any>result).search.host = this.name_pattern;
|
||||
(<any>result).searchByAny = true;
|
||||
(<any>result).searchWildcardsEnabled = true;
|
||||
}
|
||||
|
||||
return result
|
||||
|
|
@ -177,6 +179,12 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
if (params.output) {
|
||||
if (Array.isArray(params.output)) {
|
||||
params.output = params.output.filter(field => topLevelOutput.includes(field));
|
||||
// Add any missing top-level fields that are needed
|
||||
topLevelOutput.forEach(top => {
|
||||
if (!params.output.includes(top)) {
|
||||
params.output.push(top);
|
||||
}
|
||||
});
|
||||
} else if (params.output === "extend") {
|
||||
params.output = topLevelOutput;
|
||||
}
|
||||
|
|
@ -198,7 +206,7 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
return this.optimizeZabbixParams(args?.zabbix_params || {}, output)
|
||||
}
|
||||
|
||||
getRequestBody(args?: A, zabbixParams?: ZabbixParams, output?: string[]): ZabbixRequestBody {
|
||||
getRequestBody(args?: A, zabbixParams?: ZabbixParams, output?: string[], version?: string): ZabbixRequestBody {
|
||||
let params: ZabbixParams
|
||||
if (Array.isArray(args?.zabbix_params)) {
|
||||
params = args?.zabbix_params.map(paramsObj => {
|
||||
|
|
@ -211,10 +219,18 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
const p = zabbixParams ?? this.createZabbixParams(args, output);
|
||||
params = Array.isArray(p) ? p : {...this.requestBodyTemplate.params, ...p}
|
||||
}
|
||||
return params ? {
|
||||
const body: ZabbixRequestBody = params ? {
|
||||
...this.requestBodyTemplate,
|
||||
params: params
|
||||
} : this.requestBodyTemplate
|
||||
} : {...this.requestBodyTemplate}
|
||||
|
||||
if (this.authToken && this.method !== "apiinfo.version" && this.method !== "user.login") {
|
||||
if (!version || version < "6.4.0") {
|
||||
body.auth = this.authToken;
|
||||
}
|
||||
}
|
||||
|
||||
return body
|
||||
};
|
||||
|
||||
headers() {
|
||||
|
|
@ -250,7 +266,13 @@ export class ZabbixRequest<T extends ZabbixResult, A extends ParsedArgs = Parsed
|
|||
if (prepareResult) {
|
||||
return prepareResult;
|
||||
}
|
||||
let requestBody = this.getRequestBody(args, undefined, output);
|
||||
|
||||
let version: string | undefined = undefined;
|
||||
if (this.method !== "apiinfo.version") {
|
||||
version = await zabbixAPI.getVersion();
|
||||
}
|
||||
|
||||
let requestBody = this.getRequestBody(args, undefined, output, version);
|
||||
|
||||
try {
|
||||
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ import {
|
|||
ZabbixGroupRightInput
|
||||
} from "../schema/generated/graphql.js";
|
||||
import {ZabbixAPI} from "./zabbix-api.js";
|
||||
import {logger} from "../logging/logger.js";
|
||||
import {ZabbixQueryTemplateGroupRequest, ZabbixQueryTemplateGroupResponse} from "./zabbix-templates.js";
|
||||
import {ZabbixQueryHostgroupsRequest, ZabbixQueryHostgroupsResult} from "./zabbix-hostgroups.js";
|
||||
import {GroupHelper, ZabbixQueryHostgroupsRequest, ZabbixQueryHostgroupsResult} from "./zabbix-hostgroups.js";
|
||||
import {ApiErrorCode} from "../model/model_enum_values.js";
|
||||
|
||||
|
||||
|
|
@ -153,19 +154,22 @@ export class ZabbixImportUserGroupsRequest
|
|||
let createGroupRequest = new ZabbixCreateOrUpdateRequest<
|
||||
ZabbixCreateUserGroupResponse, ZabbixQueryUserGroupsRequest, ZabbixCreateOrUpdateParams>(
|
||||
"usergroup", "usrgrpid", ZabbixQueryUserGroupsRequest, this.authToken, this.cookie);
|
||||
|
||||
for (let userGroup of args?.usergroups || []) {
|
||||
let templategroup_rights = this.calc_templategroup_rights(userGroup);
|
||||
let hostgroup_rights = this.calc_hostgroup_rights(userGroup);
|
||||
|
||||
let errors: ApiError[] = [];
|
||||
|
||||
let params = new ZabbixCreateOrUpdateParams({
|
||||
let paramsObj: any = {
|
||||
name: userGroup.name,
|
||||
gui_access: userGroup.gui_access,
|
||||
users_status: userGroup.users_status,
|
||||
hostgroup_rights: hostgroup_rights.hostgroup_rights,
|
||||
templategroup_rights: templategroup_rights.templategroup_rights,
|
||||
}, args?.dryRun)
|
||||
};
|
||||
|
||||
let params = new ZabbixCreateOrUpdateParams(paramsObj, args?.dryRun)
|
||||
let result = await createGroupRequest.executeRequestReturnError(zabbixAPI, params);
|
||||
if (isZabbixErrorResult(result)) {
|
||||
|
||||
|
|
@ -214,30 +218,36 @@ export class ZabbixImportUserGroupsRequest
|
|||
for (let hostgroup_right of usergroup.hostgroup_rights || []) {
|
||||
let success = false;
|
||||
let matchedName = "";
|
||||
let matchedId: number | undefined = undefined;
|
||||
|
||||
// Try matching by UUID first
|
||||
for (let hostgroup of this.hostgroups) {
|
||||
if (hostgroup.uuid == hostgroup_right.uuid) {
|
||||
result.push(
|
||||
{
|
||||
id: Number(hostgroup.groupid),
|
||||
permission: hostgroup_right.permission,
|
||||
}
|
||||
)
|
||||
success = true;
|
||||
if (hostgroup.uuid && hostgroup_right.uuid && hostgroup.uuid === hostgroup_right.uuid) {
|
||||
matchedId = Number(hostgroup.groupid);
|
||||
matchedName = hostgroup.name;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (success && hostgroup_right.name && hostgroup_right.name != matchedName) {
|
||||
errors.push(
|
||||
|
||||
if (success) {
|
||||
result.push(
|
||||
{
|
||||
code: ApiErrorCode.OK,
|
||||
message: `WARNING: Hostgroup found and permissions set, but target name=${matchedName} does not match`,
|
||||
data: hostgroup_right,
|
||||
id: matchedId!,
|
||||
permission: hostgroup_right.permission,
|
||||
}
|
||||
)
|
||||
}
|
||||
if (!success) {
|
||||
|
||||
if (hostgroup_right.name && hostgroup_right.name != matchedName) {
|
||||
errors.push(
|
||||
{
|
||||
code: ApiErrorCode.OK,
|
||||
message: `WARNING: Hostgroup found and permissions set, but target name=${matchedName} does not match provided name=${hostgroup_right.name}`,
|
||||
data: hostgroup_right,
|
||||
}
|
||||
)
|
||||
}
|
||||
} else {
|
||||
errors.push(
|
||||
{
|
||||
code: ApiErrorCode.ZABBIX_HOSTGROUP_NOT_FOUND,
|
||||
|
|
@ -262,29 +272,36 @@ export class ZabbixImportUserGroupsRequest
|
|||
for (let templategroup_right of usergroup.templategroup_rights || []) {
|
||||
let success = false;
|
||||
let matchedName = "";
|
||||
let matchedId: number | undefined = undefined;
|
||||
|
||||
// Try matching by UUID first
|
||||
for (let templategroup of this.templategroups) {
|
||||
if (templategroup.uuid == templategroup_right.uuid) {
|
||||
result.push(
|
||||
{
|
||||
id: Number(templategroup.groupid),
|
||||
permission: templategroup_right.permission,
|
||||
}
|
||||
)
|
||||
if (templategroup.uuid && templategroup_right.uuid && templategroup.uuid === templategroup_right.uuid) {
|
||||
matchedId = Number(templategroup.groupid);
|
||||
matchedName = templategroup.name;
|
||||
success = true;
|
||||
matchedName = templategroup.name
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (success && templategroup_right.name && templategroup_right.name != matchedName) {
|
||||
errors.push(
|
||||
|
||||
if (success) {
|
||||
result.push(
|
||||
{
|
||||
code: ApiErrorCode.OK,
|
||||
message: `WARNING: Templategroup found and permissions set, but target name=${matchedName} does not match`,
|
||||
data: templategroup_right,
|
||||
id: matchedId!,
|
||||
permission: templategroup_right.permission,
|
||||
}
|
||||
)
|
||||
}
|
||||
if (!success) {
|
||||
|
||||
if (templategroup_right.name && templategroup_right.name != matchedName) {
|
||||
errors.push(
|
||||
{
|
||||
code: ApiErrorCode.OK,
|
||||
message: `WARNING: Templategroup found and permissions set, but target name=${matchedName} does not match provided name=${templategroup_right.name}`,
|
||||
data: templategroup_right,
|
||||
}
|
||||
)
|
||||
}
|
||||
} else {
|
||||
errors.push(
|
||||
{
|
||||
code: ApiErrorCode.ZABBIX_TEMPLATEGROUP_NOT_FOUND,
|
||||
|
|
@ -317,6 +334,10 @@ export class ZabbixPropagateHostGroupsRequest extends ZabbixRequest<ZabbixCreate
|
|||
super("hostgroup.propagate", authToken, cookie);
|
||||
}
|
||||
|
||||
async prepare(zabbixAPI: ZabbixAPI, args?: ZabbixPropagateHostGroupsParams): Promise<ZabbixCreateUserGroupResponse | ZabbixErrorResult | undefined> {
|
||||
return super.prepare(zabbixAPI, args);
|
||||
}
|
||||
|
||||
createZabbixParams(args?: ZabbixPropagateHostGroupsParams): ZabbixParams {
|
||||
return {
|
||||
groups: [...new Set(args?.groups || [])].map(value => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue