def test_getURLComponents(self): from gecoscc.utils import getURLComponents components = getURLComponents('https://www.google.es') self.assertEqual(components['protocol'], 'https') self.assertEqual(components['port'], '443') self.assertEqual(components['host_name'], 'www.google.es')
def internal_server_connections(context, request): # Prepare the connection filter settings = get_current_registry().settings cfilter = [] chef_url = settings.get('chef.url') chef_url_comp = getURLComponents(chef_url) chef_filter = {} chef_filter['name'] = 'chef' chef_filter['remote_host'] = socket.gethostbyname(chef_url_comp['host_name']) chef_filter['remote_port'] = chef_url_comp['port'] logger.debug("internal_server_connections: chef filter: %s:%s"%(chef_filter['remote_host'], chef_filter['remote_port'])) cfilter.append(chef_filter) mongo_uri = settings.get('mongo_uri') mongo_url_comp = getURLComponents(mongo_uri) mongo_filter = {} mongo_filter['name'] = 'mongo' mongo_filter['remote_host'] = socket.gethostbyname(mongo_url_comp['host_name']) mongo_filter['remote_port'] = mongo_url_comp['port'] logger.debug("internal_server_connections: mongo filter: %s:%s"%(mongo_filter['remote_host'], mongo_filter['remote_port'])) cfilter.append(mongo_filter) # Get the connection list of this server STATE = { '01':'ESTABLISHED', '02':'SYN_SENT', '03':'SYN_RECV', '04':'FIN_WAIT1', '05':'FIN_WAIT2', '06':'TIME_WAIT', '07':'CLOSE', '08':'CLOSE_WAIT', '09':'LAST_ACK', '0A':'LISTEN', '0B':'CLOSING' } server_connections = [] try: fd = open("/proc/net/tcp", "r") line = fd.readline() # Skip the first line line = fd.readline() while line is not None: line_array = _remove_empty(line.split(' ')) # Split lines and remove empty spaces. if len(line_array) < 10: break l_host,l_port = _convert_ip_port(line_array[1]) # Convert ipaddress and port from hex to decimal. r_host,r_port = _convert_ip_port(line_array[2]) #tcp_id = line_array[0] state = STATE[line_array[3]] #uid = pwd.getpwuid(int(line_array[7]))[0] # Get user from UID. #inode = line_array[9] # Need the inode to get process pid. for f in cfilter: if r_host == f['remote_host'] and r_port == f['remote_port']: connection = {} connection['remote_service'] = f['name'] connection['local_host'] = l_host connection['local_port'] = l_port connection['remote_host'] = r_host connection['remote_port'] = r_port connection['state'] = state server_connections.append(connection) line = fd.readline() fd.close() except Exception as e: logger.error("internal_server_connections %s"%(str(e))) logger.error("Traceback: %s"%(traceback.format_exc())) return (0.0, 0.0) return server_connections
def internal_server_connections(context, request): # Prepare the connection filter settings = get_current_registry().settings filter = [] chef_url = settings.get('chef.url') chef_url_comp = getURLComponents(chef_url) chef_filter = {} chef_filter['name'] = 'chef' chef_filter['remote_host'] = socket.gethostbyname(chef_url_comp['host_name']) chef_filter['remote_port'] = chef_url_comp['port'] logger.debug("internal_server_connections: chef filter: %s:%s"%(chef_filter['remote_host'], chef_filter['remote_port'])) filter.append(chef_filter) mongo_uri = settings.get('mongo_uri') mongo_url_comp = getURLComponents(mongo_uri) mongo_filter = {} mongo_filter['name'] = 'mongo' mongo_filter['remote_host'] = socket.gethostbyname(mongo_url_comp['host_name']) mongo_filter['remote_port'] = mongo_url_comp['port'] logger.debug("internal_server_connections: mongo filter: %s:%s"%(mongo_filter['remote_host'], mongo_filter['remote_port'])) filter.append(mongo_filter) # Get the connection list of this server STATE = { '01':'ESTABLISHED', '02':'SYN_SENT', '03':'SYN_RECV', '04':'FIN_WAIT1', '05':'FIN_WAIT2', '06':'TIME_WAIT', '07':'CLOSE', '08':'CLOSE_WAIT', '09':'LAST_ACK', '0A':'LISTEN', '0B':'CLOSING' } server_connections = [] try: fd = file("/proc/net/tcp", "r") line = fd.readline() # Skip the first line line = fd.readline() while line is not None: line_array = _remove_empty(line.split(' ')) # Split lines and remove empty spaces. if len(line_array) < 10: break l_host,l_port = _convert_ip_port(line_array[1]) # Convert ipaddress and port from hex to decimal. r_host,r_port = _convert_ip_port(line_array[2]) #tcp_id = line_array[0] state = STATE[line_array[3]] #uid = pwd.getpwuid(int(line_array[7]))[0] # Get user from UID. #inode = line_array[9] # Need the inode to get process pid. for f in filter: if r_host == f['remote_host'] and r_port == f['remote_port']: connection = {} connection['remote_service'] = f['name'] connection['local_host'] = l_host connection['local_port'] = l_port connection['remote_host'] = r_host connection['remote_port'] = r_port connection['state'] = state server_connections.append(connection) line = fd.readline() fd.close() except Exception, e: logger.error("internal_server_connections %s"%(str(e))) logger.error("Traceback: %s"%(traceback.format_exc())) return (0.0, 0.0)