CP-22472: Add new XenCenter metadata file to the server status report

- this adds the telemetry file to the server status report produced in XenCenter

Signed-off-by: Mihaela Stoica <mihaela.stoica@citrix.com>
This commit is contained in:
Mihaela Stoica 2017-06-16 11:54:49 +01:00 committed by Stephen Turner
parent 8c85e77974
commit 2c37367155
8 changed files with 217 additions and 36 deletions

148
XenAdmin/Core/Telemetry.cs Normal file
View File

@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using System.Web.Script.Serialization;
using XenAdmin.Plugins;
using XenAPI;
namespace XenAdmin.Telemetry
{
internal struct XenCenterMetadata
{
public SystemInfo System;
public XenCenterSettings Settings;
public XenCenterInfrastructure Infrastructure;
public List<Plugin> Plugins;
public string Now;
}
internal struct SystemInfo
{
public string Version;
public string DotNetVersion;
public string Culture;
public string OsVersion;
public string OsCulture;
public string IpAddress;
}
internal struct CFU
{
public bool AllowXenCenterUpdates;
public bool AllowPatchesUpdates;
public bool AllowXenServerUpdates;
}
internal struct Proxy
{
public bool UseProxy;
public bool UseIEProxy;
public bool BypassProxyForServers;
public bool ProxyAuthentication;
public string ProxyAuthenticationMethod;
}
internal struct SaveAndRestore
{
public bool SaveSessionCredentials;
public bool RequireMasterPassword;
}
internal struct XenCenterSettings
{
public CFU CFU;
public Proxy Proxy;
public SaveAndRestore SaveAndRestore;
public string HelpLastUsed;
}
internal struct XenCenterInfrastructure
{
public int TotalConnections;
public int Connected;
}
internal struct Plugin
{
public string Name;
public string Organization;
public bool Enabled;
}
public static class XenCenterTelemetry
{
public static string GenerateMetadata(PluginManager pluginManager)
{
var metadata = new XenCenterMetadata
{
System = new SystemInfo
{
Version = Assembly.GetExecutingAssembly().GetName().Version.ToString(),
DotNetVersion = Environment.Version.ToString(4),
Culture = Thread.CurrentThread.CurrentUICulture.EnglishName,
OsVersion = Environment.OSVersion.ToString(),
OsCulture = CultureInfo.InstalledUICulture.EnglishName,
IpAddress = GetLocalIPAddress()
},
Settings = new XenCenterSettings
{
CFU = new CFU
{
AllowXenCenterUpdates = Properties.Settings.Default.AllowXenCenterUpdates,
AllowPatchesUpdates = Properties.Settings.Default.AllowPatchesUpdates,
AllowXenServerUpdates = Properties.Settings.Default.AllowXenServerUpdates
},
Proxy = new Proxy
{
UseProxy = (HTTPHelper.ProxyStyle) Properties.Settings.Default.ProxySetting == HTTPHelper.ProxyStyle.SpecifiedProxy,
UseIEProxy = (HTTPHelper.ProxyStyle) Properties.Settings.Default.ProxySetting == HTTPHelper.ProxyStyle.SystemProxy,
BypassProxyForServers = Properties.Settings.Default.BypassProxyForServers,
ProxyAuthentication = Properties.Settings.Default.ProvideProxyAuthentication,
ProxyAuthenticationMethod = ((HTTP.ProxyAuthenticationMethod)Properties.Settings.Default.ProxyAuthenticationMethod).ToString()
},
SaveAndRestore = new SaveAndRestore
{
SaveSessionCredentials = Properties.Settings.Default.SaveSession,
RequireMasterPassword = Properties.Settings.Default.RequirePass
},
HelpLastUsed = Properties.Settings.Default.HelpLastUsed
},
Infrastructure = new XenCenterInfrastructure
{
TotalConnections = ConnectionsManager.XenConnectionsCopy.Count,
Connected = ConnectionsManager.XenConnectionsCopy.Count(c => c.IsConnected)
},
Plugins = new List<Plugin>(),
Now = DateTime.UtcNow.ToString("u")
};
if (pluginManager != null)
{
foreach (var plugin in pluginManager.Plugins)
{
metadata.Plugins.Add(new Plugin
{
Name = plugin.Name,
Organization = plugin.Organization,
Enabled = plugin.Enabled
});
}
}
var obj = new Dictionary<string, object> {{Messages.XENCENTER, metadata}};
return new JavaScriptSerializer().Serialize(obj);
}
private static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = host.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork);
return ipAddress != null ? ipAddress.ToString() : null;
}
}
}

View File

@ -219,6 +219,7 @@ namespace XenAdmin
pluginManager.PluginsChanged += pluginManager_PluginsChanged;
pluginManager.LoadPlugins();
contextMenuBuilder = new ContextMenuBuilder(pluginManager, this);
((WinformsXenAdminConfigProvider) XenAdminConfigManager.Provider).PluginManager = pluginManager;
eventsPage.GoToXenObjectRequested += eventsPage_GoToXenObjectRequested;
SearchPage.SearchChanged += SearchPanel_SearchChanged;

View File

@ -39,8 +39,10 @@ using XenAdmin.Actions;
using XenAdmin.Core;
using XenAdmin.Dialogs;
using XenAdmin.Network;
using XenAdmin.Plugins;
using XenAdmin.ServerDBs;
using XenAPI;
using XenAdmin.Telemetry;
namespace XenAdmin
@ -196,5 +198,12 @@ namespace XenAdmin
{
get { return XenAdmin.Properties.Settings.Default.ShowHiddenVMs; }
}
public PluginManager PluginManager;
public string GetXenCenterMetadata()
{
return XenCenterTelemetry.GenerateMetadata(PluginManager);
}
}
}

View File

@ -90,6 +90,7 @@
<Reference Include="System.Management" />
<Reference Include="System.Security" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
@ -220,6 +221,7 @@
</Compile>
<Compile Include="Core\HealthCheck.cs" />
<Compile Include="Core\HiddenFeatures.cs" />
<Compile Include="Core\Telemetry.cs" />
<Compile Include="Diagnostics\Checks\AssertCanEvacuateCheck.cs" />
<Compile Include="Diagnostics\Checks\AssertCanEvacuateUpgradeCheck.cs" />
<Compile Include="Diagnostics\Checks\DiskSpaceForBatchUpdatesCheck.cs" />

View File

@ -127,5 +127,10 @@ namespace XenAdminTests.XenModelTests
{
get { return false; }
}
public string GetXenCenterMetadata()
{
return "";
}
}
}

View File

@ -52,7 +52,7 @@ namespace XenAdmin.Actions
private Object completeActionsMonitor = new Object();
public SystemStatusAction(List<HostWithStatus> hosts,
List<string> entries)
List<string> entries)
: base(null, Messages.ACTION_SYSTEM_STATUS_TITLE, null)
{
this.hosts = hosts;
@ -76,7 +76,7 @@ namespace XenAdmin.Actions
{
if (File.Exists(logDestination))
File.Delete(logDestination);
string logPath = XenAdminConfigManager.Provider.GetLogFile();
string logPath = XenAdminConfigManager.Provider.GetLogFile();
File.Copy(logPath, logDestination);
// Copy old XenCenter.log.* files too
DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(logPath));
@ -116,50 +116,35 @@ namespace XenAdmin.Actions
SingleHostStatusAction action = new SingleHostStatusAction(host, entries, filepath, timestring + "-" + ++i);
actions.Add(action);
action.Changed += (Action<ActionBase>)delegate
{
int total = 0;
foreach (SingleHostStatusAction a in actions)
{
int total = 0;
total += a.PercentComplete;
}
foreach (SingleHostStatusAction a in actions)
{
total += a.PercentComplete;
}
PercentComplete = (int)(total / n);
};
PercentComplete = (int)(total / n);
};
action.Completed += (Action<ActionBase>)delegate
{
lock (completeActionsMonitor)
{
lock (completeActionsMonitor)
{
completedActions--;
Monitor.PulseAll(completeActionsMonitor);
}
};
completedActions--;
Monitor.PulseAll(completeActionsMonitor);
}
};
action.RunAsync();
}
// output the slave/master info while we wait
string mastersDestination = string.Format("{0}\\{1}-Masters.txt", filepath, timestring);
if (File.Exists(mastersDestination))
File.Delete(mastersDestination);
WriteExtraInfoToFile(mastersInfo, mastersDestination);
StreamWriter sw = null;
try
{
sw = new StreamWriter(mastersDestination);
foreach (string s in mastersInfo)
sw.WriteLine(s);
sw.Flush();
}
catch (Exception e)
{
log.ErrorFormat("Exception while writing masters file: {0}", e);
}
finally
{
if (sw != null)
sw.Close();
}
// output the XenCenter metadata
var metadata = XenAdminConfigManager.Provider.GetXenCenterMetadata();
string metadataDestination = string.Format("{0}\\{1}-Telemetry.json", filepath, timestring);
WriteExtraInfoToFile(new List<string> {metadata}, metadataDestination);
// now wait for the status actions to return
lock (completeActionsMonitor)
@ -201,6 +186,31 @@ namespace XenAdmin.Actions
Description = Messages.ACTION_SYSTEM_STATUS_SUCCESSFUL;
}
private void WriteExtraInfoToFile(List<string> info, string fileName)
{
if (File.Exists(fileName))
File.Delete(fileName);
StreamWriter sw = null;
try
{
sw = new StreamWriter(fileName);
foreach (string s in info)
sw.WriteLine(s);
sw.Flush();
}
catch (Exception e)
{
log.ErrorFormat("Exception while writing {0} file: {1}", fileName, e);
}
finally
{
if (sw != null)
sw.Close();
}
}
public override void RecomputeCanCancel()
{
if (this.IsCompleted)

View File

@ -62,5 +62,6 @@ namespace XenAdmin
void UpdateServerHistory(string hostnameWithPort);
void SaveSettingsIfRequired();
bool ShowHiddenVMs { get; }
string GetXenCenterMetadata();
}
}

View File

@ -84,6 +84,11 @@ namespace XenServerHealthCheck
get { return false; }
}
public string GetXenCenterMetadata()
{
return "";
}
public int GetProxyTimeout(bool timeout)
{
return timeout ? Properties.Settings.Default.HttpTimeout : 0;