Shut down is by software 'command'. So what's needed is some way for the Pi to 'discover' that it's running on batteries and that the battery power is about to 'run out'. This does not have to be very 'clever', just 'reliable' (we don't want to shut down when there's plenty of power remaining and we don't want the Pi to be writing to the SDHC card when the power fails).
If you are using LiPO batteries, then you need to stop using them before they are totally flat (a LiPO battery can't be recharged from totally flat) Most systems cut the LiPO off at 3.0v - and it's easy enough to wire up a Zener diode together with a 'Op Amp' style comparator that will 'flip the switch' when the battery reaches 3v0
To give the Pi a chance to shut itself down after power is lost, fit a decent capacitor across the 5v supply = if you trigger 'power lost' at 4.9v, then the Pi has until the capacitor drops below 4.75 to shut down cleanly
If you wire your 'low power' detection to any GPIO pin between #4 and #27 (Hi = run, Lo = low power), you can use the existing clean-shutdown daemon from Pimoroni (for more details, see end of this page)
From this 'How To' reference and the Raspberry Pi GPIO pins spec, it's obvious that the pin you choose to 'sense' the power state can be controlled by software to enable a 'default 0' (internal pull down resistance).
To 'detect' a 24v supply as 'mains-enabled' = 'logic 1', we have to 'pull-up' the pin above 1.3v (or 2v = reports differ) but it's vital not to exceed 3.3v (or at least limit the current to 0.5mA). The internal 'pull down' appears** to be about 50k. For a 24v supply to 'pull up' this up to 2v requires a 24/2x50k = 600k resistor. Current flow into the pin from the 24v would be 22/600k = .04mA (well within the .5mA limit)
So, the cost of 'detect mains lost' is approx 1p (one resistor) !
If your Pi is being used as part of a CCTV camera, and has PiPO (or other) battery backup, you might want to keep it running (and saving to SDHC card) even when the Ethernet cable is cut :-)
However if you are only using PoE (so Ethernet out = no power) or if the Pi is unable to funtion without the Ethernet, you ca use 'los of Ethernet' == 'power-down'.
Of cousre this is a lot easier with any model B Pi since they have built-in Ethernet. The Pi B+ and later have 2 LED's on the Ethernet socket itself :- The Yellow LED is on for a 100-Mbps connection (and this off for 10mbs or when connection is lost) = same as '10M' LED on the B Green LED is on for 'link established' and blinks for 'link activity' (and off for 'no link') = same as 'LNK' LED on the B One Pi user has written custom software to monitor the Ethernet 'link' (the B 'system LEDS', which includes 2 Ethernet LEDS, '10M' or '100' and 'LINK' or 'LNK', are all accessible to the Python user, see here)
The 'best' choice on the B+ for 'Ethernet LAN down = power loss' would be to pick off the yellow LED (no modern Ethernet runs at 10mbs)
For PI's with battery backup, the 'obvious' approach to discovering that 'we are running out of (battery) power' is to monitor the battery voltage 'directly' with some sort of 'voltage detection' circuit (e.g. built around a 741 op-amp and a Zener diode etc. (see image)). An alternative is to use a special 'low voltage detector (LVD) chip' (such as the STM1061).
For example, NiMH battery voltage holds up very well during the discharge cycle with quite a steep voltage 'drop off' as it's capacity is used up, so the 'drop off' point is easy to spot.
An alternative approch is to montor the supply 'input' voltage and compare it to the Pi volts = as soon as input drops below Pi, the '/Pwr off' can be triggered. The drawback to this approach is that quite a decent capacitor is needed on the Pi 5v line to keep the Pi running long enough to shut itself down
If you are using LiPO batteries, then you need to stop using them before they are totally flat (a LiPO battery can't be recharged from the totally flat state). The typical cut-off point is 3v0 (so you need to 'trigger' the 'power-off' at 3v1 and have the Pi respond with a 'disconnect' when it shuts down). If you are using a Powerbank, this will 'switch itself off' as soon as it's internal battery voltage drops too low.
One of the above approaches should work for all UPS designs.