Community discussions

MikroTik App
 
src386
newbie
Topic Author
Posts: 27
Joined: Tue Dec 08, 2015 1:18 pm

[SOLVED] Netwatch via API (php)

Tue Dec 22, 2015 11:11 am

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   10.1.11.1            998ms                10s                  up      dec/22/2015 08:59:30
 1   10.1.12.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=10.1.11.1
I tried this :
<?php
require('../routeros_api.class.php');
$API = new RouterosAPI();
$API->debug = true;
if ($API->connect('1.1.1.1', 'admin', 'password')) {
   $API->write('/tool/netwatch/print"');
   $API->write('=?host=10.1.11.1');
   $READ = $API->read(false);
   $ARRAY = $API->parseResponse($READ);

    print_r($ARRAY);

   $API->disconnect();
}
?>
But this doesn't work :(

Does anybody can help me ?

Thanks.
Last edited by src386 on Tue Dec 22, 2015 3:50 pm, edited 1 time in total.
 
User avatar
ConnectivityEngineer
Frequent Visitor
Frequent Visitor
Posts: 57
Joined: Sat Dec 19, 2015 10:57 pm
Location: Ohio, USA
Contact:

Re: Netwatch via API (php)

Tue Dec 22, 2015 11:29 am

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 ?
 
src386
newbie
Topic Author
Posts: 27
Joined: Tue Dec 08, 2015 1:18 pm

Re: Netwatch via API (php)

Tue Dec 22, 2015 12:05 pm

@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 :
<?php
require('../routeros_api.class.php');

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

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

print_r($ARRAY[0]['status']);

}
?>
Usage :
php netwatch.php 10.1.11.1
 
User avatar
ConnectivityEngineer
Frequent Visitor
Frequent Visitor
Posts: 57
Joined: Sat Dec 19, 2015 10:57 pm
Location: Ohio, USA
Contact:

Re: Netwatch via API (php)

Tue Dec 22, 2015 12:58 pm

found this as well: http://wiki.mikrotik.com/wiki/API_PHP_p ... rom_router

<?php
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('192.168.88.1', '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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Ping someone</title>
    </head>
    <body>
        <div>
            <form action="" method="get">
                <ul>
                    <li>
                        <label for="address">Address:</label>
                        <input type="text" id="address" name="address" value="<?php
                            if (isset($_GET['address'])) {
                                echo htmlspecialchars($_GET['address']);
                            }
                        ?>" />
                    </li>
                    <li>
                        <input type="submit" id="act" name="act" value="Ping" />
                    </li>
                </ul>
            </form>
        </div>
        <?php
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>';
}
        ?>
    </body>
</html>
 
User avatar
ConnectivityEngineer
Frequent Visitor
Frequent Visitor
Posts: 57
Joined: Sat Dec 19, 2015 10:57 pm
Location: Ohio, USA
Contact:

Re: Netwatch via API (php)

Tue Dec 22, 2015 1:02 pm

I am sitting waiting another train - however - this link might help you as well...

https://github.com/pear2/Net_RouterOS/wiki
 
src386
newbie
Topic Author
Posts: 27
Joined: Tue Dec 08, 2015 1:18 pm

Re: Netwatch via API (php)

Tue Dec 22, 2015 1:03 pm

It's probably a better idea to use netwatch :)

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

Who is online

Users browsing this forum: No registered users and 10 guests