Walmart has agreed to settle a US government lawsuit alleging violations in its handling and monitoring of opioid prescriptions.