2017-01-16 20:59:50 +01:00
|
|
|
|
/* Copyright (c) Citrix Systems, Inc.
|
2013-06-24 13:41:48 +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;
|
2017-01-10 07:18:18 +01:00
|
|
|
|
using System.Text.RegularExpressions;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
using System.Text;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Alerts
|
|
|
|
|
{
|
|
|
|
|
public class PolicyAlert : IEquatable<PolicyAlert>
|
|
|
|
|
{
|
|
|
|
|
public readonly string Type;
|
|
|
|
|
public readonly string Text;
|
|
|
|
|
public readonly DateTime Time;
|
2017-01-10 07:18:18 +01:00
|
|
|
|
public readonly int numberOfVMsFailed;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2017-09-07 12:02:07 +02:00
|
|
|
|
public const int INFO_PRIORITY = 4;
|
|
|
|
|
|
2017-09-05 00:07:22 +02:00
|
|
|
|
public PolicyAlert(long priority, string name, DateTime time, string body, string policyName)
|
2016-02-15 05:35:26 +01:00
|
|
|
|
{
|
2017-09-07 12:02:07 +02:00
|
|
|
|
Type = priority == INFO_PRIORITY ? "info" : "error";
|
2017-09-05 00:07:22 +02:00
|
|
|
|
Time = time;
|
2017-01-10 07:18:18 +01:00
|
|
|
|
|
|
|
|
|
if(Type == "info")
|
|
|
|
|
{
|
|
|
|
|
Text = Message.FriendlyBody(name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* We reach here when message type is an error hence we need
|
|
|
|
|
* to parse the message body accordingly */
|
|
|
|
|
|
|
|
|
|
numberOfVMsFailed = Regex.Matches(body, "VM:").Count;
|
|
|
|
|
if(numberOfVMsFailed == 0)
|
|
|
|
|
{
|
|
|
|
|
Text = Message.FriendlyBody(name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2017-02-08 09:30:47 +01:00
|
|
|
|
sb.AppendFormat(Message.FriendlyBody(name), policyName);
|
2017-01-10 07:18:18 +01:00
|
|
|
|
string[] messageHeader = Regex.Split(body.Replace("\n", " "), "Details:");
|
|
|
|
|
|
|
|
|
|
/* get a list of all VMs that have encountered an error */
|
|
|
|
|
string[] vmList = Regex.Split(messageHeader[1], "VM:");
|
|
|
|
|
|
|
|
|
|
if(vmList.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
sb.AppendLine();
|
|
|
|
|
sb.Append(Messages.VMSS_ALERT_DETAILS);
|
|
|
|
|
}
|
|
|
|
|
/* for each VM entry parse the data and get VM name and corresponding error message */
|
2017-09-05 00:07:22 +02:00
|
|
|
|
for (int i = 1; i < vmList.Length; i++)
|
2017-01-10 07:18:18 +01:00
|
|
|
|
{
|
2017-09-05 00:07:22 +02:00
|
|
|
|
string[] tmp = Regex.Split(vmList[i].Trim(), "Error:");
|
2017-01-10 07:18:18 +01:00
|
|
|
|
string vmName = Regex.Split(tmp[0], "UUID:")[0];
|
|
|
|
|
|
|
|
|
|
string[] errorCode = Regex.Split(tmp[1].Replace("[", "").Replace("],", "").Replace("]", ""), "\',");
|
|
|
|
|
for (int errorCodeIterator = 0; errorCodeIterator < errorCode.Length; errorCodeIterator++)
|
|
|
|
|
errorCode[errorCodeIterator] = errorCode[errorCodeIterator].Replace("\'", "").Trim();
|
|
|
|
|
|
|
|
|
|
string errorMessage = new Failure(errorCode).Message;
|
|
|
|
|
sb.AppendLine();
|
2017-09-05 00:07:22 +02:00
|
|
|
|
sb.AppendFormat(Messages.VMSS_ALERT_VM_ERROR_FORMAT, i, vmName.Trim(), errorMessage);
|
2017-01-10 07:18:18 +01:00
|
|
|
|
}
|
|
|
|
|
Text = sb.ToString();
|
2016-02-15 05:35:26 +01:00
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2017-05-30 16:08:01 +02:00
|
|
|
|
public string ShortFormatBody
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-05-30 16:08:01 +02:00
|
|
|
|
get
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-05-30 16:08:01 +02:00
|
|
|
|
if (Type == "error")
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-05-30 16:08:01 +02:00
|
|
|
|
if (numberOfVMsFailed == 0)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-05-30 16:08:01 +02:00
|
|
|
|
return Message.FriendlyName(XenAPI.Message.MessageType.VMSS_SNAPSHOT_FAILED.ToString());
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
2017-05-30 16:08:01 +02:00
|
|
|
|
else
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
2017-05-30 16:08:01 +02:00
|
|
|
|
return string.Format(Messages.VM_SNAPSHOT_SCHEDULE_FAILED,
|
|
|
|
|
Message.FriendlyName(XenAPI.Message.MessageType.VMSS_SNAPSHOT_FAILED.ToString()),
|
|
|
|
|
numberOfVMsFailed);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-30 16:08:01 +02:00
|
|
|
|
return Text;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IEquatable<PolicyAlert> Members
|
|
|
|
|
|
|
|
|
|
public bool Equals(PolicyAlert other)
|
|
|
|
|
{
|
|
|
|
|
if (Text != other.Text)
|
|
|
|
|
return false;
|
|
|
|
|
if (Type != other.Type)
|
|
|
|
|
return false;
|
|
|
|
|
if (Time != other.Time)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|