Understanding Reverse Polish Notation

RPN is used in OpenNMS’ RRD graph definitions to calculate metric values, and is often not easy to understand. But it is not as complicated as it seems. If you want to create expression-based threshold definitions, you will have to use this notation.

Let’s look at an example!

OpenNMS interface error percentage graph is defined like this:

ifInErrors,ifHCInUcastPkts,ifHCInMulticastPkts,+,ifHCInBroadcastPkts,+,ifInDiscards,-,/,100,*

In the community chat Michael Kelly gave a very good explanation.

Think of RPN as a push-down stack.
You must push at least 2 values onto the stack.
Every time you provide an operator, the top two values are popped from the stack and the result is pushed back onto the stack.
When the calculation is complete, there is only one value on the stack.

We push three values (ifInErrors, ifHCInUcastPkts and ifHCInMulticastPkts) onto the stack.
Then we pop the top 2 values (ifHCInUcastPkts and ifHCInMulticastPkts) and add them and push the result back on the stack.
Next we push ifHCInBroadcastPkts onto the stack, then pop the top 2 and add them and push the result back onto the stack.
At this point we have 2 values in the stack, ifInErrors and the sum of the *castPkts.
Next we push the ifInDiscards value onto the stack, do the subtraction and push the result back on the stack.
Next we do the divide operation and push the result back onto the stack.
At this point we only have one value in the stack but the calculation isn’t finished.
We push the value 100 onto the stack and do the multiplication leaving us with the result.

I hope that this will be helpful to someone.

The final result is the same as:
((((ifHCInUcastPkts + ifHCInMulticastPkts) + ifHCInBroadcastPkts)-ifInDiscards)/ifInErrors)*100


:woman_facepalming: You can fix me, I’m a wiki post.