Lets give this a try: Download this app Temperature Gauge Pro. You may need to do this on a different system given your problem. You'll need to buy it as you want to enable the logging function. What does it tell you. If you can post a snapshot here for us to see.
If you do have access to a second system you could also give this a try: Configure your iMac in Target mode and though a cable connection to your other system see if it is stable and if it is copy off anything important and then see if you can run disk utility to check out your iMac's HD.
Review the Temperature Gauge Pro log see if the system shutdown due to over heating if its shutting down without any clues. Disconnect as much as you can to make sure the external devices are not causing you problems.
I would also check the power services of your home. Is the outlet you are using 20 Amp service? Is it properly wired & grounded? You can get a cheap outlet tester at a hardware store to check this. Do you have a surge suppressor or UPS? You may want to ask your local power utility for borrow a recording power meter if you think the power they are supplying is the issue. Lastly you may want to ask an electrician to check your homes wiring and grounding, if you live in an area which has a lot of lightning.
Let us know what you discover - Good Luck