-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfig.cs
More file actions
106 lines (98 loc) · 4.15 KB
/
Config.cs
File metadata and controls
106 lines (98 loc) · 4.15 KB
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
// The MIT License (MIT)
// Copyright (c) 2014 Practice Fusion
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// File: Config.cs
// Date: 05/19/2014
// Author: Steven Padfield
//
// From Practice Fusion technology blog post, "Windows Performance Counters for ActiveMQ", http://www.practicefusion.com/blog/performance-counters-for-activemq/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using log4net;
namespace AmqMonitor
{
/// <summary>
/// Provides access to configuration values.
/// </summary>
public static class Config
{
private static readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
static Config()
{
// Obtain configuration information from the default app config file.
SampleInterval = GetTimeSpanValue("SampleInterval");
var amqHostsString = GetStringValue("AmqHosts");
AmqHosts = amqHostsString.Split(',').Select(h => new AmqHost(h));
_logger.InfoFormat("Configuration loaded. AmqHosts={0} SampleInterval={1}", amqHostsString, SampleInterval);
}
/// <summary>
/// Gets the interval at which to sample and publish AMQ queue statistics to performance counters.
/// </summary>
public static TimeSpan SampleInterval { get; private set; }
/// <summary>
/// Gets a list of AMQ hosts to monitor.
/// </summary>
public static IEnumerable<AmqHost> AmqHosts { get; private set; }
/// <summary>
/// Gets the specified app setting value as a string. Returns "" if the app setting is not present.
/// </summary>
/// <param name="key">
/// The key of the app setting to get.
/// </param>
/// <returns>
/// The value of the specified app setting, or "" if not found.
/// </returns>
private static string GetStringValue(string key)
{
return ConfigurationManager.AppSettings[key] ?? "";
}
/// <summary>
/// Gets the specified app setting value as a TimeSpan. Returns TimeSpan.Zero if the app setting is not present or can't be
/// parsed.
/// </summary>
/// <param name="key">
/// The key of the app setting to get.
/// </param>
/// <returns>
/// The value of the specified app setting, or TimeSpan.Zero if not found.
/// </returns>
private static TimeSpan GetTimeSpanValue(string key)
{
TimeSpan value;
TimeSpan.TryParse(ConfigurationManager.AppSettings[key], out value);
return value;
}
/// <summary>
/// Validates that the configuration has legal values.
/// </summary>
public static void Validate()
{
if (SampleInterval < TimeSpan.Zero)
{
throw new Exception("Configured sample interval cannot be less than zero.");
}
if (!AmqHosts.Any())
{
throw new Exception("At least one AMQ host must be configured.");
}
}
}
}