npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fast-monitor-js

v1.0.2

Published

A simple and flexible library for monitoring data in JavaScript.

Downloads

11

Readme

Monitor.JS

Monitor.JS is a flexible and lightweight JavaScript library designed for monitoring and tracking data in real-time.

Features

  • Real-time data monitoring
  • Customizable alert system
  • Performance analysis and metrics
  • Easy integration and setup

Installation

You can install Monitor.JS from this ways

via NPM:

npm install fast-monitor-js

YARN:

yarn add fast-monitor-js

Usage

Example 1

const MonitorJS = require('fast-monitor-js');

// Define an item to monitor
const itemToMonitor = {
  name: 'Temperature',
  setter: async () => {
    // Simulate fetching temperature data (replace this with your actual data retrieval logic)
    return Math.random() * 100; // Random temperature value for demonstration
  },
  interval: 3000, // Check every 3 seconds
  alerts: [], // No alerts defined in this example
};

// Create a MonitorJS instance with the item to monitor
const monitor = new MonitorJS([itemToMonitor]);

// Start monitoring
monitor.start();

// Access monitored data after a while
setTimeout(() => {
  const temperatureData = monitor.get('Temperature');
  console.log('Current temperature:', temperatureData.now);
}, 10000); // After 10 seconds

Example 2

const MonitorJS = require('fast-monitor-js');

// Define an item with an alert for high CPU usage
const cpuMonitorItem = {
  name: 'CPU Usage',
  setter: async () => {
    // Simulate fetching CPU usage (replace this with your actual data retrieval logic)
    return Math.random() * 100; // Random CPU usage value for demonstration
  },
  interval: 5000, // Check every 5 seconds
  alerts: [
    {
      name: 'High CPU Alert',
      condition: 'more', // 'more' indicates greater than a specified value
      value: 80, // Alert if CPU usage goes above 80%
      function: async (data, item, alert) => {
        console.log(`Alert: High CPU usage detected! Current usage: ${data.now}%`);
        // Custom alert action or modification to data/item/alert object can be performed here
      },
      times: [], // No specific times for this alert
    },
  ],
};

// Create a MonitorJS instance with the CPU monitor item
const monitor = new MonitorJS([cpuMonitorItem]);

// Start monitoring
monitor.start();

Example 3: Custom Alert Action

const MonitorJS = require('fast-monitor-js');

// Define an item with a custom alert action for fluctuating stock prices
const stockMonitorItem = {
  name: 'Stock Price',
  setter: async () => {
    // Simulate fetching stock price (replace this with your actual data retrieval logic)
    return Math.random() * 200; // Random stock price value for demonstration
  },
  interval: 60000, // Check every minute
  alerts: [
    {
      name: 'Stock Fluctuation Alert',
      condition: 'more', // Check for price increases
      value: 150, // Alert if stock price goes above 150
      function: async (data, item, alert) => {
        // Custom alert action: Log and adjust the alert threshold for next time
        console.log(`Alert: Stock price surged to ${data.now}!`);
        alert.value += 10; // Increase the alert threshold by 10 for the next check
      },
      times: [], // No specific times for this alert
    },
  ],
};

// Create a MonitorJS instance with the stock monitor item
const monitor = new MonitorJS([stockMonitorItem]);

// Start monitoring
monitor.start();

This example monitors stock prices and performs a custom alert action by adjusting the alert threshold for the next check whenever the stock price surges.

Example 4: Dynamic Threshold Adjustment

const MonitorJS = require('fast-monitor-js');

// Define an item with dynamic threshold adjustment based on historical data
const dynamicThresholdItem = {
  name: 'Dynamic Threshold',
  setter: async () => {
    // Simulate fetching dynamic data (replace this with your actual data retrieval logic)
    return Math.random() * 100; // Random value for demonstration
  },
  interval: 5000, // Check every 5 seconds
  alerts: [
    {
      name: 'Dynamic Threshold Alert',
      condition: 'more', // Check for values greater than the dynamic threshold
      value: 50, // Initial threshold value
      function: async (data, item, alert) => {
        // Calculate dynamic threshold based on historical average
        const historicalAvg = data.avg || 0;
        alert.value = historicalAvg + 10; // Set the new threshold as historical average + 10
        console.log(`Alert: Dynamic value ${data.now} exceeded the adjusted threshold!`);
      },
      times: [], // No specific times for this alert
    },
  ],
};

// Create a MonitorJS instance with the item using a dynamic threshold
const monitor = new MonitorJS([dynamicThresholdItem]);

// Start monitoring
monitor.start();

This example demonstrates dynamic threshold adjustment based on historical averages of monitored data.

These examples illustrate Monitor.JS's capabilities to perform custom actions based on monitored data and adjust thresholds dynamically for more efficient monitoring. Feel free to adapt and expand upon these examples to suit your specific monitoring needs.

Advanced Configuration

Example: Monitoring CPU and RAM Usage

This example demonstrates how MonitorJS can be used to monitor CPU and RAM usage on a system.

Setup

  • The getCPUUsage function retrieves the average CPU usage percentage across all CPU cores using system data provided by Node.js os module.
  • The getRAMUsage function calculates the RAM usage percentage based on total and free memory available on the system.

Configuration

  • Two monitoring items are defined:
    • CPU Usage Monitoring
      • Setter: Utilizes getCPUUsage to fetch CPU usage.
      • Interval: Monitors CPU usage every 250 milliseconds.
      • Alerts: Triggers an alert if CPU usage exceeds 50%.
      • Time Interval: Checks CPU usage against the alert threshold every 10 seconds.
    • RAM Usage Monitoring
      • Setter: Utilizes getRAMUsage to fetch RAM usage.
      • Interval: Monitors RAM usage every 500 milliseconds.
      • Alerts: Triggers an alert if RAM usage exceeds 50%.
      • Time Interval: Checks RAM usage against the alert threshold every 5 seconds.

Running the Monitor

  • The MonitorJS instance is created with these defined monitoring items.
  • The start() method initiates the monitoring process.

Alert Actions

  • If either CPU or RAM usage exceeds the defined threshold of 50%, an alert message is logged, indicating the current average usage.

Code

const MonitorJS = require("fast-monitor-js");
const os = require("os");

function getCPUUsage() {
  const cpus = os.cpus();
  let totalUsage = 0;
  cpus.forEach((cpu) => {
    const usage = cpu.times.user + cpu.times.sys;
    const total = cpu.times.user + cpu.times.nice + cpu.times.sys + cpu.times.idle + cpu.times.irq;
    totalUsage += usage / total;
  });
  const averageUsage = totalUsage / cpus.length;
  return averageUsage * 100;
}

function getRAMUsage() {
  const totalMemory = os.totalmem();
  const freeMemory = os.freemem();
  const usedMemory = totalMemory - freeMemory;
  const usedPercentage = (usedMemory / totalMemory) * 100;
  return usedPercentage; // Return as percentage
}

const alerts = [
  {
    name: 'warning on 50 more',
    condition: 'more',
    value: 50,
    function: async (db, item) => {
      console.log(` ${item.name} : avg ${db.avg}`);
    },
  },
];

const items = [
  {
    name: "cpu_usage",
    setter: getCPUUsage,
    interval: 250,
    alerts: alerts,
    times: ["10_s"],
    max: 100,
    min: 0,
  },
  {
    name: "ram_usage",
    setter: getRAMUsage,
    interval: 500,
    alerts: alerts,
    times: ["5_s"],
  },
];

const monitor = new MonitorJS(items)

monitor.start()

Documention

See Full Documetion from this Page

Contributing

We welcome contributions from the community! Fork the repository, make your changes, and submit a pull request.

License

This project is licensed under the GNU License - see the LICENSE file for details.