Skip to content

Lab 09-02 · Ping and Netwatch

MTCNA

Module 9 · RouterOS tools. Official course slides 352–354: Tools → Netwatch (watch hosts, run a script when they go down or up), Tools → Ping, and the LAB "ping your laptop from the router, ping www.mikrotik.com, observe the difference in round-trip time". This lab does that LAB and then puts Netwatch to work with RouterOS v7's probe types.

You send a message on WhatsApp and wait. One grey tick: it left your phone. Two grey ticks: it reached your friend's phone. That little wait tells you two things: is the other side alive? and how far away is it?

Routers ask the same two questions all day long, with a tool called ping. And with Netwatch, a router can keep asking by itself, every few seconds, and do something the moment the answer changes: write a warning, send an e-mail, switch to a backup line.

What you will learn

  • Use ping from the router and read the round-trip time (RTT)
  • Explain why a host on your LAN answers in microseconds and one on the Internet in milliseconds
  • Create Netwatch probes of type icmp and tcp-conn
  • Attach up-script and down-script actions that write to the log
  • Prove the difference between "the server is alive" and "the service is working"
  • Avoid the trap of Netwatch's startup-delay (5 minutes of silence after boot)

Ping: "are you there?"

Ping sends an ICMP echo request to a host and waits for the echo reply. The time between the two is the round-trip time (RTT).

ICMP (Internet Control Message Protocol) is the network's own message service: small packets for "are you there?", "I'm here", "I can't reach that network". Ping uses two of them.

The analogy for this lab is a lighthouse keeper with a lamp. Ping is flashing the lamp at a ship and waiting for the ship to flash back. If it flashes back, the ship is there. How long it takes tells you how far it is.

flowchart LR
    R1["<b>r1</b><br>the lighthouse"]
    H["<b>host</b><br>the ship"]
    R1 -->|"echo request (flash)"| H
    H -->|"echo reply (flash back)"| R1

Netwatch: the keeper who never sleeps

Netwatch (Tools → Netwatch) probes a host again and again. When the result changes it sets the status to up or down and runs the matching script: up-script or down-script.

The keeper does not flash once; he flashes every 5 seconds, all night, and rings a bell when a ship disappears. RouterOS v7 gives him several kinds of lamp, the probe type:

Type What it checks Question it answers
icmp Ping, with loss and RTT thresholds Is the machine alive and is the path healthy?
tcp-conn Opens a TCP connection to a port Is the service on that port accepting connections?
http-get Downloads a web page and checks the answer Does the web server actually answer correctly?
https-get The same, over encrypted HTTPS Does the secure website answer correctly?
simple A plain ping, like Netwatch in RouterOS v6 Is it reachable at all?

Alive is not the same as working

A server can answer ping while its web service has crashed. Your phone can be on while WhatsApp is frozen. That is why this lab watches the same host twice: once with icmp, once with tcp-conn on port 80.

The scenario

flowchart LR
    INET["<b>internet</b><br>198.51.100.10<br>web server :80<br>40 ms away"]
    CAP["<b>class-ap</b><br>10.0.0.1 · 198.51.100.1<br>DNS: www.mikrotik.com"]
    subgraph R1["r1 · your router"]
        WAN["ether2 (WAN)<br>10.0.0.x by DHCP"]
        BR["<b>bridge1</b><br>192.168.88.1/24"]
    end
    PC1["<b>pc1</b><br>your laptop<br>DHCP 192.168.88.x"]
    INET ---|"198.51.100.0/24"| CAP
    CAP ---|"10.0.0.0/24"| WAN
    WAN --- BR
    BR ---|"ether3"| PC1
Node Role Address
internet "The Internet": web server on port 80, answers ping, made 40 ms slow on purpose 198.51.100.10
class-ap Class gateway: DHCP, DNS (www.mikrotik.com → 198.51.100.10), NAT 10.0.0.1/24
r1 Your router, working, no Netwatch yet WAN by DHCP, LAN 192.168.88.1/24
pc1 Your laptop DHCP from r1

Why ether2 and not ether1?

On a real hAP the Internet port is ether1. In the virtual lab ether1 is reserved for the lab itself (containerlab uses it to manage the router), so the Internet port is ether2 and the LAN ports start at ether3. Never touch ether1 in these labs.

Why is the Internet 40 ms away?

In the virtual lab every cable is inside the same computer, so everything would answer in under a millisecond. The internet node adds a 40 ms delay to what it sends (Linux tc netem), so it behaves like a server in another country.

Start the lab

Download the lab files and unzip them: 09-02-ping-netwatch.zip. Open a terminal inside the 09-02-ping-netwatch folder.

sudo containerlab deploy -t topology.clab.yml

Give RouterOS about two minutes to boot. Then open the router and the laptop:

ssh admin@clab-mtcna-09-02-r1                 # password: admin
docker exec -it clab-mtcna-09-02-pc1 bash

At any moment, CLAB_EXEC="docker exec" ./check.sh tells you what is already solved.

Step 1 · Ping your laptop from the router

First you need the laptop's address. r1 gave it by DHCP, so r1 knows it.

WinBox: IP → DHCP Server → Leases. Then Tools → Ping, Ping To: the lease address, Start.

/ip dhcp-server lease print
/ping 192.168.88.200 count=5

Use the address from your own lease list (in our test it was 192.168.88.200). Expected:

  SEQ HOST              SIZE TTL TIME
    0 192.168.88.200      56  64 184us
  ...
    sent=5 received=5 packet-loss=0% min-rtt=159us avg-rtt=193us max-rtt=238us

us means microseconds (millionths of a second). The laptop is one cable away.

Check yourself

  1. Write the avg-rtt you got. What does TTL 64 tell you about how many routers the reply crossed?

Done when you have five replies from your laptop.

Step 2 · Ping www.mikrotik.com and compare

The course says: open a New Window in WinBox and ping www.mikrotik.com while the first ping runs. In the CLI, just run it:

/ping www.mikrotik.com count=5

Expected: replies from 198.51.100.10 with TIME around 40ms, and a lower TTL (63).

Target Typical RTT here Why
Laptop on the LAN ~0.2 ms One cable, no router in between
www.mikrotik.com ~40 ms Two routers and a "long distance" link

The very first reply is often slower

The first packet may show 80 ms. The routers on the way had to look up the neighbour's MAC address first (ARP). From the second packet on, they remember it.

Check yourself

  1. Why did the router need the DNS to ping www.mikrotik.com, but not to ping your laptop?
  2. RTT means round trip. If the RTT is 40 ms, how long does one packet take to go one way, roughly?

Done when you can explain why the two RTTs are so different.

Step 3 · Watch the Internet server with an icmp probe

Now the keeper starts his night shift. He will flash at the Internet server every 5 seconds and write to the log when it disappears or comes back.

WinBox: Tools → Netwatch → +. Name internet-ping, Type icmp, Host 198.51.100.10, Interval 5s, Startup Delay 0s. In the Up tab: :log warning "netwatch: Internet UP". In the Down tab: :log error "netwatch: Internet DOWN".

/tool netwatch add name=internet-ping type=icmp host=198.51.100.10 interval=5s startup-delay=0s comment="Can we reach the Internet server?" up-script=":log warning \"netwatch: Internet UP\"" down-script=":log error \"netwatch: Internet DOWN\""
/tool netwatch print

Inside the scripts, the inner quotes are written \" so they do not close the outer ones.

Expected a few seconds later: status up, and in the log script,warning netwatch: Internet UP.

/log print where message~"netwatch:"

The 5-minute silence

By default RouterOS 7 waits startup-delay=5m before probing, so that a router that has just booted does not fire false alarms while its links come up. If you forget startup-delay=0s, the probe stays unknown with "probe waiting startup-delay" for five minutes. Not broken: just patient.

Check yourself

  1. Why is the very first event an UP, although nothing came back up?

Done when /tool netwatch print shows the icmp probe up.

Step 4 · Watch the web service with a tcp-conn probe

Ping only says the machine is on. A shop's web server can be on and still not sell anything. A tcp-conn probe opens a TCP connection to port 80, which only succeeds if the web server program is running.

WinBox: Tools → Netwatch → +. Name web, Type tcp-conn, Host 198.51.100.10, Port 80, Interval 5s, Startup Delay 0s, scripts as below.

/tool netwatch add name=web type=tcp-conn host=198.51.100.10 port=80 interval=5s startup-delay=0s comment="Is the web server answering?" up-script=":log warning \"netwatch: WEB UP\"" down-script=":log error \"netwatch: WEB DOWN\""
/tool netwatch print

Expected: two probes, both up.

Done when both probes are up.

Step 5 · Break the web server and watch the keeper react

Now the real test: stop the web program on the Internet server, but leave the machine running. Open a third terminal on your Ubuntu PC:

docker exec clab-mtcna-09-02-internet pkill -f http.server

Wait 10–15 seconds and look on r1:

/tool netwatch print
/log print where message~"netwatch:"

Expected: web is down, internet-ping is still up, and the log has script,error netwatch: WEB DOWN.

Start the web server again:

docker exec clab-mtcna-09-02-internet sh -c 'cd /srv/www && nohup python3 -m http.server 80 >/dev/null 2>&1 &'

A few seconds later the log shows netwatch: WEB UP.

From log line to e-mail

A down-script can run any RouterOS command. Combine this lab with Lab 09-01 and the down-script becomes /tool e-mail send to="you@mtcna.lab" subject="Web server DOWN": the router now e-mails you when the shop's website breaks.

Check yourself

  1. Write the three log lines in order with their times. How many seconds passed between killing the server and the DOWN line? Why not zero?
  2. Which probe would have warned you if the whole internet machine had been switched off?

Done when you have seen WEB DOWN and WEB UP while the icmp probe stayed up. Run CLAB_EXEC="docker exec" ./check.sh: it repeats this test by itself and must end with RESULT: lab solved.

If something goes wrong

Symptom Likely cause Fix
Status unknown, comment "probe waiting startup-delay" Default startup-delay of 5 minutes /tool netwatch set [find] startup-delay=0s
/ping www.mikrotik.com says no route or cannot resolve r1 has no WAN address or DNS yet /ip dhcp-client print must show bound on ether2
The web probe never goes down You killed the process on the wrong node The command must target clab-mtcna-09-02-internet
Nothing appears in the log Script typo, e.g. quotes not escaped /tool netwatch print detail and compare with the command above
Script syntax error Missing \ before an inner quote Rewrite with \" around the message

Clean up

sudo containerlab destroy -t topology.clab.yml --cleanup

Key takeaways

  • Ping sends an ICMP echo request and measures the round-trip time (RTT).
  • A LAN host answers in microseconds; an Internet host in milliseconds: distance and routers add up.
  • Netwatch probes hosts on an interval and runs an up-script or down-script when the status changes.
  • Type icmp says the machine is alive; tcp-conn says the service on that port works.
  • Netwatch waits a startup-delay (5 minutes by default) before it starts probing.

Quick check

1. Which ICMP messages does ping use?
  • Echo request and echo reply
  • SYN and ACK
  • Destination unreachable and time exceeded
  • DHCP discover and offer

Ping sends an echo request and waits for an echo reply. SYN/ACK belong to TCP; time exceeded is what traceroute uses (next lab).

2. From r1, your laptop answers in 0.2 ms and www.mikrotik.com in 40 ms. What is the main reason?
  • The packets to www.mikrotik.com cross more routers and a longer path
  • Laptops answer faster than servers by design
  • DNS is queried again for every ping packet
  • The router gives priority to the LAN

RTT grows with distance and with every router on the way. Your laptop is one cable away; the Internet server is behind two routers and a long link.

3. What does Netwatch do when a probe changes from up to down?
  • Runs the probe's down-script
  • Reboots the router
  • Disables the interface towards that host
  • Deletes the probe

Netwatch only observes and then runs the script you wrote in down-script (or up-script when it comes back). What happens next is up to you: a log line, an e-mail, a route change.

4. A web server answers ping, but its website does not load. Which Netwatch type would detect it?
  • tcp-conn to port 80
  • icmp
  • simple
  • icmp with a shorter interval

Ping only proves the machine is alive. tcp-conn opens a connection to the service's port, which fails when the web program has crashed.

5. You just added a probe and it shows status unknown with 'probe waiting startup-delay'. What is happening?
  • Netwatch waits 5 minutes by default before probing
  • The host does not exist
  • The probe type is not supported on this router
  • The interval is too short

RouterOS 7 has a default startup-delay of 5 minutes, to avoid false alarms right after a reboot. Set startup-delay=0s to start at once.

6. A shop has a router with two Internet lines. Pick the two things a Netwatch icmp probe on the main line's gateway could do in its down-script.
  • Write a warning in the log
  • Send an e-mail to the IT person
  • Repair the broken cable
  • Make the ISP answer the phone faster

A down-script runs RouterOS commands: logging, /tool e-mail send, enabling a backup route... It cannot fix anything physical, but it can make sure a human knows at once.

Next: when a ping fails, where along the way did it fail? That is the job of traceroute and the rest of the toolbox.