From 9c1e00eb94e811a708e4afc296329cf17b6a1055 Mon Sep 17 00:00:00 2001
From: Gunnar Beutner <gunnar.beutner@icinga.com>
Date: Tue, 15 May 2018 10:27:13 +0200
Subject: [PATCH] Introduce the 'Environment' variable

---
 doc/17-language-reference.md     | 1 +
 icinga-app/icinga.cpp            | 2 ++
 lib/icinga/icingaapplication.cpp | 3 ++-
 lib/remote/apilistener.cpp       | 8 +++++++-
 4 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/doc/17-language-reference.md b/doc/17-language-reference.md
index 5cd612001..d2a865811 100644
--- a/doc/17-language-reference.md
+++ b/doc/17-language-reference.md
@@ -400,6 +400,7 @@ BuildCompilerVersion|**Read-only.** The version of the compiler Icinga was built
 BuildHostName       |**Read-only.** The name of the host Icinga was built on, e.g. "acheron".
 ApplicationVersion  |**Read-only.** The application version, e.g. "2.9.0".
 MaxConcurrentChecks |**Read-write**. The number of max checks run simultaneously. Defaults to 512.
+Environment         |**Read-write**. The name of the Icinga environment. Included in the SNI host name when making outbound connections. Defaults to "production".
 
 
 Advanced runtime constants. Please only use them if advised by support or developers.
diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp
index c63c1c822..321d7f74f 100644
--- a/icinga-app/icinga.cpp
+++ b/icinga-app/icinga.cpp
@@ -221,6 +221,8 @@ static int Main()
 	Application::DeclareConcurrency(std::thread::hardware_concurrency());
 	Application::DeclareMaxConcurrentChecks(Application::GetDefaultMaxConcurrentChecks());
 
+	ScriptGlobal::Set("Environment", "production");
+
 	ScriptGlobal::Set("AttachDebugger", false);
 
 	ScriptGlobal::Set("PlatformKernel", Utility::GetPlatformKernel());
diff --git a/lib/icinga/icingaapplication.cpp b/lib/icinga/icingaapplication.cpp
index 6817f7f7f..454c78d6d 100644
--- a/lib/icinga/icingaapplication.cpp
+++ b/lib/icinga/icingaapplication.cpp
@@ -81,7 +81,8 @@ void IcingaApplication::StatsFunc(const Dictionary::Ptr& status, const Array::Pt
 			{ "enable_perfdata", icingaapplication->GetEnablePerfdata() },
 			{ "pid", Utility::GetPid() },
 			{ "program_start", Application::GetStartTime() },
-			{ "version", Application::GetAppVersion() }
+			{ "version", Application::GetAppVersion() },
+			{ "environment", ScriptGlobal::Get("Environment", &Empty) }
 		}));
 	}
 
diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp
index c53cd103c..d7e01da67 100644
--- a/lib/remote/apilistener.cpp
+++ b/lib/remote/apilistener.cpp
@@ -382,10 +382,16 @@ void ApiListener::AddConnection(const Endpoint::Ptr& endpoint)
 
 	TcpSocket::Ptr client = new TcpSocket();
 
+	String serverName = endpoint->GetName();
+
+	String env = ScriptGlobal::Get("Environment", &Empty);
+	if (env != "" && env != "production")
+		serverName += ":" + env;
+
 	try {
 		endpoint->SetConnecting(true);
 		client->Connect(host, port);
-		NewClientHandler(client, endpoint->GetName(), RoleClient);
+		NewClientHandler(client, serverName, RoleClient);
 		endpoint->SetConnecting(false);
 	} catch (const std::exception& ex) {
 		endpoint->SetConnecting(false);