There are a few apps (like Rescuetime) that track what you're doing with your computer. And there are some tools that can help you when you decide you want to eliminate distrcations (StayFocused Chrome plugin), Selfcontrol. And approaches that can help too (e.g. Pomodoro, full-screen emacs).
What I'd like help with more specifically is fragmentation. Too often I find that when I am performing a difficult task (writing, coding), my brain wants to switch off by moving to other tasks (eg email/internet/etc - which sometimes are legit, but often are unnecessary).
I would like to be constrained so that I keep my focus on my app, but when I try to switch apps (using command-tab), this starts a 5 minute timer & returns me to my document. At the end of the 5 minute timer a popup asks me if I still want to switch apps (if I still feel the need to).