Page 1 of 1

[SOLVED] Netwatch via API (php)

Posted: Tue Dec 22, 2015 11:11 am
by src386
Hello everyone,

I need to monitor two hosts using netwatch :
[admin@R103] /tool netwatch> /tool netwatch print 
Flags: X - disabled 
 #   HOST                 TIMEOUT              INTERVAL             STATUS  SINCE               
 0            998ms                10s                  up      dec/22/2015 08:59:30
 1            998ms                10s                  up      dec/22/2015 08:59:30
I would like to retrieve the status (up or down) on Zabbix (monitoring server) so I was thinking of using the routeros-api PHP.

I would like to send a command like this :
/tool netwatch print where host=
I tried this :
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('', 'admin', 'password')) {
   $READ = $API->read(false);
   $ARRAY = $API->parseResponse($READ);


But this doesn't work :(

Does anybody can help me ?


Re: Netwatch via API (php)

Posted: Tue Dec 22, 2015 11:29 am
by ConnectivityEngineer
What you could do is setup a script so when netwatch on the host finds it is down send an snmp trap to Zabbix.

We stopped using Zabbix because of the lack of dependencies AND thinks like this (admit its been a few years)

Just our usage case...

Anyhow -

You can setup a trigger prototype using the "nodata" function.

Of course this is outside of the API functionality ... I admit that.

Are you running the check on a cron basis ? or ?

Re: Netwatch via API (php)

Posted: Tue Dec 22, 2015 12:05 pm
by src386
@ConnectivityEngineer : I am not familiar with snmp traps, how does it works ?
I was thinking of using the script as a custom userparameter file.
EDIT: okay I understand snmp traps, but netwatch has no oid.

I finally have the answer for the script :

if ($argc<2){
    echo "Host manquant\n";
$ip_host = $argv[1];

$API = new RouterosAPI();
$API->debug = false;
if ($API->connect(', 'admin', 'password')) {
$ARRAY =  $API->comm("/tool/netwatch/print", array(
    ".proplist" => "status",
    "?host"     => "$ip_host",


Usage :
php netwatch.php

Re: Netwatch via API (php)

Posted: Tue Dec 22, 2015 12:58 pm
by ConnectivityEngineer
found this as well: ... rom_router

use PEAR2\Net\RouterOS;
require_once 'PEAR2/Autoload.php';

if (isset($_GET['act'])) {//This is merely to ensure the form was submitted.

    //Adjust RouterOS IP, username and password accordingly.
    $client = new RouterOS\Client('', 'admin', 'password');

    //This is just one approach that allows you to create a multi purpose form,
    //with ping being just one action.
    if ($_GET['act'] === 'Ping' && isset($_GET['address'])) {
        //Ping can run for unlimited time, but for PHP,
        //we need to actually stop it at some point.
        $pingRequest = new RouterOS\Request('/ping count=3');
        $results = $client->sendSync($pingRequest->setArgument('address', $_GET['address']));
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
        <title>Ping someone</title>
            <form action="" method="get">
                        <label for="address">Address:</label>
                        <input type="text" id="address" name="address" value="<?php
                            if (isset($_GET['address'])) {
                                echo htmlspecialchars($_GET['address']);
                        ?>" />
                        <input type="submit" id="act" name="act" value="Ping" />
if (isset($_GET['act'])) {//There's no need to execute this if the form was not submitted yet.
    echo '<div>Results:<ul>';
    foreach ($results as $result) {
        //Add whatever you want displayed in this section.
        echo '<li>Time:', $result('time'), '</li>';
    echo '</ul></div>';

Re: Netwatch via API (php)

Posted: Tue Dec 22, 2015 1:02 pm
by ConnectivityEngineer
I am sitting waiting another train - however - this link might help you as well...

Re: Netwatch via API (php)

Posted: Tue Dec 22, 2015 1:03 pm
by src386
It's probably a better idea to use netwatch :)

Now I am interested in SNMP trap, might be a good alternative.