29 using System.Collections;
30 using System.Collections.Generic;
34 using OpenSim.Framework;
35 using OpenMetaverse.StructuredData;
37 namespace OpenSim.Framework.Monitoring
42 public static class StatsManager
45 public const string AllSubCommand =
"all";
48 public const string ListSubCommand =
"list";
51 public static HashSet<string> SubCommands =
new HashSet<string> { AllSubCommand, ListSubCommand };
59 public static SortedDictionary<string, SortedDictionary<string, SortedDictionary<string, Stat>>> RegisteredStats
60 =
new SortedDictionary<string, SortedDictionary<string, SortedDictionary<string, Stat>>>();
68 public static SimExtraStatsCollector SimExtraStats {
get; set; }
72 console.Commands.AddCommand(
76 "stats show [list|all|(<category>[.<container>])+",
77 "Show statistical information for this server",
78 "If no final argument is specified then legacy statistics information is currently shown.\n"
79 +
"'list' argument will show statistic categories.\n"
80 +
"'all' will show all statistics.\n"
81 +
"A <category> name will show statistics from that category.\n"
82 +
"A <category>.<container> name will show statistics from that category in that container.\n"
83 +
"More than one name can be given separated by spaces.\n"
84 +
"THIS STATS FACILITY IS EXPERIMENTAL AND DOES NOT YET CONTAIN ALL STATS",
85 HandleShowStatsCommand);
87 console.Commands.AddCommand(
91 "show stats [list|all|(<category>[.<container>])+",
92 "Alias for 'stats show' command",
93 HandleShowStatsCommand);
95 StatsLogger.RegisterConsoleCommands(console);
98 public static void HandleShowStatsCommand(
string module,
string[] cmd)
104 foreach (
string name
in cmd.Skip(2))
106 string[] components = name.Split(
'.');
108 string categoryName = components[0];
109 string containerName = components.Length > 1 ? components[1] : null;
110 string statName = components.Length > 2 ? components[2] : null;
112 if (categoryName == AllSubCommand)
114 OutputAllStatsToConsole(con);
116 else if (categoryName == ListSubCommand)
118 con.Output(
"Statistic categories available are:");
119 foreach (
string category
in RegisteredStats.Keys)
120 con.OutputFormat(
" {0}", category);
124 SortedDictionary<string, SortedDictionary<string, Stat>> category;
125 if (!RegisteredStats.TryGetValue(categoryName, out category))
127 con.OutputFormat(
"No such category as {0}", categoryName);
131 if (
String.IsNullOrEmpty(containerName))
133 OutputCategoryStatsToConsole(con, category);
137 SortedDictionary<string, Stat> container;
138 if (category.TryGetValue(containerName, out container))
140 if (
String.IsNullOrEmpty(statName))
142 OutputContainerStatsToConsole(con, container);
147 if (container.TryGetValue(statName, out stat))
149 OutputStatToConsole(con, stat);
154 "No such stat {0} in {1}.{2}", statName, categoryName, containerName);
160 con.OutputFormat(
"No such container {0} in category {1}", containerName, categoryName);
170 if (SimExtraStats != null)
171 con.Output(SimExtraStats.Report());
173 OutputAllStatsToConsole(con);
177 public static List<string> GetAllStatsReports()
179 List<string> reports =
new List<string>();
181 foreach (var category
in RegisteredStats.Values)
182 reports.AddRange(GetCategoryStatsReports(category));
189 foreach (
string report
in GetAllStatsReports())
193 private static List<string> GetCategoryStatsReports(
194 SortedDictionary<
string, SortedDictionary<string, Stat>> category)
196 List<string> reports =
new List<string>();
198 foreach (var container
in category.Values)
199 reports.AddRange(GetContainerStatsReports(container));
204 private static void OutputCategoryStatsToConsole(
205 ICommandConsole con, SortedDictionary<
string, SortedDictionary<string, Stat>> category)
207 foreach (
string report
in GetCategoryStatsReports(category))
211 private static List<string> GetContainerStatsReports(SortedDictionary<string, Stat> container)
213 List<string> reports =
new List<string>();
215 foreach (Stat stat
in container.Values)
216 reports.Add(stat.ToConsoleString());
221 private static void OutputContainerStatsToConsole(
224 foreach (
string report
in GetContainerStatsReports(container))
228 private static void OutputStatToConsole(
ICommandConsole con, Stat stat)
230 con.Output(stat.ToConsoleString());
261 public static OSDMap GetStatsAsOSDMap(
string pCategoryName,
string pContainerName,
string pStatName)
265 lock (RegisteredStats)
267 foreach (
string catName
in RegisteredStats.Keys)
271 if (!(
String.IsNullOrEmpty(pCategoryName) || pCategoryName == AllSubCommand || pCategoryName == catName))
275 foreach (
string contName
in RegisteredStats[catName].Keys)
277 if (!(
string.IsNullOrEmpty(pContainerName) || pContainerName == AllSubCommand || pContainerName == contName))
282 SortedDictionary<string, Stat> theStats = RegisteredStats[catName][contName];
283 foreach (
string statName
in theStats.Keys)
285 if (!(
String.IsNullOrEmpty(pStatName) || pStatName == AllSubCommand || pStatName == statName))
288 statMap.Add(statName, theStats[statName].ToBriefOSDMap());
291 contMap.Add(contName, statMap);
293 map.Add(catName, contMap);
300 public static Hashtable HandleStatsRequest(Hashtable request)
302 Hashtable responsedata =
new Hashtable();
304 int response_code = 200;
305 string contenttype =
"text/json";
307 string pCategoryName = StatsManager.AllSubCommand;
308 string pContainerName = StatsManager.AllSubCommand;
309 string pStatName = StatsManager.AllSubCommand;
311 if (request.ContainsKey(
"cat")) pCategoryName = request[
"cat"].ToString();
312 if (request.ContainsKey(
"cont")) pContainerName = request[
"cat"].ToString();
313 if (request.ContainsKey(
"stat")) pStatName = request[
"stat"].ToString();
315 string strOut = StatsManager.GetStatsAsOSDMap(pCategoryName, pContainerName, pStatName).ToString();
318 if (request.ContainsKey(
"callback"))
320 strOut = request[
"callback"].ToString() +
"(" + strOut +
");";
326 responsedata[
"int_response_code"] = response_code;
327 responsedata[
"content_type"] = contenttype;
328 responsedata[
"keepalive"] =
false;
329 responsedata[
"str_response_string"] = strOut;
330 responsedata[
"access_control_allow_origin"] =
"*";
362 public static bool RegisterStat(Stat stat)
364 SortedDictionary<string, SortedDictionary<string, Stat>> category = null, newCategory;
365 SortedDictionary<string, Stat> container = null, newContainer;
367 lock (RegisteredStats)
372 if (TryGetStatParents(stat, out category, out container))
378 if (container != null)
379 newContainer =
new SortedDictionary<string, Stat>(container);
381 newContainer =
new SortedDictionary<string, Stat>();
383 if (category != null)
384 newCategory =
new SortedDictionary<string, SortedDictionary<string, Stat>>(category);
386 newCategory =
new SortedDictionary<string, SortedDictionary<string, Stat>>();
388 newContainer[stat.ShortName] = stat;
389 newCategory[stat.Container] = newContainer;
390 RegisteredStats[stat.Category] = newCategory;
401 public static bool DeregisterStat(Stat stat)
403 SortedDictionary<string, SortedDictionary<string, Stat>> category = null, newCategory;
404 SortedDictionary<string, Stat> container = null, newContainer;
406 lock (RegisteredStats)
408 if (!TryGetStatParents(stat, out category, out container))
411 newContainer =
new SortedDictionary<string, Stat>(container);
412 newContainer.Remove(stat.ShortName);
414 newCategory =
new SortedDictionary<string, SortedDictionary<string, Stat>>(category);
415 newCategory.Remove(stat.Container);
417 newCategory[stat.Container] = newContainer;
418 RegisteredStats[stat.Category] = newCategory;
424 public static bool TryGetStat(
string category,
string container,
string statShortName, out Stat stat)
427 SortedDictionary<string, SortedDictionary<string, Stat>> categoryStats;
429 lock (RegisteredStats)
431 if (!TryGetStatsForCategory(category, out categoryStats))
434 SortedDictionary<string, Stat> containerStats;
436 if (!categoryStats.TryGetValue(container, out containerStats))
439 return containerStats.TryGetValue(statShortName, out stat);
443 public static bool TryGetStatsForCategory(
444 string category, out SortedDictionary<
string, SortedDictionary<string, Stat>> stats)
446 lock (RegisteredStats)
447 return RegisteredStats.TryGetValue(category, out stats);
458 public static List<Stat> GetStatsFromEachContainer(
string category,
string statShortName)
460 SortedDictionary<string, SortedDictionary<string, Stat>> categoryStats;
462 lock (RegisteredStats)
464 if (!RegisteredStats.TryGetValue(category, out categoryStats))
467 List<Stat> stats = null;
469 foreach (SortedDictionary<string, Stat> containerStats
in categoryStats.Values)
471 if (containerStats.ContainsKey(statShortName))
474 stats =
new List<Stat>();
476 stats.Add(containerStats[statShortName]);
484 public static bool TryGetStatParents(
486 out SortedDictionary<
string, SortedDictionary<string, Stat>> category,
487 out SortedDictionary<string, Stat> container)
492 lock (RegisteredStats)
494 if (RegisteredStats.TryGetValue(stat.Category, out category))
496 if (category.TryGetValue(stat.Container, out container))
498 if (container.ContainsKey(stat.ShortName))
507 public static void RecordStats()
509 lock (RegisteredStats)
511 foreach (SortedDictionary<
string, SortedDictionary<string, Stat>> category
in RegisteredStats.Values)
513 foreach (SortedDictionary<string, Stat> container
in category.Values)
515 foreach (Stat stat
in container.Values)
OpenMetaverse.StructuredData.OSDMap OSDMap
StatVerbosity
Verbosity of stat.
MeasuresOfInterest
Measures of interest for this stat.