Files
UptimeSharp/README.md
T

286 lines
6.8 KiB
Markdown
Raw Normal View History

![UptimeSharp](https://raw.github.com/ceee/UptimeSharp/master/Assets/github-header.png)
2013-09-05 22:00:46 +02:00
**UptimeSharp** is a .NET portable class library that integrates the [UptimeRobot API](http://www.uptimerobot.com/api.asp).
2013-08-13 22:22:46 +02:00
2014-04-07 11:51:52 +02:00
The wrapper consists of the following parts:
2013-08-25 22:27:20 +02:00
- Get and modify monitors
- Get and modify alert contacts
2014-04-07 11:51:52 +02:00
- Register accounts (unofficial)
2013-08-25 22:27:20 +02:00
## Install UptimeSharp using [NuGet](https://www.nuget.org/packages/UptimeSharp/)
2013-08-25 22:27:20 +02:00
```
Install-Package UptimeSharp
```
## Usage Example
2014-02-02 13:35:57 +01:00
Get your [API Key UptimeRobot](http://uptimerobot.com/dashboard#mySettings) (right section - "API Settings")
2013-08-25 22:27:20 +02:00
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
List<Monitor> monitors = await _client.GetMonitors()
monitors.ForEach(
item => Debug.WriteLine(item.Name + " | " + item.Type)
2013-08-25 22:27:20 +02:00
);
```
Which will output:
frontendplay | HTTP
google | Keyword
localhost | Ping
...
## Constructor
```csharp
UptimeClient(string apiKey)
```
2014-04-09 16:34:16 +02:00
Get your [API Key UptimeRobot](http://uptimerobot.com/dashboard#mySettings) (left section under "API Information").
<br>Automatic authentication is not available in the moment, but the integration in the API is planned according to UptimeRobot.
2013-08-25 22:27:20 +02:00
## Retrieve
Get list of all monitors:
```csharp
List<Monitor> items = await _client.GetMonitors();
2013-08-25 22:27:20 +02:00
```
Get monitors by ID - or a single monitor:
```csharp
2014-02-02 13:34:07 +01:00
List<Monitor> items = await _client.GetMonitors(new string[]{ "12891", "98711" });
2013-08-25 22:27:20 +02:00
// or
Monitor item = await _client.GetMonitor("12891");
2013-08-25 22:27:20 +02:00
```
Provide additional params for more data:
```csharp
List<Monitor> items = await _client.GetMonitors(
2014-02-02 13:32:07 +01:00
monitorIDs: new string[]{ "12891", "98711" },
2013-08-25 22:27:20 +02:00
customUptimeRatio: new float[] { 7, 30, 45 },
2014-02-28 15:12:40 +01:00
includeDetails: true
2013-08-25 22:27:20 +02:00
);
```
`monitorIDs`: You can remove this parameter if you want to retrieve all monitors _(default: null)_
<br>
`customUptimeRatio`: the number of days to calculate the uptime ratio(s) for _(default: null)_
<br>
2014-02-28 15:12:40 +01:00
`includeDetails`: include log, alerts and response times, if true _(default: false)_
2013-08-25 22:27:20 +02:00
<br>
## Add
Adds/creates a new monitor.
```csharp
Task<Monitor> AddMonitor(
2013-08-25 22:27:20 +02:00
string name,
string target,
2013-08-25 22:27:20 +02:00
Type type = Type.HTTP,
Subtype subtype = Subtype.Unknown,
int? port = null,
string keywordValue = null,
KeywordType keywordType = KeywordType.Unknown,
string[] alerts = null,
2013-08-25 22:27:20 +02:00
string HTTPUsername = null,
string HTTPPassword = null
)
```
2013-09-08 22:47:51 +02:00
Example - Watch a SMTP Server:
2013-08-25 22:27:20 +02:00
```csharp
Monitor monitor = await _client.AddMonitor(
2013-08-25 22:27:20 +02:00
name: "cee",
target: "127.0.0.1",
2013-08-25 22:27:20 +02:00
type: Type.Port,
subtype: Subtype.SMTP
);
```
`name`: A friendly name for the new monitor
<br>
`target`: The URI or IP to watch
2013-08-25 22:27:20 +02:00
<br>
`type`: The type of the monitor (see [# Monitor Types](#monitor-types))
<br>
`subtype`: The subtype of the monitor (only for Type.Port) (see [# Monitor Types](#monitor-types))
<br>
`port`: The port (only for Subtype.Custom)
<br>
`keywordValue`: The keyword value (for Type.Keyword)
<br>
`keywordType`: Type of the keyword (for Type.Keyword)
<br>
`alerts`: An ID list of existing alerts to notify
<br>
`HTTPUsername`: The HTTP username
<br>
`HTTPPassword`: The HTTP password
As you can see, a lot of these parameters are only available if you've specified the correct `Type`.
<br>
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 = await _client.DeleteMonitor("12891");
2013-08-25 22:27:20 +02:00
```
Delete a monitor by a Monitor instance:
```csharp
// Monitor myMonitor = ...
bool isSuccess = await _client.DeleteMonitor(myMonitor);
2013-08-25 22:27:20 +02:00
```
## 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 = await _client.ModifyMonitor(myMonitor);
2013-08-25 22:27:20 +02:00
```
**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<Alert> items = await _client.GetAlerts();
2013-08-25 22:27:20 +02:00
```
Retrieve alerts by IDs:
```csharp
List<Alert> items = await _client.GetAlerts(new string[]{ "12897", "98711" });
2013-08-25 22:27:20 +02:00
```
Retrieve a specific alert:
```csharp
Alert item = await _client.GetAlert("12897");
2013-08-25 22:27:20 +02:00
```
Adds an alert _(Due to UptimeRobot API limitations SMS and Twitter alert contact types are not supported yet)_:
```csharp
Alert alert = await _client.AddAlert(AlertType.Email, "uptimesharp@outlook.com");
2013-08-25 22:27:20 +02:00
```
Adds an alert from instance:
```csharp
// Alert myAlert = ...
Alert alert = await _client.AddAlert(myAlert);
2013-08-25 22:27:20 +02:00
```
Deletes an alert:
```csharp
bool isSuccess = await _client.DeleteAlert("12897");
2013-08-25 22:27:20 +02:00
```
Deletes an alert from instance:
```csharp
// Alert myAlert = ...
bool isSuccess = await _client.DeleteAlert(myAlert);
2013-08-25 22:27:20 +02:00
```
2013-08-13 22:22:46 +02:00
2013-09-08 22:47:51 +02:00
## Monitor Types
- [HTTP](#http-monitoring)
- [Keyword](#keyword)
- [Ping](#ping)
- [Port](#port)
### HTTP Monitoring
Simple HTTP monitor which requests the webpage every 5 minutes and checks for HTTP Status 200 OK.
### Keyword Monitoring
The keyword monitor is sniffing the page content if a specified keyword exists/not exists.
The keyword is submitted via the **keywordValue** parameter. The **keywordType** parameter specifies if the value should exist or not exist.
### Ping Monitoring
This type lets you monitor a server by pinging it.
### Port Monitoring
If you want to monitor a port, you need to specify a **subType** which is a common port a custom one:
- HTTP :80
- HTTPS :443
- FTP :21
- SMTP :25
- POP3 :110
- IMAP :143
- Custom Port (use the "port" parameter, if this option is selected)
2013-08-13 22:22:46 +02:00
---
## Supported platforms
2014-04-07 11:51:52 +02:00
UptimeSharp is a **Portable Class Library**, therefore it's compatible with multiple platforms and Universal Apps:
- **.NET** >= 4.5 (including WPF)
2014-04-07 11:51:52 +02:00
- **Windows Phone** (Silverlight + WinPRT) >= 8
- **Windows Store** >= 8
- **Xamarin** iOS + Android
2014-04-13 16:51:19 +02:00
- _WP7 and Silverlight are dropped in 4.0, use UptimeSharp < 3.0, if you want to support them_
2013-08-13 22:22:46 +02:00
## Dependencies
- [Microsoft.Bcl.Async](https://www.nuget.org/packages/Microsoft.Bcl.Async/)
- [Microsoft.Net.Http](https://www.nuget.org/packages/Microsoft.Net.Http/)
- [Newtonsoft.Json](https://www.nuget.org/packages/Newtonsoft.Json/)
- [PropertyChanged.Fody](https://github.com/Fody/PropertyChanged)
2013-08-13 22:22:46 +02:00
## Contributors
| [![ceee](http://gravatar.com/avatar/9c61b1f4307425f12f05d3adb930ba66?s=70)](https://github.com/ceee "Tobias Klika") |
|---|
| [ceee](https://github.com/ceee) |
2013-08-13 22:22:46 +02:00
## License
[MIT License](https://github.com/ceee/UptimeSharp/blob/master/LICENSE-MIT)