Skip to content

Commit

Permalink
Add script that displays network config on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
gthiemonge committed Jun 10, 2024
1 parent e262bd5 commit 382ba56
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ if [ "$1" = "octavia-health-manager" ]; then
/usr/local/bin/container-scripts/octavia_hm_advertisement.py octavia
fi

# Ignore possible errors
/usr/local/bin/container-scripts/octavia_status.py || true

exec /usr/bin/$1 --config-file /usr/share/octavia/octavia-dist.conf --config-file /etc/octavia/octavia.conf
45 changes: 45 additions & 0 deletions templates/octaviaamphoracontroller/bin/octavia_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
#
# Copyright 2024 Red Hat Inc.
#
# Licensed 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 pyroute2 import IPRoute

ip = IPRoute()

ifaces = {}

for link in ip.get_links():
attrs = {k: v for k, v in link['attrs']}
ifaces[link['index']] = attrs['IFLA_IFNAME']

for addr in ip.get_addr():
attrs = {k: v for k, v in addr['attrs']}
print(f"addr {attrs['IFA_ADDRESS']}/{addr['prefixlen']} "
f"dev {ifaces[addr['index']]}")

for route in ip.get_routes():
attrs = {k: v for k, v in route['attrs']}
if attrs['RTA_TABLE'] != 254:
continue
suffix = f"/{route['dst_len']}" if route['dst_len'] else ""
route_str = f"route {attrs.get('RTA_DST', 'default')}{suffix} "
if attrs.get('RTA_GATEWAY'):
route_str += f"via {attrs.get('RTA_GATEWAY')} "
route_str += f"dev {ifaces[attrs['RTA_OIF']]} "
if attrs.get('RTA_PREFSRC'):
suffix = f"/{route['src_len']}" if route['src_len'] else ""
route_str += f"prefsrc {attrs.get('RTA_PREFSRC')}{suffix} "

print(route_str)

0 comments on commit 382ba56

Please sign in to comment.