-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathServerManager.cs
376 lines (325 loc) · 12.2 KB
/
ServerManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//-----------------------------------------------------------------------
// This file is part of the Microsoft Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.AnalysisServices;
using System.Data;
namespace Microsoft.AnalysisServices.Samples.ActivityViewer
{
/// <summary>
/// A class to hold an alert on this server with details
/// </summary>
public class Alert : IDisposable
{
/// <summary>
/// The rule violated
/// </summary>
internal Rule rule;
/// <summary>
/// The table of rows which violated the rule
/// </summary>
internal DataTable table;
/// <summary>
/// The culprit's identity
/// </summary>
internal string violator;
/// <summary>
/// The server this alert was raised in
/// </summary>
internal ServerManager server;
/// <summary>
/// the priority of this alert
/// </summary>
internal int priority;
/// <summary>
/// Contructor for new Alert
/// </summary>
/// <param name="row">The culprit row</param>
/// <param name="rule">The violated rule</param>
/// <param name="column">The column in row containing culprit's identity</param>
public Alert(DataRow row, Rule rule, string column)
{
if (rule == null || column == null || row == null)
throw new ArgumentException("Cannot make alerts with null values");
this.table = new DataTable();
foreach (DataColumn col in row.Table.Columns)
table.Columns.Add(col.ColumnName, col.DataType);
AddRow(row);
this.rule = rule;
violator = row[column].ToString();
priority = rule.priority;
}
/// <summary>
/// Adds a row to this alert's violated list
/// </summary>
/// <param name="row">The row to add</param>
public void AddRow(DataRow row){
if (row == null)
throw new ArgumentException("Cannot add null row to alert");
foreach (DataRow drow in table.Rows)
if (drow["SESSION_SPID"].Equals(row["SESSION_SPID"]))
return;
table.Rows.Add(row.ItemArray);
}
/// <summary>
/// Sets the server this alert was raised on
/// </summary>
/// <param name="Server">The server this alert was raised on</param>
public void SetServer(ServerManager server)
{
this.server = server;
}
/// <summary>
/// Override ToString to give Alert context
/// </summary>
/// <returns></returns>
public override string ToString()
{
return rule.condition.ToString();
}
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
table.Dispose();
}
#endregion
}
/// <summary>
/// Class to manage a server
/// </summary>
public class ServerManager : IDisposable
{
#region Member Variables
/// <summary>
/// Analysis Services Manager to talk to server
/// </summary>
internal ASCommands ASMan;
/// <summary>
/// Rule Manager on this server
/// </summary>
internal RuleManager RuleMan;
/// <summary>
/// This server's name
/// </summary>
internal string Name;
/// <summary>
/// List of Alerts on the server
/// </summary>
internal List<Alert> alerts = null;
/// <summary>
/// The refresh interval for grids on this server. Refresh is off by default.
/// </summary>
internal int dataRefreshInterval;
#endregion
#region Constructor/Destructor
/// <summary>
/// Constructor for server manager
/// </summary>
/// <param name="connection">The connection to the server this will manage</param>
public ServerManager(string connection)
{
connection = FixProvider(connection);
ASMan = new ASCommands(connection);
RuleMan = new RuleManager();
//set up rule delegates to server functions
RuleMan.getData = new DataDel(ASMan.DiscoverSessions);
RuleMan.kill = new ActionDel(CancelSessionForRule);
RuleMan.alert = new ActionDel(AddAlertForRule);
RuleMan.ruleGetData = new QueryToData(ASMan.DataTableFromQuery);
this.alerts = new List<Alert>();
this.Name = DataSourceFromConnString(connection);
}
public ServerManager(string connection, int refresh)
{
dataRefreshInterval = refresh;
connection = FixProvider(connection);
ASMan = new ASCommands(connection);
RuleMan = new RuleManager();
//set up rule delegates to server functions
RuleMan.getData = new DataDel(ASMan.DiscoverSessions);
RuleMan.kill = new ActionDel(CancelSessionForRule);
RuleMan.alert = new ActionDel(AddAlertForRule);
RuleMan.ruleGetData = new QueryToData(ASMan.DataTableFromQuery);
this.alerts = new List<Alert>();
this.Name = DataSourceFromConnString(connection);
}
~ServerManager(){
//disconnect from the server on destruction
Disconnect();
}
#endregion
#region Public Methods
/// <summary>
/// A delegate to be called from the rules for killing sessions
/// </summary>
/// <param name="arg2">the Session SPID as an integer</param>
private void CancelSessionForRule(object arg1, object arg2){
try{
ASMan.CancelSession((int)arg2);
}
catch{}
}
/// <summary>
/// A delegate to be called from the rules to add an alert.
/// Also used to clear alerts list with 2 null arguments
/// </summary>
/// <param name="arg1">The culprit row as a DataRow</param>
/// <param name="arg2">the violated rule as a Rule</param>
private void AddAlertForRule(object arg1, object arg2){
if (arg1 == null && arg2 == null)
{
alerts.Clear();
return;
}
try
{
AddAlert(new Alert((DataRow)arg1, (Rule)arg2, "SESSION_USER_NAME"));
}
catch { }
}
/// <summary>
/// Adds an alert to this server. Only one alert with the same violated rule and culprit session is allowed
/// </summary>
/// <param name="alert">The alert to attemtp to add</param>
public void AddAlert(Alert alert)
{
Alert existing = alerts.Find(a => a.rule.ToString().Equals(alert.rule.ToString()) && a.violator.Equals(alert.violator));
if (existing == null)
{
int index = alerts.FindLastIndex(a => a.priority == alert.priority - 1);
alerts.Insert(index+1, alert);
alert.SetServer(this);
}
else
foreach (DataRow row in alert.table.Rows)
existing.AddRow(row);
}
/// <summary>
/// Connect this server to start processing rules
/// </summary>
public void Connect()
{
ASMan.Connect();
RuleMan.Start();
}
/// <summary>
/// Disconnect this server and stop processing rules
/// </summary>
public void Disconnect()
{
RuleMan.Stop();
ASMan.Disconnect();
}
/// <summary>
/// Change the connection string on this server
/// </summary>
/// <param name="newString">The string to which to try to change to</param>
public void NewConnectionString(string newString)
{
string oldstring = ASMan.ConnectionString;
newString = FixProvider(newString);
ASMan.ChangeConnectionString(newString);
try
{
TestConnection();
Name = DataSourceFromConnString(newString);
}
catch
{
ASMan.ChangeConnectionString(oldstring);
throw;
}
ASMan.Reconnect();
}
/// <summary>
/// Fixes the provider to be msolap in a connection string, regardless of what it was set to before or if it existed.
/// Also formats the string to have consistent spacing.
/// </summary>
/// <param name="conn">The connection string to modify</param>
/// <returns>A new connection string with msolap as the provider</returns>
public static string FixProvider(string conn)
{
string[] buff = conn.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
conn = "";
foreach(string s in buff)
if (s.Trim().ToLowerInvariant().StartsWith("provider"))
conn += "";
else if (!String.IsNullOrEmpty(s))
{
string[] temp = s.Split(new char[] { '=' }, 2);
conn += temp[0].Trim() + " = " + temp[1].Trim() + " ; ";
}
conn += "Provider = msolap";
return conn;
}
/// <summary>
/// Test the connection on this server. Will throw exception if connection is bad or if Microsoft.AnalysisServices.Samples.ActivityViewer DB is not installed on the server
/// </summary>
public void TestConnection()
{
Server test = new Server();
test.Connect(ASMan.ConnectionString);
if (double.Parse(test.Version.Substring(0,4)) < 10.0)
{
test.Disconnect();
throw new ConnectionException("Server version must be at least 10.0");
}
test.Disconnect();
}
/// <summary>
/// Parse the data source out of a connection string
/// </summary>
/// <param name="connString">The string to parse data source out of</param>
/// <returns>Returns "" if string is invalid or not formatted properly</returns>
private static string DataSourceFromConnString(string connectionString)
{
ConnectionInfo ci = new ConnectionInfo(connectionString);
string dataSource = ci.Server;
if (ci.Port != null)
dataSource += ":" + ci.Port;
if (ci.InstanceName != null)
dataSource += @"\" + ci.InstanceName;
return dataSource;
}
#endregion
#region Configuration Methods
/// <summary>
/// Saves the data for this session to the given XmlTextWriter
/// </summary>
/// <param name="writer">The writer to which to write the session data</param>
public void SaveConfigData(XmlWriter writer)
{
writer.WriteStartElement("Server");
//write server information and ask RuleMan to save its rules for us
writer.WriteElementString("String", ASMan.ConnectionString);
RuleMan.SaveConfigData(writer);
writer.WriteEndElement();
}
#endregion
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
Disconnect();
ASMan.Dispose();
RuleMan.Dispose();
}
#endregion
}
}