eagle-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From haoch <...@git.apache.org>
Subject [GitHub] eagle pull request #763: [EAGLE-849] Refactor system metric collector script...
Date Thu, 05 Jan 2017 05:52:59 GMT
Github user haoch commented on a diff in the pull request:

    https://github.com/apache/eagle/pull/763#discussion_r94717088
  
    --- Diff: eagle-external/hadoop_jmx_collector/system_metric_collector.py ---
    @@ -0,0 +1,300 @@
    +# !/usr/bin/python
    +#
    +# 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.
    +#
    +
    +from metric_collector import MetricCollector, Runner
    +import logging, socket, string, os, re, time
    +
    +
    +class SystemMetricCollector(MetricCollector):
    +    METRIC_PREFIX = "system"
    +    METRIC_NAME_EXCLUDE = re.compile(r"[\(|\)]")
    +
    +    def run(self):
    +        self.try_exec_func(
    +            self.collect_cpu_metric,
    +            self.collect_uptime_metric,
    +            self.collect_memory_metric,
    +            self.collect_loadavg_metric,
    +            self.collect_ipmi_cpu_temp,
    +            self.collect_nic_metric,
    +            self.collect_smartdisk_metric,
    +            self.collect_diskstat_metric
    +        )
    +
    +    def try_exec_func(self, *funcs):
    +        for func in funcs:
    +            try:
    +                logging.info("Executing: %s", func.__name__)
    +                func()
    +            except Exception as e:
    +                logging.warn("Failed to execute: %s", func.__name__)
    +                logging.exception(e)
    +
    +    # ====================================
    +    # CPU Usage
    +    # ====================================
    +
    +    def collect_cpu_metric(self):
    +        cpu_metric = self.new_metric()
    +        cpu_info = os.popen('cat /proc/stat').readlines()
    +        demension = ["cpu", "user", "nice", "system", "idle", "wait", "irq", "softirq",
"steal", "guest"]
    +
    +        total_cpu = 0
    +        total_cpu_usage = 0
    +        cpu_stat_pre = None
    +
    +        data_dir = "/tmp/eagle_cpu_stat_previous"
    +        if os.path.exists(data_dir):
    +            fd = open(data_dir, "r")
    +            cpu_stat_pre = fd.read()
    +            fd.close()
    +
    +        for item in cpu_info:
    +            if re.match(r'^cpu\d+', item) is None:
    +                continue
    +
    +            items = re.split("\s+", item.strip())
    +            demens = min(len(demension), len(items))
    +            tuple = dict()
    +            for i in range(1, demens):
    +                tuple[demension[i]] = int(items[i])
    +                cpu_metric['timestamp'] = int(round(time.time() * 1000))
    +                cpu_metric['metric'] = self.METRIC_PREFIX + "." + 'cpu.' + demension[i]
    +                cpu_metric['device'] = items[0]
    +                cpu_metric['value'] = items[i]
    +                self.collect(cpu_metric)
    +
    +            per_cpu_usage = tuple["user"] + tuple["nice"] + tuple["system"] + tuple["wait"]
+ tuple["irq"] + tuple[
    +                "softirq"] + tuple["steal"] + tuple["guest"]
    +            per_cpu_total = tuple["user"] + tuple["nice"] + tuple["system"] + tuple["idle"]
+ tuple["wait"] + tuple[
    +                "irq"] + \
    +                            tuple["softirq"] + tuple["steal"] + tuple["guest"]
    +            total_cpu += per_cpu_total
    +            total_cpu_usage += per_cpu_usage
    +
    +            # system.cpu.usage
    +            cpu_metric['timestamp'] = int(round(time.time() * 1000))
    +            cpu_metric['metric'] = self.METRIC_PREFIX + "." + 'cpu.' + "perusage"
    +            cpu_metric['device'] = items[0]
    +            cpu_metric['value'] = str(round(per_cpu_usage * 100.0 / per_cpu_total, 2))
    +            self.collect(cpu_metric)
    +
    +        cup_stat_current = str(total_cpu_usage) + " " + str(total_cpu)
    --- End diff --
    
    In fact, it's current CPU usage: 
    
        Sum(Single CPU Usage) / Sum (Single CPU Total)
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message