The easiest and most basic way of detecting a power loss would be through snmp trap configuration. Every time a Power Supply fails or comes online an SNMP trap will be logged. Once you configure snmp and trap groups along with a trap receiver you can then monitor the time between power off and on.
(KB on Health monitor of CPU, but has config options for trap-groups)
http://kb.juniper.net/InfoCenter/index?page=content&id=KB16450
The more complicated way would be to have a system polling the device via ssh and grep certain key words to verify its status:
For the 4200 you would have to use:
When the power fails you will see "failed" for the status. So when you use the ssh login mentioned before poll this command and grep for "failed":
juniper@access2-2# run show chassis environment |match power
Power FPC 0 Power Supply 0 Failed
FPC 0 Power Supply 1 OK
If you gather this along with the system date then you can count down the time you have available for the UPS batteries:
juniper@access2-2# run show system uptime
fpc0:
--------------------------------------------------------------------------
Current time: 2010-12-06 00:26:14 EST
System booted: 2010-11-24 01:36:29 EST (1w4d 22:49 ago)
Protocols started: 2010-11-24 01:37:52 EST (1w4d 22:48 ago)
Last configured: 2010-12-05 21:30:42 EST (02:55:32 ago) by juniper
12:26AM up 11 days, 22:50, 1 user, load averages: 0.08, 0.05, 0.01
One recommendation I would make, if you are not already running it would be to upgrade to the 10.4R5 release, which has had a "resilient file system" feature, for cases much like you are seeing with "corrupted junos software" from a improper shutdown.
You can see the details of how to upgrade and the feature details in the 10.4R5 release notes:
Select "Junos OS Release Notes for EX Switches" ---> The first section is "New Features" which talks about the "resilient dual-root Partitions":
http://www.juniper.net/techpubs/en_US/junos10.4/information-products/topic-collections/release-notes/10.4/index.html
I hope this helps,
Wade
==========================================================
If the information provided helps please check it as an "accepted solution"