diff --git a/e.github/FUNDING.yml b/.github/FUNDING.yml similarity index 100% rename from e.github/FUNDING.yml rename to .github/FUNDING.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..6310a86 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,86 @@ +on: push +name: CI +jobs: + npm-tests: + env: + CHROME_BIN: /usr/bin/chromium + runs-on: [self-hosted] + strategy: + matrix: + node: ['14', '16', '18'] + name: Node Tests + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v2 + with: + node-version: ${{matrix.node}} + - run: npm install + - name: test + continue-on-error: true + id: test + run: npm test + - name: retry + continue-on-error: true + id: retry1 + if: steps.test.outcome=='failure' + run: npm test + - name: set the status + if: always() + run: | + if ${{ steps.test.outcome=='success' || steps.retry1.outcome=='success' }}; then + echo "Tests Passed" + else + exit 1 + fi + docker-tests: + runs-on: [self-hosted] + timeout-minutes: 5 + steps: + - name: Checkout Code + uses: actions/checkout@v3 + - name: Docker test + run: | + wget https://github.com/tkp1n/chromium-ci/raw/41510dc154c4184f7e09461ba76f86f61c460070/seccomp/chromium.json + docker build -t prom-act-test . + container_name=$(docker run --security-opt seccomp=chromium.json --detach prom-act-test) + echo $container_name + docker inspect $container_name + docker ps --no-trunc + until [ "`docker inspect -f {{.State.Health.Status}} $container_name`" == "healthy" ]; do + sleep 10; + echo "Waiting for container to be healthy" + docker inspect -f {{.State.Health}} $container_name + docker logs $container_name + done; + docker inspect $container_name + sleep 5 + # Show usage + docker exec $container_name wget -q -O- localhost:3000/metrics | grep act_fup + # Check logs + docker logs $container_name + # Stop and kill the test container + docker stop $container_name && docker rm $container_name && docker rmi prom-act-test + publish: + needs: [npm-tests, docker-tests] + runs-on: [self-hosted] + steps: + - name: Checkout Code + uses: actions/checkout@v3 + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Push to GitHub Container Registry + uses: docker/build-push-action@v2 + if: ${{github.ref_name != 'master'}} + with: + push: true + tags: ghcr.io/${{ github.repository }}:${{github.ref_name}} + - name: Push to GitHub Container Registry (latest) + uses: docker/build-push-action@v2 + if: ${{github.ref_name == 'master'}} + with: + push: true + tags: ghcr.io/${{ github.repository }}:latest diff --git a/.gitignore b/.gitignore index e47366d..f289bad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -screenshots/ \ No newline at end of file +screenshots/ +chromium.json diff --git a/Dockerfile b/Dockerfile index 9f8655a..8889a66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,43 @@ -FROM schliflo/docker-puppeteer:17.0.0 +FROM alpine LABEL maintainer "Nemo " +RUN apk add --no-cache \ + chromium \ + nss \ + freetype \ + harfbuzz \ + ca-certificates \ + ttf-freefont \ + nodejs \ + npm + +# Tell Puppeteer to skip installing Chrome. We'll be using the installed package. +ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \ + PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \ + CHROME_BIN=/usr/bin/chromium-browser + +# Add user so we don't need --no-sandbox. +RUN addgroup -S pptruser && adduser -S -G pptruser pptruser \ + && addgroup pptruser audio \ + && addgroup pptruser video \ + && mkdir -p /home/pptruser/Downloads /app \ + && chown -R pptruser:pptruser /home/pptruser \ + && chown -R pptruser:pptruser /app + +USER pptruser + WORKDIR /app -COPY package.json package-lock.json /app/ +COPY --chown=pptruser package.json package-lock.json /app/ RUN npm install -COPY index.js server.js prom.js *.md /app/ +COPY --chown=pptruser index.js server.js prom.js *.md /app/ -ENTRYPOINT ["/usr/local/bin/node", "server.js"] +ENTRYPOINT ["/usr/bin/node", "server.js"] EXPOSE 3000 + +HEALTHCHECK --interval=1m --timeout=5s \ + CMD wget -q http://localhost:3000/ || exit 1 diff --git a/README.md b/README.md index ad95247..acc1cb5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # prometheus-act-exporter -![Docker Image Version (latest semver)](https://img.shields.io/docker/v/captn3m0/prometheus-act-exporter) ![Docker Image Size (latest semver)](https://img.shields.io/docker/image-size/captn3m0/prometheus-act-exporter) [![npm version](https://badge.fury.io/js/prometheus-act-exporter.svg)](https://badge.fury.io/js/prometheus-act-exporter) [![License: WTFPL](https://img.shields.io/badge/License-WTFPL-blue.svg)](http://www.wtfpl.net/) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](http://makeapullrequest.com) +![Docker Image Version (latest semver)](https://img.shields.io/docker/v/captn3m0/prometheus-act-exporter) ![Docker Image Size (latest semver)](https://img.shields.io/docker/image-size/captn3m0/prometheus-act-exporter) [![npm version](https://badge.fury.io/js/prometheus-act-exporter.svg)](https://badge.fury.io/js/prometheus-act-exporter) [![License: WTFPL](https://img.shields.io/badge/License-WTFPL-blue.svg)](http://www.wtfpl.net/) [![CI](https://github.com/captn3m0/prometheus-act-exporter/actions/workflows/ci.yml/badge.svg)](https://github.com/captn3m0/prometheus-act-exporter/actions/workflows/ci.yml) Exposes your current ACT FUP usage as prometheus metrics. Scrapes the data from the ACT Portal website by using [puppeteer](https://developers.google.com/web/tools/puppeteer/). This only supports [ACT Fibernet](https://www.actcorp.in/) in India. @@ -42,17 +42,7 @@ act_fup_aggregate_total_bytes 900000000 Install it with `npm i prometheus-act-exporter`. -```js -const act = require("prometheus-act-exporter"); -let m = await act.getUsage(); -// Returns -// { -// live: { usedBytes: 0, totalBytes: 800000000 }, -// flexibytes: { usedBytes: 102580000, totalBytes: 100000000 }, -// aggregate: { usedBytes: 102580000, totalBytes: 900000000 } -// } -// calculations made assuming ACT is using SI GB (exactly 1 billion bytes) -``` +See `test.js` for sample usage. # Configuration @@ -68,20 +58,14 @@ You can pass the following environment variables: If running via Docker, here are some simple cookbook configurations: -`docker run -it -p 3000:3000 -e captn3m0/prometheus-act-exporter` - -Run a simple test server locally in debug mode and test it on `http://localhost:3000/metrics` - -## Node - -```sh -export DISABLE_HEADLESS=1 -# Change to the correct invocation -export CHROME_BIN=$(which chromium) -npm install -node server.js -curl localhost:3000/metrics ``` +wget https://github.com/tkp1n/chromium-ci/raw/41510dc154c4184f7e09461ba76f86f61c460070/seccomp/chromium.json +docker run --security-opt seccomp=chromium.json -it -p 3000:3000 -e captn3m0/prometheus-act-exporter +``` + +Note that the above uses a [minimal secure seccomp profile for running Chromium inside Docker](https://github.com/docker/for-linux/issues/496#issuecomment-441149510). + +**Warning**: You should [not be running](https://ndportmann.com/chrome-in-docker/) this using `--privileged` or `--cap-add=SYS_ADMIN`, or `--no-sandbox`. # LICENSE diff --git a/index.js b/index.js index 7179675..70bf392 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ const puppeteer = require("puppeteer-core"); -const containerized = require("containerized"); const MY_PACKAGE_SELECTOR_ID = 'table[style="margin-top:-10px;"] tr:first-child+tr'; @@ -28,12 +27,15 @@ async function getUsage() { }; try { - await page.goto(MY_ACCOUNT_URL, {timeout: 5000}); + await page.goto(MY_ACCOUNT_URL, {timeout: 10000}); + await page.waitForTimeout(5000); + await page.waitForSelector(MY_PACKAGE_SELECTOR_ID) await page.click(MY_PACKAGE_SELECTOR_ID); // Wait for the page to switch - await page.waitForFunction( - 'document.querySelector(".dtl-header-text").innerText === "My Package"' - ); + await page.waitForFunction(()=>{ + return document.querySelector(".dtl-header-text").innerText === "My Package" && + document.getElementById('_ACTMyAccount_WAR_ACTMyAccountportlet_:processingPanel').style.display === 'none' + }); dataUsage = await page.evaluate(sel => { let elements = document.getElementsByClassName(sel); @@ -71,6 +73,7 @@ async function getUsage() { metrics = dataUsage; return metrics } catch (e) { + console.log(e) throw new Error("Failed scraping data from ACT"); } finally { page.close(); @@ -78,10 +81,7 @@ async function getUsage() { } function chromeLaunchConfig() { - let defaultArgs = []; - if (containerized()) { - defaultArgs = ["--no-sandbox", "--disable-setuid-sandbox"]; - } + let defaultArgs = ['--disable-dev-shm-usage']; var options = { // These are set for Docker usage // https://github.com/alekzonder/docker-puppeteer#before-usage @@ -106,12 +106,11 @@ function chromeLaunchConfig() { return options; } -// Async IIFE FTW -(async () => { - browser = await puppeteer.launch(chromeLaunchConfig()); - console.log("Browser Initialized"); -})(); - module.exports = { - getUsage: getUsage + getUsage: getUsage, + onReady: async (cb)=>{ + browser = await puppeteer.launch(chromeLaunchConfig()); + console.log("Browser Initialized"); + cb(browser) + } }; diff --git a/package-lock.json b/package-lock.json index d9f722b..a124c04 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,21 +9,20 @@ "version": "3.1.2", "license": "WTFPL", "dependencies": { - "containerized": "^1.0.2", "prom-client": "^14.0.1", "puppeteer-core": "^17.1.1" } }, "node_modules/@types/node": { - "version": "17.0.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.0.tgz", - "integrity": "sha512-eMhwJXc931Ihh4tkU+Y7GiLzT/y/DBNpNtr4yU9O2w3SYBsr9NaOPhQlLKRmoWtI54uNwuo0IOUFQjVOTZYRvw==", + "version": "18.0.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.0.tgz", + "integrity": "sha512-cHlGmko4gWLVI27cGJntjs/Sj8th9aYwplmZFwmmgYQQvL5NUsgVJG7OddLvNfLqYS31KFN0s3qlaD9qCaxACA==", "optional": true }, "node_modules/@types/yauzl": { - "version": "2.9.2", - "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz", - "integrity": "sha512-8uALY5LTvSuHgloDVUvWP3pIauILm+8/0pDMokuDYIoNsOkSwd5AiHBTSEJjKTDcZr5z8UpgOWZkxBF4iJftoA==", + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.10.0.tgz", + "integrity": "sha512-Cn6WYCm0tXv8p6k+A8PvbDG763EDpBoTzHdA+Q/MF6H3sapGjCm9NzoaJncJS9tUKSuCoDs9XHxYYsQDgxR6kw==", "optional": true, "dependencies": { "@types/node": "*" @@ -65,9 +64,9 @@ ] }, "node_modules/bintrees": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.1.tgz", - "integrity": "sha1-DmVcm5wkNeqraL9AJyJtK1WjRSQ=" + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz", + "integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==" }, "node_modules/bl": { "version": "4.1.0", @@ -114,7 +113,7 @@ "node_modules/buffer-crc32": { "version": "0.2.13", "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", - "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=", + "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", "engines": { "node": "*" } @@ -127,12 +126,7 @@ "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" - }, - "node_modules/containerized": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/containerized/-/containerized-1.0.2.tgz", - "integrity": "sha1-3+0xSeq1BC7wsG6FHH2PTcZ1DYg=" + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, "node_modules/cross-fetch": { "version": "3.1.5", @@ -193,7 +187,7 @@ "node_modules/fd-slicer": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", - "integrity": "sha1-JcfInLH5B3+IkbvmHY85Dq4lbx4=", + "integrity": "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==", "dependencies": { "pend": "~1.2.0" } @@ -206,7 +200,7 @@ "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==" }, "node_modules/get-stream": { "version": "5.2.0", @@ -223,14 +217,14 @@ } }, "node_modules/glob": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", - "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", "dependencies": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", "inherits": "2", - "minimatch": "^3.0.4", + "minimatch": "^3.1.1", "once": "^1.3.0", "path-is-absolute": "^1.0.0" }, @@ -275,7 +269,7 @@ "node_modules/inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", - "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", "dependencies": { "once": "^1.3.0", "wrappy": "1" @@ -287,9 +281,9 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "node_modules/minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", "dependencies": { "brace-expansion": "^1.1.7" }, @@ -329,7 +323,7 @@ "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", "dependencies": { "wrappy": "1" } @@ -337,7 +331,7 @@ "node_modules/path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", "engines": { "node": ">=0.10.0" } @@ -345,7 +339,7 @@ "node_modules/pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", - "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=" + "integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==" }, "node_modules/progress": { "version": "2.0.3", @@ -482,22 +476,22 @@ } }, "node_modules/tdigest": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.1.tgz", - "integrity": "sha1-Ljyyw56kSeVdHmzZEReszKRYgCE=", + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz", + "integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==", "dependencies": { - "bintrees": "1.0.1" + "bintrees": "1.0.2" } }, "node_modules/through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", - "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==" }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, "node_modules/unbzip2-stream": { "version": "1.4.3", @@ -511,17 +505,17 @@ "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "node_modules/whatwg-url": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", "dependencies": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" @@ -530,7 +524,7 @@ "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "node_modules/ws": { "version": "8.8.1", @@ -555,7 +549,7 @@ "node_modules/yauzl": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", - "integrity": "sha1-x+sXyT4RLLEIb6bY5R+wZnt5pfk=", + "integrity": "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==", "dependencies": { "buffer-crc32": "~0.2.3", "fd-slicer": "~1.1.0" @@ -564,15 +558,15 @@ }, "dependencies": { "@types/node": { - "version": "17.0.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.0.tgz", - "integrity": "sha512-eMhwJXc931Ihh4tkU+Y7GiLzT/y/DBNpNtr4yU9O2w3SYBsr9NaOPhQlLKRmoWtI54uNwuo0IOUFQjVOTZYRvw==", + "version": "18.0.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.0.tgz", + "integrity": "sha512-cHlGmko4gWLVI27cGJntjs/Sj8th9aYwplmZFwmmgYQQvL5NUsgVJG7OddLvNfLqYS31KFN0s3qlaD9qCaxACA==", "optional": true }, "@types/yauzl": { - "version": "2.9.2", - "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz", - "integrity": "sha512-8uALY5LTvSuHgloDVUvWP3pIauILm+8/0pDMokuDYIoNsOkSwd5AiHBTSEJjKTDcZr5z8UpgOWZkxBF4iJftoA==", + "version": "2.10.0", + "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.10.0.tgz", + "integrity": "sha512-Cn6WYCm0tXv8p6k+A8PvbDG763EDpBoTzHdA+Q/MF6H3sapGjCm9NzoaJncJS9tUKSuCoDs9XHxYYsQDgxR6kw==", "optional": true, "requires": { "@types/node": "*" @@ -597,9 +591,9 @@ "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==" }, "bintrees": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.1.tgz", - "integrity": "sha1-DmVcm5wkNeqraL9AJyJtK1WjRSQ=" + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.2.tgz", + "integrity": "sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==" }, "bl": { "version": "4.1.0", @@ -632,7 +626,7 @@ "buffer-crc32": { "version": "0.2.13", "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", - "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=" + "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==" }, "chownr": { "version": "1.1.4", @@ -642,12 +636,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" - }, - "containerized": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/containerized/-/containerized-1.0.2.tgz", - "integrity": "sha1-3+0xSeq1BC7wsG6FHH2PTcZ1DYg=" + "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==" }, "cross-fetch": { "version": "3.1.5", @@ -692,7 +681,7 @@ "fd-slicer": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", - "integrity": "sha1-JcfInLH5B3+IkbvmHY85Dq4lbx4=", + "integrity": "sha512-cE1qsB/VwyQozZ+q1dGxR8LBYNZeofhEdUNGSMbQD3Gw2lAzX9Zb3uIU6Ebc/Fmyjo9AWWfnn0AUCHqtevs/8g==", "requires": { "pend": "~1.2.0" } @@ -705,7 +694,7 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" + "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==" }, "get-stream": { "version": "5.2.0", @@ -716,14 +705,14 @@ } }, "glob": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", - "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", + "version": "7.2.3", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", + "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", "inherits": "2", - "minimatch": "^3.0.4", + "minimatch": "^3.1.1", "once": "^1.3.0", "path-is-absolute": "^1.0.0" } @@ -745,7 +734,7 @@ "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", - "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", "requires": { "once": "^1.3.0", "wrappy": "1" @@ -757,9 +746,9 @@ "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "minimatch": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", - "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", "requires": { "brace-expansion": "^1.1.7" } @@ -785,7 +774,7 @@ "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", "requires": { "wrappy": "1" } @@ -793,12 +782,12 @@ "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" + "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==" }, "pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", - "integrity": "sha1-elfrVQpng/kRUzH89GY9XI4AelA=" + "integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==" }, "progress": { "version": "2.0.3", @@ -900,22 +889,22 @@ } }, "tdigest": { - "version": "0.1.1", - "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.1.tgz", - "integrity": "sha1-Ljyyw56kSeVdHmzZEReszKRYgCE=", + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.2.tgz", + "integrity": "sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==", "requires": { - "bintrees": "1.0.1" + "bintrees": "1.0.2" } }, "through": { "version": "2.3.8", "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", - "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==" }, "tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, "unbzip2-stream": { "version": "1.4.3", @@ -929,17 +918,17 @@ "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, "webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "whatwg-url": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", + "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", "requires": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" @@ -948,7 +937,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "ws": { "version": "8.8.1", @@ -959,7 +948,7 @@ "yauzl": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/yauzl/-/yauzl-2.10.0.tgz", - "integrity": "sha1-x+sXyT4RLLEIb6bY5R+wZnt5pfk=", + "integrity": "sha512-p4a9I6X6nu6IhoGmBqAcbJy1mlC4j27vEPZX9F4L4/vZT3Lyq1VkFHw/V/PUcB9Buo+DG3iHkT0x3Qya58zc3g==", "requires": { "buffer-crc32": "~0.2.3", "fd-slicer": "~1.1.0" diff --git a/package.json b/package.json index af92922..98c7714 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Exports ACT Fibernet data usage as prometheus metrics", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node test.js" }, "keywords": [ "actcorp", @@ -14,15 +14,14 @@ ], "repository": { "type": "git", - "url": "git+ssh://git@git.captnemo.in/nemo/prometheus-act-exporter.git" + "url": "git+ssh://github.com/captn3m0/prometheus-act-exporter.git" }, "bugs": { - "url": "https://git.captnemo.in/nemo/prometheus-act-exporter/issues" + "url": "https://github.com/captn3m0/prometheus-act-exporter/issues" }, "author": "Nemo ", "license": "WTFPL", "dependencies": { - "containerized": "^1.0.2", "prom-client": "^14.0.1", "puppeteer-core": "^17.1.1" } diff --git a/server.js b/server.js index bee0381..33c7659 100644 --- a/server.js +++ b/server.js @@ -3,32 +3,15 @@ const port = 3000; const metrics = require("./index"); const promFormatter = require("./prom"); -CACHE = {}; +CACHE = null; const requestHandler = async (req, res) => { - let date = new Date(Date.now()).toLocaleString(); - console.log(`${date}: ${req.url}`); switch (req.url) { case "/metrics": res.setHeader("Content-Type", promFormatter.contentType); - metrics.getUsage().then( - (data) => { - console.log(data); - console.log("Setting cache"); - CACHE = data; - promFormatter.format(data).then((data) => { - res.end(data); - }); - }, - (err) => { - console.log(err); - console.log("Got error, using cache"); - console.log(CACHE); - promFormatter.format(CACHE).then((data) => { - res.end(data); - }); - } - ); + promFormatter.format(CACHE).then((data) => { + res.end(data); + }); break; default: res.writeHead(302, { @@ -41,10 +24,29 @@ const requestHandler = async (req, res) => { const server = http.createServer(requestHandler); -server.listen(port, (err) => { - if (err) { - return console.log("could not initialize web server", err); - } +metrics.onReady((browser) => { + let t; + (function refreshCache() { + metrics.getUsage().then((data) => { + let date = new Date(Date.now()).toLocaleString(); + console.log(`${date}: Updated Cache`); + // Start server now if this is the first run + if (!CACHE) { + server.listen(port, (err) => { + if (err) { + return console.log("could not initialize web server", err); + } + console.log(`server is listening on ${port}`); + }); + } + CACHE = data; + }).finally(()=>{ + t = setTimeout(refreshCache, 15 * 60 * 1000); + }); + })(); - console.log(`server is listening on ${port}`); + process.on("exit", function () { + browser.close(); + clearTimeout(t) + }); }); diff --git a/test.js b/test.js new file mode 100644 index 0000000..01e7920 --- /dev/null +++ b/test.js @@ -0,0 +1,12 @@ +const act = require("./index"); +const assert = require('assert') +act.onReady(async (browser)=>{ + metrics = await act.getUsage(); + console.log(metrics); + for (i of ['live', 'aggregate', 'flexibytes']) { + console.log(i) + assert.ok(typeof metrics[i]['usedBytes'] === 'number') + assert.ok(typeof metrics[i]['totalBytes'] === 'number') + } + browser.close(); +});