Subject: Bug#684: select_session offers offline servers to X2Go Client

From: Sergey Savko <[email-masked]>
Date: Fri, 5 Dec 2014 20:14:51 +0400 (MSK)

This patch work after patch from http://bugs.x2go.org/cgi-bin/bugreport.cgi?bug=686

-- 
С уважением,
Сергей Савко,
начальник IT отдела.
+7-931-361-04-02

From 5f8a636aa7029baf5ff5df779538d22062b3ee74 Mon Sep 17 00:00:00 2001
From: Sergey Savko <[email-masked]>
Date: Fri, 5 Dec 2014 19:09:34 +0300
Subject: [PATCH] Gives a coefficient of 1 for the server, if the server is
 down.

---
 x2gobroker/agent.py               |  5 +++++
 x2gobroker/brokers/base_broker.py | 38 +++++++++++++++++++++++++++-----------
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/x2gobroker/agent.py b/x2gobroker/agent.py
index e4bef4d..ba9662b 100644
--- a/x2gobroker/agent.py
+++ b/x2gobroker/agent.py
@@ -172,6 +172,8 @@ def _call_remote_broker_agent(username, task, cmdline_args=[], remote_agent=None
         '{username}'.format(username=username),
         '{task}'.format(task=task),
     ]
+    
+    
 
     for cmdline_arg in cmdline_args:
         cmd_line.append('"{arg}"'.format(arg=cmdline_arg))
@@ -183,6 +185,9 @@ def _call_remote_broker_agent(username, task, cmdline_args=[], remote_agent=None
     else:
         remote_port = 22
 
+    if icmp_ping(remote_hostname) is False:
+	return (False, [])
+
     # now, connect and use paramiko Client to negotiate SSH2 across the connection
     try:
         client = paramiko.SSHClient()
diff --git a/x2gobroker/brokers/base_broker.py b/x2gobroker/brokers/base_broker.py
index fc0f44f..270bab7 100644
--- a/x2gobroker/brokers/base_broker.py
+++ b/x2gobroker/brokers/base_broker.py
@@ -1220,19 +1220,32 @@ class X2GoBroker(object):
             busy_servers = None
 	    busy_each_server = dict()
 	    while server_list :
-		remote_agent_server = server_list[-1]
-		remote_agent_port = profile[u'sshport']
-		remote_agent = {u'hostname': remote_agent_server, u'port': remote_agent_port, }
-		try:
-		    success, busy_each_servers = x2gobroker.agent.find_busy_servers(username=username, remote_agent=remote_agent)
-		except x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException:
-		    pass
-		if busy_servers is None:
-		    busy_servers = busy_each_servers
-		else :
-		    busy_servers.update(busy_each_servers)
+		if x2gobroker.agent.icmp_ping(server_list[-1]):
+		    remote_agent_server = server_list[-1]
+		    remote_agent_port = profile[u'sshport']
+		    remote_agent = {u'hostname': remote_agent_server, u'port': remote_agent_port, }
+		    try:
+			success, busy_each_server = x2gobroker.agent.find_busy_servers(username=username, remote_agent=remote_agent)
+		    except x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException:
+			pass
+		    if busy_servers is None:
+			busy_servers = busy_each_server
+		    else :
+			busy_servers.update(busy_each_server)
+		else:
+		    busy_each_server.update({ server_list[-1] : 1 })
+		    if busy_servers is None:
+			busy_servers = busy_each_server
+		    else :
+			busy_servers.update(busy_each_server)
 		server_list = server_list[0:-1]
 
+#            try:
+#                success, busy_servers = x2gobroker.agent.find_busy_servers(username=username, remote_agent=remote_agent)
+#		print "server load"
+#            except x2gobroker.x2gobroker_exceptions.X2GoBrokerAgentException:
+#                pass
+
             if busy_servers is not None:
 
                 # if we do not get here, we failed to query a valid agent...
@@ -1271,6 +1284,9 @@ class X2GoBroker(object):
 
                 busy_server_list = [ (load, server) for server, load in busy_servers.items() ]
                 busy_server_list.sort(reverse=True)
+		
+		if busy_server_list[0][0] == 1 :
+		    logger_broker.warning('base_broker.X2GoBroker.select_session(): no broker agent could be contacted, this does not look good. We tried these agent hosts: {agent_hosts}'.format(agent_hosts=unicode(server_list)))
 
                 logger_broker.debug('base_broker.X2GoBroker.select_session(): load balancer analysis: {server_load}'.format(server_load=unicode(busy_server_list)))
 
-- 
1.9.1