Skip to content

Commit

Permalink
Add badges for Uptime Robot (#947)
Browse files Browse the repository at this point in the history
* numberOfDays optional (default 30) for Uptime Robot ratio
* Require a monitor key, not a user key
* Update gitignore for Jetbrains
  • Loading branch information
crazy-max authored and paulmelnikow committed Apr 20, 2017
1 parent c5bd4a4 commit a079b68
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Thumbs.db
.Spotlight-V100
.Trashes

# Jetbrains
/.idea

# Created by https://www.gitignore.io/api/node

### Node ###
Expand Down
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,18 @@ <h3 id="miscellaneous"> Miscellaneous </h3>
<td><img src='https://img.shields.io/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg?maxAge=2592000' alt=''/></td>
<td><code>https://img.shields.io/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg</code></td>
</tr>
<tr><th> Uptime Robot status: </th>
<td><img src='https://img.shields.io/uptimerobot/status/m778918918-3e92c097147760ee39d02d36.svg?maxAge=2592000' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/status/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
<tr><th> Uptime Robot ratio: </th>
<td><img src='https://img.shields.io/uptimerobot/ratio/m778918918-3e92c097147760ee39d02d36.svg?maxAge=2592000' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/ratio/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
<tr><th> Uptime Robot ratio (7 days): </th>
<td><img src='https://img.shields.io/uptimerobot/ratio/7/m778918918-3e92c097147760ee39d02d36.svg?maxAge=2592000' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/ratio/7/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
</tbody></table>

<h3 id="miscellaneous"> Longer Miscellaneous </h3>
Expand Down
132 changes: 132 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6089,6 +6089,138 @@ cache(function(data, match, sendBadge, request) {
});
}));

// Uptime Robot status integration.
// API documentation : https://uptimerobot.com/api
camp.route(/^\/uptimerobot\/status\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var monitorApiKey = match[1]; // eg, m778918918-3e92c097147760ee39d02d36
var format = match[2];
var badgeData = getBadgeData('status', data);
var options = {
method: 'POST',
json: true,
body: {
"api_key": monitorApiKey,
"format": "json"
},
uri: 'https://api.uptimerobot.com/v2/getMonitors'
};
// A monitor API key must start with "m"
if (monitorApiKey.substring(0, "m".length) !== "m") {
badgeData.text[1] = 'must use a monitor key';
sendBadge(format, badgeData);
return;
}
request(options, function(err, res, json) {
if (err !== null || res.statusCode >= 500 || typeof json !== 'object') {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
if (json.stat === 'fail') {
badgeData.text[1] = 'vendor error';
if (json.error && typeof json.error.message === 'string') {
badgeData.text[1] = json.error.message;
}
badgeData.colorscheme = 'lightgrey';
sendBadge(format, badgeData);
return;
}
var status = json.monitors[0].status;
if (status === 0) {
badgeData.text[1] = 'paused';
badgeData.colorscheme = 'yellow';
} else if (status === 1) {
badgeData.text[1] = 'not checked yet';
badgeData.colorscheme = 'yellowgreen';
} else if (status === 2) {
badgeData.text[1] = 'up';
badgeData.colorscheme = 'brightgreen';
} else if (status === 8) {
badgeData.text[1] = 'seems down';
badgeData.colorscheme = 'orange';
} else if (status === 9) {
badgeData.text[1] = 'down';
badgeData.colorscheme = 'red';
} else {
badgeData.text[1] = 'invalid';
badgeData.colorscheme = 'lightgrey';
}
sendBadge(format, badgeData);
} catch(e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// Uptime Robot ratio integration.
// API documentation : https://uptimerobot.com/api
camp.route(/^\/uptimerobot\/ratio(\/[^\/]+)?\/(.*)\.(svg|png|gif|jpg|json)$/,
cache(function(data, match, sendBadge, request) {
var numberOfDays = match[1]; // eg, 7, null if querying 30
var monitorApiKey = match[2]; // eg, m778918918-3e92c097147760ee39d02d36
var format = match[3];
var badgeData = getBadgeData('uptime', data);
if (numberOfDays) {
numberOfDays = numberOfDays.slice(1);
} else {
numberOfDays = '30';
}
var options = {
method: 'POST',
json: true,
body: {
"api_key": monitorApiKey,
"custom_uptime_ratios": numberOfDays,
"format": "json"
},
uri: 'https://api.uptimerobot.com/v2/getMonitors'
};
// A monitor API key must start with "m"
if (monitorApiKey.substring(0, "m".length) !== "m") {
badgeData.text[1] = 'must use a monitor key';
sendBadge(format, badgeData);
return;
}
request(options, function(err, res, json) {
if (err !== null) {
badgeData.text[1] = 'inaccessible';
sendBadge(format, badgeData);
return;
}
try {
if (json.stat === 'fail') {
badgeData.text[1] = 'vendor error';
if (json.error && typeof json.error.message === 'string') {
badgeData.text[1] = json.error.message;
}
badgeData.colorscheme = 'lightgrey';
sendBadge(format, badgeData);
return;
}
var percent = parseFloat(json.monitors[0].custom_uptime_ratio);
badgeData.text[1] = percent + '%';
if (percent <= 10) {
badgeData.colorscheme = 'red';
} else if (percent <= 30) {
badgeData.colorscheme = 'yellow';
} else if (percent <= 50) {
badgeData.colorscheme = 'yellowgreen';
} else if (percent <= 70) {
badgeData.colorscheme = 'green';
} else {
badgeData.colorscheme = 'brightgreen';
}
sendBadge(format, badgeData);
} catch (e) {
badgeData.text[1] = 'invalid';
sendBadge(format, badgeData);
}
});
}));

// Any badge.
camp.route(/^\/(:|badge\/)(([^-]|--)*?)-(([^-]|--)*)-(([^-]|--)+)\.(svg|png|gif|jpg)$/,
function(data, match, end, ask) {
Expand Down
12 changes: 12 additions & 0 deletions try.html
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,18 @@ <h3 id="miscellaneous"> Miscellaneous </h3>
<td><img src='/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg' alt=''/></td>
<td><code>https://img.shields.io/swagger/valid/2.0/https/bitbucket.org/api/swagger.json.svg</code></td>
</tr>
<tr><th> Uptime Robot status: </th>
<td><img src='/uptimerobot/status/m778918918-3e92c097147760ee39d02d36.svg' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/status/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
<tr><th> Uptime Robot ratio: </th>
<td><img src='/uptimerobot/ratio/m778918918-3e92c097147760ee39d02d36.svg' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/ratio/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
<tr><th> Uptime Robot ratio (7 days): </th>
<td><img src='/uptimerobot/ratio/7/m778918918-3e92c097147760ee39d02d36.svg' alt=''/></td>
<td><code>https://img.shields.io/uptimerobot/ratio/7/m778918918-3e92c097147760ee39d02d36.svg</code></td>
</tr>
</tbody></table>

<h3 id="miscellaneous"> Longer Miscellaneous </h3>
Expand Down

0 comments on commit a079b68

Please sign in to comment.