PHP Classes

PHP Network Monitor: Monitor the statistics of network interfaces

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 123 All time: 9,441 This week: 107Up
Version License PHP version Categories
php-network-monitor 1.0.0Custom (specified...5Networking, Unix, PHP 8
Description 

Author

This package can monitor the statistics of network interfaces.

It can check the available network interfaces in the current computer running under a Linux-based operating system.

The package can also retrieve statistics about a given network interface using the vnstat program or checking system virtual files containing values of several types of statistics of the traffic that goes through the specified network interface, like the number of transmitted bytes.

Innovation Award
PHP Programming Innovation award nominee
June 2021
Number 6
Monitoring aspects of a server running on the Internet can be useful to diagnose problems that need to be fixed quickly to avoid causing damages to businesses that depend on that server.

This package implements a solution to monitor network connections on a given Linux server using the vnstat program or checking the system virtual files related with networking connections.

Manuel Lemos
Picture of Isa Eken
  Performance   Level  
Name: Isa Eken <contact>
Classes: 20 packages by
Country: Turkey Turkey
Age: 21
All time rank: 276744 in Turkey Turkey
Week rank: 312 Up6 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 13x

Example

<?php

use IsaEken\NetworkMonitor\NetworkMonitor;

require_once
__DIR__ . '/../vendor/autoload.php';

if (
$_SERVER['REQUEST_METHOD'] === 'POST') {
   
header('Content-Type: application/json');

    if (!
NetworkMonitor::hasInterface($_POST['interface'] ?? '')) {
        return print
json_encode((object) [
           
'name' => null,
           
'directory' => null,
           
'statistics' => (object) [],
        ]);
    }

   
$interface = NetworkMonitor::findInterface($_POST['interface'] ?? '');
    return print
json_encode((object) [
       
'name' => $interface->getName(),
       
'directory' => $interface->directory,
       
'statistics' => (object) $interface->statistics()->toArray(),
    ]);
}

$composer = json_decode(file_get_contents(__DIR__ . '/../composer.json'));
$version = $composer->version;
$title = sprintf('Network Monitor - v%s', $version);

?><!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title><?= $title ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.2/tailwind.min.css" integrity="sha512-ghzNCvgK81aIdKyuTnLazeFEzs2F8AHLWyCYsvJHPqgGf8OpS/yRrq6seFxik5n08mjRmX8ETGPrHKkDkwBekw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/alpinejs/2.3.0/alpine.js" integrity="sha512-nIwdJlD5/vHj23CbO2iHCXtsqzdTTx3e3uAmpTm4x2Y8xCIFyWu4cSIV8GaGe2UNVq86/1h9EgUZy7tn243qdA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js" integrity="sha512-bZS47S7sPOxkjU/4Bt0zrhEtWx0y0CRkhEp8IckzK+ltifIIE9EMIMTuT/mEzoIMewUINruDBIR/jJnbguonqQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body class="bg-gray-900 text-gray-100 min-w-screen">
<div class="max-w-4xl my-12 mx-auto space-y-4" x-data="data()">
    <div>
        <div class="text-2xl">
            <?= $title ?>
</div>
        <div class="text-xl">
            <span class="text-xs mr-2">developed by</span><a href="https://isaeken.com.tr" target="_blank">Isa Eken</a>
        </div>
    </div>
    <div>
        <label for="interfaces">Select Interface</label>
        <select x-on:change="interface = $event.target.value; updateInterval(); load();" name="interfaces" id="interfaces" class="block w-full py-2 text-gray-700">
            <?php foreach (NetworkMonitor::getInterfaces() as $interface): ?>
<option x-bind:value="'<?= $interface->getName() ?>'" value="<?= $interface->getName() ?>" <?= $interface->getName() === 'eth0' ? 'selected' : '' ?>>
                    <?= $interface->getName() ?>
</option>
            <?php endforeach; ?>
</select>
    </div>
    <div>
        <label for="refresh">Refresh</label>
        <select x-on:change="interval = $event.target.value; updateInterval(); load();" name="refresh" id="refresh" class="block w-full py-2 text-gray-700">
            <?php $index = 0; foreach ([0, 5, 15, 30, 60, 300, 1800, 3600, 43200] as $seconds): ?>
<option x-bind:value="<?= $seconds ?>" value="<?= $seconds ?>" <?= $index++ === 0 ? 'selected' : '' ?>>
                    <?php
                   
if ($seconds === 0) {
                        echo
'No refresh';
                    }
                    else {
                       
$datetime1 = new DateTime('@0');
                       
$datetime2 = new DateTime("@$seconds");
                        echo
$datetime1->diff($datetime2)->format('%a days, %h hours, %i minutes and %s seconds');
                    }
                   
?>
</option>
            <?php endforeach; ?>
</select>
    </div>
    <div class="text-xl" x-show="time_left !== null && time_left > 0">
        Refreshing in <span x-text="time_left" id="time_left">0</span> seconds
    </div>
    <div>
        <div id="interface_name" x-text="interface" class="mb-4 text-xl"></div>
        <table class="w-full">
            <template x-for="item in statistics">
                <tr class="hover:bg-gray-800">
                    <td class="py-2" x-text="item[0]"></td>
                    <td class="py-2" x-text="item[1]"></td>
                </tr>
            </template>
        </table>
    </div>
    <div class="mb-4">
        <p>
            Please run
            <code class="bg-black rounded text-sm mx-2 px-2 py-1 select-all">sudo apt-get install vnstat</code>
            and
            <code class="bg-black rounded text-sm mx-2 px-2 py-1 select-all">sudo service vnstat enable && sudo service vnstat start</code>
            for the app to work properly.
        </p>
    </div>
    <script>
        let int = 'eth0';
        let statistics = [];

        function load() {
            loadData(document.querySelector('[x-data]').__x.$data.interface).then(function (data) {
                document.querySelector('[x-data]').__x.$data.statistics = data;
            });
        }

        function data() {
            return {
                interface: int,
                statistics: statistics,
                time_left: 0,
                time_left__: null,
                interval: 0,
                interval__: null,
                updateInterval: function () {
                    this.time_left = this.interval;
                    let time_left = this.time_left;
                    let interval = this.interval;

                    clearInterval(this.interval__);
                    clearInterval(this.time_left__);

                    if (interval > 1) {
                        this.interval__ = setInterval(function () {
                            load();
                            time_left = interval;
                        }, interval * 1000);

                        this.time_left__ = setInterval(function () {
                            time_left = time_left - 1;
                            document.getElementById('time_left').textContent = time_left;
                        }, 1000);
                    }

                    this.time_left = time_left;
                },
                load: function () {
                    load();
                }
            };
        }

        async function loadData(interfaceName) {
            let statistics = [];
            const params = new URLSearchParams();
            params.append('interface', interfaceName);

            await axios({
                method: 'POST',
                url: '<?= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ?>',
                data: params,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                }
            }).then(function (response) {
                statistics = Object.entries(response.data.statistics);
            });

            return statistics;
        }
    </script>
</div>
</body>
</html>


Details

Network Monitor

Network monitoring application for ubuntu written in PHP.

Requirements

  • PHP ^8.0
  • Linux based operating system (Tested on Ubuntu 20.04)
  • vnstat

Installation

git clone https://github.com/isaeken/php-network-monitor.git network-monitor &&
cd network-monitor

composer install

sudo apt-get install vnstat &&
sudo service vnstat enable &&
sudo service vnstat start

Usage

Using with CLI

composer run monitor <interface> <?refresh>
composer run monitor eth0
composer run monitor eth0 refresh

Using in web

Host public folder using web server. (Nginx recommended)

Host using PHP development server:

php -S 0.0.0.0:80 public

LICENSE

The MIT License (MIT). Please see License File for more information.


  Files folder image Files  
File Role Description
Files folder imagebin (1 file)
Files folder imagepublic (1 file)
Files folder imagesrc (2 files)
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE.md Lic. License text
Plain text file README.md Doc. Documentation

  Files folder image Files  /  bin  
File Role Description
  Plain text file monitor.php Example Example script

  Files folder image Files  /  public  
File Role Description
  Plain text file index.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file NetworkInterface.php Class Class source
  Plain text file NetworkMonitor.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:123
This week:0
All time:9,441
This week:107Up