My immediate guess is that you have an iMac that is suffering from bad capacitors. These were a common issue with a generation of iMacs, and Apple even extended the warranty on these models to address the issue. I'd back up if you haven't already, and researching. If it is the issue, then you can look into getting a capacitor kit and DIY if you are handy with a soldering iron. If not, its a good time to shop for a new mac.