Roman Safronov | 7257588 | 2022-02-10 12:26:43 +0200 | [diff] [blame] | 1 | # Copyright 2022 OpenStack Foundation |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | import signal |
| 16 | import subprocess |
| 17 | |
| 18 | import fixtures |
| 19 | |
| 20 | from oslo_log import log |
| 21 | |
| 22 | |
| 23 | LOG = log.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | class NetDowntimeMeter(fixtures.Fixture): |
| 27 | def __init__(self, dest_ip, interval='0.2'): |
| 28 | self.dest_ip = dest_ip |
| 29 | # Note: for intervals lower than 0.2 ping requires root privileges |
| 30 | self.interval = interval |
| 31 | self.ping_process = None |
| 32 | |
| 33 | def _setUp(self): |
| 34 | self.start_background_pinger() |
| 35 | |
| 36 | def start_background_pinger(self): |
| 37 | cmd = ['ping', '-q', '-s1'] |
| 38 | cmd.append('-i{}'.format(self.interval)) |
| 39 | cmd.append(self.dest_ip) |
| 40 | LOG.debug("Starting background pinger to '{}' with interval {}".format( |
| 41 | self.dest_ip, self.interval)) |
| 42 | self.ping_process = subprocess.Popen( |
| 43 | cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 44 | self.addCleanup(self.cleanup) |
| 45 | |
| 46 | def cleanup(self): |
| 47 | if self.ping_process and self.ping_process.poll() is None: |
| 48 | LOG.debug('Terminating background pinger with pid {}'.format( |
| 49 | self.ping_process.pid)) |
| 50 | self.ping_process.terminate() |
| 51 | self.ping_process = None |
| 52 | |
| 53 | def get_downtime(self): |
| 54 | self.ping_process.send_signal(signal.SIGQUIT) |
| 55 | # Example of the expected output: |
| 56 | # 264/274 packets, 3% loss |
| 57 | output = self.ping_process.stderr.readline().strip().decode('utf-8') |
| 58 | if output and len(output.split()[0].split('/')) == 2: |
| 59 | succ, total = output.split()[0].split('/') |
| 60 | return (int(total) - int(succ)) * float(self.interval) |
| 61 | else: |
| 62 | LOG.warning('Unexpected output obtained from the pinger: %s', |
| 63 | output) |