Statistics Export via POST: Difference between revisions

From Allegro Network Multimeter Manual
Jump to navigation Jump to search
Access restrictions were established for this page. If you see this message, you have no access to this page.
(make node.js example compatible with recent node.js versions)
mNo edit summary
 
Line 27: Line 27:
  const server = http.createServer((req, res) => {
  const server = http.createServer((req, res) => {
   
   
   if (req.methodr!= 'POST') {
   if (req.method != 'POST') {
         res.statusCode = 405;
         res.statusCode = 405;
         res.end();
         res.end();
Line 46: Line 46:
         let data = Buffer.concat(dataChunks);
         let data = Buffer.concat(dataChunks);
         fs.writeFile(outputFilename, data, (err) => {
         fs.writeFile(outputFilename, data, (err) => {
             if (errr!= null) {
             if (err != null) {
                   console.log(err);
                   console.log(err);
                   res.statusCode = 500;
                   res.statusCode = 500;

Latest revision as of 12:22, 25 May 2022

The Statistics Export allows querying the API of the Allegro Network Multimeter and transmit JSON results to a remote server via HTTP POST requests. The API path can be determined by using a browser debug console on any of the modules of the Allegro Network Multimeter.

The configuration can be found in the menu "Settings -> Remote access & export -> Statistics export".

Example:

This will generate a POST /interface-stats-for-mm-1234 request to 10.0.0.1 every minute and send the interface statistics of the Allegro Network Multimeter.

Example receiving server

To test the correct setup, a simple example server using Node.js can be used to receive the POST requests and show the transmitted data.

The following script is a basic Node.js server (with minimal error handling) which reads all POST requests and stores them to a file:

const http = require('http');
const fs = require('fs');

const listenAddress = '0.0.0.0';
const port = 3000;

let fileCount = 1;

const server = http.createServer((req, res) => {

  if (req.method != 'POST') {
        res.statusCode = 405;
        res.end();
        return;
  }

  let id = fileCount++;

  let outputFilename = `${req.url.substring(1).replace(/\//g, '_')}_${id}.json`;

  console.log(`${id}: ${req.method} ${req.url} => ${outputFilename}`);

  let dataChunks = [];

  req.on('data', chunk => dataChunks.push(chunk));

  req.on('end', () => {
        let data = Buffer.concat(dataChunks);
        fs.writeFile(outputFilename, data, (err) => {
            if (err != null) {
                 console.log(err);
                 res.statusCode = 500;
            } else {
                 res.statusCode = 200;
            }
            res.end();
        });
  });
});

server.listen(port, listenAddress, () => {
  console.log(`Server running at http://${listenAddress}:${port}/`);
});