How to Build RouterOS SFP Diagnostic Scripts for Real-Time Optical Monitoring and Alerts
Detect degrading optics, dirty fiber, and environmental stress before a link fails.
Fiber links often fail quietly before they fail completely. A degrading laser, dirty connector, damaged patch cord, or excessive cabinet temperature can leave an interface in the link-up state while receive power falls toward the receiver sensitivity floor.
The result is often intermittent packet loss, rising FCS or CRC errors, retransmissions, unstable routing sessions, and application slowness that appears unrelated to the optical link. By the time the interface finally drops, the problem may have been affecting customers for days or weeks.
Goal: Use RouterOS Digital Diagnostics Monitoring data to collect optical readings, compare them against known baselines and thresholds, log meaningful changes, and alert operations before a marginal optic becomes an outage.
Why SFP Failures Often Go Undetected
Traditional monitoring often checks only interface status, traffic utilization, packet counters, and reachability. Those are important signals, but they usually identify a problem after service quality has already been affected.
- The link remains up until optical margin is nearly exhausted.
- Interface up/down monitoring does not identify gradual receive-power decline.
- Outdoor cabinets and poorly ventilated racks can accelerate optic aging through heat.
- Dirty or damaged fiber jumpers may cause intermittent impairment before complete failure.
- A low Rx-power value has little meaning without a known baseline and link-budget context.
For operators with dozens or hundreds of fiber uplinks, manually checking optics does not scale. A better approach is to collect Digital Diagnostics Monitoring data at commissioning, preserve the baseline, and continuously evaluate changes against a documented policy.
Understanding DDM and DOM Data in RouterOS
Most modern optical modules support Digital Diagnostics Monitoring, also called Digital Optical Monitoring. Depending on the optic and RouterOS hardware support, RouterOS can report receive power, transmit power, temperature, supply voltage, transmitter bias current, module type, wavelength, EEPROM status, and other diagnostics.
RouterOS displays the values through /interface ethernet monitor. Field availability varies by transceiver type, vendor, RouterOS release, and router hardware, so validate the actual fields returned by every production optic before relying on a script.
| Metric | What It Can Indicate |
|---|---|
| Rx Power | Dirty connectors, fiber bends, attenuation, poor splice quality, failing remote optics, or a declining optical margin. |
| Tx Power | Laser-output decline, module stress, or an optic approaching end of life. |
| Tx Bias Current | Laser aging or increasing effort required to maintain optical output. |
| Temperature | Outdoor-cabinet heat, inadequate ventilation, fan failures, or stressed optics. |
| Supply Voltage | Power instability, marginal cabinet power, or module-level voltage problems. |
DDM values are not interchangeable between all optic models. A receive-power reading that is healthy for one long-reach single-mode optic may be dangerously low for another module or fiber design. Always compare values to the optic data sheet, its alarm values, the engineered link budget, and the commissioning baseline for that specific link.
Verify SFP Diagnostic Fields Before Writing a Script
Start by checking the optic manually. The following command returns the available diagnostics for an individual SFP interface:
/interface ethernet monitor sfp-sfpplus1 onceOn a supported optic, RouterOS may return values similar to:
status: link-ok
sfp-module-present: yes
sfp-temperature: 33C
sfp-supply-voltage: 3.237V
sfp-tx-bias-current: 2mA
sfp-tx-power: -5.792dBm
sfp-rx-power: -5.220dBm
eeprom-checksum: goodAlso review interface detail when commissioning an optic:
/interface ethernet print detail where name="sfp-sfpplus1"Do not assume every optic supports every field. A missing DDM field is not the same as a value of zero. Scripts should report unavailable diagnostics clearly rather than treating missing data as an optical failure.
Build a Scalable Script Architecture
A reliable diagnostic design separates collection, evaluation, alerting, and reporting. This makes it easier to test each component and avoids creating an unmanageable monolithic script.
- Port list: Define the interfaces to monitor in one location.
- Collector: Read the diagnostic values from each optic.
- Evaluator: Compare readings against warning and critical thresholds.
- State tracking: Alert only when the status changes or remains critical for a defined number of polling cycles.
- Reporter: Send meaningful logs, syslog messages, emails, or monitoring events.
- Trend storage: Export readings to a central platform for graphing and long-term analysis.
For example, a router with eight uplinks can poll each optic every five minutes. A warning may require three consecutive low readings before an alert is sent, preventing a transient event from generating a maintenance ticket. A recovery notification should be generated only when the reading returns to the normal range.
RouterOS Example: Collect DDM Values from Multiple SFP Ports
The following example loops through a selected list of interfaces and writes available diagnostics to the RouterOS log. This is a starting point for collection and validation. Update the port list to match the router.
:local ports {"sfp-sfpplus1";"sfp-sfpplus2";"sfp28-1";"sfp28-2"}
:foreach port in=$ports do={
:local linkStatus ""
:local modulePresent ""
:local rxPower ""
:local txPower ""
:local temperature ""
:local voltage ""
:local biasCurrent ""
/interface ethernet monitor $port once do={
:set linkStatus $"status"
:set modulePresent $"sfp-module-present"
:set rxPower $"sfp-rx-power"
:set txPower $"sfp-tx-power"
:set temperature $"sfp-temperature"
:set voltage $"sfp-supply-voltage"
:set biasCurrent $"sfp-tx-bias-current"
}
:if ($modulePresent != "yes") do={
:log warning ("OPTICS | port=" . $port . " | module=missing")
} else={
:log info ("OPTICS | port=" . $port . \
" | status=" . $linkStatus . \
" | rx=" . $rxPower . \
" | tx=" . $txPower . \
" | temp=" . $temperature . \
" | voltage=" . $voltage . \
" | bias=" . $biasCurrent)
}
}Run the script manually before scheduling it. Confirm that every interface name is correct, that the optic reports diagnostics, and that the expected fields are populated.
Thresholds: Compare Baseline Drift and Absolute Limits
Thresholds should not be copied blindly between links. Define them from three sources:
- The optic vendor’s documented warning and alarm levels.
- The engineered fiber link budget and receiver sensitivity.
- The measured baseline at commissioning.
Absolute thresholds protect against immediate failure. Baseline deviation identifies degradation before the optic reaches its vendor low-alarm value. For example, a 3 dB receive-power drop from a commissioning baseline of -8 dBm is significant, even if the current reading remains above the vendor’s low-alarm value.
The example below shows the concept of parsing the numeric portion of a dBm reading, then comparing it to warning and critical limits. The threshold values are examples only and must be replaced with values appropriate for the specific optic and link budget.
:local getDbm do={
:local rawValue $1
:local unitPosition [:find $rawValue "dBm"]
:if ($unitPosition = nil) do={
:return ""
}
:return [:tonum [:pick $rawValue 0 $unitPosition]]
}
:local rxWarning -14
:local rxCritical -17
:local rxRaw ""
/interface ethernet monitor sfp-sfpplus1 once do={
:set rxRaw $"sfp-rx-power"
}
:local rxDbm [$getDbm $rxRaw]
:if ($rxDbm = "") do={
:log warning "OPTICS | port=sfp-sfpplus1 | Rx diagnostic unavailable"
} else={
:if ($rxDbm < $rxCritical) do={
:log error ("OPTICS CRITICAL | port=sfp-sfpplus1 | rx=" . $rxRaw)
} else={
:if ($rxDbm < $rxWarning) do={
:log warning ("OPTICS WARNING | port=sfp-sfpplus1 | rx=" . $rxRaw)
} else={
:log info ("OPTICS NORMAL | port=sfp-sfpplus1 | rx=" . $rxRaw)
}
}
}For production use, build separate warning and critical limits per port or optic class. A 10 km single-mode optic, a 40 km optic, a CWDM optic, and a short-reach multimode optic should not necessarily use the same receive-power threshold.
Schedule the Diagnostic Script
Five-minute polling is a practical starting point for active uplinks. It provides useful operational visibility without generating unnecessary script activity or log volume. Longer intervals may be appropriate for highly stable backbone links, while critical links may justify more frequent checks after validating CPU impact.
/system script
add name=optics-ddm-monitor \
policy=read,write,test,policy \
source={
# Paste the validated optics monitoring script here
}
/system scheduler
add name=optics-ddm-monitor \
interval=5m \
start-time=startup \
on-event=optics-ddm-monitor \
policy=read,write,test,policyTest the script manually first:
/system script run optics-ddm-monitor
/log print where message~"OPTICS"Send Alerts to Syslog and Email
Warnings should be visible without overwhelming the operations team. A practical policy is to write warnings to local and remote syslog, reserve email or on-call alerts for critical conditions, and send recovery notices when a condition clears.
RouterOS can forward script logs to a remote syslog collector:
/system logging action
add name=remote-optics \
target=remote \
remote=10.255.20.50 \
remote-port=514 \
bsd-syslog=yes
/system logging
add topics=script action=remote-optics prefix="OPTICS"For smaller environments, email can be used for critical alerts after SMTP is configured on the router:
/tool e-mail send \
to="noc@example.com" \
subject="CRITICAL Optical Alert: sfp-sfpplus1" \
body="Rx power on sfp-sfpplus1 is below the critical threshold. Review optics, fiber cleanliness, patch cords, and remote transmit power."Netwatch can complement DDM monitoring by testing reachability to a remote endpoint across the fiber path. It does not read optical values, but it can verify whether an optic alarm corresponds to an actual connectivity or service-impact condition.
Validate Script Performance Before Production Deployment
Before enabling diagnostics across a production fleet, validate performance on the actual RouterOS release and router model. This is especially important on devices that already handle high packet rates, routing convergence, queues, firewall processing, BGP, MPLS, VXLAN, or other CPU-intensive functions.
- Confirm all monitored ports return valid diagnostics after a reboot.
- Confirm the script handles a link flap and a removed optic correctly.
- Temporarily lower a threshold to validate warning and critical alert paths.
- Measure collection time with the full production interface list.
- Confirm script execution completes well inside the scheduler interval.
- Monitor CPU, memory, and script execution behavior over at least 24 hours.
- Confirm that unavailable DDM data produces a clear unavailable status rather than a false-normal result.
- Retest after RouterOS upgrades, optic changes, hardware changes, and major configuration changes.
Frequently Asked Questions
Can RouterOS detect SFP degradation before a link fails?
Yes. RouterOS can read supported optic diagnostics such as receive power, transmit power, transmitter bias current, temperature, and voltage. By comparing these readings to documented thresholds and commissioning baselines, an operator can identify degradation before it causes a link-down event.
Do all SFP modules support DDM?
No. Many modern optical modules support diagnostics, but field availability depends on the optic, vendor implementation, interface type, router hardware, and RouterOS release. Always validate diagnostic support with /interface ethernet monitor <interface> once.
What Rx power should trigger an alert?
There is no universal threshold. Use the optic data sheet, receiver sensitivity, engineered link budget, warning and alarm thresholds, and the known baseline for that specific link. A meaningful drop from baseline can matter even before a vendor low-alarm value is reached.
Can these scripts work on existing RouterOS routers?
Yes. RouterOS scripts, scheduler entries, logging, email, and remote syslog can be added to existing installations. Validate field names and script impact on the exact RouterOS version and hardware model before enabling broad production polling.
The Bottom Line
Optical monitoring should not begin only after a customer-impacting outage. A documented baseline, practical warning thresholds, RouterOS collection scripts, and centralized alerting give operators time to clean a connector, replace a patch cord, correct cabinet cooling, or replace a marginal optic before service is affected.
The best SFP monitoring design combines optic diagnostics, interface errors, reachability checks, link-budget knowledge, and trend analysis. When those elements work together, fiber failures become predictable maintenance events instead of emergency outages.
References
- MikroTik RouterOS Ethernet and SFP Diagnostics Documentation
- MikroTik RouterOS Scripting Documentation
- MikroTik RouterOS Scripting Tips and Tricks
- MikroTik Wired Interface and Transceiver Compatibility
Need help monitoring your optical network?
Link Technologies, Inc. provides MikroTik scripting, fiber diagnostic monitoring, RouterOS automation, optical-link troubleshooting, syslog integration, and ISP-grade network engineering.
Contact Our Team Explore Our ServicesDennis Burgess is the Chief Technology Officer of Link Technologies, Inc., a MikroTik Certified Trainer, and a network engineer with more than 20 years of experience supporting ISP, WISP, enterprise, and infrastructure environments.
Link Technologies, Inc.
IT Infrastructure • Wireless • Fiber • Hosting • Cloud Services
shop.linktechs.net • sales@linktechs.net • 314-735-0270
Leave your comment