summaryrefslogtreecommitdiff
path: root/ambari-metrics/ambari-metrics-host-monitoring/src/main/python/core/metric_collector.py
blob: c28fd038804a25d607765a5d1edb3f1fca10de5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env 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.
'''

import logging
from time import time
from host_info import HostInfo
from event_definition import HostMetricCollectEvent, ProcessMetricCollectEvent

logger = logging.getLogger()

DEFAULT_HOST_APP_ID = '_HOST'

class MetricsCollector():
  """
  The main Reader thread that dequeues events from the event queue and
  submits a metric record to the emit buffer. Implementation of dequeue is
  not required if Timer class is used for metric groups.
  """

  def __init__(self, emit_queue, application_metric_map, host_info):
    self.emit_queue = emit_queue
    self.application_metric_map = application_metric_map
    self.host_info = host_info
  pass

  def process_event(self, event):
    if event.get_classname() == HostMetricCollectEvent.__name__:
      self.process_host_collection_event(event)
    elif event.get_classname() == ProcessMetricCollectEvent.__name__:
      self.process_process_collection_event(event)
    else:
      logger.warn('Unknown event in queue')
    pass

  def process_host_collection_event(self, event):
    startTime = int(round(time() * 1000))
    metrics = None

    if 'cpu' in event.get_group_name():
      metrics = self.host_info.get_cpu_times()

    elif 'disk' in event.get_group_name():
      metrics = self.host_info.get_combined_disk_usage()
      metrics.update(self.host_info.get_disk_io_counters())

    elif 'network' in event.get_group_name():
      metrics = self.host_info.get_network_info()

    elif 'mem' in event.get_group_name():
      metrics = self.host_info.get_mem_info()

    elif 'process' in event.get_group_name():
      metrics = self.host_info.get_process_info()

    elif 'all' in event.get_group_name():
      metrics = {}
      metrics.update(self.host_info.get_cpu_times())
      metrics.update(self.host_info.get_combined_disk_usage())
      metrics.update(self.host_info.get_network_info())
      metrics.update(self.host_info.get_mem_info())
      metrics.update(self.host_info.get_process_info())
      metrics.update(self.host_info.get_disk_io_counters())

    else:
      logger.warn('Unknown metric group.')
    pass

    if metrics:
      self.application_metric_map.put_metric(DEFAULT_HOST_APP_ID, metrics, startTime)
    pass

  def process_process_collection_event(self, event):
    """
    Collect Process level metrics and update the application metric map
    """
    pass