I have heard that on one occasion a certain monk was dwelling among the Kosalans in a forest thicket. Now at that time, he spent the day's abiding thinking evil, unskillful thoughts: i.e., thoughts of sensuality, thoughts of ill will, thoughts of doing harm.
Then the devata inhabiting the forest thicket, feeling sympathy for the monk, desiring his benefit, desiring to bring him to his senses, approached him and addressed him with this verse:
From inappropriate attention you're being chewed by your thoughts. Relinquishing what's inappropriate, contemplate appropriately. Keeping your mind on the Teacher, the Dhamma, the Sangha, your virtues, you will arrive at joy, rapture, pleasure without doubt. Then, saturated with joy, you will put an end to suffering & stress.
The monk, chastened by the devata, came to his senses.
See also: any of the suttas on the six recollections (e.g., SN 11.3, AN 3.70, AN 11.12, AN 11.13); passage 36 in Wings to Awakening; "The Ten Recollections" (Study Guide).