Files
UptimeSharp/UptimeSharp.OldClassLib/Components/Monitor.cs
T

162 lines
5.1 KiB
C#
Raw Normal View History

using RestSharp;
using System.Collections.Generic;
using UptimeSharp.Models;
namespace UptimeSharp
{
/// <summary>
/// UptimeClient
/// </summary>
public partial class UptimeClient
{
2013-08-18 14:42:21 +02:00
/// <summary>
2013-08-21 22:50:30 +02:00
/// Retrieves specified monitors from UptimeRobot
/// </summary>
2013-08-21 22:50:30 +02:00
/// <param name="monitors">monitor list</param>
2013-08-24 01:52:14 +02:00
/// <param name="customUptimeRatio">The custom uptime ratio.</param>
/// <param name="showLog">if set to <c>true</c> [show log].</param>
/// <param name="showAlerts">if set to <c>true</c> [show alerts].</param>
2013-08-25 16:24:52 +02:00
/// <returns>
/// Monitor List
/// </returns>
2013-08-25 22:27:20 +02:00
public List<Monitor> GetMonitors(int[] monitorIDs = null, float[] customUptimeRatio = null, bool showLog = false, bool showAlerts = true)
{
2013-08-21 22:50:30 +02:00
RetrieveParameters parameters = new RetrieveParameters()
{
2013-08-25 22:27:20 +02:00
Monitors = monitorIDs,
2013-08-21 22:50:30 +02:00
CustomUptimeRatio = customUptimeRatio,
ShowAlerts = showAlerts,
ShowLog = showLog
};
return Get<RetrieveResponse>("getMonitors", parameters.Convert()).Items;
}
/// <summary>
/// Retrieves a monitor from UptimeRobot
/// </summary>
/// <param name="monitorId">a specific monitor ID</param>
2013-08-25 16:24:52 +02:00
/// <param name="customUptimeRatio">The custom uptime ratio.</param>
/// <param name="showLog">if set to <c>true</c> [show log].</param>
/// <param name="showAlerts">if set to <c>true</c> [show alerts].</param>
/// <returns>
/// The Monitor
/// </returns>
public Monitor GetMonitor(int monitorId, float[] customUptimeRatio = null, bool showLog = false, bool showAlerts = true)
{
2013-08-25 16:24:52 +02:00
List<Monitor> monitors = GetMonitors(new int[] { monitorId }, customUptimeRatio, showLog, showAlerts);
return monitors.ToArray().Length > 0 ? monitors[0] : null;
}
2013-08-18 12:08:08 +02:00
/// <summary>
/// Deletes a monitor
/// </summary>
2013-08-24 01:52:14 +02:00
/// <param name="monitorId">a specific monitor ID</param>
2013-08-25 16:24:52 +02:00
/// <returns>
/// Success state
/// </returns>
public bool DeleteMonitor(int monitorId)
2013-08-18 12:08:08 +02:00
{
2013-08-21 22:50:30 +02:00
return Get<DefaultResponse>("deleteMonitor", Parameter("monitorID", monitorId)).Status;
2013-08-18 12:08:08 +02:00
}
/// <summary>
/// Deletes a monitor
/// </summary>
/// <param name="monitor">The monitor.</param>
2013-08-25 16:24:52 +02:00
/// <returns>
/// Success state
/// </returns>
public bool DeleteMonitor(Monitor monitor)
2013-08-18 12:08:08 +02:00
{
2013-08-25 16:24:52 +02:00
return DeleteMonitor(monitor.ID);
2013-08-18 12:08:08 +02:00
}
2013-08-18 14:26:01 +02:00
2013-08-18 14:42:21 +02:00
/// <summary>
/// Creates a monitor.
/// </summary>
2013-08-18 14:26:01 +02:00
/// <param name="name">The name of the new monitor.</param>
/// <param name="uri">The URI or IP to watch.</param>
2013-08-21 22:50:30 +02:00
/// <param name="type">The type of the monitor.</param>
2013-08-25 22:27:20 +02:00
/// <param name="subtype">The subtype of the monitor (if port).</param>
2013-08-18 14:26:01 +02:00
/// <param name="port">The port (only for Subtype.Custom).</param>
2013-08-21 22:50:30 +02:00
/// <param name="keywordValue">The keyword value.</param>
/// <param name="keywordType">Type of the keyword.</param>
2013-08-25 22:27:20 +02:00
/// <param name="alerts">An ID list of existing alerts to notify.</param>
2013-08-24 01:52:14 +02:00
/// <param name="HTTPUsername">The HTTP username.</param>
2013-08-25 22:27:20 +02:00
/// <param name="HTTPPassword">The HTTP password.</param>
2013-08-18 14:26:01 +02:00
/// <returns>
2013-08-25 16:24:52 +02:00
/// Success state
2013-08-18 14:26:01 +02:00
/// </returns>
2013-08-25 16:24:52 +02:00
public bool AddMonitor(string name, string uri, Type type = Type.HTTP, Subtype subtype = Subtype.Unknown,
int? port = null, string keywordValue = null, KeywordType keywordType = KeywordType.Unknown,
string[] alerts = null, string HTTPUsername = null, string HTTPPassword = null)
2013-08-18 14:26:01 +02:00
{
2013-08-21 22:50:30 +02:00
MonitorParameters parameters = new MonitorParameters()
2013-08-18 14:26:01 +02:00
{
Name = name,
Uri = uri,
2013-08-21 22:50:30 +02:00
Type = type,
2013-08-18 14:26:01 +02:00
Subtype = subtype,
Port = port,
KeywordType = keywordType,
2013-08-21 22:50:30 +02:00
KeywordValue = keywordValue,
Alerts = alerts,
HTTPPassword = HTTPPassword,
HTTPUsername = HTTPUsername
};
2013-08-19 21:56:10 +02:00
2013-08-21 22:50:30 +02:00
return Get<DefaultResponse>("newMonitor", parameters.Convert()).Status;
2013-08-19 21:56:10 +02:00
}
/// <summary>
/// Edits a monitor.
/// </summary>
/// <param name="monitor">The monitor.</param>
/// <returns>
2013-08-25 16:24:52 +02:00
/// Success state
2013-08-19 21:56:10 +02:00
/// </returns>
2013-08-25 16:24:52 +02:00
public bool ModifyMonitor(Monitor monitor)
2013-08-19 21:56:10 +02:00
{
List<string> alerts = null;
2013-08-19 21:56:10 +02:00
if (monitor.Alerts != null)
{
monitor.Alerts.ForEach(item => alerts.Add(item.ID));
2013-08-19 21:56:10 +02:00
}
MonitorParameters parameters = new MonitorParameters()
{
Name = monitor.Name,
Uri = monitor.UriString != null ? monitor.UriString : null,
Port = monitor.Port,
HTTPPassword = monitor.HTTPPassword,
HTTPUsername = monitor.HTTPUsername,
KeywordType = monitor.KeywordType,
KeywordValue = monitor.KeywordValue,
Subtype = monitor.Subtype,
Alerts = alerts != null ? alerts.ToArray() : null
};
List<Parameter> paramList = parameters.Convert();
// fix bad behaviour in API if no subtype is submitted
if(parameters.Subtype == Subtype.Unknown)
{
paramList.Add(Parameter("monitorSubType", 0));
2013-08-19 21:56:10 +02:00
}
paramList.Add(Parameter("monitorID", monitor.ID));
2013-08-19 21:56:10 +02:00
return Get<DefaultResponse>("editMonitor", paramList).Status;
}
}
}