there are two ways:
1. near-realtime, which is easier. Use SQL queries to get the information from real time tables from ICM. The information is not really realtime, since it is updated in 5-10 seconds interval.
2. CTI monitor, which might be challenging. Use the CTI interface of ICM to 'subscribe' for all/selected events, including state changes and then do your calculations. It's realtime, but again, programming it may take a while.