I recently started trying to create a setup that can monitor CPU and RAM usage on a remote server. After quite a bit of thinking I created a SQL database and shell script that saved outputted data into the DB. This wasn’t ideal for remote servers so I created a poor man’s API with Node & Express. It takes a web call from remote systems running a shell script on a schedule. It then saves it to a table and I’m using grafana to visualize the data. Its in a super early state now with no validation but it is functional. Here’s my code for Node:
var express = require('express'); var router = express.Router(); var bodyParser = require('body-parser'); var mysql = require('mysql'); var con = mysql.createConnection({ host:"localhost", user:"perfmon", password:"passwordhere", database:"perfmon" }); con.connect(function (err) { if (err) throwerr; console.log("Connected to SQL database"); }); /* GET home page. */ router.get('/', function (req, res, next) { varserverName=req.query.serverName; varCPU=req.query.CPU; varRAM=req.query.RAM; //sqlInsertPerfmon = "INSERT INTO serverbuilddata (ServerName, CPUCount, RAMCount) VALUES ('" + serverName + "'," + CPUCount + "," + serverMemory + ');' sqlInsertPerfmon="INSERT INTO perfdata (serverName, CPU, RAM) VALUES ('"+serverName+"',"+CPU+","+RAM+");" console.log('SQL Statement: '+sqlInsertPerfmon); con.query(sqlInsertPerfmon, function (err, result) { if (err) throw(err); console.log("Performance Data Logged"); res.render('perfmon', { title:'Perfmon', serverName:serverName, CPU:CPU, RAM:RAM }); }); }); module.exports = router;