diff --git a/pkg/datasource/models.go b/pkg/datasource/models.go index 01af4c0..8edeb30 100644 --- a/pkg/datasource/models.go +++ b/pkg/datasource/models.go @@ -98,8 +98,9 @@ type QueryFilter struct { // QueryOptions model type QueryOptions struct { - ShowDisabledItems bool `json:"showDisabledItems"` - DisableDataAlignment bool `json:"disableDataAlignment"` + ShowDisabledItems bool `json:"showDisabledItems"` + DisableDataAlignment bool `json:"disableDataAlignment"` + UseZabbixValueMapping bool `json:"useZabbixValueMapping"` } // QueryOptions model diff --git a/pkg/datasource/response_handler.go b/pkg/datasource/response_handler.go index b0d7c48..d5d157b 100644 --- a/pkg/datasource/response_handler.go +++ b/pkg/datasource/response_handler.go @@ -92,17 +92,17 @@ func convertTimeSeriesToDataFrame(series []*timeseries.TimeSeriesData) *data.Fra return wideFrame } -func convertTimeSeriesToDataFrames(series []*timeseries.TimeSeriesData) []*data.Frame { +func convertTimeSeriesToDataFrames(series []*timeseries.TimeSeriesData, valuemaps []zabbix.ValueMap) []*data.Frame { frames := make([]*data.Frame, 0) for _, s := range series { - frames = append(frames, seriesToDataFrame(s)) + frames = append(frames, seriesToDataFrame(s, valuemaps)) } return frames } -func seriesToDataFrame(series *timeseries.TimeSeriesData) *data.Frame { +func seriesToDataFrame(series *timeseries.TimeSeriesData, valuemaps []zabbix.ValueMap) *data.Frame { timeFileld := data.NewFieldFromFieldType(data.FieldTypeTime, 0) timeFileld.Name = data.TimeSeriesTimeFieldName @@ -128,6 +128,11 @@ func seriesToDataFrame(series *timeseries.TimeSeriesData) *data.Frame { }, } + if len(valuemaps) > 0 { + mappings := getValueMapping(*item, valuemaps) + valueField.Config.Mappings = mappings + } + frame := data.NewFrame(seriesName, timeFileld, valueField) for _, point := range series.TS { @@ -248,3 +253,28 @@ func parseItemUpdateInterval(delay string) *time.Duration { return &interval } + +func getValueMapping(item zabbix.Item, valueMaps []zabbix.ValueMap) data.ValueMappings { + mappings := make([]data.ValueMapping, 0) + zabbixMap := zabbix.ValueMap{} + + for _, vm := range valueMaps { + if vm.ID == item.ValueMapID { + zabbixMap = vm + break + } + } + + if zabbixMap.ID == "" { + return mappings + } + + for _, m := range zabbixMap.Mappings { + mappings = append(mappings, data.ValueMapper{ + m.Value: { + Text: m.NewValue, + }, + }) + } + return mappings +} diff --git a/pkg/datasource/zabbix.go b/pkg/datasource/zabbix.go index ee55391..8b9e704 100644 --- a/pkg/datasource/zabbix.go +++ b/pkg/datasource/zabbix.go @@ -145,7 +145,15 @@ func (ds *ZabbixDatasourceInstance) applyDataProcessing(ctx context.Context, que } } - frames := convertTimeSeriesToDataFrames(series) + valueMaps := make([]zabbix.ValueMap, 0) + if query.Options.UseZabbixValueMapping { + valueMaps, err = ds.zabbix.GetValueMappings(ctx) + if err != nil { + ds.logger.Error("Error getting value maps", "error", err) + valueMaps = []zabbix.ValueMap{} + } + } + frames := convertTimeSeriesToDataFrames(series, valueMaps) return frames, nil } diff --git a/pkg/zabbix/methods.go b/pkg/zabbix/methods.go index 1aa6611..4f05395 100644 --- a/pkg/zabbix/methods.go +++ b/pkg/zabbix/methods.go @@ -397,6 +397,22 @@ func (ds *Zabbix) GetAllGroups(ctx context.Context) ([]Group, error) { return groups, err } +func (ds *Zabbix) GetValueMappings(ctx context.Context) ([]ValueMap, error) { + params := ZabbixAPIParams{ + "output": "extend", + "selectMappings": "extend", + } + + result, err := ds.Request(ctx, &ZabbixAPIRequest{Method: "valuemap.get", Params: params}) + if err != nil { + return nil, err + } + + var valuemaps []ValueMap + err = convertTo(result, &valuemaps) + return valuemaps, err +} + func (ds *Zabbix) GetVersion(ctx context.Context) (int, error) { result, err := ds.request(ctx, "apiinfo.version", ZabbixAPIParams{}) if err != nil { diff --git a/pkg/zabbix/models.go b/pkg/zabbix/models.go index deaf9dc..ef31415 100644 --- a/pkg/zabbix/models.go +++ b/pkg/zabbix/models.go @@ -99,3 +99,14 @@ type Application struct { Name string `json:"name"` ID string `json:"applicationid"` } + +type ValueMap struct { + ID string `json:"valuemapid"` + Name string `json:"name"` + Mappings []ValueMapping `json:"mappings"` +} + +type ValueMapping struct { + Value string `json:"value"` + NewValue string `json:"newvalue"` +}