Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include macro filtering for hosts #1921

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion debug-backend.sh
100755 → 100644
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these files show as modifed when they were not i added a space to see the contents

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash
if [ "$1" == "-h" ]; then
echo "Usage: ${BASH_SOURCE[0]} [plugin process name] [port]"
exit
Expand Down
2 changes: 1 addition & 1 deletion devenv/zas-agent/run_zas_agent.sh
100755 → 100644
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these files show as modifed when they were not i added a space to see the contents

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash

# Run redis server first
# cd /zas/redis-3.2.9/src/
Expand Down
2 changes: 1 addition & 1 deletion docs/make-docs
100755 → 100644
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these files show as modifed when they were not i added a space to see the contents

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/sh
# The source of this file is https://raw.githubusercontent.com/grafana/writers-toolkit/main/docs/make-docs.
# # `make-docs` procedure changelog
#
Expand Down
94 changes: 87 additions & 7 deletions src/datasource/components/VariableQueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
{ value: VariableQueryTypes.ItemTag, label: 'Item tag' },
{ value: VariableQueryTypes.Item, label: 'Item' },
{ value: VariableQueryTypes.ItemValues, label: 'Item values' },
{ value: VariableQueryTypes.UserMacroName, label: 'User macro Name' },
{ value: VariableQueryTypes.UserMacroValue, label: 'User macro Value' },
];

defaults: VariableQueryData = {
Expand All @@ -23,6 +25,8 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
application: '',
itemTag: '',
item: '',
userMacroName: '',
userMacroValue: '',
};

constructor(props: VariableQueryProps) {
Expand Down Expand Up @@ -69,8 +73,8 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
};

handleQueryChange = () => {
const { queryType, group, host, application, itemTag, item } = this.state;
const queryModel = { queryType, group, host, application, itemTag, item };
const { queryType, group, host, application, itemTag, item, userMacroName , userMacroValue } = this.state;
const queryModel = { queryType, group, host, application, itemTag, item, userMacroName, userMacroValue };
this.props.onChange(queryModel, `Zabbix - ${queryType}`);
};

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

const { group, host, application, itemTag, item } = this.state;
const { group, host, application, itemTag, item, userMacroName, userMacroValue } = this.state;
const queryType = selectedItem.value;
const queryModel = { queryType, group, host, application, itemTag, item };
const queryModel = { queryType, group, host, application, itemTag, item, userMacroName, userMacroValue };
this.props.onChange(queryModel, `Zabbix - ${queryType}`);
};

render() {
const { selectedQueryType, legacyQuery, group, host, application, itemTag, item } = this.state;
const { selectedQueryType, legacyQuery, group, host, application, itemTag, item,userMacroName, userMacroValue } = this.state;
const { datasource } = this.props;
const supportsItemTags = datasource?.zabbix?.isZabbix54OrHigherSync() || false;

Expand Down Expand Up @@ -116,7 +120,11 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
</InlineField>
</InlineFieldRow>

{selectedQueryType.value !== VariableQueryTypes.Group && (
{( selectedQueryType.value === VariableQueryTypes.Application ||
selectedQueryType.value === VariableQueryTypes.ItemTag ||
selectedQueryType.value === VariableQueryTypes.Item ||
selectedQueryType.value === VariableQueryTypes.ItemValues ||
selectedQueryType.value === VariableQueryTypes.Host ) && (
<InlineFieldRow>
<InlineField label="Host" labelWidth={16}>
<ZabbixInput
Expand All @@ -129,10 +137,12 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
</InlineFieldRow>
)}



{(selectedQueryType.value === VariableQueryTypes.Application ||
selectedQueryType.value === VariableQueryTypes.ItemTag ||
selectedQueryType.value === VariableQueryTypes.Item ||
selectedQueryType.value === VariableQueryTypes.ItemValues) && (
selectedQueryType.value === VariableQueryTypes.ItemValues ) && (
<>
{supportsItemTags && (
<InlineFieldRow>
Expand Down Expand Up @@ -173,6 +183,75 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
</InlineField>
</InlineFieldRow>
)}

</>
)}

{selectedQueryType.value === VariableQueryTypes.UserMacroName && (
<InlineFieldRow>
<InlineField label="Macro Name" labelWidth={16}>
<Input
width={30}
value={userMacroName}
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroName')}
onBlur={this.handleQueryChange}
/>
</InlineField>
</InlineFieldRow>
)}

{selectedQueryType.value === VariableQueryTypes.UserMacroValue && (
<>

<InlineFieldRow>
<InlineField label="Macro Name" labelWidth={16}>
<Input
width={30}
value={userMacroName}
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroName')}
onBlur={this.handleQueryChange}
/>
</InlineField>
</InlineFieldRow>

<InlineFieldRow>
<InlineField label="Macro Value" labelWidth={16}>
<Input
width={30}
value={userMacroValue}
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroValue')}
onBlur={this.handleQueryChange}
/>
</InlineField>
</InlineFieldRow>

</>

)}

{selectedQueryType.value === VariableQueryTypes.Host && (
<>
<InlineFieldRow>
<InlineField label="Macro Name" labelWidth={16}>
<Input
width={30}
value={userMacroName}
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroName')}
onBlur={this.handleQueryChange}
/>
</InlineField>
</InlineFieldRow>

<InlineFieldRow>
<InlineField label="Macro Value" labelWidth={16}>
<Input
width={30}
value={userMacroValue}
onChange={(evt) => this.handleQueryUpdate(evt, 'userMacroValue')}
onBlur={this.handleQueryChange}
/>
</InlineField>
</InlineFieldRow>
</>
)}

Expand All @@ -188,3 +267,4 @@ export class ZabbixVariableQueryEditor extends PureComponent<VariableQueryProps,
);
}
}

13 changes: 10 additions & 3 deletions src/datasource/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,18 +828,24 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
queryModel = utils.parseLegacyVariableQuery(query);
}

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

const { group, host, application, item } = queryModel;
const { group, host, application, item ,userMacroName, userMacroValue} = queryModel;

switch (queryModel.queryType) {
case VariableQueryTypes.Group:
resultPromise = this.zabbix.getGroups(queryModel.group);
break;
case VariableQueryTypes.UserMacroName:
resultPromise = this.zabbix.getUserMacrosNames(queryModel.group,queryModel.userMacroName);
break;
case VariableQueryTypes.UserMacroValue:
resultPromise = this.zabbix.getUserMacrosValues(queryModel.group,queryModel.userMacroName, queryModel.userMacroValue);
break;
case VariableQueryTypes.Host:
resultPromise = this.zabbix.getHosts(queryModel.group, queryModel.host);
resultPromise = this.zabbix.getHosts(queryModel.group, queryModel.host,userMacroName || null , userMacroValue || null);
break;
case VariableQueryTypes.Application:
resultPromise = this.zabbix.getApps(queryModel.group, queryModel.host, queryModel.application);
Expand All @@ -856,6 +862,7 @@ export class ZabbixDatasource extends DataSourceApi<ZabbixMetricsQuery, ZabbixDS
queryModel.item
);
break;

case VariableQueryTypes.ItemValues:
const range = options?.range;
resultPromise = this.zabbix.getItemValues(group, host, application, item, { range });
Expand Down
12 changes: 6 additions & 6 deletions src/datasource/specs/datasource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ describe('ZabbixDatasource', () => {

it('should return hosts', (done) => {
const tests = [
{ query: '*.*', expect: ['/.*/', '/.*/'] },
{ query: '.', expect: ['', ''] },
{ query: 'Backend.*', expect: ['Backend', '/.*/'] },
{ query: 'Back*.', expect: ['Back*', ''] },
{ query: '*.*', expect: ['/.*/', '/.*/',null, null] },
{ query: '.', expect: ['', '', null,null] },
{ query: 'Backend.*', expect: ['Backend', '/.*/', null,null] },
{ query: 'Back*.', expect: ['Back*', '',null,null] },
];

for (const test of tests) {
ctx.ds.metricFindQuery(test.query);
expect(ctx.ds.zabbix.getHosts).toBeCalledWith(test.expect[0], test.expect[1]);
expect(ctx.ds.zabbix.getHosts).toBeCalledWith(test.expect[0], test.expect[1],test.expect[2],test.expect[3]);
ctx.ds.zabbix.getHosts.mockClear();
}
done();
Expand Down Expand Up @@ -315,7 +315,7 @@ describe('ZabbixDatasource', () => {
let query = '*.*';

ctx.ds.metricFindQuery(query);
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/', '/.*/');
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/','/.*/',null,null);
done();
});
});
Expand Down
7 changes: 7 additions & 0 deletions src/datasource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export interface VariableQuery {
itemTag?: string;
item?: string;
macro?: string;
userMacro?: String;
userMacroName?: string;
userMacroValue?: string;
}

export type LegacyVariableQuery = VariableQuery | string;
Expand All @@ -84,6 +87,10 @@ export enum VariableQueryTypes {
ItemTag = 'itemTag',
Item = 'item',
ItemValues = 'itemValues',
UserMacro = 'userMacro',
UserMacroName = 'userMacroName',
UserMacroValue = 'userMacroValue',

}

export interface ProblemDTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ export class ZabbixAPIConnector {
return this.request('hostgroup.get', params);
}

getUserMacrosByGroup(groupids) {
const params = {
output: ['hostmacroid', 'macro', 'value', 'hostid'],
groupids: groupids,
};

return this.request('usermacro.get', params);
}

getHosts(groupids): Promise<any[]> {
const params: any = {
output: ['hostid', 'name', 'host'],
Expand Down Expand Up @@ -245,6 +254,15 @@ export class ZabbixAPIConnector {
return this.request('usermacro.get', params);
}

getMacrosByGroup(groupids) {
const params = {
output: 'extend',
groupids: groupids,
};

return this.request('usermacro.get', params);
}

getUserMacros(hostmacroids) {
const params = {
output: 'extend',
Expand Down
2 changes: 1 addition & 1 deletion src/datasource/zabbix/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface ZabbixConnector {
getVersion: () => Promise<string>;

getGroups: (groupFilter?) => any;
getHosts: (groupFilter?, hostFilter?) => any;
getHosts: (groupFilter?, hostFilter?, userMacroName?, userMacroValue?) => any;
getApps: (groupFilter?, hostFilter?, appFilter?) => any;
getUMacros: (groupFilter?, hostFilter?, macroFilter?) => any;
getItems: (groupFilter?, hostFilter?, appFilter?, itemTagFilter?, itemFilter?, options?) => any;
Expand Down
Loading