wu-sheng commented on a change in pull request #6176: URL: https://github.com/apache/skywalking/pull/6176#discussion_r573506318
########## File path: oap-server/server-receiver-plugin/skywalking-zabbix-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/zabbix/provider/ZabbixMetrics.java ########## @@ -0,0 +1,312 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.skywalking.oap.server.receiver.zabbix.provider; + +import com.google.common.collect.ImmutableMap; +import io.vavr.Tuple; +import io.vavr.Tuple2; +import lombok.Builder; +import lombok.Data; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.apache.commons.lang3.time.StopWatch; +import org.apache.commons.text.StringTokenizer; +import org.apache.skywalking.apm.util.StringUtil; +import org.apache.skywalking.oap.meter.analyzer.MetricConvert; +import org.apache.skywalking.oap.meter.analyzer.dsl.Sample; +import org.apache.skywalking.oap.meter.analyzer.dsl.SampleFamily; +import org.apache.skywalking.oap.meter.analyzer.dsl.SampleFamilyBuilder; +import org.apache.skywalking.oap.server.core.analysis.meter.MeterSystem; +import org.apache.skywalking.oap.server.library.util.CollectionUtils; +import org.apache.skywalking.oap.server.receiver.zabbix.provider.config.ZabbixConfig; +import org.apache.skywalking.oap.server.receiver.zabbix.provider.protocol.bean.ZabbixRequest; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import static com.google.common.collect.ImmutableMap.toImmutableMap; + +/** + * Management all Zabbix metrics + */ +@Slf4j +public class ZabbixMetrics { + + private final List<ZabbixConfig> originalConfigs; + + /** + * All enabled service and instance group + */ + private List<InstanceGroup> allServices = new ArrayList<>(); + + public ZabbixMetrics(List<ZabbixConfig> originalConfigs, MeterSystem meterSystem) { + this.originalConfigs = originalConfigs; + initConfigs(meterSystem); + } + + /** + * Get all key names when Zabbix agent queried + */ + public Set<String> getAllMonitorMetricNames(String hostName) { + // Find instance group + return findInstanceGroup(hostName).map(InstanceGroup::getEnabledKeys).orElse(null); + } + + /** + * Receive agent data and convert to meter system + */ + public ConvertStatics convertMetrics(List<ZabbixRequest.AgentData> agentDataList) { + if (CollectionUtils.isEmpty(agentDataList)) { + return ConvertStatics.EMPTY; + } + + return agentDataList.stream() + // Group by host + .collect(Collectors.groupingBy(ZabbixRequest.AgentData::getHost)).entrySet().stream() + // Convert every agent data list + .map(e -> findInstanceGroup(e.getKey()).map(instanceGroup -> instanceGroup.convertToMeter(e.getValue())).orElse(null)) + .filter(Objects::nonNull) + // Merge all statics + .reduce(ConvertStatics::merge) + .orElse(ConvertStatics.EMPTY); + } + + private Optional<InstanceGroup> findInstanceGroup(String hostName) { + // Find service group, support using cache + return allServices.stream().filter(group -> group.matchesWithHostName(hostName)).findAny(); + } + + private void initConfigs(MeterSystem meterSystem) { + // Temporary instance group cache, encase generate multiple instance group + HashMap<String, InstanceGroup> tmpGroupCache = new HashMap<>(); + + // Each config entities + originalConfigs.forEach(c -> + c.getEntities().getHostPatterns().forEach(instance -> + tmpGroupCache.computeIfAbsent(instance, ins -> { + InstanceGroup instanceGroup = new InstanceGroup(ins, meterSystem); + allServices.add(instanceGroup); + return instanceGroup; + }).appendMetrics(c))); + } + + /** + * Metrics convert to meter system statics + */ + @Builder + @Getter + public static class ConvertStatics { + public static final ConvertStatics EMPTY = ConvertStatics.builder().build(); + private int total; + private int success; + private int failed; + private double useTime; + + public ConvertStatics merge(ConvertStatics statics) { + this.total += statics.total; + this.success += statics.success; + this.failed += statics.failed; + this.useTime += statics.useTime; + return this; + } + } + + /** + * Instance group metrics Review comment: ```suggestion * The group of instances according to hostPatterns defined in Zabbix rule file ``` ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
