PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/docgenerator/AWSSDKDocSamples/CloudWatch/CloudWatchSamples.cs

https://gitlab.com/CORP-RESELLER/aws-sdk-net
C# | 373 lines | 307 code | 64 blank | 2 comment | 6 complexity | 0b538f12d72a29b0ed4596e7ac15e150 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Amazon.CloudWatch;
  7. using Amazon.CloudWatch.Model;
  8. namespace AWSSDKDocSamples.CloudWatch
  9. {
  10. class CloudWatchSamples : ISample
  11. {
  12. public static void CWGetMetricStatistics()
  13. {
  14. #region CWGetMetricStatistics
  15. var client = new AmazonCloudWatchClient();
  16. var request = new GetMetricStatisticsRequest
  17. {
  18. Dimensions = new List<Dimension>() { dimension },
  19. EndTime = DateTime.Today,
  20. MetricName = "CPUUtilization",
  21. Namespace = "AWS/EC2",
  22. // Get statistics by day.
  23. Period = (int)TimeSpan.FromDays(1).TotalSeconds,
  24. // Get statistics for the past month.
  25. StartTime = DateTime.Today.Subtract(TimeSpan.FromDays(30)),
  26. Statistics = new List<string>() { "Minimum" },
  27. Unit = StandardUnit.Percent
  28. };
  29. var response = client.GetMetricStatistics(request);
  30. if (response.Datapoints.Count > 0)
  31. {
  32. foreach (var point in response.Datapoints)
  33. {
  34. Console.WriteLine(point.Timestamp.ToShortDateString() +
  35. " " + point.Minimum + "%");
  36. }
  37. }
  38. #endregion
  39. Console.ReadLine();
  40. }
  41. public static void CWPutMetricData()
  42. {
  43. #region CWPutMetricData
  44. var client = new AmazonCloudWatchClient();
  45. var dimension = new Dimension
  46. {
  47. Name = "Desktop Machine Metrics",
  48. Value = "Virtual Desktop Machine Usage"
  49. };
  50. var metric1 = new MetricDatum
  51. {
  52. Dimensions = new List<Dimension>(),
  53. MetricName = "Desktop Machines Online",
  54. StatisticValues = new StatisticSet(),
  55. Timestamp = DateTime.Today,
  56. Unit = StandardUnit.Count,
  57. Value = 14
  58. };
  59. var metric2 = new MetricDatum
  60. {
  61. Dimensions = new List<Dimension>(),
  62. MetricName = "Desktop Machines Offline",
  63. StatisticValues = new StatisticSet(),
  64. Timestamp = DateTime.Today,
  65. Unit = StandardUnit.Count,
  66. Value = 7
  67. };
  68. var metric3 = new MetricDatum
  69. {
  70. Dimensions = new List<Dimension>(),
  71. MetricName = "Desktop Machines Online",
  72. StatisticValues = new StatisticSet(),
  73. Timestamp = DateTime.Today,
  74. Unit = StandardUnit.Count,
  75. Value = 12
  76. };
  77. var metric4 = new MetricDatum
  78. {
  79. Dimensions = new List<Dimension>(),
  80. MetricName = "Desktop Machines Offline",
  81. StatisticValues = new StatisticSet(),
  82. Timestamp = DateTime.Today,
  83. Unit = StandardUnit.Count,
  84. Value = 9
  85. };
  86. var request = new PutMetricDataRequest
  87. {
  88. MetricData = new List<MetricDatum>() { metric1, metric2,
  89. metric3, metric4 },
  90. Namespace = "Example.com Custom Metrics"
  91. };
  92. client.PutMetricData(request);
  93. #endregion
  94. }
  95. public static void CWListMetrics()
  96. {
  97. #region CWListMetrics
  98. var client = new AmazonCloudWatchClient();
  99. var filter = new DimensionFilter
  100. {
  101. Name = "InstanceType",
  102. Value = "t1.micro"
  103. };
  104. var request = new ListMetricsRequest
  105. {
  106. Dimensions = new List<DimensionFilter>() { filter },
  107. MetricName = "CPUUtilization",
  108. Namespace = "AWS/EC2"
  109. };
  110. var response = new ListMetricsResponse();
  111. do
  112. {
  113. response = client.ListMetrics(request);
  114. if (response.Metrics.Count > 0)
  115. {
  116. foreach (var metric in response.Metrics)
  117. {
  118. Console.WriteLine(metric.MetricName +
  119. " (" + metric.Namespace + ")");
  120. foreach (var dimension in metric.Dimensions)
  121. {
  122. Console.WriteLine(" " + dimension.Name + ": "
  123. + dimension.Value);
  124. }
  125. }
  126. }
  127. else
  128. {
  129. Console.WriteLine("No metrics found.");
  130. }
  131. request.NextToken = response.NextToken;
  132. } while (!string.IsNullOrEmpty(response.NextToken));
  133. #endregion
  134. Console.ReadLine();
  135. }
  136. public static void CWDescribeAlarms()
  137. {
  138. #region CWDescribeAlarms
  139. var client = new AmazonCloudWatchClient();
  140. var request = new DescribeAlarmsRequest
  141. {
  142. AlarmNames = new List<string>()
  143. { "awseb-e-b36EXAMPLE-stack-CloudwatchAlarmLow-1KAKH4EXAMPLE" },
  144. MaxRecords = 1,
  145. StateValue = StateValue.ALARM
  146. };
  147. var response = new DescribeAlarmsResponse();
  148. do
  149. {
  150. response = client.DescribeAlarms(request);
  151. foreach (var alarm in response.MetricAlarms)
  152. {
  153. Console.WriteLine(alarm.AlarmName);
  154. Console.WriteLine(alarm.AlarmDescription);
  155. Console.WriteLine(alarm.MetricName + " " +
  156. alarm.ComparisonOperator + " " + alarm.Threshold);
  157. Console.WriteLine();
  158. }
  159. request.NextToken = response.NextToken;
  160. } while (!string.IsNullOrEmpty(response.NextToken));
  161. #endregion
  162. Console.ReadLine();
  163. }
  164. public static void CWDescribeAlarmHistory()
  165. {
  166. #region CWDescribeAlarmHistory
  167. var client = new AmazonCloudWatchClient();
  168. var request = new DescribeAlarmHistoryRequest
  169. {
  170. AlarmName =
  171. "awseb-e-kkbEXAMPLE-stack-CloudwatchAlarmLow-1WVXD9EXAMPLE",
  172. EndDate = DateTime.Today,
  173. HistoryItemType = HistoryItemType.Action,
  174. MaxRecords = 1,
  175. StartDate = DateTime.Today.Subtract(TimeSpan.FromDays(30))
  176. };
  177. var response = new DescribeAlarmHistoryResponse();
  178. do
  179. {
  180. response = client.DescribeAlarmHistory(request);
  181. foreach (var item in response.AlarmHistoryItems)
  182. {
  183. Console.WriteLine(item.AlarmName);
  184. Console.WriteLine(item.HistorySummary);
  185. Console.WriteLine();
  186. }
  187. request.NextToken = response.NextToken;
  188. } while (!string.IsNullOrEmpty(response.NextToken));
  189. #endregion
  190. Console.ReadLine();
  191. }
  192. public static void CWPutMetricAlarm()
  193. {
  194. #region CWPutMetricAlarm
  195. var client = new AmazonCloudWatchClient();
  196. var dimension = new Dimension
  197. {
  198. Name = "Desktop Machine Metrics",
  199. Value = "Virtual Desktop Machine Usage"
  200. };
  201. var request = new PutMetricAlarmRequest
  202. {
  203. ActionsEnabled = true,
  204. AlarmActions = new List<string>()
  205. { "arn:aws:sns:us-east-1:80398EXAMPLE:CodingTestResults" },
  206. AlarmDescription = "Too many instances offline",
  207. AlarmName = "Offline Instances",
  208. ComparisonOperator = ComparisonOperator.GreaterThanOrEqualToThreshold,
  209. Dimensions = new List<Dimension>() { dimension },
  210. EvaluationPeriods = 1,
  211. MetricName = "Desktop Machines Offline",
  212. Namespace = "Example.com Custom Metrics",
  213. Period = (int)TimeSpan.FromMinutes(5).TotalSeconds,
  214. Statistic = new Statistic("Maximum"),
  215. Threshold = 5,
  216. Unit = StandardUnit.Count
  217. };
  218. client.PutMetricAlarm(request);
  219. #endregion
  220. }
  221. public static void CWDescribeAlarmsForMetric()
  222. {
  223. #region CWDescribeAlarmsForMetric
  224. var client = new AmazonCloudWatchClient();
  225. var dimension = new Dimension
  226. {
  227. Name = "AutoScalingGroupName",
  228. Value = "awseb-e-kkbEXAMPLE-stack-AutoScalingGroup-F4TAUEXAMPLE"
  229. };
  230. var request = new DescribeAlarmsForMetricRequest
  231. {
  232. Dimensions = new List<Dimension>() { dimension },
  233. MetricName = "NetworkOut",
  234. Namespace = "AWS/EC2"
  235. };
  236. var response = client.DescribeAlarmsForMetric(request);
  237. if (response.MetricAlarms.Count > 0)
  238. {
  239. foreach (var alarm in response.MetricAlarms)
  240. {
  241. Console.WriteLine();
  242. Console.WriteLine(alarm.AlarmName);
  243. Console.WriteLine(alarm.AlarmDescription);
  244. Console.WriteLine(alarm.MetricName + " " +
  245. alarm.ComparisonOperator + " " + alarm.Threshold);
  246. }
  247. }
  248. else
  249. {
  250. Console.WriteLine("No alarms.");
  251. }
  252. #endregion
  253. Console.ReadLine();
  254. }
  255. public static void CWDeleteAlarms()
  256. {
  257. #region CWDeleteAlarms
  258. var client = new AmazonCloudWatchClient();
  259. var request = new DeleteAlarmsRequest
  260. {
  261. AlarmNames = new List<string>() { "t1.microCPUUtilization" }
  262. };
  263. client.DeleteAlarms(request);
  264. #endregion
  265. }
  266. public static void CWSetAlarmState()
  267. {
  268. #region CWSetAlarmState
  269. var client = new AmazonCloudWatchClient();
  270. var request = new SetAlarmStateRequest
  271. {
  272. AlarmName = "Offline Instances",
  273. StateReason = "Too many instances are offline.",
  274. StateValue = StateValue.ALARM
  275. };
  276. client.SetAlarmState(request);
  277. #endregion
  278. }
  279. public static void CWEnableAlarmActions()
  280. {
  281. #region CWEnableAlarmActions
  282. var client = new AmazonCloudWatchClient();
  283. var request = new EnableAlarmActionsRequest
  284. {
  285. AlarmNames = new List<string>()
  286. { "awseb-e-kkbEXAMPLE-stack-CloudwatchAlarmLow-1WVXD9EXAMPLE" }
  287. };
  288. client.EnableAlarmActions(request);
  289. #endregion
  290. }
  291. public static void CWDisableAlarmActions()
  292. {
  293. #region CWDisableAlarmActions
  294. var client = new AmazonCloudWatchClient();
  295. var request = new DisableAlarmActionsRequest
  296. {
  297. AlarmNames = new List<string>()
  298. { "awseb-e-kkbEXAMPLE-stack-CloudwatchAlarmLow-1WVXD9EXAMPLE" }
  299. };
  300. client.DisableAlarmActions(request);
  301. #endregion
  302. }
  303. #region ISample Members
  304. public virtual void Run()
  305. {
  306. }
  307. #endregion
  308. }
  309. }