Community discussions

MikroTik App
 
deanMKD1
Member
Member
Topic Author
Posts: 366
Joined: Fri Dec 12, 2014 12:06 am
Location: Macedonia
Contact:

PHP Bandwidth monitoring script (Day,Week,Month)

Fri Nov 20, 2015 1:53 am

Hi i found this php script somewhere on net, but looks that not works. If someone can check code, and make changes to get work, will be good to everyone !

I made some changes in init.php because script was created to work with SQLLite. Included .sql file into archive.

Thanks !

Script code:
:local sysnumber [/system routerboard get value-name=serial-number]
:local txbyte [/interface ethernet get ether1-gateway value-name=driver-tx-byte]
:local rxbyte [/interface ethernet get ether1-gateway value-name=driver-rx-byte]
/tool fetch url=("http://URL/collector.php\?sn=$sysnumber&tx=$txbyte&rx=$rxbyte") mode=http keep-result=no
You do not have the required permissions to view the files attached to this post.
 
deanMKD1
Member
Member
Topic Author
Posts: 366
Joined: Fri Dec 12, 2014 12:06 am
Location: Macedonia
Contact:

Re: PHP Bandwidth monitoring script (Day,Week,Month)

Fri Nov 20, 2015 11:38 pm

Anyone got this to work? :?
 
morf
Member Candidate
Member Candidate
Posts: 183
Joined: Tue Jun 21, 2011 5:31 pm
Location: Saint-Petersburg

Re: PHP Bandwidth monitoring script (Day,Week,Month)

Sat Nov 21, 2015 7:42 pm

В целом, удобно. В общем, график по интерфейсу можно отрисовать прямо в RouterOS, с просмотром через winbox и http.
А так, хорошая работа, ознакомился.
 
deanMKD1
Member
Member
Topic Author
Posts: 366
Joined: Fri Dec 12, 2014 12:06 am
Location: Macedonia
Contact:

Re: PHP Bandwidth monitoring script (Day,Week,Month)

Sat Nov 21, 2015 10:41 pm

English please..Not everyone understand russian. :D
 
User avatar
Bigfoot
Frequent Visitor
Frequent Visitor
Posts: 76
Joined: Sat Jan 15, 2011 10:41 am
Location: South Africa

Re: PHP Bandwidth monitoring script (Day,Week,Month)

Tue Nov 24, 2015 2:02 am

Hi deanMKD1
I have been playing with the code, I have change the file names init.php to config.php
the config.php is in sub folder called "include," the init.php is no cluttered, this my fist attempt to get this working :lol:
There is some problems in the code that I have change , hope you can fill in the gaps.
I'm new to this PHP to MySQL, will keep on working on it hope to get it working 100%.

Here is the config.php code:
<?php
  $link = mysql_connect('localhost', 'root', 'password');
  if (!$link) die('Could not connect: ' . mysql_error());
  mysql_select_db('tikstat');

    $db_host="localhost";
    $db_user="root";
    $db_pass="password";
    $db_database="tikstat";

    global $db;
    $db =  new mysqli($db_host, $db_user, $db_pass, $db_database);
    if ($db->connect_error) die('Could not connect');
  
?>
Here is the collector.php code:
<?php
/*
	/collector.php?sn=<SERIAL NUMBER>&tx=<INTERFACE OUR BYTES>&rx=<INTERFACE IN BYTES>
*/
include("include/config.php");
// Check input data
if (isset($_GET[sn]) 
	and isset($_GET[tx]) and is_numeric($_GET[tx])
	and isset($_GET[rx]) and is_numeric($_GET[rx])) {
	$device_serial = substr($_GET[sn], 0, 12);
} else {
	echo 'No code fail';
	exit;
}

// Create new devices if not exist
	$link = "insert into devices(id, last_tx, last_rx, FROM devices WHERE sn='".$device_serial."'";
    $link = mysql_query($db);

if (!is_numeric($device[0][id])) {
	 	$link = "INSERT INTO devices(sn, last_check, last_tx, last_rx) VALUES ('".$device_serial."', '".time()."', '".$_GET[tx]."', '".$_GET[rx]."')";
	$device[0][id] = $db->lastInsertRowid();
	$txBytes = $_GET[tx];
	$rxBytes = $_GET[rx];
	$link = mysql_query($db);
	echo 'INSERT INTO device';
} else {
	// update last receiving data
	$db = "UPDATE devices SET last_check='".time()."', last_tx='".$_GET[tx]."', last_rx='".$_GET[rx]."' WHERE id='".$device[0][id]."'";
	// check last received value
	if ($device[0][last_tx] > $_GET[tx]) {
		$txBytes = $_GET[tx];
	} else {
		$txBytes = $_GET[tx]-$device[0][last_tx];
	}
	if ($device[0][last_rx] > $_GET[rx]) {
		$rxBytes = $_GET[rx];
	} else {
		$rxBytes = $_GET[rx]-$device[0][last_rx];
	}
	$link = mysql_query($db);	
}

	$db = "SELECT * FROM traffic WHERE device_id='".$device[0][id]."' AND datetime = ".mktime(date('H'),0,0)." LIMIT 1";
	$link = mysql_query($db);
if (is_numeric($link[0][id])) {	
	$db = "UPDATE traffic SET tx='".($link[0][tx]+$txBytes)."', rx='".($link[0][rx]+$rxBytes)."' WHERE id='".$link[0][id]."'";
	$link = mysql_query($db);
} else {
	
$db = "INSERT INTO traffic(device_id, datetime, tx, rx) VALUES (".$device[0][id].", ".mktime(date('H'),0,0).", ".$txBytes.", ".$rxBytes.")";
	$link = mysql_query($db);	
}
?>
 
User avatar
Bigfoot
Frequent Visitor
Frequent Visitor
Posts: 76
Joined: Sat Jan 15, 2011 10:41 am
Location: South Africa

Re: PHP Bandwidth monitoring script (Day,Week,Month)

Sat Dec 12, 2015 9:42 pm

Some update:

I have change the SQL to the following, can add more than one interface from a couple of RB's
-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Dec 12, 2015 at 06:52 PM
-- Server version: 5.6.21
-- PHP Version: 5.5.19
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `tikstat`
--
-- --------------------------------------------------------
--
-- Table structure for table `devices`
--
CREATE TABLE IF NOT EXISTS `devices` (
`id` mediumint(9) NOT NULL,
  `sn` text COLLATE utf8_unicode_ci,
  `comment` text COLLATE utf8_unicode_ci,
  `last_tx` int(11) DEFAULT NULL,
  `last_rx` int(11) DEFAULT NULL,
  `last_check` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `traffic`
--
CREATE TABLE IF NOT EXISTS `traffic` (
`id` mediumint(9) NOT NULL,
  `device_id` int(10) unsigned NOT NULL,
  `datetime` datetime NOT NULL,
  `tx` int(10) unsigned NOT NULL,
  `rx` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `devices`
--
ALTER TABLE `devices`
 ADD PRIMARY KEY (`id`);
--
-- Indexes for table `traffic`
--
ALTER TABLE `traffic`
 ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `traffic` (`device_id`,`datetime`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `devices`
--
ALTER TABLE `devices`
MODIFY `id` mediumint(9) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1;
--
-- AUTO_INCREMENT for table `traffic`
--
ALTER TABLE `traffic`
MODIFY `id` mediumint(9) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
I have made some changes to collector.php it's working now with mysql, I'm working on it if anybody can help it would be appreciated.
<?php
include 'config.php';
include 'opendb.php';

if (isset($_GET[sn]) 
	and isset($_GET[tx]) and is_numeric($_GET[tx])
	and isset($_GET[rx]) and is_numeric($_GET[rx])) {
	$device_serial = substr($_GET[sn], 0, 12);
} else {
    echo '<table border="2" align="center"><tr><td>';
    echo '<align="center">This PHP scripts will only run from the Mikrotik Routerboard Tikstat Script.<br />';
	echo '</td></tr></table>';
	exit;
}  
// Delete empty records start
  mysql_query("delete from devices where sn = '0'");
// Delete empty records end  
  $res = mysql_query("select id ,last_tx, last_rx from devices where sn='".$_GET[sn]."'");
  $row = mysql_fetch_row($res);
  if ($row[0]==0)
      $sql = "insert into devices (sn,last_tx,last_rx,last_check) values ('".$_GET[sn]."','".$_GET[tx]."','".$_GET[rx]."',CURRENT_TIMESTAMP)";
   else
    $sql =  "update devices set "."sn='".$device_serial."'".",last_tx='".$_GET[tx]."'".",last_rx='".$_GET[rx]."'".",last_check = CURRENT_TIMESTAMP"." where sn='".$device_serial."'";
  mysql_query($sql);
  mysql_close();

  echo ' "OK";';
?> 
Here is the include config.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'tikstat';
?> 
Here is the include opendb.php
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?> 
Tiksat scrip update:
:local sysnumber [/system routerboard get value-name=serial-number];
:local txbyte [/interface get [find name="ether8_ADSL"] tx-byte];
:local rxbyte [/interface get [find name="ether8_ADSL"] rx-byte];

# convert to MB
# :set txbyte (($txbyte / 1048576))
# :set rxbyte (($rxbyte / 1048576))

:set txbyte (($txbyte / 1000))
:set rxbyte (($rxbyte / 1000))

:log warning "$sysnumber ...";
:log warning "$txbyte ...";
:log warning "$rxbyte ...";

/tool fetch url=("http://192.168.0.105:81/tikstat-master/collector.php\?sn=$sysnumber&tx=$txbyte&rx=$rxbyte") mode=http keep-result=no;
:log warning "Tikstat inset to DB has run...";
hope to get it working 100% :lol:
 
deanMKD1
Member
Member
Topic Author
Posts: 366
Joined: Fri Dec 12, 2014 12:06 am
Location: Macedonia
Contact:

Re: PHP Bandwidth monitoring script (Day,Week,Month)

Sun Dec 13, 2015 11:08 pm

Its nice to hear that someone get to work on this ! Where is index.php file code?

Do you have seen index.php from tikstats.zip archive?

UPDATE: I tryed myself and make a index.php file that grab data from mysql database.. Also changed a little to get device name instead of serial number, so now is question how to make a monthly, weekly, and yearly show the traffic.
You do not have the required permissions to view the files attached to this post.
 
penwelldlamini
just joined
Posts: 10
Joined: Thu Jan 22, 2015 4:08 pm

Re: PHP Bandwidth monitoring script (Day,Week,Month)

Fri Mar 24, 2017 12:45 pm

Hi can I get the full working code for this
 
deanMKD1
Member
Member
Topic Author
Posts: 366
Joined: Fri Dec 12, 2014 12:06 am
Location: Macedonia
Contact:

Re: PHP Bandwidth monitoring script (Day,Week,Month)

Wed Dec 13, 2017 1:26 am

Not working this code anymore on RoS 6.40.X or higher.

Who is online

Users browsing this forum: No registered users and 15 guests