get packet-loss value from ping

Is there a script that gets the built-in packet-loss percentage value of mikrotik’s ping command and send a mail when a specific threshold is reached? I’m not very good at scripting, could please someone point me in the right direction?

Below is an extract from the Badjr’s failover detection code

Take it for a spin

:local totalpingsreceived 0;
:local avgrtt 0;
:local pinglimit 4

/tool flood-ping address=8.8.8.8 count=$pinglimit interval=00:00:01 timeout=00:00:01 do={
        :if ($sent = $pinglimit) do={
             :set totalpingsreceived $"received"
             :if ($totalpingsreceived > 0) do={
                  :set avgrtt $"avg-rtt"
             }
        }
}

:put ("Total Pings Received: ".$totalpingsreceived)
:put ("Avg Rtt: ".$avgrtt)
:put ("Percentage: ".(($pinglimit*100) / ($totalpingsreceived)))