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