Page 1 of 1

netwatch on web script

Posted: Mon Nov 17, 2008 4:09 pm
by fosben
Was abit bored in the weekend and wrote a script that publish the netwatch status on a web page.

Needed:

-webserver with php and mysql
-ros 3.16 (Works on earlier versions too, but I used that version...)

Please note that I have not taken any action to secure this scripts, so dont publish them on external webservers :)

The netwatch code for up :
/tool fetch mode=http address=www.yourpage.no src-path=("/net_status.php?router=1&status=1&time=".[/system clock get time]."%20".[/system clock get date]) dst-path="/netwatch" port=80 host=www.yourpage.no
The netwatch code for down :
/tool fetch mode=http address=www.yourpage.no src-path=("/net_status.php?router=1&status=2&time=".[/system clock get time]."%20".[/system clock get date]) dst-path="/netwatch" port=80 host=www.yourpage.no
Note the following php string options :
router=1 <-its the id of the router in the database- (you have to change this value in the up/down script based on the id of the router in the database, if you dont, it will only update data for 1 router)
status=1 (or 2) <-status 1 means router is up, status 2 means router is down
time <- time and date for action

databasesetup :

Make a database called 'netwatch' and a table called 'netwatch'
Make the following fields : id (Int,auto,index), name (varchar), status (int), time (varchar)

Now insert a couple of routers in the database, you only have to fill in the 'name' field, the others will be filled later automatically.

The database connection script: (databaseconfig.php)
<?php

$conn = mysql_connect("localhost", "username", "password") or die(mysql_error());
$db=mysql_select_db("netwatch") or die(mysql_error());
?>


php script for accepting values: (net_status.php)
<?php

/**
 * @author bent ole fosse
 * 
 */
include "databaseconfig.php";

//get data from header
$router=$_GET['router'];
$status=$_GET['status'];
$time=$_GET['time'];

$query="UPDATE netwatch SET status='".$status."', time='".$time."' WHERE id='".$router."'";

$result=mysql_query($query)or die(mysql_error());

echo "done";
?>
Script for publishing values on a page: (display.php)
<?php
/**
 * @author bent ole fosse
 * 
 */
include "databaseconfig.php";

$query="SELECT * FROM netwatch";
$result=mysql_query($query)or die(mysql_error());
$num=mysql_numrows($result);

echo "<table width=800>";

echo "<tr><td align=left>Router</td><td>Time</td><td>Status</td></tr>";

echo "<tr><td align=left>";

	for ($i=0; $i<$num; $i++) 
	
		{
			if (mysql_result($result,$i,"status")=="1")
			{
			echo "<font color=#04B404>".mysql_result($result,$i,"name")."</font><br>";
			}else{
			echo "<font color=#FF0000>".mysql_result($result,$i,"name")."</font><br>";
			}

		}

echo "</td><td>";

	for ($i=0; $i<$num; $i++) 
	
		{
			if (mysql_result($result,$i,"status")=="1")
			{
			echo "<font color=#04B404>".mysql_result($result,$i,"time")."</font><br>";
			}else{
			echo "<font color=#FF0000>".mysql_result($result,$i,"time")."</font><br>";
			}

		}

echo "</td><td>";

	for ($i=0; $i<$num; $i++) 
	
		{
			if (mysql_result($result,$i,"status")=="1")
			{
			echo "<font color=#04B404>Up</font><br>";
			}else{
			echo "<font color=#FF0000>Down</font><br>";
			}

		}

echo "</td></tr>";

echo "<tr><td colspan=3><center>Hopefully this works ok...</center></td></tr>";
echo "</table>";
?>

Re: netwatch on web script

Posted: Mon Nov 17, 2008 4:18 pm
by normis
wow this is great, by the way, if you put it in the wiki, you will get a free RouterOS license ;)

Re: netwatch on web script

Posted: Mon Nov 17, 2008 5:15 pm
by fosben
wow this is great, by the way, if you put it in the wiki, you will get a free RouterOS license ;)
nice :)

its posted here : http://wiki.mikrotik.com/wiki/Netwatch_on_web

Re: netwatch on web script

Posted: Wed May 06, 2009 10:59 am
by EngAMoktar
First , I wanna thank you for this great idea ,
Now I'm trying to do something like this , but I have a problem here , I hope you help

when I try to send something contain spaces it only send the firs part
like this
:for i from=2 to=5 do={
:local id [/queue simple find target-addresses="192.168.1.$i/32"];
:local name [/queue simple get $id name];
:local targ [/queue simple get $id target-addresses];
/tool fetch mode=http address=mysite.com src-path=("/mt/insert.php?name=$name&dst=$targ") dst-path="/netwatch" port=80 host=mysite.com}
if the name was "Sarah Mark"
it only send the Sarah and ignore every thing after it including the $targ..!
if you noticed that I'm not make an update , but an Insert.
I wanna insert my simple queue names and targets to the database.

is there any way to get through , I'll be thankful .

Re: netwatch on web script

Posted: Fri Jun 19, 2009 6:07 pm
by fosben
Sorry Ive been away for awhile and didnt see your post before now and dont know if you have fixed it or not..

The reason it doesnt work is the blank space. The variable containing "Sarah Mark" must be formatted into "Sarah%20Mark" before sending it to your web server as they dont like blank spaces..

Re: netwatch on web script

Posted: Sat Dec 04, 2010 3:06 pm
by sw0rdf1sh
Great job.Actually the mikrotik asks for the php,sets the values,the php in the domain stores the values in sql and display.php shows the sql entries in the whole world.It was what I was looking for.

There is one thing I noticed after testing.
I have 2 adsl modem/routers connected in a 433AH (lets call it ROS1).
I have 2 backhaul links from ROS1 to 2 433AH routers (Lets say them ROS2,ROS3)
The interval for the request of the get_status.php is 10min.

I am using this code for netwatch in ROS1 checking the 2 adsl lines and the 2 links to ROS2,ROS3 getting correct status to a webpage.The only thing is that when the adsl modems get disconnected the values stored in the database still stay there so the user seeing my domain sees "Online" status for everything.Actually it is offline because the get_status.php is not requested anymore by the offline router.

Should the php have something about erasing those values every 9min and 59sec.?So every 10 min the indication will be correct?Just a thought.
PS:Maybe checking the fileatime/filemtime value in php?Am I in the right direction?

I also want to know for showing number of users in hotspot if this code would work with another field called users in the table:
/tool fetch mode=http address=www.yourpage.no src-path=("/net_status.php?router=1&status=1&time=".[/system clock get time]."%20".[/system clock get date]&users=".[/ip hotspot access print count-only]) dst-path="/netwatch" port=80 host=www.yourpage.no

Re: netwatch on web script

Posted: Thu Dec 16, 2010 2:47 am
by sw0rdf1sh
So after several tests and having no knowledge of scripting in mikrotik devices I used this workaround for mikrotik hotspot user stats on an external website with no ability to use rrdtool. (Posibly a shared hosting with cpanel, php and mysql).
I hope I can help many others having the same problem I did.That was sending every minute the number of active hotspot users to my website.

I'm also in the proccess of writing a joomla module where you can set the router names,etc...
This is not my scripting.I simply modifided the above one.Also it is no longer in netwatch.It is in scripting/schedules

So...This is what you have to do:
As listed above by fosben:
Make a database called 'netwatch' and a table called 'netwatch'
Make the following fields : id (Int,auto,index), name (varchar), users (varchar)

Now insert a couple of routers in the database, you only have to fill in the 'name' field, the others will be filled later automatically.
You need the 3 php files listed above:
databaseconfig.php
<?php

$conn = mysql_connect("localhost", "username", "password") or die(mysql_error());
$db=mysql_select_db("netwatch") or die(mysql_error());
?>
net_status.php
<?php

/**
 * @author bent ole fosse
 * 
 */
include "databaseconfig.php";

//get data from header
$hotspot=$_GET['hotspot'];
$users=$_GET['users'];

$query="UPDATE netwatch SET users='".$users."' WHERE id='".$hotspot."'";

$result=mysql_query($query)or die(mysql_error());

echo "done";
?>
display.php
<?php
/**
 * @author bent ole fosse
 * 
 */
include "databaseconfig.php";

$query="SELECT * FROM netwatch";
$result=mysql_query($query)or die(mysql_error());
$num=mysql_numrows($result);

echo "<table width=200>";

echo "<tr><td align=left>Hotspot</td><td>Users</td></tr>";

echo "<tr><td align=left>";

	for ($i=0; $i<$num; $i++) 
	
		{	
			echo "<font color=#04B404>".mysql_result($result,$i,"name")."</font><br>";			
		}

echo "</td><td>";

	for ($i=0; $i<$num; $i++) 
	
		{
			echo "<font color=#04B404>".mysql_result($result,$i,"users")."</font><br>";
		}

echo "</td>";
	
echo "<tr><td colspan=3><center>Live users status</center></td></tr>";
echo "</table>";
?>
In the above display.php you should probably want to add the time the file is being fetched by the router or just keep the time field in the database for every router.I just added the time that the file is being accessed.
<?php

// Change to the name of the file

$last_modified = fileatime("net_status.php");

// Display the results

// eg. Last modified Monday, 27th October, 2003 @ 02:59pm

print "Last access " . date("l, dS F, Y @ h:ia", $last_modified);

?> 
Now for the mikrotik routers (I am using v4):
In /System Scripting
Add a script with name "hotspotusers" and policy tick everything.Source for this is:
:local husers [/ip hotspot host print count-only];
/tool fetch mode=http address=www.webpage.com src-path=("/folder/net_status.php?hotspot=1&users=$husers") dst-path="/netwatch" port=80 host=www.webpage.com}
hotspot=1 is the name of the router.It should be as it is set in the database.Different number for every router.1,2,3,etc
In /system scheduler
Add a schedule with name "hotspotusers schedule" start date "Jan/01/1970" start time "00:00:00" interval "00:01:00" delay "00:00:00" On event "hotspotusers" and tick everything in policy so when scheduler runs the script also runs.

This way every 1 minute every mikrotik router fetches net_status.php and Stores the number of users in the database.
Any user asking for your domain (http://www.webpage.com) will see display.php and get the last stored values from the database.
Hope this helps.Sure helped me.
All credit to fosben

Re: netwatch on web script

Posted: Wed Nov 04, 2015 4:05 pm
by w4hyucahyo
The netwatch code for up :
Code: Select all

/tool fetch mode=http address=www.yourpage.no src-path=("/net_status.php?router=1&status=1&time=".[/system clock get time]."%20".[/system clock get date]) dst-path="/netwatch" port=80 host=www.yourpage.no


The netwatch code for down :
Code: Select all

/tool fetch mode=http address=www.yourpage.no src-path=("/net_status.php?router=1&status=2&time=".[/system clock get time]."%20".[/system clock get date]) dst-path="/netwatch" port=80 host=www.yourpage.no

1. for those script must be put in MikroTik router or in php script?
2. in http address= is our xampp local host like http://localhost/bla..bla or must be domain name/ can i user ip address?
bacouse i use xampp for apache server so what http address should i use?
3. Ros is Router OS?

Sorry i am newbie.
Thank you.

Re: netwatch on web script

Posted: Thu Nov 05, 2015 1:38 am
by deanMKD1
1. Files MUST be placed in your server, because is connected to MySQL database. If you store files in your RB, will not work it, because DB is missing.

2. http adress = your website URL or IP adress on your localhost and host = user IP adress that you want to monitoring. (Note that IP adress on user MUST be STATIC !! )

3. RoS = Router OS.

Question for developers on this script> Can every Up time and DOWN time to be stored in different row in DB, not only latest state of device/user, so we can see when user is UP and how long was UP before goes down?

Re: netwatch on web script

Posted: Fri Nov 06, 2015 10:17 am
by w4hyucahyo
1. in up and down screenshoot attachments also in web server and running on web please advice.
2. after on web time is doesn't appear i put varchar type data. please advice.

Thank you.

Re: netwatch on web script

Posted: Fri Nov 06, 2015 10:28 am
by w4hyucahyo
So in RB does't have script in tool>netwatch at all just put in web server?