Fix using value mapping from zabbix, closes #1222
This commit is contained in:
@@ -100,6 +100,7 @@ type QueryFilter struct {
|
|||||||
type QueryOptions struct {
|
type QueryOptions struct {
|
||||||
ShowDisabledItems bool `json:"showDisabledItems"`
|
ShowDisabledItems bool `json:"showDisabledItems"`
|
||||||
DisableDataAlignment bool `json:"disableDataAlignment"`
|
DisableDataAlignment bool `json:"disableDataAlignment"`
|
||||||
|
UseZabbixValueMapping bool `json:"useZabbixValueMapping"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// QueryOptions model
|
// QueryOptions model
|
||||||
|
|||||||
@@ -92,17 +92,17 @@ func convertTimeSeriesToDataFrame(series []*timeseries.TimeSeriesData) *data.Fra
|
|||||||
return wideFrame
|
return wideFrame
|
||||||
}
|
}
|
||||||
|
|
||||||
func convertTimeSeriesToDataFrames(series []*timeseries.TimeSeriesData) []*data.Frame {
|
func convertTimeSeriesToDataFrames(series []*timeseries.TimeSeriesData, valuemaps []zabbix.ValueMap) []*data.Frame {
|
||||||
frames := make([]*data.Frame, 0)
|
frames := make([]*data.Frame, 0)
|
||||||
|
|
||||||
for _, s := range series {
|
for _, s := range series {
|
||||||
frames = append(frames, seriesToDataFrame(s))
|
frames = append(frames, seriesToDataFrame(s, valuemaps))
|
||||||
}
|
}
|
||||||
|
|
||||||
return frames
|
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 := data.NewFieldFromFieldType(data.FieldTypeTime, 0)
|
||||||
timeFileld.Name = data.TimeSeriesTimeFieldName
|
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)
|
frame := data.NewFrame(seriesName, timeFileld, valueField)
|
||||||
|
|
||||||
for _, point := range series.TS {
|
for _, point := range series.TS {
|
||||||
@@ -248,3 +253,28 @@ func parseItemUpdateInterval(delay string) *time.Duration {
|
|||||||
|
|
||||||
return &interval
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
return frames, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -397,6 +397,22 @@ func (ds *Zabbix) GetAllGroups(ctx context.Context) ([]Group, error) {
|
|||||||
return groups, err
|
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) {
|
func (ds *Zabbix) GetVersion(ctx context.Context) (int, error) {
|
||||||
result, err := ds.request(ctx, "apiinfo.version", ZabbixAPIParams{})
|
result, err := ds.request(ctx, "apiinfo.version", ZabbixAPIParams{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -99,3 +99,14 @@ type Application struct {
|
|||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
ID string `json:"applicationid"`
|
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"`
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user