|
| 1 | +# Copyright (c) 2023, INRIA |
| 2 | +# Copyright (c) 2023, University of Lille |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions are met: |
| 7 | +# |
| 8 | +# * Redistributions of source code must retain the above copyright notice, this |
| 9 | +# list of conditions and the following disclaimer. |
| 10 | +# |
| 11 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +# this list of conditions and the following disclaimer in the documentation |
| 13 | +# and/or other materials provided with the distribution. |
| 14 | +# |
| 15 | +# * Neither the name of the copyright holder nor the names of its |
| 16 | +# contributors may be used to endorse or promote products derived from |
| 17 | +# this software without specific prior written permission. |
| 18 | +# |
| 19 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +from powerapi.handler import StartHandler, PoisonPillMessageHandler as PoisonPillHandler |
| 31 | +from powerapi.processor.handlers import ProcessorReportHandler |
| 32 | +from powerapi.report import HWPCReport |
| 33 | +from ._utils import get_instance_name_from_libvirt_cgroup |
| 34 | +from .metadata_cache_manager import ServerMetadata, MetadataSyncFailed |
| 35 | + |
| 36 | + |
| 37 | +class StartMessageHandler(StartHandler): |
| 38 | + """ |
| 39 | + Start message handler for the OpenStack processor actor. |
| 40 | + """ |
| 41 | + |
| 42 | + def initialization(self): |
| 43 | + """ |
| 44 | + Initialize the OpenStack processor. |
| 45 | + """ |
| 46 | + for actor in self.state.target_actors: |
| 47 | + actor.connect_data() |
| 48 | + |
| 49 | + self.state.monitor_agent.start() |
| 50 | + |
| 51 | + |
| 52 | +class PoisonPillMessageHandler(PoisonPillHandler): |
| 53 | + """ |
| 54 | + PoisonPill message handler for the OpenStack processor actor. |
| 55 | + """ |
| 56 | + |
| 57 | + def teardown(self, soft: bool = False): |
| 58 | + """ |
| 59 | + Teardown the OpenStack processor. |
| 60 | + """ |
| 61 | + for actor in self.state.target_actors: |
| 62 | + actor.socket_interface.close() |
| 63 | + |
| 64 | + |
| 65 | +class HWPCReportHandler(ProcessorReportHandler): |
| 66 | + """ |
| 67 | + Generic report handler for the OpenStack processor actor. |
| 68 | + Used to add the server metadata (from the OpenStack API) to the processed report. |
| 69 | + """ |
| 70 | + |
| 71 | + def try_get_server_metadata(self, sensor_name: str, instance_name: str) -> ServerMetadata | None: |
| 72 | + """ |
| 73 | + Try to get the server metadata from the cache. |
| 74 | + :param sensor_name: Name of the sensor |
| 75 | + :param instance_name: Name of the instance to fetch metadata for |
| 76 | + :return: Server metadata entry or None if not found |
| 77 | + """ |
| 78 | + server_metadata = None |
| 79 | + try: |
| 80 | + server_metadata = self.state.metadata_cache_manager.get_server_metadata(sensor_name, instance_name) |
| 81 | + if server_metadata is None: |
| 82 | + # Retry once after syncing the metadata cache. |
| 83 | + self.state.metadata_cache_manager.sync_metadata_cache_from_api() |
| 84 | + server_metadata = self.state.metadata_cache_manager.get_server_metadata(sensor_name, instance_name) |
| 85 | + except MetadataSyncFailed as exn: |
| 86 | + self.state.logger.warning(exn) |
| 87 | + |
| 88 | + return server_metadata |
| 89 | + |
| 90 | + def handle(self, msg: HWPCReport): |
| 91 | + """ |
| 92 | + Process an HWPCReport to add the Kubernetes metadata. |
| 93 | + :param msg: The HWPCReport to process |
| 94 | + """ |
| 95 | + instance_name = get_instance_name_from_libvirt_cgroup(msg.target) |
| 96 | + if instance_name is not None: |
| 97 | + server_metadata = self.try_get_server_metadata(msg.sensor, instance_name) |
| 98 | + if server_metadata is None: |
| 99 | + # Drop the report if the server metadata is not present in the cache. |
| 100 | + return |
| 101 | + |
| 102 | + msg.target = server_metadata.server_name |
| 103 | + msg.metadata['openstack'] = vars(server_metadata) |
| 104 | + |
| 105 | + self._send_report(msg) |
0 commit comments