Artyom Lukianov has uploaded a new change for review. Change subject: hooks:checkips: add checkips hook ......................................................................
hooks:checkips: add checkips hook Give possibilty to check connectivity between host and given addresses. Update network stats with state of addresses. Change-Id: I53cec37310f0f1844d6fe244419fd8c10e9b7ebb Signed-off-by: Lukianov Artyom <[email protected]> --- M configure.ac M vdsm_hooks/Makefile.am A vdsm_hooks/checkips/Makefile.am A vdsm_hooks/checkips/README A vdsm_hooks/checkips/after_get_stats.py 5 files changed, 177 insertions(+), 0 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/02/54102/1 diff --git a/configure.ac b/configure.ac index 51cc514..d3c5282 100644 --- a/configure.ac +++ b/configure.ac @@ -380,6 +380,7 @@ vdsm/virt/vmdevices/Makefile vdsm_hooks/Makefile vdsm_hooks/allocate_net/Makefile + vdsm_hooks/checkips/Makefile vdsm_hooks/checkimages/Makefile vdsm_hooks/diskunmap/Makefile vdsm_hooks/ethtool_options/Makefile diff --git a/vdsm_hooks/Makefile.am b/vdsm_hooks/Makefile.am index d23fb1c..478fcd3 100644 --- a/vdsm_hooks/Makefile.am +++ b/vdsm_hooks/Makefile.am @@ -35,6 +35,7 @@ if HOOKS SUBDIRS += \ allocate_net \ + checkips \ checkimages \ diskunmap \ extnet \ diff --git a/vdsm_hooks/checkips/Makefile.am b/vdsm_hooks/checkips/Makefile.am new file mode 100644 index 0000000..0ec1cbf --- /dev/null +++ b/vdsm_hooks/checkips/Makefile.am @@ -0,0 +1,30 @@ +# +# Copyright 2014 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Refer to the README and COPYING files for full details of the license +# +# +EXTRA_DIST = \ + after_get_stats.py + +install-data-local: + $(MKDIR_P) $(DESTDIR)$(vdsmhooksdir)/after_get_stats + $(INSTALL_SCRIPT) $(srcdir)/after_get_stats.py \ + $(DESTDIR)$(vdsmhooksdir)/after_get_stats/10_checkips + +uninstall-local: + $(RM) $(DESTDIR)$(vdsmhooksdir)/after_get_stats/10_checkips diff --git a/vdsm_hooks/checkips/README b/vdsm_hooks/checkips/README new file mode 100644 index 0000000..5a68b20 --- /dev/null +++ b/vdsm_hooks/checkips/README @@ -0,0 +1,6 @@ +check_ips vdsm hook +===================== +This hook check connectivity from host to given addresses + +This hook is useful in cases where you need to check connectivity from host to +specific vlan, before you start vm on it. diff --git a/vdsm_hooks/checkips/after_get_stats.py b/vdsm_hooks/checkips/after_get_stats.py new file mode 100644 index 0000000..586718a --- /dev/null +++ b/vdsm_hooks/checkips/after_get_stats.py @@ -0,0 +1,139 @@ +#!/usr/bin/env python +# +# Copyright 2014 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Refer to the README and COPYING files for full details of the license +# +import os +import sys +import socket +import hooking +import traceback + +CHECK_IPS = 'checkips' +HELP_ARG = "-h" +TEST_ARG = "-t" +HELP_TEXT = """usage: %(prog)s [%(help)s] [%(test)s] + +Check connectivity from host to specific address via ping + +optional arguments: + %(help)s show this help message and exit + %(test)s run a dry test for the hook +""" % { + 'help': HELP_ARG, + 'test': TEST_ARG, + 'prog': sys.argv[0] +} + + +def _parse_addresses(): + return [address for address in os.environ[CHECK_IPS].split(',')] + + +def _is_ipv4_address(address): + try: + socket.inet_pton(socket.AF_INET, address) + except socket.error: # not a valid address + return False + return True + + +def _is_address_connective(address): + ping = 'ping' if _is_ipv4_address(address) else 'ping6' + command = [ping, '-c', '1', address] + rc, out, err = hooking.execCmd(command, raw=True) + return False if rc else True + + +def allocate_given_address(stats_json): + addresses = _parse_addresses() + network = stats_json['network'] + sample_time = network.itervalues().next()['sampleTime'] + for address in addresses: + state = 'up' if _is_address_connective(address) else 'down' + network[address] = { + 'name': address, + 'rx': '0', + 'rxErrors': '0', + 'rxRate': '0.0', + 'sampleTime': sample_time, + 'speed': '1000', + 'state': state, + 'tx': '0', + 'txDropped': '0', + 'txErrors': '0', + 'txRate': '0.0' + } + + +def test(): + os.environ[CHECK_IPS] = '127.0.0.1,::1,test.test' + test_stats = {} + network = { + 'eno1': { + 'name': 'eno1', + 'rx': '66167953505', + 'rxDropped': '2454', + 'rxErrors': '0', + 'rxRate': '0.1', + 'sampleTime': 1456488197.897933, + 'speed': '10000', + 'state': 'up', + 'tx': '3583097015', + 'txDropped': '0', + 'txErrors': '0', + 'txRate': '0.0' + }, + } + test_stats['network'] = network + allocate_given_address(test_stats) + expected_states = { + '127.0.0.1': 'up', + '::1': 'up', + 'test.test': 'down' + } + for interface, state in expected_states.iteritems(): + test_msg = 'pass' + if test_stats['network'][interface]['state'] != state: + test_msg = 'fail' + print( + 'test %s: interface %s has state %s' % + (test_msg, interface, state) + ) + + +def main(): + stats_json = hooking.read_json() + allocate_given_address(stats_json) + hooking.write_json(stats_json) + + +if __name__ == '__main__': + if HELP_ARG in sys.argv: + hooking.exit_hook(HELP_TEXT) + + try: + if TEST_ARG in sys.argv: + test() + else: + main() + except: + hooking.exit_hook( + 'check ips hook: [unexpected error]: %s\n' % + traceback.format_exc() + ) -- To view, visit https://gerrit.ovirt.org/54102 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I53cec37310f0f1844d6fe244419fd8c10e9b7ebb Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Artyom Lukianov <[email protected]> _______________________________________________ vdsm-patches mailing list [email protected] https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches
