prometheus-act-exporter/index.js

77 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-06-02 20:37:29 +00:00
const puppeteer = require('puppeteer');
const containerized = require('containerized');
2018-06-02 20:37:29 +00:00
2018-06-04 05:36:02 +00:00
const MY_PACKAGE_SELECTOR_ID =
'table[style="margin-top:-10px;"] tr:first-child+tr';
const DATA_SELECTOR = 'packagecol3';
2018-06-02 20:37:29 +00:00
2018-06-04 06:16:11 +00:00
const DATA_USAGE_REGEX = /\d+\.\d{0,2}/g;
var browser;
2018-06-02 20:37:29 +00:00
2018-06-03 18:29:52 +00:00
async function getUsage() {
2018-06-04 05:50:24 +00:00
let metrics = {
used: null,
total: null,
2018-06-03 18:29:52 +00:00
};
2018-06-04 05:50:24 +00:00
2018-06-04 06:16:11 +00:00
const page = await browser.newPage();
2018-06-04 05:50:24 +00:00
try {
await page.goto('http://portal.actcorp.in/group/blr/myaccount');
await page.click(MY_PACKAGE_SELECTOR_ID);
await page.waitFor(3000),
(text = await page.evaluate(sel => {
return document.getElementsByClassName(sel)[3].innerText;
}, DATA_SELECTOR));
2018-06-04 06:16:11 +00:00
[metrics.used, metrics.total] = text
.match(DATA_USAGE_REGEX)
.map(x => parseFloat(x));
2018-06-04 05:50:24 +00:00
} catch (e) {
2018-06-04 06:16:11 +00:00
console.log("Couldn't scrape ACT page, faced an error");
console.log(e);
return null;
2018-06-04 05:50:24 +00:00
} finally {
page.close();
return metrics;
}
2018-06-02 20:37:29 +00:00
}
2018-06-04 06:16:11 +00:00
function chromeLaunchConfig() {
let defaultArgs = [];
if (containerized()) {
defaultArgs = ['--no-sandbox', '--disable-setuid-sandbox'];
}
2018-06-04 06:16:11 +00:00
var options = {
// These are set for Docker usage
// https://github.com/alekzonder/docker-puppeteer#before-usage
args: defaultArgs.concat(
2018-06-04 06:16:11 +00:00
process.env.hasOwnProperty('PROXY_SERVER')
? [`--proxy-server=${process.env['PROXY_SERVER']}`]
: []
),
2018-06-04 05:50:24 +00:00
};
2018-06-04 06:16:11 +00:00
if (process.env.hasOwnProperty('DISABLE_HEADLESS')) {
options.headless = false;
}
if (process.env.hasOwnProperty('CHROME_BIN')) {
options.executablePath = process.env['CHROME_BIN'];
}
console.log('Launching Chrome with args:');
console.log(options);
return options;
}
// Async IIFE FTW
(async () => {
browser = await puppeteer.launch(chromeLaunchConfig());
console.log('Browser Initialized');
2018-06-04 05:50:24 +00:00
})();
2018-06-04 06:16:11 +00:00
module.exports = {
getUsage: getUsage,
};