|
| 1 | +// Copyright 2021 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package collector |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "github.com/mitchellh/mapstructure" |
| 19 | +) |
| 20 | + |
| 21 | +// clusterSettingsResponse is a representation of a Elasticsearch Cluster Settings |
| 22 | +type clusterSettingsResponse struct { |
| 23 | + Defaults clusterSettingsSection `json:"defaults"` |
| 24 | + Persistent clusterSettingsSection `json:"persistent"` |
| 25 | + Transient clusterSettingsSection `json:"transient"` |
| 26 | +} |
| 27 | + |
| 28 | +// clusterSettingsSection is a representation of a Elasticsearch Cluster Settings |
| 29 | +type clusterSettingsSection struct { |
| 30 | + Cluster clusterSettingsCluster `json:"cluster" mapstructure:",squash"` |
| 31 | +} |
| 32 | + |
| 33 | +// clusterSettingsCluster is a representation of a Elasticsearch clusterSettingsCluster Settings |
| 34 | +type clusterSettingsCluster struct { |
| 35 | + Routing clusterSettingsRouting `json:"routing" mapstructure:",squash"` |
| 36 | + |
| 37 | + MaxShardsPerNode string `json:"max_shards_per_node" mapstructure:"cluster.max_shards_per_node"` |
| 38 | +} |
| 39 | + |
| 40 | +// clusterSettingsRouting is a representation of a Elasticsearch Cluster shard routing configuration |
| 41 | +type clusterSettingsRouting struct { |
| 42 | + Allocation clusterSettingsAllocation `json:"allocation" mapstructure:",squash"` |
| 43 | +} |
| 44 | + |
| 45 | +// clusterSettingsAllocation is a representation of a Elasticsearch Cluster shard routing allocation settings |
| 46 | +type clusterSettingsAllocation struct { |
| 47 | + Enabled string `json:"enable" mapstructure:"cluster.routing.allocation.enable"` |
| 48 | + Disk clusterSettingsDisk `json:"disk" mapstructure:",squash"` |
| 49 | +} |
| 50 | + |
| 51 | +// clusterSettingsDisk is a representation of a Elasticsearch Cluster shard routing disk allocation settings |
| 52 | +type clusterSettingsDisk struct { |
| 53 | + ThresholdEnabled string `json:"threshold_enabled" mapstructure:"cluster.routing.allocation.disk.threshold_enabled"` |
| 54 | + Watermark clusterSettingsWatermark `json:"watermark" mapstructure:",squash"` |
| 55 | +} |
| 56 | + |
| 57 | +// clusterSettingsWatermark is representation of Elasticsearch Cluster shard routing disk allocation watermark settings |
| 58 | +type clusterSettingsWatermark struct { |
| 59 | + FloodStage string `json:"flood_stage" mapstructure:"cluster.routing.allocation.disk.watermark.flood_stage"` |
| 60 | + High string `json:"high" mapstructure:"cluster.routing.allocation.disk.watermark.high"` |
| 61 | + Low string `json:"low" mapstructure:"cluster.routing.allocation.disk.watermark.low"` |
| 62 | +} |
| 63 | + |
| 64 | +func (c *clusterSettingsSection) UnmarshalJSON(data []byte) error { |
| 65 | + var settings map[string]interface{} |
| 66 | + |
| 67 | + if err := json.Unmarshal(data, &settings); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + settings = flatten(settings) |
| 72 | + return mapstructure.Decode(settings, c) |
| 73 | +} |
| 74 | + |
| 75 | +func flatten(m map[string]interface{}) map[string]interface{} { |
| 76 | + result := make(map[string]interface{}) |
| 77 | + for k1, v1 := range m { |
| 78 | + if n, ok := v1.(map[string]interface{}); ok { |
| 79 | + for k2, v2 := range flatten(n) { |
| 80 | + result[k1+"."+k2] = v2 |
| 81 | + } |
| 82 | + } else { |
| 83 | + result[k1] = v1 |
| 84 | + } |
| 85 | + } |
| 86 | + return result |
| 87 | +} |
0 commit comments