2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2015-06-04 10:00:45 +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 XenAdmin.Network;
|
|
|
|
|
using XenAPI;
|
2015-08-21 16:18:18 +02:00
|
|
|
|
using XenAdmin.Model;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
|
|
|
|
|
namespace XenServerHealthCheck
|
|
|
|
|
{
|
|
|
|
|
public class RequestUploadTask
|
|
|
|
|
{
|
|
|
|
|
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
|
|
|
|
private static T Get<T>(Dictionary<string, T> d, string k) where T : class
|
|
|
|
|
{
|
|
|
|
|
return d != null && d.ContainsKey(k) ? d[k] : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int IntKey(Dictionary<string, string> d, string k, int def)
|
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
string s = Get(d, k);
|
|
|
|
|
return s != null && int.TryParse(s, out result) ? result : def;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool BoolKey(Dictionary<string, string> d, string k)
|
|
|
|
|
{
|
|
|
|
|
string v = Get(d, k);
|
|
|
|
|
return v == null ? false : v == "true";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static double DueAfterHour = 24;
|
2015-07-02 07:11:54 +02:00
|
|
|
|
private static bool CanLock(string UploadLock, bool onDemand)
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
2015-07-06 18:10:00 +02:00
|
|
|
|
if (string.IsNullOrEmpty(UploadLock))
|
2015-06-10 11:32:52 +02:00
|
|
|
|
return true;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
|
|
|
|
|
List<string> currentLock = new List<string>(UploadLock.Split('|'));
|
|
|
|
|
DateTime currentTime = DateTime.UtcNow;
|
|
|
|
|
DateTime lockTime = DateTime.Parse(currentLock[1]);
|
|
|
|
|
|
2015-07-02 07:11:54 +02:00
|
|
|
|
if (currentLock[0] == Properties.Settings.Default.UUID && onDemand)
|
|
|
|
|
return true;
|
|
|
|
|
|
2015-06-26 07:36:30 +02:00
|
|
|
|
if (currentLock[0] == Properties.Settings.Default.UUID)
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
|
|
|
|
if ((DateTime.Compare(lockTime.AddHours(DueAfterHour), currentTime) <= 0))
|
2015-06-10 11:32:52 +02:00
|
|
|
|
return true;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
else if ((DateTime.Compare(lockTime.AddHours(DueAfterHour), currentTime) <= 0))
|
2015-06-10 11:32:52 +02:00
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int SleepForLockConfirm = 10 * 1000; // 10 seconds
|
2015-06-10 12:57:42 +02:00
|
|
|
|
private static bool getLock(IXenConnection connection, Session session)
|
|
|
|
|
{
|
2015-07-02 07:11:54 +02:00
|
|
|
|
Dictionary<string, string> config = Pool.get_health_check_config(session, connection.Cache.Pools[0].opaque_ref);
|
2015-06-26 07:36:30 +02:00
|
|
|
|
string newUploadLock = Properties.Settings.Default.UUID;
|
2015-07-29 09:44:41 +02:00
|
|
|
|
newUploadLock += "|" + HealthCheckSettings.DateTimeToString(DateTime.UtcNow);
|
|
|
|
|
config[HealthCheckSettings.UPLOAD_LOCK] = newUploadLock;
|
2015-06-18 09:32:56 +02:00
|
|
|
|
Pool.set_health_check_config(session, connection.Cache.Pools[0].opaque_ref, config);
|
2015-06-10 12:57:42 +02:00
|
|
|
|
System.Threading.Thread.Sleep(SleepForLockConfirm);
|
2015-06-18 09:32:56 +02:00
|
|
|
|
config = Pool.get_health_check_config(session, connection.Cache.Pools[0].opaque_ref);
|
2015-07-29 09:44:41 +02:00
|
|
|
|
return config[HealthCheckSettings.UPLOAD_LOCK] == newUploadLock;
|
2015-06-10 12:57:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool Request(IXenConnection connection, Session session)
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
|
|
|
|
bool needRetry = false;
|
2015-06-18 09:32:56 +02:00
|
|
|
|
Dictionary<string, string> config = Pool.get_health_check_config(session, connection.Cache.Pools[0].opaque_ref);
|
2015-07-29 09:44:41 +02:00
|
|
|
|
if (BoolKey(config, HealthCheckSettings.STATUS) == false)
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
2015-07-01 08:29:27 +02:00
|
|
|
|
ServerListHelper.instance.removeServerCredential(connection.Hostname);
|
2015-06-04 10:00:45 +02:00
|
|
|
|
log.InfoFormat("Will not report for XenServer {0} that was not Enroll", connection.Hostname);
|
2015-06-10 11:32:52 +02:00
|
|
|
|
return false;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
//Check if there already some service doing the uploading already
|
2015-07-29 09:44:41 +02:00
|
|
|
|
if (CanLock(Get(config, HealthCheckSettings.UPLOAD_LOCK), false) == false)
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
|
|
|
|
log.InfoFormat("Will not report for XenServer {0} that already locked", connection.Hostname);
|
2015-06-10 11:32:52 +02:00
|
|
|
|
return false;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//No Lock has been set before, Check upload is due
|
2015-08-21 16:21:47 +02:00
|
|
|
|
int intervalInDays = IntKey(config, HealthCheckSettings.INTERVAL_IN_DAYS, HealthCheckSettings.DEFAULT_INTERVAL_IN_DAYS);
|
2015-06-10 11:32:52 +02:00
|
|
|
|
DateTime lastSuccessfulUpload = DateTime.UtcNow;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
bool haveSuccessfulUpload = false;
|
2015-07-29 09:44:41 +02:00
|
|
|
|
if (config.ContainsKey(HealthCheckSettings.LAST_SUCCESSFUL_UPLOAD))
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-02 11:54:00 +02:00
|
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
|
lastSuccessfulUpload = HealthCheckSettings.StringToDateTime(Get(config, HealthCheckSettings.LAST_SUCCESSFUL_UPLOAD));
|
2015-06-04 10:00:45 +02:00
|
|
|
|
haveSuccessfulUpload = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Catch exception when Parse LAST_SUCCESSFUL_UPLOAD", exn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (haveSuccessfulUpload)
|
|
|
|
|
{//If last successful update not due. return
|
2015-06-10 11:32:52 +02:00
|
|
|
|
if (DateTime.Compare(DateTime.UtcNow, lastSuccessfulUpload.AddDays(intervalInDays)) < 0)
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
2015-06-10 11:32:52 +02:00
|
|
|
|
log.InfoFormat("Will not report for XenServer {0} that was not due {1} days", connection.Hostname, intervalInDays);
|
|
|
|
|
return false;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
|
if (config.ContainsKey(HealthCheckSettings.LAST_FAILED_UPLOAD))
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-29 09:44:41 +02:00
|
|
|
|
DateTime LastFailedUpload = HealthCheckSettings.StringToDateTime(Get(config, HealthCheckSettings.LAST_FAILED_UPLOAD));
|
2015-06-04 10:00:45 +02:00
|
|
|
|
|
2015-08-21 16:21:47 +02:00
|
|
|
|
int retryInterval = IntKey(config, HealthCheckSettings.RETRY_INTERVAL, HealthCheckSettings.DEFAULT_RETRY_INTERVAL);
|
2015-06-04 10:00:45 +02:00
|
|
|
|
if (DateTime.Compare(LastFailedUpload.AddDays(retryInterval), DateTime.UtcNow) <= 0)
|
|
|
|
|
{
|
2018-04-20 11:28:08 +02:00
|
|
|
|
if ((!haveSuccessfulUpload) ||
|
|
|
|
|
(DateTime.Compare(lastSuccessfulUpload, LastFailedUpload) < 0))
|
|
|
|
|
{
|
2019-11-08 21:56:04 +01:00
|
|
|
|
log.InfoFormat("Retry since retryInterval{0} - {1} > {2} met", LastFailedUpload, DateTime.UtcNow, retryInterval);
|
2018-04-20 11:28:08 +02:00
|
|
|
|
needRetry = true;
|
|
|
|
|
}
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Catch exception when check if retry was needed", exn);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-14 03:48:14 +02:00
|
|
|
|
DateTime currentTime = DateTime.Now;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
if (!needRetry)
|
|
|
|
|
{//Check if uploading schedule meet only for new upload
|
|
|
|
|
|
2015-06-10 11:32:52 +02:00
|
|
|
|
DayOfWeek dayOfWeek;
|
2015-07-29 09:44:41 +02:00
|
|
|
|
if (!Enum.TryParse(Get(config, HealthCheckSettings.DAY_OF_WEEK), out dayOfWeek))
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
|
|
|
|
log.Error("DAY_OF_WEEK not existed");
|
2015-06-10 11:32:52 +02:00
|
|
|
|
return false;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
2015-07-29 09:44:41 +02:00
|
|
|
|
if (!config.ContainsKey(HealthCheckSettings.TIME_OF_DAY))
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
|
|
|
|
log.Error("TIME_OF_DAY not existed");
|
2015-06-10 11:32:52 +02:00
|
|
|
|
return false;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
|
int TimeOfDay = IntKey(config, HealthCheckSettings.TIME_OF_DAY, HealthCheckSettings.GetDefaultTime());
|
2015-09-10 09:29:58 +02:00
|
|
|
|
if (currentTime.DayOfWeek != dayOfWeek || currentTime.Hour != TimeOfDay)
|
2015-06-04 10:00:45 +02:00
|
|
|
|
{
|
|
|
|
|
log.InfoFormat("Will not report for XenServer {0} for incorrect schedule", connection.Hostname);
|
2015-06-10 11:32:52 +02:00
|
|
|
|
return false;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
2015-09-10 09:29:58 +02:00
|
|
|
|
log.InfoFormat("Upload schedule for {0} is {1}:{2}, meet current time {3}", connection.Hostname, dayOfWeek, TimeOfDay, currentTime.ToString());
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
2015-06-10 12:57:42 +02:00
|
|
|
|
return getLock(connection, session);
|
|
|
|
|
}
|
2015-06-10 11:32:52 +02:00
|
|
|
|
|
2015-06-10 12:57:42 +02:00
|
|
|
|
public static bool OnDemandRequest(IXenConnection connection, Session session)
|
|
|
|
|
{
|
2015-06-18 09:32:56 +02:00
|
|
|
|
Dictionary<string, string> config = Pool.get_health_check_config(session, connection.Cache.Pools[0].opaque_ref);
|
2015-07-29 09:44:41 +02:00
|
|
|
|
if (BoolKey(config, HealthCheckSettings.STATUS) == false)
|
2015-06-10 12:57:42 +02:00
|
|
|
|
{
|
|
|
|
|
log.InfoFormat("Will not report on demand for XenServer {0} that was not Enroll", connection.Hostname);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Check if there already some service doing the uploading already
|
2015-07-29 09:44:41 +02:00
|
|
|
|
if (CanLock(Get(config, HealthCheckSettings.UPLOAD_LOCK), true) == false)
|
2015-06-10 12:57:42 +02:00
|
|
|
|
{
|
|
|
|
|
log.InfoFormat("Will not report for XenServer {0} that already locked", connection.Hostname);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 09:44:41 +02:00
|
|
|
|
var newUploadRequest = Get(config, HealthCheckSettings.NEW_UPLOAD_REQUEST);
|
2015-07-06 18:10:00 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(newUploadRequest))
|
2015-06-10 12:57:42 +02:00
|
|
|
|
{
|
2015-07-06 18:10:00 +02:00
|
|
|
|
DateTime newUploadRequestTime;
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-29 09:44:41 +02:00
|
|
|
|
newUploadRequestTime = HealthCheckSettings.StringToDateTime(newUploadRequest);
|
2015-07-06 18:10:00 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exn)
|
|
|
|
|
{
|
|
|
|
|
log.Error("Exception while parsing NEW_UPLOAD_REQUEST", exn);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-08-21 16:21:47 +02:00
|
|
|
|
DateTime newUploadRequestDueTime = newUploadRequestTime.AddMinutes(HealthCheckSettings.UPLOAD_REQUEST_VALIDITY_INTERVAL);
|
2015-07-06 18:10:00 +02:00
|
|
|
|
if (DateTime.Compare(newUploadRequestDueTime, DateTime.UtcNow) >= 1)
|
2015-06-10 12:57:42 +02:00
|
|
|
|
{
|
2015-07-06 18:10:00 +02:00
|
|
|
|
log.InfoFormat("Will report on demand for XenServer {0} since the demand was requested on {1} (UTC time)", connection.Hostname, newUploadRequestTime);
|
2015-06-10 12:57:42 +02:00
|
|
|
|
return getLock(connection, session);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-08-21 16:21:47 +02:00
|
|
|
|
log.InfoFormat("Will not report on demand for XenServer {0} since the demand requested on {1} (UTC time) expired after {2} minutes",
|
|
|
|
|
connection.Hostname, newUploadRequestTime, HealthCheckSettings.UPLOAD_REQUEST_VALIDITY_INTERVAL);
|
2015-06-10 12:57:42 +02:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2015-06-04 10:00:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|