Coverage for nova/compute/monitors/base.py: 87%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Licensed under the Apache License, Version 2.0 (the "License"); you may 

2# not use this file except in compliance with the License. You may obtain 

3# a copy of the License at 

4# 

5# http://www.apache.org/licenses/LICENSE-2.0 

6# 

7# Unless required by applicable law or agreed to in writing, software 

8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

10# License for the specific language governing permissions and limitations 

11# under the License. 

12 

13import abc 

14 

15from nova.objects import fields 

16 

17 

18class MonitorBase(metaclass=abc.ABCMeta): 

19 """Base class for all resource monitor plugins. 

20 

21 A monitor is responsible for adding a set of related metrics to 

22 a `nova.objects.MonitorMetricList` object after the monitor has 

23 performed some sampling or monitoring action. 

24 """ 

25 

26 def __init__(self, compute_manager): 

27 self.compute_manager = compute_manager 

28 self.source = None 

29 

30 @abc.abstractmethod 

31 def get_metric_names(self): 

32 """Get available metric names. 

33 

34 Get available metric names, which are represented by a set of keys 

35 that can be used to check conflicts and duplications 

36 

37 :returns: set containing one or more values from 

38 :py:attr: nova.objects.fields.MonitorMetricType.ALL 

39 """ 

40 raise NotImplementedError('get_metric_names') 

41 

42 @abc.abstractmethod 

43 def populate_metrics(self, metric_list): 

44 """Monitors are responsible for populating this metric_list object 

45 with nova.objects.MonitorMetric objects with values collected via 

46 the respective compute drivers. 

47 

48 Note that if the monitor class is responsible for tracking a *related* 

49 set of metrics -- e.g. a set of percentages of CPU time allocated to 

50 user, kernel, and idle -- it is the responsibility of the monitor 

51 implementation to do a single sampling call to the underlying monitor 

52 to ensure that related metric values make logical sense. 

53 

54 :param metric_list: A mutable reference of the metric list object 

55 """ 

56 raise NotImplementedError('populate_metrics') 

57 

58 

59class CPUMonitorBase(MonitorBase): 

60 """Base class for all monitors that return CPU-related metrics.""" 

61 

62 def get_metric_names(self): 

63 return set([ 

64 fields.MonitorMetricType.CPU_FREQUENCY, 

65 fields.MonitorMetricType.CPU_USER_TIME, 

66 fields.MonitorMetricType.CPU_KERNEL_TIME, 

67 fields.MonitorMetricType.CPU_IDLE_TIME, 

68 fields.MonitorMetricType.CPU_IOWAIT_TIME, 

69 fields.MonitorMetricType.CPU_USER_PERCENT, 

70 fields.MonitorMetricType.CPU_KERNEL_PERCENT, 

71 fields.MonitorMetricType.CPU_IDLE_PERCENT, 

72 fields.MonitorMetricType.CPU_IOWAIT_PERCENT, 

73 fields.MonitorMetricType.CPU_PERCENT, 

74 ])