-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add shields for Uptime Robot #947
Changes from 2 commits
e55ceff
ce56400
204b290
a0e1afb
7a3c558
91f69ae
1bd8766
3ed5543
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6030,6 +6030,121 @@ 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, u956-afus321g565fghr519 | ||
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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you include a link to the API docs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course (https://uptimerobot.com/api) |
||
}; | ||
request(options, function(err, res, json) { | ||
if (err !== null || res.statusCode >= 500 || typeof json !== 'object') { | ||
badgeData.text[1] = 'inaccessible'; | ||
sendBadge(format, badgeData); | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a "monitor not found" branch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If an incorrect api key is entered for a monitor the following json is returned : {
"stat": "fail",
"error": {
"type": "invalid_parameter",
"parameter_name": "api_key",
"passed_value": "u956-afus321g565fghr519",
"message": "api_key not found."
}
} I can add this case if this is what you want with something like that : ...
if (json.stat === 'fail') {
badgeData.text[1] = 'unknown error';
if (json.error) {
badgeData.text[1] = json.error.message;
}
badgeData.colorscheme = 'lightgrey';
sendBadge(format, badgeData);
return;
}
var status = json.monitors[0].status;
if (status === 0) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding that. Looks great. Though, I think it would be better to check that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 91f69ae |
||
try { | ||
if (json.stat === 'fail') { | ||
badgeData.text[1] = 'unknown error'; | ||
if (json.error) { | ||
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)$/, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In testing this out, I found it was easy to miss the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you're right 1bd8766 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's still required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oups forgot the optional part... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 3ed5543 |
||
cache(function(data, match, sendBadge, request) { | ||
var monitorApiKey = match[1]; // eg, u956-afus321g565fghr519 | ||
var numberOfDays = match[2]; // eg, 30 | ||
var format = match[3]; | ||
var badgeData = getBadgeData('uptime', data); | ||
var options = { | ||
method: 'POST', | ||
json: true, | ||
body: { | ||
"api_key": monitorApiKey, | ||
"custom_uptime_ratios": numberOfDays, | ||
"format": "json" | ||
}, | ||
uri: 'https://api.uptimerobot.com/v2/getMonitors' | ||
}; | ||
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] = 'unknown error'; | ||
if (json.error) { | ||
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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This example looks like the format used for Uptime Robot "full API keys". You should instead use a "monitor-specific API key" that looks like
m775661887-da53aab04d8b7fe0ed74e653
(mxxxxx
rather thanuxxxxx
at the start) as monitor-specific API keys only allow read-only access.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Let's go a step further and reject the user keys, to reduce the chance that people share them by accident.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes indeed on the API documentation they don't provide a monitor api key example.
Btw i asked a question about the API rate limit and they said :
So i will include an example with one of my monitor