<s> import argparse <EOL> import datetime <EOL> import logging <EOL> import os <EOL> import re <EOL> import urlparse <EOL> from keystoneclient . v2_0 import client <EOL> from neutronclient . neutron import client as neutron <EOL> from oschecks import utils <EOL> def _check_neutron_api ( ) : <EOL> neutron = utils . Neutron ( ) <EOL> neutron . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = int , default = <NUM_LIT:5> , <EOL> help = '<STR_LIT>' ) <EOL> neutron . add_argument ( '<STR_LIT:-c>' , dest = '<STR_LIT>' , type = int , default = <NUM_LIT:10> , <EOL> help = '<STR_LIT>' ) <EOL> options , args , client = neutron . setup ( ) <EOL> elapsed , networks = utils . timeit ( client . list_networks ) <EOL> if not networks or len ( networks . get ( '<STR_LIT>' , [ ] ) ) <= <NUM_LIT:0> : <EOL> utils . critical ( "<STR_LIT>" ) <EOL> if elapsed > options . critical : <EOL> utils . critical ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( options . critical , elapsed ) ) <EOL> elif elapsed > options . warning : <EOL> utils . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( options . warning , elapsed ) ) <EOL> else : <EOL> utils . ok ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( len ( networks [ '<STR_LIT>' ] ) , elapsed , elapsed ) ) <EOL> def check_neutron_api ( ) : <EOL> utils . safe_run ( _check_neutron_api ) <EOL> DAEMON_DEFAULT_PORT = <NUM_LIT> <EOL> def mangle_url ( orig_url , url ) : <EOL> try : <EOL> endpoint_url = urlparse . urlparse ( url ) <EOL> except Exception as e : <EOL> utils . unknown ( "<STR_LIT>" <EOL> + "<STR_LIT>" % e ) <EOL> scheme = endpoint_url . scheme <EOL> if scheme is None : <EOL> utils . unknown ( "<STR_LIT>" <EOL> + "<STR_LIT>" % e ) <EOL> catalog_url = urlparse . urlparse ( orig_url ) <EOL> port = endpoint_url . port <EOL> if port is None : <EOL> if catalog_url . port is None : <EOL> port = DAEMON_DEFAULT_PORT <EOL> else : <EOL> port = catalog_url . port <EOL> netloc = "<STR_LIT>" % ( endpoint_url . hostname , port ) <EOL> url = urlparse . urlunparse ( [ scheme , <EOL> netloc , <EOL> catalog_url . path , <EOL> catalog_url . params , <EOL> catalog_url . query , <EOL> catalog_url . fragment ] ) <EOL> return url <EOL> class Novautils ( object ) : <EOL> def __init__ ( self , nova_client , tenant_id ) : <EOL> self . nova_client = nova_client <EOL> self . msgs = [ ] <EOL> self . start = self . totimestamp ( ) <EOL> self . notifications = [ "<STR_LIT>" % self . start ] <EOL> self . connection_done = False <EOL> self . all_floating_ips = [ ] <EOL> self . fip = None <EOL> self . network_id = None <EOL> self . tenant_id = tenant_id <EOL> @ staticmethod <EOL> def totimestamp ( dt = None , epoch = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) : <EOL> if not dt : <EOL> dt = datetime . datetime . utcnow ( ) <EOL> td = dt - epoch <EOL> return int ( ( td . microseconds + ( td . seconds + td . days * <NUM_LIT> * <NUM_LIT> ) <EOL> * <NUM_LIT:10> ** <NUM_LIT:6> ) / <NUM_LIT> ) <EOL> def check_connection ( self , force = False ) : <EOL> if not self . connection_done or force : <EOL> try : <EOL> self . connection_done = self . nova_client . list_ports ( ) <EOL> except Exception as e : <EOL> utils . critical ( "<STR_LIT>" % e ) <EOL> def get_duration ( self ) : <EOL> return self . totimestamp ( ) - self . start <EOL> def list_floating_ips ( self ) : <EOL> if not self . all_floating_ips : <EOL> for floating_ip in self . nova_client . list_floatingips ( <EOL> fields = [ '<STR_LIT>' , '<STR_LIT:id>' ] , <EOL> tenant_id = self . tenant_id ) [ '<STR_LIT>' ] : <EOL> self . all_floating_ips . append ( floating_ip ) <EOL> return self . all_floating_ips <EOL> def check_existing_floatingip ( self , floating_ip = None , delete = False ) : <EOL> count = <NUM_LIT:0> <EOL> found_ips = [ ] <EOL> for ip in self . list_floating_ips ( ) : <EOL> if floating_ip == '<STR_LIT:all>' or floating_ip . match ( <EOL> ip [ '<STR_LIT>' ] ) : <EOL> if delete : <EOL> self . nova_client . delete_floatingip ( ip [ '<STR_LIT:id>' ] ) <EOL> found_ips . append ( ip [ '<STR_LIT>' ] ) <EOL> count += <NUM_LIT:1> <EOL> if count > <NUM_LIT:0> : <EOL> if delete : <EOL> self . notifications . append ( "<STR_LIT>" <EOL> % ( count , '<STR_LIT:{>' + '<STR_LIT:U+002CU+0020>' . join ( <EOL> found_ips ) + '<STR_LIT:}>' ) ) <EOL> else : <EOL> self . msgs . append ( "<STR_LIT>" <EOL> % ( count , '<STR_LIT:U+002CU+0020>' . join ( found_ips ) ) <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" ) <EOL> def get_network_id ( self , router_name ) : <EOL> if not self . msgs : <EOL> if not self . network_id : <EOL> try : <EOL> self . network_id = self . nova_client . list_networks ( <EOL> name = router_name , fields = '<STR_LIT:id>' ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> except Exception : <EOL> self . msgs . append ( "<STR_LIT>" <EOL> % router_name ) <EOL> def create_floating_ip ( self ) : <EOL> if not self . msgs : <EOL> try : <EOL> body = { '<STR_LIT>' : { '<STR_LIT>' : self . network_id } } <EOL> self . fip = self . nova_client . create_floatingip ( body = body ) <EOL> self . notifications . append ( <EOL> "<STR_LIT>" % self . fip [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> except Exception as e : <EOL> self . msgs . append ( "<STR_LIT>" % e ) <EOL> def delete_floating_ip ( self ) : <EOL> if not self . msgs : <EOL> try : <EOL> self . nova_client . delete_floatingip ( <EOL> self . fip [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> except Exception : <EOL> self . msgs . append ( "<STR_LIT>" <EOL> % self . fip [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> def fip_type ( string ) : <EOL> if string == '<STR_LIT:all>' : <EOL> return '<STR_LIT:all>' <EOL> else : <EOL> return re . compile ( string ) <EOL> def _check_neutron_floating_ip ( ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' <EOL> + '<STR_LIT>' <EOL> + '<STR_LIT>' <EOL> + '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , type = str , <EOL> default = os . getenv ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT:username>' , type = str , <EOL> default = os . getenv ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT:password>' , type = str , <EOL> default = os . getenv ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , type = str , <EOL> default = os . getenv ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , type = str , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , type = str , <EOL> default = "<STR_LIT>" , <EOL> help = '<STR_LIT>' <EOL> + '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> + '<STR_LIT>' <EOL> + '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , type = int , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' <EOL> + '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , type = fip_type , <EOL> default = None , <EOL> help = '<STR_LIT>' <EOL> + '<STR_LIT>' <EOL> + '<STR_LIT>' <EOL> + '<STR_LIT>' <EOL> + '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> type = str , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:count>' , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> try : <EOL> nova_client = client . Client ( <EOL> username = args . username , <EOL> tenant_name = args . tenant , <EOL> password = args . password , <EOL> auth_url = args . auth_url , <EOL> ) <EOL> nova_client . authenticate ( ) <EOL> except Exception as e : <EOL> utils . critical ( "<STR_LIT>" % e ) <EOL> try : <EOL> endpoint = nova_client . service_catalog . get_endpoints ( <EOL> '<STR_LIT>' ) [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ args . endpoint_type ] <EOL> if args . endpoint_url : <EOL> endpoint = mangle_url ( endpoint , args . endpoint_url ) <EOL> token = nova_client . service_catalog . get_token ( ) [ '<STR_LIT:id>' ] <EOL> if args . verbose : <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> neutron_client = neutron . Client ( '<STR_LIT>' , endpoint_url = endpoint , <EOL> token = token ) <EOL> except Exception as e : <EOL> utils . critical ( "<STR_LIT>" % e ) <EOL> util = Novautils ( neutron_client , nova_client . tenant_id ) <EOL> util . check_connection ( ) <EOL> if args . floating_ip : <EOL> util . check_existing_floatingip ( args . floating_ip , args . force_delete ) <EOL> util . get_network_id ( args . ext_router_name ) <EOL> util . create_floating_ip ( ) <EOL> util . delete_floating_ip ( ) <EOL> if util . msgs : <EOL> utils . critical ( "<STR_LIT:U+002CU+0020>" . join ( util . msgs ) ) <EOL> duration = util . get_duration ( ) <EOL> notification = "<STR_LIT>" <EOL> if util . notifications : <EOL> notification = "<STR_LIT:(>" + "<STR_LIT:U+002CU+0020>" . join ( util . notifications ) + "<STR_LIT:)>" <EOL> utils . ok ( "<STR_LIT>" <EOL> % ( notification , duration ) ) <EOL> def check_neutron_floating_ip ( ) : <EOL> utils . safe_run ( _check_neutron_floating_ip ) </s>
