29 using System.Collections.Generic;
31 using System.Reflection;
35 namespace OpenSim.Framework.Monitoring
40 public static class ChecksManager
42 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
45 public const string ListSubCommand =
"list";
48 public static HashSet<string> SubCommands =
new HashSet<string> { ListSubCommand };
56 public static SortedDictionary<string, SortedDictionary<string, SortedDictionary<string, Check>>> RegisteredChecks
57 =
new SortedDictionary<string, SortedDictionary<string, SortedDictionary<string, Check>>>();
59 public static void RegisterConsoleCommands(ICommandConsole console)
61 console.Commands.AddCommand(
66 "Show checks configured for this server",
67 "If no argument is specified then info on all checks will be shown.\n"
68 +
"'list' argument will show check categories.\n"
69 +
"THIS FACILITY IS EXPERIMENTAL",
70 HandleShowchecksCommand);
73 public static void HandleShowchecksCommand(
string module,
string[] cmd)
75 ICommandConsole con = MainConsole.Instance;
79 foreach (
string name
in cmd.Skip(2))
81 string[] components = name.Split(
'.');
83 string categoryName = components[0];
86 if (categoryName == ListSubCommand)
88 con.Output(
"check categories available are:");
90 foreach (
string category
in RegisteredChecks.Keys)
91 con.OutputFormat(
" {0}", category);
124 OutputAllChecksToConsole(con);
133 public static bool RegisterCheck(Check check)
135 SortedDictionary<string, SortedDictionary<string, Check>> category = null, newCategory;
136 SortedDictionary<string, Check> container = null, newContainer;
138 lock (RegisteredChecks)
143 if (TryGetCheckParents(check, out category, out container))
149 if (container != null)
150 newContainer =
new SortedDictionary<string, Check>(container);
152 newContainer =
new SortedDictionary<string, Check>();
154 if (category != null)
155 newCategory =
new SortedDictionary<string, SortedDictionary<string, Check>>(category);
157 newCategory =
new SortedDictionary<string, SortedDictionary<string, Check>>();
159 newContainer[check.ShortName] = check;
160 newCategory[check.Container] = newContainer;
161 RegisteredChecks[check.Category] = newCategory;
172 public static bool DeregisterCheck(Check check)
174 SortedDictionary<string, SortedDictionary<string, Check>> category = null, newCategory;
175 SortedDictionary<string, Check> container = null, newContainer;
177 lock (RegisteredChecks)
179 if (!TryGetCheckParents(check, out category, out container))
182 newContainer =
new SortedDictionary<string, Check>(container);
183 newContainer.Remove(check.ShortName);
185 newCategory =
new SortedDictionary<string, SortedDictionary<string, Check>>(category);
186 newCategory.Remove(check.Container);
188 newCategory[check.Container] = newContainer;
189 RegisteredChecks[check.Category] = newCategory;
195 public static bool TryGetCheckParents(
197 out SortedDictionary<
string, SortedDictionary<string, Check>> category,
198 out SortedDictionary<string, Check> container)
203 lock (RegisteredChecks)
205 if (RegisteredChecks.TryGetValue(check.Category, out category))
207 if (category.TryGetValue(check.Container, out container))
209 if (container.ContainsKey(check.ShortName))
218 public static void CheckChecks()
220 lock (RegisteredChecks)
222 foreach (SortedDictionary<
string, SortedDictionary<string, Check>> category
in RegisteredChecks.Values)
224 foreach (SortedDictionary<string, Check> container
in category.Values)
226 foreach (Check check
in container.Values)
228 if (!check.CheckIt())
230 "[CHECKS MANAGER]: Check {0}.{1}.{2} failed with message {3}", check.Category, check.Container, check.ShortName, check.LastFailureMessage);
237 private static void OutputAllChecksToConsole(ICommandConsole con)
239 foreach (var category
in RegisteredChecks.Values)
241 OutputCategoryChecksToConsole(con, category);
245 private static void OutputCategoryChecksToConsole(
246 ICommandConsole con, SortedDictionary<
string, SortedDictionary<string, Check>> category)
248 foreach (var container
in category.Values)
250 OutputContainerChecksToConsole(con, container);
254 private static void OutputContainerChecksToConsole(ICommandConsole con, SortedDictionary<string, Check> container)
256 foreach (Check check
in container.Values)
258 con.Output(check.ToConsoleString());