How to detect APC UPS battery usage and run a script when on battery

apcapc-smart-upsautomationelectrical-powerups

I have a couple APC UPS –

  • Smart-UPS RT 6000 RM XL
  • Smart-UPS RT 5000 RM XL

Unfortunately the power in my office likes to go out (out of my control) and hence the equipment powered by these UPS shuts down. They power a VMware infrastructure environment (VMware Lab Manager) and what I'd like to do is detect when one is on battery (say has been for x amount of time or has x percentage left) and run a script on this event.

What software do I need to detect a on-battery event and have it run a script?

Thanks!