← BackROYAL ANKIT AHIRAN
Skill 03 · C++

NetMon

A fast C++ terminal monitor displaying real-time CPU and RAM usage with crisp ASCII art and dynamic [|||||| ] loading bars.

DEMO

What Was Built

NetMon is a lightweight C++ console system monitor that polls system CPU load and memory stats at short intervals, rendering ASCII header graphics and formatted resource bars directly inside the terminal.

How It Looked

A dark developer terminal topped with a green NetMon ASCII banner followed by color-coded CPU, RAM, and swap loading bars ([|||||| ]) updating smoothly without screen flicker.

What Was Learned

  • Reading Linux virtual system stats (/proc/stat and /proc/meminfo) with C++ standard streams.
  • Structuring classes and refresh loops in C++ for smooth terminal rendering with minimal CPU overhead.
  • Building clear ASCII visualizers for numbers so resource spikes are readable at a glance.

Step-by-Step How to Run

  1. Compile netmon.cpp with C++17 optimization enabled using g++.
  2. Run the binary from a standard Linux terminal window.
  3. Watch real-time CPU and RAM percentage bars refresh every second.
Compile & Launch
g++ -O2 -std=c++17 netmon.cpp -o netmon && ./netmon
Sample Code: netmon.cpp

Sample for learning, not my actual file

netmon.cpp
// Sample for learning, not my actual file.
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>

static std::string make_bar(int pct) {
    int filled = (pct * 16) / 100;
    return "[" + std::string(filled, '|') + std::string(16 - filled, ' ') + "]";
}

static int read_ram_pct() {
    std::ifstream f("/proc/meminfo");
    std::string key;
    long val = 0, total = 0, avail = 0;
    while (f >> key >> val) {
        std::string unit; std::getline(f, unit);
        if (key == "MemTotal:") total = val;
        else if (key == "MemAvailable:") avail = val;
        if (total && avail) break;
    }
    return total > 0 ? static_cast<int>(((total - avail) * 100) / total) : 0;
}

static void read_cpu(long &idle, long &total) {
    std::ifstream f("/proc/stat");
    std::string tag; long a=0, b=0, c=0, d=0, e=0, g=0, h=0;
    f >> tag >> a >> b >> c >> d >> e >> g >> h;
    idle = d + e; total = a + b + c + d + e + g + h;
}

int main() {
    long i1 = 0, t1 = 0, i2 = 0, t2 = 0;
    read_cpu(i1, t1);
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    read_cpu(i2, t2);
    long dt = t2 - t1, di = i2 - i1;
    int cpu = dt > 0 ? static_cast<int>(100 * (dt - di) / dt) : 0;
    int ram = read_ram_pct();
    std::cout << "NetMon (Sample)\n";
    std::cout << "CPU  " << make_bar(cpu) << " " << cpu << "%\n";
    std::cout << "RAM  " << make_bar(ram) << " " << ram << "%\n";
    return 0;
}

Proof

Sample Outputtty1
 _   _      _   __  __             
| \ | | ___| |_|  \/  | ___  _ __  
CPU  [||||||||||      ]  62%
RAM  [||||||||        ]  51%
Refresh interval: 1000ms
Interface Capture Slot
CPU [|||||||||| ] 62%
Low-overhead terminal gauge visualizing memory and CPU utilization smoothly.
Console frame capture for NetMon resource bars.