Netwatch on web

From MikroTik Wiki
Jump to: navigation, search

This is a script that publish the netwatch status on a web page.

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


Needed

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

Netwatch Code

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

Important

Note the following php string options :

router=1 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


Database setup

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)

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)

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";
?>

The final display script

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>";
?>