Scripting for POE status

Ok so this is a bit related to another couple of threads.

mikrotik-hex-s-doesnt-show-full-output-info
reading-poe-status-with-script
poe-power-script

So I’m looking to develop/improve some code and I need it to detect if any port has POE output active.
To do some testing I’ve hacked together a bit of code which I can just dump into a ssh console to test.

Code to test
[
:put ("--- POE Status Report ---");
:foreach line in=[/interface ethernet poe monitor [find] as-value once] do={
:local status ($line->"poe-out-status");
:local interfaceName ($line->"name");
:if ($status = "powered-on") do={
:put ("Interface: " . $interfaceName . " | Status: " . $status);
} 
}
:put ("---------------------------");
]

On a CRS328-24P-4S+ this spits back a logical result.

--- POE Status Report ---
Interface: ether1 - 192.168.1.30 | Status: powered-on
Interface: ether3 - 192.168.1.31 | Status: powered-on
Interface: ether5 - 192.168.1.32 | Status: powered-on
---------------------------

On a RB951Ui-2nD or RB952Ui-5ac2nD this script fails to spit out the correct (in my head at least) output.

If I dig a little deeper and just run this snippet below, all three devices show a logical output and all three have an interface with poe-out-status=powered-on

:put [/interface ethernet poe monitor [find] as-value once]

Typical output

CRS328 output
id=*1;name=ether1 - 192.168.1.30;poe-out=auto-on;poe-out-current=62;poe-out-power=32;poe-out-power-pair=b;poe-out-status=powered-on;poe-out-voltage=519;poe-voltage=auto; (Pruned output)
-------------------------------------------------------------------------------
RB951s output
.id=*6;name=ether5;poe-out=auto-on;poe-out-power-pair=b;poe-out-status=powered-on

So the question is, what am I missing about the “code to test” that stops it from finding the “poe-out-status” field in the RB951’s. I’ve been going around in circles for about a day and I could really use some help.

Your code works fine on my RB951Ui-2HnD. Perhaps your interfaces have bad characters in their names. For example, my ether5_camera port prevented the script from working, but the ether5camera port worked.

I am running 7.20.6, if this is another ‘feature’ I might just have to fork my eyes out..

One of the RB’s has “ether5” as the name. I would have thought that should work.
The other had something more complicated which I modified, it didn’t change the outcome.

It seems like foreach iterates through all interfaces, not just those with POE, so it seemed to work on switches with all POE ports.

You probably need to first get port numbers with POE and then iterate through them used numbers.

or try this

:foreach line in=[/interface ethernet find] do={
:local interfaceName [/interface ethernet get $line name];
:local status ([/interface ethernet poe monitor $line once as-value]->"poe-out-status");
:if (([:len $status] > 0) and ($status = "powered-on")) do={
:log info ("Interface: " . $interfaceName . " | POE-Status: " . $status);
}
}

I tried a couple of more devices. A hAP ac and a L009UiGS, both behave the same as the RB951’s. So yeah maybe your on to something with the thought of it working on devices with all POE ports.
I tried your script but it fails to catch anything, even on the CRS328.

I’ll have a look tomorrow with some fresh eyes.

ok, by the way, I checked it on hEX PoE and everything works fine there too.

I recommend not renaming interfaces to indicate their usage, but instead use comments for that.

I know that renaming the interface makes it automatically propagate to other places like setting an IP address, but having nonstandard names can also be a nuisance.

Right, @safelogj good thing I went to bed. I didn’t even spot your snippet was sending the output to the log rather than putting out to console. That is working on all the four devices. Thanks a bunch.

@pe1chl I take it you have been on the receiving end of interface renaming issues? I’ve learnt from either yourself or one of the other masters around here to at least not to have spaces in names.

Well, when it is the cause of a problem it would be best to undo it. But anyway, WHEN you rename them at least make sure you do not use any special characters. Anything that makes it into a quoted name in an export should certainly be avoided.

This can be used as a starting point for quite a few things.

{
    /interface ethernet
    :put "Flags: D - Disabled, E - Enabled, N - Not PoE capable, P - PoE Capable"
    :put "NAME\t\t\tPRIORITY\tPOE-OUT\t\tINPUT\tSTATUS\t\t\tCURRENT\t\tVOLTAGE\t\tPOWER"
    :foreach IF in=[find] do={
        :local IFdi  "E"                    ; :if ([get $IF disabled]) do={:set IFdi "D"}
        :local IFco  [get $IF comment]      ; :if ($IFco != "") do={:set IFco ";;; $IFco\r\n    "}
        :local IFdn  [get $IF default-name]
        :local IFpc  "N"
        :local IFtmp [get $IF]
        :local IFpo  ($IFtmp->"poe-out")
        :local IFpr  ($IFtmp->"poe-priority")
        :local IFpv  ($IFtmp->"poe-voltage")
        :if ([:typeof $IFpv]="nothing") do={:set IFpv "="}
        :if ([:typeof $IFpo]!="nothing") do={:set IFpc "P"}
        :local MN    ""
        :local MNst  ""
        :local MNcu  ""
        :local MNvo  ""
        :local MNpo  ""
        :if ($IFpo~"(auto-on|forced-on)") do={
            :set MN   [poe monitor [get $IF name] once as-value]
            :set MNst ($MN->"poe-out-status")
            :if ($MNst="powered-on") do={
                :set MNcu (($MN->"poe-out-current") . "mA")
                :set MNvo (($MN->"poe-out-voltage") . "00mV")
                :set MNpo (($MN->"poe-out-power")   . "00mW")
            }
        }
        :put "$IFdi $IFpc $IFco$IFdn\t\t$IFpr\t\t$IFpo\t\t$IFpv\t$MNst\t\t$MNcu\t\t$MNvo\t\t$MNpo"
    }
}

Out example on CRS112-8P-4S (yes, is like RouterOS style):

Flags: D - Disabled, E - Enabled, N - Not PoE capable, P - PoE Capable
NAME                    PRIORITY        POE-OUT         INPUT   STATUS                  CURRENT         VOLTAGE         POWER
E P ;;; PTP
    ether1              10              auto-on         low     powered-on              227mA           26500mV         6000mW
E P ;;; (non collegato)
    ether2              20              auto-on         low     waiting-for-load
E P ;;; DIFF
    ether3              30              auto-on         low     powered-on              240mA           26500mV         6300mW
E P ;;; DIFF
    ether4              40              auto-on         low     powered-on              142mA           26600mV         3700mW
E P ;;; DIFF
    ether5              50              auto-on         low     powered-on              139mA           26600mV         3600mW
E P ;;; [PoE OFF] - RISERVA - DIFF
    ether6              60              off             low
E P ;;; (non collegato)
    ether7              70              auto-on         low     waiting-for-load
E P ;;; [PoE OFF] - CLI
    ether8              80              off             low
E N ;;; (nessun modulo)
    sfp9                                                =
E N ;;; (nessun modulo)
    sfp10                                               =
E N ;;; (nessun modulo)
    sfp11                                               =
E N ;;; (nessun modulo)
    sfp12                                               =

Out example on CRS318-16P-2S+

Flags: D - Disabled, E - Enabled, N - Not PoE capable, P - PoE Capable
NAME                    PRIORITY        POE-OUT         INPUT   STATUS                  CURRENT         VOLTAGE         POWER
E P ether1              10              forced-on       low     powered-on              239mA           25700mV         6100mW
E P ether2              20              forced-on       low     powered-on              347mA           25700mV         8900mW
E P ether3              30              auto-on         low     powered-on              164mA           25700mV         4200mW
E P ether4              40              auto-on         auto    powered-on              175mA           49300mV         8600mW
E P ether5              50              forced-on       low     powered-on              226mA           25700mV         5800mW
E P ether6              60              off             auto
E P ether7              70              off             low
E P ether8              80              off             low
E P ether9              89              auto-on         low     powered-on              160mA           25800mV         4100mW
E P ether10             90              auto-on         low     powered-on              156mA           25800mV         4000mW
E P ether11             91              auto-on         low     powered-on              155mA           25800mV         3900mW
E P ether12             92              forced-on       low     powered-on              357mA           25600mV         9100mW
E P ether13             93              forced-on       low     powered-on              355mA           25700mV         9100mW
E P ether14             94              off             low
E P ether15             95              auto-on         low     powered-on              113mA           25800mV         2900mW
E P ether16             96              auto-on         low     waiting-for-load
E N sfp-sfpplus1                                                =
E N sfp-sfpplus2                                                =

Out example on RB960PGS (PowerBox Pro)

Flags: D - Disabled, E - Enabled, N - Not PoE capable, P - PoE Capable
NAME                    PRIORITY        POE-OUT         INPUT   STATUS                  CURRENT         VOLTAGE         POWER
E N ether1                                              =
E P ;;; PTP
    ether2              10              auto-on         =       powered-on              149mA           27700mV         4100mW
E P ;;; DIFF
    ether3              20              auto-on         =       powered-on              161mA           27700mV         4400mW
E P ;;; DIFF
    ether4              30              auto-on         =       powered-on              157mA           27700mV         4300mW
E P ;;; DIFF
    ether5              40              auto-on         =       powered-on              221mA           27700mV         6100mW
E N ;;; (nessun modulo)
    sfp1