diff --git a/README.md b/README.md index ae63bc9..de5773b 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,238 @@ # UptimeSharp -> Under development - don't use yet! +**UptimeSharp** is a C#.NET class library that integrates the [UptimeRobot API](http://http://www.uptimerobot.com/api.asp). -**UptimeSharp** is a C#.NET class library, that integrates the [UptimeRobot API](http://http://www.uptimerobot.com/api.asp). +The wrapper consists of 2 parts: + +- Get and modify monitors +- Get and modify alert contacts + +[uptimesharp.frontendplay.com](http://uptimesharp.frontendplay.com/) (coming soon) + +## Install using NuGet + +``` +Install-Package UptimeSharp +``` + +[nuget.org/packages/UptimeSharp](https://www.nuget.org/packages/UptimeSharp/) (coming soon) + +## Usage Example + +Get your [API Key UptimeRobot](http://uptimerobot.com/mySettings.asp) (left section under "API Information") + +Include the UptimeSharp namespace and it's associated models: + +```csharp +using UptimeSharp; +using UptimeSharp.Models; +``` + +Initialize UptimeClient with: + +```csharp +UptimeClient _client = new UptimeClient("[YOUR_API_KEY]"); +``` + +Do a simple request - e.g. get all your monitors: + +```csharp +_client.GetMonitors().ForEach( + item => Console.WriteLine(item.Name + " | " + item.Type) +); +``` + +Which will output: + + frontendplay | HTTP + google | Keyword + localhost | Ping + ... + + +## Constructor + +```csharp +UptimeClient(string apiKey) +``` + +Get your [API Key UptimeRobot](http://uptimerobot.com/mySettings.asp) (left section under "API Information") + + +## Retrieve + +Get list of all monitors: + +```csharp +List items = _client.GetMonitors(); +``` + +Get monitors by ID - or a single monitor: + +```csharp +List items = _client.GetMonitors(new int[]{ 12891, 98711 }); +// or +Monitor item = _client.GetMonitor(12891); +``` + +Provide additional params for more data: + +```csharp +List items = _client.GetMonitors( + monitorIDs: new int[]{ 12891, 98711 }, + // the number of days to calculate the uptime ratio(s) for + customUptimeRatio: new float[] { 7, 30, 45 }, + // include log, if true (default: false) + showLog: true, + // include alerts, if true (default: true) + showAlerts = true +); +``` + +`monitorIDs`: You can remove this parameter if you want to retrieve all monitors _(default: null)_ +
+`customUptimeRatio`: the number of days to calculate the uptime ratio(s) for _(default: null)_ +
+`showLog`: include log, if true _(default: false)_ +
+`showAlerts`: include alerts, if true _(default: true)_ +
+ + +## Add + +Adds/creates a new monitor. + +```csharp +bool AddMonitor( + string name, + string uri, + Type type = Type.HTTP, + Subtype subtype = Subtype.Unknown, + int? port = null, + string keywordValue = null, + KeywordType keywordType = KeywordType.Unknown, + int[] alerts = null, + string HTTPUsername = null, + string HTTPPassword = null +) +``` + +Example - Watch the a SMTP Server: + +```csharp +bool isSuccess = _client.AddMonitor( + name: "cee", + uri: "127.0.0.1", + type: Type.Port, + subtype: Subtype.SMTP +); +``` + +`name`: A friendly name for the new monitor +
+`uri`: The URI or IP to watch +
+`type`: The type of the monitor (see [# Monitor Types](#monitor-types)) +
+`subtype`: The subtype of the monitor (only for Type.Port) (see [# Monitor Types](#monitor-types)) +
+`port`: The port (only for Subtype.Custom) +
+`keywordValue`: The keyword value (for Type.Keyword) +
+`keywordType`: Type of the keyword (for Type.Keyword) +
+`alerts`: An ID list of existing alerts to notify +
+`HTTPUsername`: The HTTP username +
+`HTTPPassword`: The HTTP password + +As you can see, a lot of these parameters are only available if you've specified the correct `Type`. +
+If you've selected `Type.Port` for example, UptimeSharp will ignore the `keywordValue` and `keywordType` parameters, even if you submitted valid ones. + + +## Delete + +Delete a monitor by ID: + +```csharp +bool isSuccess = _client.DeleteMonitor(12891); +``` + +Delete a monitor by a Monitor instance: + +```csharp +// Monitor myMonitor = ... +bool isSuccess = _client.DeleteMonitor(myMonitor); +``` + + +## Modify + +In order to modify an existing monitor, just alter the properties of the Monitor instance and call the `ModifyMonitor` method: + +```csharp +// Monitor myMonitor = ... +myMonitor.Name = "my new name :-)"; +bool isSuccess = _client.ModifyMonitor(myMonitor); +``` + +**Important:** It is not possible to alter the `Type` of a monitor after its creation! In case you want to do this, you have to delete the monitor and create a new one with the changed type. + +### Modify Alerts + +Retrieve all alerts: + +```csharp +List items = _client.GetAlerts(); +``` + +Retrieve alerts by IDs: + +```csharp +List items = _client.GetAlerts(new int[]{ 12897, 98711 }); +``` + +Retrieve a specific alert: + +```csharp +Alert item = _client.GetAlert(12897); +``` + +Adds an alert _(Due to UptimeRobot API limitations SMS and Twitter alert contact types are not supported yet)_: + +```csharp +bool isSuccess = _client.AddAlert(AlertType.Email, "uptimesharp@outlook.com"); +``` + +Adds an alert from instance: + +```csharp +// Alert myAlert = ... +bool isSuccess = _client.AddAlert(myAlert); +``` + +Deletes an alert: + +```csharp +bool isSuccess = _client.DeleteAlert(12897); +``` + +Deletes an alert from instance: + +```csharp +// Alert myAlert = ... +bool isSuccess = _client.DeleteAlert(myAlert); +``` --- ## Release History -- 2013-08-13 v0.1.0 initial +- 2013-08-25 v0.1.0 Monitor and Alert APIs ## Dependencies diff --git a/UptimeSharp/Components/Monitor.cs b/UptimeSharp/Components/Monitor.cs index 31c6d5b..cff9d41 100644 --- a/UptimeSharp/Components/Monitor.cs +++ b/UptimeSharp/Components/Monitor.cs @@ -20,11 +20,11 @@ namespace UptimeSharp /// /// Monitor List /// - public List GetMonitors(int[] monitors = null, float[] customUptimeRatio = null, bool showLog = false, bool showAlerts = true) + public List GetMonitors(int[] monitorIDs = null, float[] customUptimeRatio = null, bool showLog = false, bool showAlerts = true) { RetrieveParameters parameters = new RetrieveParameters() { - Monitors = monitors, + Monitors = monitorIDs, CustomUptimeRatio = customUptimeRatio, ShowAlerts = showAlerts, ShowLog = showLog @@ -84,19 +84,19 @@ namespace UptimeSharp /// The name of the new monitor. /// The URI or IP to watch. /// The type of the monitor. - /// The subtype of the port. + /// The subtype of the monitor (if port). /// The port (only for Subtype.Custom). /// The keyword value. /// Type of the keyword. - /// A ID list of existing alerts to notify. - /// The HTTP password. + /// An ID list of existing alerts to notify. /// The HTTP username. + /// The HTTP password. /// /// Success state /// 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, - int[] alerts = null, string HTTPPassword = null, string HTTPUsername = null) + int[] alerts = null, string HTTPUsername = null, string HTTPPassword = null) { MonitorParameters parameters = new MonitorParameters()