Emergency shutdown pump

Hello,
I would like help with the following project.
A pump hangs from a switchable socket with energy measurement. If the pump runs 3 times within 10 minutes, the socket should be switched off and after 30 seconds. be switched on again.
If the pump runs again 3 times within 10 minutes, the socket should be switched off permanently.
It is then switched on manually after a visual inspection of the system.
I thank you in advance for your support.
Dieter