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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using XenAPI;
|
|
|
|
|
using XenAdmin.Network;
|
|
|
|
|
using XenAdmin.Core;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace XenAdmin.Alerts
|
|
|
|
|
{
|
|
|
|
|
public class PerfmonOptionsDefinition
|
|
|
|
|
{
|
|
|
|
|
public const String MAIL_DESTINATION_KEY_NAME = "mail-destination";
|
|
|
|
|
public const String SMTP_MAILHUB_KEY_NAME = "ssmtp-mailhub";
|
2017-03-31 13:10:16 +02:00
|
|
|
|
public const String MAIL_LANGUAGE_KEY_NAME = "mail-language";
|
|
|
|
|
|
|
|
|
|
private class MailLanguageList
|
|
|
|
|
{
|
2017-04-13 07:34:30 +02:00
|
|
|
|
private Dictionary<String, String> _list;
|
2017-03-31 13:10:16 +02:00
|
|
|
|
|
2017-04-13 07:34:30 +02:00
|
|
|
|
public MailLanguageList(Dictionary<String, String> initList)
|
2017-03-31 13:10:16 +02:00
|
|
|
|
{
|
2017-04-13 07:34:30 +02:00
|
|
|
|
_list = new Dictionary<String, String>(initList);
|
2017-03-31 13:10:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String CodeFromName(String name)
|
|
|
|
|
{
|
|
|
|
|
String ret = null;
|
|
|
|
|
|
2017-04-17 09:27:50 +02:00
|
|
|
|
if (null == name)
|
|
|
|
|
return ret;
|
|
|
|
|
|
2017-03-31 13:10:16 +02:00
|
|
|
|
foreach(KeyValuePair<String, String> pair in _list)
|
|
|
|
|
{
|
|
|
|
|
if(pair.Value == name)
|
|
|
|
|
{
|
|
|
|
|
ret = pair.Key;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String NameFromCode(String code)
|
|
|
|
|
{
|
|
|
|
|
String ret = null;
|
|
|
|
|
|
2017-04-17 09:27:50 +02:00
|
|
|
|
if (null == code)
|
|
|
|
|
return ret;
|
|
|
|
|
|
2017-03-31 13:10:16 +02:00
|
|
|
|
foreach (KeyValuePair<String, String> pair in _list)
|
|
|
|
|
{
|
|
|
|
|
if (pair.Key == code)
|
|
|
|
|
{
|
|
|
|
|
ret = pair.Value;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 07:34:30 +02:00
|
|
|
|
public bool HasCode(String code)
|
2017-03-31 13:10:16 +02:00
|
|
|
|
{
|
2017-04-24 04:26:59 +02:00
|
|
|
|
return null == code ? false : _list.ContainsKey(code);
|
2017-03-31 13:10:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 07:34:30 +02:00
|
|
|
|
public Dictionary<String, String> dataSource()
|
2017-03-31 13:10:16 +02:00
|
|
|
|
{
|
2017-04-13 07:34:30 +02:00
|
|
|
|
return new Dictionary<String, String>(_list);
|
2017-03-31 13:10:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
|
|
|
|
private static readonly char[] mailHubDelim = new char[] { ':' };
|
|
|
|
|
private readonly String mailHub;
|
|
|
|
|
private readonly String mailDestination;
|
2017-03-31 13:10:16 +02:00
|
|
|
|
private readonly String mailLanguageCode;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2017-04-13 07:34:30 +02:00
|
|
|
|
private static MailLanguageList ml_list = new MailLanguageList(new Dictionary<String, String>() {
|
|
|
|
|
{Messages.MAIL_LANGUAGE_ENGLISH_CODE, Messages.MAIL_LANGUAGE_ENGLISH_NAME},
|
|
|
|
|
{Messages.MAIL_LANGUAGE_CHINESE_CODE, Messages.MAIL_LANGUAGE_CHINESE_NAME},
|
|
|
|
|
{Messages.MAIL_LANGUAGE_JAPANESE_CODE, Messages.MAIL_LANGUAGE_JAPANESE_NAME}
|
2017-03-31 13:10:16 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
public PerfmonOptionsDefinition(String mailHub, String mailDestination, String mailLanguageCode)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
{
|
|
|
|
|
this.mailHub = mailHub;
|
|
|
|
|
this.mailDestination = mailDestination;
|
2017-03-31 13:10:16 +02:00
|
|
|
|
this.mailLanguageCode = mailLanguageCode;
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String MailHub
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return mailHub;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String MailDestination
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return mailDestination;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 13:10:16 +02:00
|
|
|
|
public String MailLanguageCode
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return mailLanguageCode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String MailLanguageName
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ml_list.NameFromCode(mailLanguageCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-24 13:41:48 +02:00
|
|
|
|
public static String GetSmtpServerAddress(string mailHub)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] words = mailHub.Split(mailHubDelim);
|
|
|
|
|
if (words.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
return words[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String GetSmtpPort(string mailHub)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] words = mailHub.Split(mailHubDelim);
|
|
|
|
|
if (words.Length > 1)
|
|
|
|
|
{
|
|
|
|
|
return words[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
return "25";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return String.Format("mail-destination: {0} ssmtp-mailhub: {1}", MailDestination, MailHub);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static PerfmonOptionsDefinition GetPerfmonOptionsDefinitions(IXenObject xmo)
|
|
|
|
|
{
|
|
|
|
|
if (xmo == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
IXenConnection connection = xmo.Connection;
|
|
|
|
|
if (connection == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
string mailDestination = GetMailDestination(connection);
|
|
|
|
|
string mailHub = GetSmtpMailHub(connection);
|
2017-03-31 13:10:16 +02:00
|
|
|
|
string mailLanguageCode = GetMailLanguageCode(connection);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
|
2017-04-13 07:34:30 +02:00
|
|
|
|
if (mailDestination == null || mailHub == null)
|
2013-06-24 13:41:48 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
2017-03-31 13:10:16 +02:00
|
|
|
|
return new PerfmonOptionsDefinition(mailHub, mailDestination, mailLanguageCode);
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetMailDestination(IXenConnection connection)
|
|
|
|
|
{
|
|
|
|
|
Pool pool = Helpers.GetPoolOfOne(connection);
|
|
|
|
|
if (pool == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Dictionary<String, String> other_config = Helpers.GetOtherConfig(pool);
|
|
|
|
|
if (other_config == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!other_config.ContainsKey(MAIL_DESTINATION_KEY_NAME))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
String mailAddress = other_config[MAIL_DESTINATION_KEY_NAME];
|
|
|
|
|
if (mailAddress == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
mailAddress.Trim();
|
|
|
|
|
if (String.IsNullOrEmpty(mailAddress))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return mailAddress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetSmtpMailHub(IXenConnection connection)
|
|
|
|
|
{
|
|
|
|
|
Pool pool = Helpers.GetPoolOfOne(connection);
|
|
|
|
|
if (pool == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Dictionary<String, String> other_config = Helpers.GetOtherConfig(pool);
|
|
|
|
|
if (other_config == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!other_config.ContainsKey(SMTP_MAILHUB_KEY_NAME))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
String mailHub = other_config[SMTP_MAILHUB_KEY_NAME];
|
|
|
|
|
if (mailHub == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
mailHub.Trim();
|
|
|
|
|
if (String.IsNullOrEmpty(mailHub))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return mailHub;
|
|
|
|
|
}
|
2017-03-31 13:10:16 +02:00
|
|
|
|
|
|
|
|
|
public static string GetMailLanguageCode(IXenConnection connection)
|
|
|
|
|
{
|
|
|
|
|
Pool pool = Helpers.GetPoolOfOne(connection);
|
|
|
|
|
if (pool == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Dictionary<String, String> other_config = Helpers.GetOtherConfig(pool);
|
|
|
|
|
if (other_config == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!other_config.ContainsKey(MAIL_LANGUAGE_KEY_NAME))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
String mailLanguageCode = other_config[MAIL_LANGUAGE_KEY_NAME];
|
|
|
|
|
if (mailLanguageCode == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
mailLanguageCode.Trim();
|
|
|
|
|
if (String.IsNullOrEmpty(mailLanguageCode))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return mailLanguageCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String MailLanguageNameFromCode(String code)
|
|
|
|
|
{
|
|
|
|
|
return ml_list.NameFromCode(code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String MailLanguageCodeFromName(String name)
|
|
|
|
|
{
|
|
|
|
|
return ml_list.CodeFromName(name);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-13 07:34:30 +02:00
|
|
|
|
public static bool MailLanguageHasCode(String code)
|
2017-03-31 13:10:16 +02:00
|
|
|
|
{
|
2017-04-13 07:34:30 +02:00
|
|
|
|
return ml_list.HasCode(code);
|
2017-03-31 13:10:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object MailLanguageDataSource()
|
|
|
|
|
{
|
|
|
|
|
return PerfmonOptionsDefinition.ml_list.dataSource();
|
|
|
|
|
}
|
2013-06-24 13:41:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|