2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2015-05-22 11:29:53 +02:00
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms,
|
|
|
|
|
* with or without modification, are permitted provided
|
|
|
|
|
* that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* * Redistributions of source code must retain the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer.
|
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ServiceProcess;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
2015-06-26 07:36:30 +02:00
|
|
|
|
using System.Threading;
|
2015-07-01 08:29:27 +02:00
|
|
|
|
using XenAdmin;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
|
|
|
|
namespace XenServerHealthCheck
|
|
|
|
|
{
|
|
|
|
|
public partial class XenServerHealthCheckService : ServiceBase
|
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
2015-07-02 08:47:57 +02:00
|
|
|
|
private CancellationTokenSource cts = new CancellationTokenSource();
|
2015-05-22 11:29:53 +02:00
|
|
|
|
|
|
|
|
|
public XenServerHealthCheckService()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2021-02-26 02:26:53 +01:00
|
|
|
|
ServiceName = XenAdmin.Core.BrandManager.ProductBrand + " Health Check";
|
2015-05-22 11:29:53 +02:00
|
|
|
|
AutoLog = false;
|
2015-12-14 14:11:35 +01:00
|
|
|
|
|
|
|
|
|
XenAPI.Session.UserAgent = string.Format("XenServerHealthCheck/API-{0}", API_Version.LATEST);
|
|
|
|
|
|
2015-05-22 11:29:53 +02:00
|
|
|
|
if (!System.Diagnostics.EventLog.SourceExists("XenServerHealthCheck"))
|
|
|
|
|
{
|
|
|
|
|
System.Diagnostics.EventLog.CreateEventSource(
|
|
|
|
|
"XenServerHealthCheck", "XenServerHealthCheckLog");
|
|
|
|
|
}
|
2015-07-01 08:29:27 +02:00
|
|
|
|
|
2015-07-16 12:54:37 +02:00
|
|
|
|
XenAdminConfigManager.Provider = new XenServerHealthCheckConfigProvider();
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
private static void initConfig()
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
2015-07-14 08:49:10 +02:00
|
|
|
|
if (string.IsNullOrEmpty(Properties.Settings.Default.UUID))
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
2015-06-26 07:36:30 +02:00
|
|
|
|
Properties.Settings.Default.UUID = System.Guid.NewGuid().ToString();
|
|
|
|
|
Properties.Settings.Default.Save();
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
2015-07-14 08:49:10 +02:00
|
|
|
|
|
2016-06-22 14:05:21 +02:00
|
|
|
|
ReconfigureConnectionSettings();
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
log.InfoFormat("XenServer Health Check Service {0} starting...", Properties.Settings.Default.UUID);
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-22 14:05:21 +02:00
|
|
|
|
public static void ReconfigureConnectionSettings()
|
|
|
|
|
{
|
|
|
|
|
XenAPI.Session.Proxy = XenAdminConfigManager.Provider.GetProxyFromSettings(null);
|
2017-01-20 15:47:04 +01:00
|
|
|
|
XenAPI.HTTP.CurrentProxyAuthenticationMethod =
|
|
|
|
|
(HTTP.ProxyAuthenticationMethod)Properties.Settings.Default.ProxyAuthenticationMethod;
|
2016-06-22 14:05:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 11:29:53 +02:00
|
|
|
|
protected override void OnStart(string[] args)
|
|
|
|
|
{
|
|
|
|
|
// Set up a timer to trigger the uploading service.
|
2015-06-26 07:36:30 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
initConfig();
|
|
|
|
|
CredentialReceiver.instance.Init();
|
|
|
|
|
ServerListHelper.instance.Init();
|
|
|
|
|
System.Timers.Timer timer = new System.Timers.Timer();
|
2015-08-07 06:01:02 +02:00
|
|
|
|
timer.Interval = Registry.HealthCheckTimeInterval * 60000;
|
2015-08-10 05:57:31 +02:00
|
|
|
|
log.InfoFormat("XenServer Health Check Service will be scheduled every {0} milliseconds", timer.Interval);
|
2015-06-26 07:36:30 +02:00
|
|
|
|
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
|
|
|
|
|
timer.Start();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exp)
|
|
|
|
|
{
|
2015-07-01 12:19:44 +02:00
|
|
|
|
EventLog.WriteEntry(exp.Message, System.Diagnostics.EventLogEntryType.FailureAudit);
|
2015-06-26 07:36:30 +02:00
|
|
|
|
Stop();
|
|
|
|
|
}
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnStop()
|
|
|
|
|
{
|
2015-06-04 08:10:24 +02:00
|
|
|
|
log.Info("XenServer Health Check Service stopping...");
|
2015-07-01 08:29:27 +02:00
|
|
|
|
CredentialReceiver.instance.UnInit();
|
2015-07-02 08:47:57 +02:00
|
|
|
|
cts.Cancel();
|
2015-07-01 08:29:27 +02:00
|
|
|
|
bool canStop;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
canStop = true;
|
|
|
|
|
List<ServerInfo> servers = ServerListHelper.instance.GetServerList();
|
|
|
|
|
foreach (ServerInfo server in servers)
|
|
|
|
|
{
|
2015-07-14 07:59:35 +02:00
|
|
|
|
if (server.task != null && !(server.task.IsCompleted || server.task.IsCanceled || server.task.IsFaulted))
|
2015-07-01 08:29:27 +02:00
|
|
|
|
canStop = false;
|
|
|
|
|
}
|
|
|
|
|
if(canStop == false)
|
|
|
|
|
Thread.Sleep(1000);
|
2015-07-14 07:59:35 +02:00
|
|
|
|
} while (canStop == false);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
|
|
|
|
|
{
|
2015-06-04 08:10:24 +02:00
|
|
|
|
log.Info("XenServer Health Check Service start to refresh uploading tasks");
|
2015-06-04 10:00:45 +02:00
|
|
|
|
|
|
|
|
|
//We need to check if CIS can be accessed in current enviroment
|
2015-07-01 08:29:27 +02:00
|
|
|
|
|
|
|
|
|
List<ServerInfo> servers = ServerListHelper.instance.GetServerList();
|
|
|
|
|
foreach (ServerInfo server in servers)
|
2015-05-22 11:29:53 +02:00
|
|
|
|
{
|
2015-07-02 08:47:57 +02:00
|
|
|
|
if (server.task != null && (!server.task.IsCompleted || !server.task.IsCanceled || !server.task.IsFaulted))
|
2015-07-01 08:29:27 +02:00
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 05:26:30 +01:00
|
|
|
|
bool needReconnect = false;
|
|
|
|
|
|
|
|
|
|
log.InfoFormat("Check server {0} with user {1}", server.HostName, server.UserName);
|
|
|
|
|
|
2015-07-01 08:29:27 +02:00
|
|
|
|
Session session = new Session(server.HostName, 80);
|
2015-06-10 12:57:42 +02:00
|
|
|
|
session.APIVersion = API_Version.LATEST;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-12-14 14:11:35 +01:00
|
|
|
|
session.login_with_password(server.UserName, server.Password, Helper.APIVersionString(API_Version.LATEST), Session.UserAgent);
|
2015-11-03 05:26:30 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
if (exn is Failure && ((Failure)exn).ErrorDescription[0] == Failure.HOST_IS_SLAVE)
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
string coordinatorName = ((Failure)exn).ErrorDescription[1];
|
|
|
|
|
if (ServerListHelper.instance.UpdateServerCredential(server, coordinatorName))
|
2015-11-03 05:26:30 +01:00
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
log.InfoFormat("Refresh credential to master {0} need refresh connection", coordinatorName);
|
|
|
|
|
server.HostName = coordinatorName;
|
2015-11-03 05:26:30 +01:00
|
|
|
|
needReconnect = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-08-31 12:31:16 +02:00
|
|
|
|
log.InfoFormat("Remove credential since it is the supporter of coordinator {0}", coordinatorName);
|
2015-11-03 05:26:30 +01:00
|
|
|
|
if (session != null)
|
|
|
|
|
session.logout();
|
|
|
|
|
log.Error(exn, exn);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (session != null)
|
|
|
|
|
session.logout();
|
|
|
|
|
log.Error(exn, exn);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (needReconnect)
|
|
|
|
|
{
|
|
|
|
|
if (session != null)
|
|
|
|
|
session.logout();
|
2021-08-31 12:31:16 +02:00
|
|
|
|
log.InfoFormat("Reconnect to coordinator {0}", server.HostName);
|
2015-11-03 05:26:30 +01:00
|
|
|
|
session = new Session(server.HostName, 80);
|
|
|
|
|
session.APIVersion = API_Version.LATEST;
|
2015-12-14 14:11:35 +01:00
|
|
|
|
session.login_with_password(server.UserName, server.Password, Helper.APIVersionString(API_Version.LATEST), Session.UserAgent);
|
2015-11-03 05:26:30 +01:00
|
|
|
|
}
|
|
|
|
|
XenConnection connectionInfo = new XenConnection();
|
|
|
|
|
connectionInfo.Hostname = server.HostName;
|
|
|
|
|
connectionInfo.Username = server.UserName;
|
|
|
|
|
connectionInfo.Password = server.Password;
|
2015-07-01 08:29:27 +02:00
|
|
|
|
connectionInfo.LoadCache(session);
|
|
|
|
|
if (RequestUploadTask.Request(connectionInfo, session) || RequestUploadTask.OnDemandRequest(connectionInfo, session))
|
2015-07-01 16:58:58 +02:00
|
|
|
|
{
|
2015-06-11 14:31:29 +02:00
|
|
|
|
// Create a task to collect server status report and upload to CIS server
|
|
|
|
|
log.InfoFormat("Start to upload server status report for XenServer {0}", connectionInfo.Hostname);
|
|
|
|
|
|
|
|
|
|
XenServerHealthCheckBundleUpload upload = new XenServerHealthCheckBundleUpload(connectionInfo);
|
|
|
|
|
Action uploadAction = delegate()
|
|
|
|
|
{
|
2015-07-02 08:47:57 +02:00
|
|
|
|
upload.runUpload(cts.Token);
|
2015-06-11 14:31:29 +02:00
|
|
|
|
};
|
|
|
|
|
System.Threading.Tasks.Task task = new System.Threading.Tasks.Task(uploadAction);
|
2015-07-01 16:58:58 +02:00
|
|
|
|
task.Start();
|
2015-07-02 08:47:57 +02:00
|
|
|
|
|
|
|
|
|
server.task = task;
|
|
|
|
|
ServerListHelper.instance.UpdateServerInfo(server);
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
2015-06-10 12:57:42 +02:00
|
|
|
|
session.logout();
|
|
|
|
|
session = null;
|
2015-05-22 11:29:53 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
2015-06-10 12:57:42 +02:00
|
|
|
|
if (session != null)
|
|
|
|
|
session.logout();
|
2015-05-22 11:29:53 +02:00
|
|
|
|
log.Error(exn, exn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|