EtcdMonitoringModule.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using log4net;
  32. using Mono.Addins;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using netcd;
  40. using netcd.Serialization;
  41. using netcd.Advanced;
  42. using netcd.Advanced.Requests;
  43. namespace OpenSim.Region.OptionalModules.Framework.Monitoring
  44. {
  45. /// <summary>
  46. /// Allows to store monitoring data in etcd, a high availability
  47. /// name-value store.
  48. /// </summary>
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "EtcdMonitoringModule")]
  50. public class EtcdMonitoringModule : INonSharedRegionModule, IEtcdModule
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. protected Scene m_scene;
  54. protected IEtcdClient m_client;
  55. protected bool m_enabled = false;
  56. protected string m_etcdBasePath = String.Empty;
  57. protected bool m_appendRegionID = true;
  58. public string Name
  59. {
  60. get { return "EtcdMonitoringModule"; }
  61. }
  62. public Type ReplaceableInterface
  63. {
  64. get { return null; }
  65. }
  66. public void Initialise(IConfigSource source)
  67. {
  68. if (source.Configs["Etcd"] == null)
  69. return;
  70. IConfig etcdConfig = source.Configs["Etcd"];
  71. string etcdUrls = etcdConfig.GetString("EtcdUrls", String.Empty);
  72. if (etcdUrls == String.Empty)
  73. return;
  74. m_etcdBasePath = etcdConfig.GetString("BasePath", m_etcdBasePath);
  75. m_appendRegionID = etcdConfig.GetBoolean("AppendRegionID", m_appendRegionID);
  76. if (!m_etcdBasePath.EndsWith("/"))
  77. m_etcdBasePath += "/";
  78. try
  79. {
  80. string[] endpoints = etcdUrls.Split(new char[] {','});
  81. List<Uri> uris = new List<Uri>();
  82. foreach (string endpoint in endpoints)
  83. uris.Add(new Uri(endpoint.Trim()));
  84. m_client = new EtcdClient(uris.ToArray(), new DefaultSerializer(), new DefaultSerializer());
  85. }
  86. catch (Exception e)
  87. {
  88. m_log.DebugFormat("[ETCD]: Error initializing connection: " + e.ToString());
  89. return;
  90. }
  91. m_log.DebugFormat("[ETCD]: Etcd module configured");
  92. m_enabled = true;
  93. }
  94. public void Close()
  95. {
  96. //m_client = null;
  97. m_scene = null;
  98. }
  99. public void AddRegion(Scene scene)
  100. {
  101. m_scene = scene;
  102. if (m_enabled)
  103. {
  104. if (m_appendRegionID)
  105. m_etcdBasePath += m_scene.RegionInfo.RegionID.ToString() + "/";
  106. m_log.DebugFormat("[ETCD]: Using base path {0} for all keys", m_etcdBasePath);
  107. try
  108. {
  109. m_client.Advanced.CreateDirectory(new CreateDirectoryRequest() {Key = m_etcdBasePath});
  110. }
  111. catch (Exception e)
  112. {
  113. m_log.ErrorFormat("Exception trying to create base path {0}: " + e.ToString(), m_etcdBasePath);
  114. }
  115. scene.RegisterModuleInterface<IEtcdModule>(this);
  116. }
  117. }
  118. public void RemoveRegion(Scene scene)
  119. {
  120. }
  121. public void RegionLoaded(Scene scene)
  122. {
  123. }
  124. public bool Store(string k, string v)
  125. {
  126. return Store(k, v, 0);
  127. }
  128. public bool Store(string k, string v, int ttl)
  129. {
  130. Response resp = m_client.Advanced.SetKey(new SetKeyRequest() { Key = m_etcdBasePath + k, Value = v, TimeToLive = ttl });
  131. if (resp == null)
  132. return false;
  133. if (resp.ErrorCode.HasValue)
  134. {
  135. m_log.DebugFormat("[ETCD]: Error {0} ({1}) storing {2} => {3}", resp.Cause, (int)resp.ErrorCode, m_etcdBasePath + k, v);
  136. return false;
  137. }
  138. return true;
  139. }
  140. public string Get(string k)
  141. {
  142. Response resp = m_client.Advanced.GetKey(new GetKeyRequest() { Key = m_etcdBasePath + k });
  143. if (resp == null)
  144. return String.Empty;
  145. if (resp.ErrorCode.HasValue)
  146. {
  147. m_log.DebugFormat("[ETCD]: Error {0} ({1}) getting {2}", resp.Cause, (int)resp.ErrorCode, m_etcdBasePath + k);
  148. return String.Empty;
  149. }
  150. return resp.Node.Value;
  151. }
  152. public void Delete(string k)
  153. {
  154. m_client.Advanced.DeleteKey(new DeleteKeyRequest() { Key = m_etcdBasePath + k });
  155. }
  156. public void Watch(string k, Action<string> callback)
  157. {
  158. m_client.Advanced.WatchKey(new WatchKeyRequest() { Key = m_etcdBasePath + k, Callback = (x) => { callback(x.Node.Value); } });
  159. }
  160. }
  161. }