1
0

Check.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Text;
  29. namespace OpenSim.Framework.Monitoring
  30. {
  31. public class Check
  32. {
  33. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  34. public static readonly char[] DisallowedShortNameCharacters = { '.' };
  35. /// <summary>
  36. /// Category of this stat (e.g. cache, scene, etc).
  37. /// </summary>
  38. public string Category { get; private set; }
  39. /// <summary>
  40. /// Containing name for this stat.
  41. /// FIXME: In the case of a scene, this is currently the scene name (though this leaves
  42. /// us with a to-be-resolved problem of non-unique region names).
  43. /// </summary>
  44. /// <value>
  45. /// The container.
  46. /// </value>
  47. public string Container { get; private set; }
  48. /// <summary>
  49. /// Action used to check whether alert should go off.
  50. /// </summary>
  51. /// <remarks>
  52. /// Should return true if check passes. False otherwise.
  53. /// </remarks>
  54. public Func<Check, bool> CheckFunc { get; private set; }
  55. /// <summary>
  56. /// Message from the last failure, if any. If there is no message or no failure then will be null.
  57. /// </summary>
  58. /// <remarks>
  59. /// Should be set by the CheckFunc when applicable.
  60. /// </remarks>
  61. public string LastFailureMessage { get; set; }
  62. public StatVerbosity Verbosity { get; private set; }
  63. public string ShortName { get; private set; }
  64. public string Name { get; private set; }
  65. public string Description { get; private set; }
  66. public Check(
  67. string shortName,
  68. string name,
  69. string description,
  70. string category,
  71. string container,
  72. Func<Check, bool> checkFunc,
  73. StatVerbosity verbosity)
  74. {
  75. if (ChecksManager.SubCommands.Contains(category))
  76. throw new Exception(
  77. string.Format("Alert cannot be in category '{0}' since this is reserved for a subcommand", category));
  78. foreach (char c in DisallowedShortNameCharacters)
  79. {
  80. if (shortName.IndexOf(c) != -1)
  81. throw new Exception(string.Format("Alert name {0} cannot contain character {1}", shortName, c));
  82. }
  83. ShortName = shortName;
  84. Name = name;
  85. Description = description;
  86. Category = category;
  87. Container = container;
  88. CheckFunc = checkFunc;
  89. Verbosity = verbosity;
  90. }
  91. public bool CheckIt()
  92. {
  93. return CheckFunc(this);
  94. }
  95. public virtual string ToConsoleString()
  96. {
  97. return string.Format(
  98. "{0}.{1}.{2} - {3}",
  99. Category,
  100. Container,
  101. ShortName,
  102. Description);
  103. }
  104. }
  105. }