Code-as-Monitor: Constraint-aware Visual Programming for Reactive and Proactive Robotic Failure Detection