1. The most reliable way to see if an AI is plotting against you is to go to the diplo screen and see what they say about waging war against someone else. If they say "We have enough on our hands right now" that means they are either already in a war or are plotting one. Since you don't want to go to the diplo screen every turn you should use the BUG mod or BAT mod, which basically doesn't alter anything about the core game but gives you a lot of useful info. It will make a red fist appear beside a leader name if they are plotting war or are in a war. It is EXTREMELY useful for avoiding war.
2. You can ask any AI which is Pleased with you to gift you something of small value (1 gold, small tech). If they give it to you they automatically have a 10 turn peace treaty! So if you see them plotting war, 10 turns is a lot to put up a defense.
3. On the highest levels it's pretty hard to have enough units to survive attacks, so you mostly rely on diplomacy (getting AIs near you to Pleased/Friendly). Up to Emperor I'd say you can maintain a good defensive army in addition to diplomacy.
4. The AIs most likely to attack you are your neighbours, so they are the ones you want to get to Pleased/Friendly to be safe from attacks. How to do that?
Open Borders gives you +1 and after some time +2
Resource trades give you +1 after some time and later +2
Shared Religion can give you between +1 (early on) and +7 I think (later with very religious AIs)
Shared favorite civic can give you about the same as shared religion
Making trades favorable for the AI (tech or gold) can give you up to +4 (the more favorable the better)
Those are the most common ways to get an AI to Pleased/Friendly. You will need between +3 and +5 to get them to Pleased so you don't need to use all of those. Note that you should at least get your immediate neighbours to Pleased unless you plan to attack them soon. Open borders, pick a religion they share (if they have different ones you might not want to pick any), make them favorable trades, see if they have a useful favorite civic. And try to avoid negative attitude (from trading with their Worst Enemy or refusing their demands) from them.
5. The very comprehensive guide to see which leaders will give you which attitude bonuses is here:
http://forums.civfanatics.com/showthread.php?t=478563
6. All AIs can attack at Cautious. A good part of them can attack at Pleased (but is less likely to do so than at Cautious). None can attack at Friendly (save for Catherine being bribed to do so).