Skip to content

Commit 058d6f5

Browse files
committed
adding user macros names values query type to filter hosts in zabbix
1 parent c1db47e commit 058d6f5

File tree

14 files changed

+301
-54
lines changed

14 files changed

+301
-54
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ install:
66
yarn install --pure-lockfile
77
# Backend
88
go install -v ./pkg/
9-
GO111MODULE=off go get -u golang.org/x/lint/golint
9+
# GO111MODULE=off go get -u golang.org/x/lint/golint
1010

1111
deps-go:
1212
go install -v ./pkg/

debug-backend.sh

100755100644
File mode changed.

devenv/zas-agent/run_zas_agent.sh

100755100644
File mode changed.

docs/make-docs

100755100644
File mode changed.

go.mod

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module github.com/alexanderzobnin/grafana-zabbix
22

3-
go 1.22.1
3+
go 1.22.0
4+
5+
toolchain go1.23.3
46

57
require (
68
github.com/bitly/go-simplejson v0.5.1
@@ -9,7 +11,7 @@ require (
911
github.com/patrickmn/go-cache v2.1.0+incompatible
1012
github.com/prometheus/client_golang v1.20.5
1113
github.com/stretchr/testify v1.9.0
12-
golang.org/x/net v0.30.0
14+
golang.org/x/net v0.31.0
1315
gotest.tools v2.2.0+incompatible
1416
)
1517

@@ -88,11 +90,12 @@ require (
8890
go.opentelemetry.io/otel/trace v1.31.0 // indirect
8991
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
9092
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
91-
golang.org/x/mod v0.17.0 // indirect
92-
golang.org/x/sync v0.8.0 // indirect
93-
golang.org/x/sys v0.26.0 // indirect
94-
golang.org/x/text v0.19.0 // indirect
95-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
93+
golang.org/x/lint v0.0.0-20241112194109-818c5a804067 // indirect
94+
golang.org/x/mod v0.22.0 // indirect
95+
golang.org/x/sync v0.9.0 // indirect
96+
golang.org/x/sys v0.27.0 // indirect
97+
golang.org/x/text v0.20.0 // indirect
98+
golang.org/x/tools v0.27.0 // indirect
9699
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
97100
google.golang.org/genproto/googleapis/api v0.0.0-20241007155032-5fefd90f89a9 // indirect
98101
google.golang.org/genproto/googleapis/rpc v0.0.0-20241007155032-5fefd90f89a9 // indirect

go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,30 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
237237
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
238238
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
239239
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
240+
golang.org/x/lint v0.0.0-20241112194109-818c5a804067 h1:adDmSQyFTCiv19j015EGKJBoaa7ElV0Q1Wovb/4G7NA=
241+
golang.org/x/lint v0.0.0-20241112194109-818c5a804067/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
242+
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
240243
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
241244
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
242245
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
243246
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
247+
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
248+
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
244249
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
245250
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
246251
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
247252
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
248253
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
249254
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
255+
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
256+
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
250257
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
251258
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
252259
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
253260
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
254261
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
262+
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
263+
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
255264
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
256265
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
257266
golang.org/x/sys v0.0.0-20191020152052-9984515f0562/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -267,16 +276,23 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
267276
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
268277
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
269278
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
279+
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
280+
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
270281
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
271282
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
272283
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
273284
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
285+
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
286+
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
274287
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
275288
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
289+
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
276290
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
277291
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
278292
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
279293
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
294+
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
295+
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
280296
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
281297
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
282298
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/datasource/models.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ type QueryModel struct {
5454
Application QueryFilter `json:"application"`
5555
ItemTag QueryFilter `json:"itemTag"`
5656
Item QueryFilter `json:"item"`
57+
UserMacro QueryFilter `json:"userMacro"`
58+
59+
5760

5861
// Item ID mode
5962
ItemIDs string `json:"itemids,omitempty"`

src/datasource/components/VariableQueryEditor.tsx

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
1313
{ value: VariableQueryTypes.ItemTag, label: 'Item tag' },
1414
{ value: VariableQueryTypes.Item, label: 'Item' },
1515
{ value: VariableQueryTypes.ItemValues, label: 'Item values' },
16+
{ value: VariableQueryTypes.UserMacroName, label: 'User macro Name' },
17+
{ value: VariableQueryTypes.UserMacroValue, label: 'User macro Value' },
1618
];
1719

1820
defaults: VariableQueryData = {
@@ -23,6 +25,8 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
2325
application: '',
2426
itemTag: '',
2527
item: '',
28+
userMacroName: '',
29+
userMacroValue: '',
2630
};
2731

2832
constructor(props: VariableQueryProps) {
@@ -69,8 +73,8 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
6973
};
7074

7175
handleQueryChange = () => {
72-
const { queryType, group, host, application, itemTag, item } = this.state;
73-
const queryModel = { queryType, group, host, application, itemTag, item };
76+
const { queryType, group, host, application, itemTag, item, userMacroName , userMacroValue } = this.state;
77+
const queryModel = { queryType, group, host, application, itemTag, item, userMacroName, userMacroValue };
7478
this.props.onChange(queryModel, `Zabbix - ${queryType}`);
7579
};
7680

@@ -81,14 +85,14 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
8185
queryType: selectedItem.value,
8286
});
8387

84-
const { group, host, application, itemTag, item } = this.state;
88+
const { group, host, application, itemTag, item, userMacroName, userMacroValue } = this.state;
8589
const queryType = selectedItem.value;
86-
const queryModel = { queryType, group, host, application, itemTag, item };
90+
const queryModel = { queryType, group, host, application, itemTag, item, userMacroName, userMacroValue };
8791
this.props.onChange(queryModel, `Zabbix - ${queryType}`);
8892
};
8993

9094
render() {
91-
const { selectedQueryType, legacyQuery, group, host, application, itemTag, item } = this.state;
95+
const { selectedQueryType, legacyQuery, group, host, application, itemTag, item,userMacroName, userMacroValue } = this.state;
9296
const { datasource } = this.props;
9397
const supportsItemTags = datasource?.zabbix?.isZabbix54OrHigherSync() || false;
9498

@@ -116,7 +120,11 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
116120
</InlineField>
117121
</InlineFieldRow>
118122

119-
{selectedQueryType.value !== VariableQueryTypes.Group && (
123+
{( selectedQueryType.value === VariableQueryTypes.Application ||
124+
selectedQueryType.value === VariableQueryTypes.ItemTag ||
125+
selectedQueryType.value === VariableQueryTypes.Item ||
126+
selectedQueryType.value === VariableQueryTypes.ItemValues ||
127+
selectedQueryType.value === VariableQueryTypes.Host ) && (
120128
<InlineFieldRow>
121129
<InlineField label="Host" labelWidth={16}>
122130
<ZabbixInput
@@ -129,10 +137,12 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
129137
</InlineFieldRow>
130138
)}
131139

140+
141+
132142
{(selectedQueryType.value === VariableQueryTypes.Application ||
133143
selectedQueryType.value === VariableQueryTypes.ItemTag ||
134144
selectedQueryType.value === VariableQueryTypes.Item ||
135-
selectedQueryType.value === VariableQueryTypes.ItemValues) && (
145+
selectedQueryType.value === VariableQueryTypes.ItemValues ) && (
136146
<>
137147
{supportsItemTags && (
138148
<InlineFieldRow>
@@ -173,6 +183,75 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
173183
</InlineField>
174184
</InlineFieldRow>
175185
)}
186+
187+
</>
188+
)}
189+
190+
{selectedQueryType.value === VariableQueryTypes.UserMacroName && (
191+
<InlineFieldRow>
192+
<InlineField label="Macro Name" labelWidth={16}>
193+
<Input
194+
width={30}
195+
value={userMacroName}
196+
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroName')}
197+
onBlur={this.handleQueryChange}
198+
/>
199+
</InlineField>
200+
</InlineFieldRow>
201+
)}
202+
203+
{selectedQueryType.value === VariableQueryTypes.UserMacroValue && (
204+
<>
205+
206+
<InlineFieldRow>
207+
<InlineField label="Macro Name" labelWidth={16}>
208+
<Input
209+
width={30}
210+
value={userMacroName}
211+
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroName')}
212+
onBlur={this.handleQueryChange}
213+
/>
214+
</InlineField>
215+
</InlineFieldRow>
216+
217+
<InlineFieldRow>
218+
<InlineField label="Macro Value" labelWidth={16}>
219+
<Input
220+
width={30}
221+
value={userMacroValue}
222+
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroValue')}
223+
onBlur={this.handleQueryChange}
224+
/>
225+
</InlineField>
226+
</InlineFieldRow>
227+
228+
</>
229+
230+
)}
231+
232+
{selectedQueryType.value === VariableQueryTypes.Host && (
233+
<>
234+
<InlineFieldRow>
235+
<InlineField label="Macro Name" labelWidth={16}>
236+
<Input
237+
width={30}
238+
value={userMacroName}
239+
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroName')}
240+
onBlur={this.handleQueryChange}
241+
/>
242+
</InlineField>
243+
</InlineFieldRow>
244+
245+
<InlineFieldRow>
246+
<InlineField label="Macro Value" labelWidth={16}>
247+
<Input
248+
width={30}
249+
value={userMacroValue}
250+
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroValue')}
251+
onBlur={this.handleQueryChange}
252+
/>
253+
</InlineField>
254+
</InlineFieldRow>
176255
</>
177256
)}
178257

@@ -188,3 +267,4 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
188267
);
189268
}
190269
}
270+

src/datasource/datasource.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,18 +828,24 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
828828
queryModel = utils.parseLegacyVariableQuery(query);
829829
}
830830

831-
for (const prop of ['group', 'host', 'application', 'itemTag', 'item']) {
831+
for (const prop of ['group', 'host', 'application', 'itemTag', 'item', 'userMacroName', 'userMacroValue']) {
832832
queryModel[prop] = this.replaceTemplateVars(queryModel[prop], {});
833833
}
834834

835-
const { group, host, application, item } = queryModel;
835+
const { group, host, application, item ,userMacroName, userMacroValue} = queryModel;
836836

837837
switch (queryModel.queryType) {
838838
case VariableQueryTypes.Group:
839839
resultPromise = this.zabbix.getGroups(queryModel.group);
840840
break;
841+
case VariableQueryTypes.UserMacroName:
842+
resultPromise = this.zabbix.getUserMacrosNames(queryModel.group,queryModel.userMacroName);
843+
break;
844+
case VariableQueryTypes.UserMacroValue:
845+
resultPromise = this.zabbix.getUserMacrosValues(queryModel.group,queryModel.userMacroName, queryModel.userMacroValue);
846+
break;
841847
case VariableQueryTypes.Host:
842-
resultPromise = this.zabbix.getHosts(queryModel.group, queryModel.host);
848+
resultPromise = this.zabbix.getHosts(queryModel.group, queryModel.host,userMacroName || null , userMacroValue || null);
843849
break;
844850
case VariableQueryTypes.Application:
845851
resultPromise = this.zabbix.getApps(queryModel.group, queryModel.host, queryModel.application);
@@ -856,6 +862,7 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
856862
queryModel.item
857863
);
858864
break;
865+
859866
case VariableQueryTypes.ItemValues:
860867
const range = options?.range;
861868
resultPromise = this.zabbix.getItemValues(group, host, application, item, { range });

src/datasource/specs/datasource.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,15 @@ describe('ZabbixDatasource', () => {
259259

260260
it('should return hosts', (done) => {
261261
const tests = [
262-
{ query: '*.*', expect: ['/.*/', '/.*/'] },
263-
{ query: '.', expect: ['', ''] },
264-
{ query: 'Backend.*', expect: ['Backend', '/.*/'] },
265-
{ query: 'Back*.', expect: ['Back*', ''] },
262+
{ query: '*.*', expect: ['/.*/', '/.*/',null, null] },
263+
{ query: '.', expect: ['', '', null,null] },
264+
{ query: 'Backend.*', expect: ['Backend', '/.*/', null,null] },
265+
{ query: 'Back*.', expect: ['Back*', '',null,null] },
266266
];
267267

268268
for (const test of tests) {
269269
ctx.ds.metricFindQuery(test.query);
270-
expect(ctx.ds.zabbix.getHosts).toBeCalledWith(test.expect[0], test.expect[1]);
270+
expect(ctx.ds.zabbix.getHosts).toBeCalledWith(test.expect[0], test.expect[1],test.expect[2],test.expect[3]);
271271
ctx.ds.zabbix.getHosts.mockClear();
272272
}
273273
done();
@@ -315,7 +315,7 @@ describe('ZabbixDatasource', () => {
315315
let query = '*.*';
316316

317317
ctx.ds.metricFindQuery(query);
318-
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/', '/.*/');
318+
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/','/.*/',null,null);
319319
done();
320320
});
321321
});

0 commit comments

Comments
 (0)