|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/go-kit/log" |
| 6 | + "github.com/go-kit/log/level" |
| 7 | + "github.com/tencentyun/tencentcloud-exporter/pkg/common" |
| 8 | + "github.com/tencentyun/tencentcloud-exporter/pkg/instance" |
| 9 | + "github.com/tencentyun/tencentcloud-exporter/pkg/metric" |
| 10 | + "github.com/tencentyun/tencentcloud-exporter/pkg/util" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + ClbPrivateNamespace = "QCE/LB_PRIVATE" |
| 15 | + ClbPrivateInstanceidKey = "vip" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + ClbPrivateExcludeMetrics = []string{ |
| 20 | + "ConnRatio", "OverloadCurConn", "SnatFail", // clb_snat_vip |
| 21 | + "PvvInpkg", "PvvOutpkg", "PvvConnum", "PvvIntraffic", "PvvNewConn", "PvvOuttraffic", // new_vpcid_proto_vip_vport |
| 22 | + "VvIntraffic", "VvInpkg", "VvNewConn", "VvOutpkg", "VvOuttraffic", "VvConnum", // new_vip_vpcid |
| 23 | + } |
| 24 | +) |
| 25 | + |
| 26 | +func init() { |
| 27 | + registerHandler(ClbPrivateNamespace, defaultHandlerEnabled, NewClbPrivateHandler) |
| 28 | +} |
| 29 | + |
| 30 | +type ClbPrivateHandler struct { |
| 31 | + baseProductHandler |
| 32 | +} |
| 33 | + |
| 34 | +func (h *ClbPrivateHandler) IsMetricMetaVaild(meta *metric.TcmMeta) bool { |
| 35 | + return true |
| 36 | +} |
| 37 | + |
| 38 | +func (h *ClbPrivateHandler) GetNamespace() string { |
| 39 | + return ClbPrivateNamespace |
| 40 | +} |
| 41 | + |
| 42 | +func (h *ClbPrivateHandler) IsMetricVaild(m *metric.TcmMetric) bool { |
| 43 | + if util.IsStrInList(ClbPrivateExcludeMetrics, m.Meta.MetricName) { |
| 44 | + return false |
| 45 | + } |
| 46 | + var dimensions []string |
| 47 | + for _, v := range m.Meta.SupportDimensions { |
| 48 | + dimensions = append(dimensions, v) |
| 49 | + } |
| 50 | + if len(dimensions) == 0 { |
| 51 | + return false |
| 52 | + } |
| 53 | + _, ok := excludeMetricName[m.Meta.MetricName] |
| 54 | + if ok { |
| 55 | + return false |
| 56 | + } |
| 57 | + p, err := m.Meta.GetPeriod(m.Conf.StatPeriodSeconds) |
| 58 | + if err != nil { |
| 59 | + return false |
| 60 | + } |
| 61 | + if p != m.Conf.StatPeriodSeconds { |
| 62 | + return false |
| 63 | + } |
| 64 | + return true |
| 65 | +} |
| 66 | +func (h *ClbPrivateHandler) GetSeries(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { |
| 67 | + if m.Conf.IsIncludeOnlyInstance() { |
| 68 | + return h.GetSeriesByOnly(m) |
| 69 | + } |
| 70 | + |
| 71 | + if m.Conf.IsIncludeAllInstance() { |
| 72 | + return h.GetSeriesByAll(m) |
| 73 | + } |
| 74 | + |
| 75 | + if m.Conf.IsCustomQueryDimensions() { |
| 76 | + return h.GetSeriesByCustom(m) |
| 77 | + } |
| 78 | + |
| 79 | + return nil, fmt.Errorf("must config all_instances or only_include_instances or custom_query_dimensions") |
| 80 | +} |
| 81 | + |
| 82 | +func (h *ClbPrivateHandler) GetSeriesByOnly(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { |
| 83 | + var slist []*metric.TcmSeries |
| 84 | + for _, insId := range m.Conf.OnlyIncludeInstances { |
| 85 | + ins, err := h.collector.InstanceRepo.Get(insId) |
| 86 | + if err != nil { |
| 87 | + level.Error(h.logger).Log("msg", "Instance not found", "id", insId) |
| 88 | + continue |
| 89 | + } |
| 90 | + sl, err := h.getSeriesByMetricType(m, ins) |
| 91 | + if err != nil { |
| 92 | + level.Error(h.logger).Log("msg", "Create metric series fail", |
| 93 | + "metric", m.Meta.MetricName, "instacne", ins.GetInstanceId()) |
| 94 | + continue |
| 95 | + } |
| 96 | + slist = append(slist, sl...) |
| 97 | + } |
| 98 | + return slist, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (h *ClbPrivateHandler) GetSeriesByAll(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { |
| 102 | + var slist []*metric.TcmSeries |
| 103 | + insList, err := h.collector.InstanceRepo.ListByFilters(m.Conf.InstanceFilters) |
| 104 | + if err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + for _, ins := range insList { |
| 108 | + if len(m.Conf.ExcludeInstances) != 0 && util.IsStrInList(m.Conf.ExcludeInstances, ins.GetInstanceId()) { |
| 109 | + continue |
| 110 | + } |
| 111 | + sl, err := h.getSeriesByMetricType(m, ins) |
| 112 | + if err != nil { |
| 113 | + level.Error(h.logger).Log("msg", "Create metric series fail", |
| 114 | + "metric", m.Meta.MetricName, "instacne", ins.GetInstanceId(), "error", err) |
| 115 | + continue |
| 116 | + } |
| 117 | + slist = append(slist, sl...) |
| 118 | + } |
| 119 | + return slist, nil |
| 120 | +} |
| 121 | + |
| 122 | +func (h *ClbPrivateHandler) GetSeriesByCustom(m *metric.TcmMetric) ([]*metric.TcmSeries, error) { |
| 123 | + var slist []*metric.TcmSeries |
| 124 | + for _, ql := range m.Conf.CustomQueryDimensions { |
| 125 | + v, ok := ql[h.monitorQueryKey] |
| 126 | + if !ok { |
| 127 | + level.Error(h.logger).Log( |
| 128 | + "msg", fmt.Sprintf("not found %s in queryDimensions", h.monitorQueryKey), |
| 129 | + "ql", fmt.Sprintf("%v", ql)) |
| 130 | + continue |
| 131 | + } |
| 132 | + ins, err := h.collector.InstanceRepo.Get(v) |
| 133 | + if err != nil { |
| 134 | + level.Error(h.logger).Log("msg", "Instance not found", "err", err, "id", v) |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + sl, err := h.getSeriesByMetricType(m, ins) |
| 139 | + if err != nil { |
| 140 | + level.Error(h.logger).Log("msg", "Create metric series fail", |
| 141 | + "metric", m.Meta.MetricName, "instacne", ins.GetInstanceId()) |
| 142 | + continue |
| 143 | + } |
| 144 | + slist = append(slist, sl...) |
| 145 | + } |
| 146 | + return slist, nil |
| 147 | +} |
| 148 | + |
| 149 | +func (h *ClbPrivateHandler) getSeriesByMetricType(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { |
| 150 | + var dimensions []string |
| 151 | + for _, v := range m.Meta.SupportDimensions { |
| 152 | + dimensions = append(dimensions, v) |
| 153 | + } |
| 154 | + return h.getClbPrivateSeries(m, ins) |
| 155 | +} |
| 156 | + |
| 157 | +func (h *ClbPrivateHandler) getClbPrivateSeries(m *metric.TcmMetric, ins instance.TcInstance) ([]*metric.TcmSeries, error) { |
| 158 | + var series []*metric.TcmSeries |
| 159 | + vpcId, err := ins.GetFieldValueByName("VpcId") |
| 160 | + if err != nil { |
| 161 | + level.Error(h.logger).Log("msg", "ClusterId not found") |
| 162 | + } |
| 163 | + ql := map[string]string{ |
| 164 | + h.monitorQueryKey: ins.GetMonitorQueryKey(), |
| 165 | + "vpcId": vpcId, |
| 166 | + } |
| 167 | + s, err := metric.NewTcmSeries(m, ql, ins) |
| 168 | + if err != nil { |
| 169 | + return nil, err |
| 170 | + } |
| 171 | + series = append(series, s) |
| 172 | + return series, nil |
| 173 | +} |
| 174 | + |
| 175 | +func NewClbPrivateHandler(cred common.CredentialIface, c *TcProductCollector, logger log.Logger) (handler ProductHandler, err error) { |
| 176 | + handler = &ClbPrivateHandler{ |
| 177 | + baseProductHandler{ |
| 178 | + monitorQueryKey: ClbPrivateInstanceidKey, |
| 179 | + collector: c, |
| 180 | + logger: logger, |
| 181 | + }, |
| 182 | + } |
| 183 | + return |
| 184 | + |
| 185 | +} |
0 commit comments