An Exert From the Karaṇīya Mettā Sutta Commentary (Suttanipāta-Aṭṭhakathā) by Buddhaghosa. 
A pdf version of this text is available here.
How did it come about? The brief version is that some beggar-monks  in sight of the Himalayas were troubled by spirits and sought out the Blessed One in Sāvatthī. The Blessed One spoke of this thread for the purpose of protection and as a meditation practice.
The long version of the story goes like this: one-time the Blessed One was staying in Sāvatthī with the rainy season approaching. At that time a great number of beggar-monks from different nations desiring to begin their rainy season retreat came into the Buddha's presence to get a meditation subject. To the passionate types he gave the eleven-fold reflection on unloveliness; to the hot heads he gave the four-fold meditation on loving-kindness; for the deluded types he prescribed mindfulness of death; to people up 'in their heads' he gave the mindfulness of breathing practice; recollection of the Buddha was recommended for faith types; and analysis of the four elements for the intelligent types; and thus he taught the 84,000 meditation subjects to those that suited them.
And then five hundred beggar-monks  having received their meditation practice in the presence of the Blessed One set off seeking suitable accommodation [sappāya-senāsanaṃ] and villages for alms gathering. In the hinterlands they saw a mountain in the Himalayas with flat rocks like blue rock-crystal, adorned by a forest grove with cool dense dark shadows, with sand strewn about like pearls on a silver platter, and surrounded by a cool pleasant pure river.  They stayed the night there and in the morning after attending to their bodies, they entered a nearby village for alms. The village was a dense settling of a thousand people full of faith and confidence. In those border regions the sight of religious wanderers was rare and the delighted villagers having fed the beggar-monks implored them "Good sirs, why not dwell here for the three months of the rainy season?" They built five hundred meditation huts, and provided a platform and seat, bowls of water for drinking and water for washing, and all means of support.
On the second day the beggar-monks entered another village for alms. There also the people, having waited on the monks, implored them to stay for the rainy season. Not seeing any obstacles the beggar-monks assented. They entered the forest grove sat at the foot of trees resolutely all night and day, beating the block to mark the watches of the night,  dwelling full of wise attention. The brightness [teja] of the virtuous beggar-monks interfered with the brightness of the spirits of the trees,  who one by one took their children down from their magic palaces [vimānā] and wandered here and there. They looked on from a distance, and just as when a king or his prime-minister might commandeer a house and the people might ask "when will they leave?" the tree-spirits asked "when are these good men going to leave?". They thought "it looks like they will stay the whole three months of the rainy season. We won't be able to survive down here with our children having had to descend from our magic palaces. We must try to frighten them away." That night while the monks were engaged in their practices the tree spirits appeared before them in the terrifying forms of yakkhas, making frightful noises. Seeing those forms and hearing those sounds the hearts of the beggar-monks pounded, and they turned pale. They could not find any calm in their minds, and upset again and again by fear they were shocked and bewildered. The tree spirits also made a pungent stink that caused the beggar monks splitting headaches, but they did not tell each other about these incidents.
Then one day the senior monk asked the monks to assemble for a meeting. He said "friends when we entered this forest grove a few days ago we had good complexion, we were accomplished, and had clear senses. However now we are haggard and pale. Is this not a good place to stay?" One monk spoke up about his terrifying experiences. Then everyone confirmed that is was the same for them. The elder said "friends, the Blessed One has decreed two possible starting times for the retreat. Since this accommodation is unsuitable we will go and ask the Blessed One for better accommodation and start our retreat again." The monks all said "sādhu"  to that, and without further discussion, leaving all their bedding but taking bowl and robe, they embarked on the journey back to Sāvatthī. By and by they came to the city and met the Blessed One. Seeing them he asked why they had broken the rule about not travelling during the rains retreat, and they related to him all that had happened.  The Blessed One then cast his mind over the whole of India , even considering the places and seats of animals  but did not see suitable accommodation. He said to the beggar monks: "there is no other place you might go to in order to attain the destruction of theinfluxes [āsava].  Go monks, and stay depending on those lodgings. However if you wish to be unafraid of the spirits then learn this protective spell and let this be both your protection and your meditation subject. And he taught them the Karaṇīya Metta Sutta. 
Then having completed the teaching the Buddha said to the monks: "go, monks, and dwell in that very forest grove. On the eight days in the month for listening to the Dhamma  you should repeatedly recite  this sutta having beaten the wooden block [of the watches].  Give dhamma discourses [on the sutta], talk it over and rejoice in it. Devote yourselves to cultivation and pursuit of this meditation. Those spirits will not cause you to see frightful hallucinations, and they will only wish you well and be friendly. The bhikkhus assented, saying "sadhu", then rose from their seats and respectfully  went there. And [this time] the spirits were pleased and joyful to see them, and said "good sirs, we wish you health and happiness". They personally swept out the cabins, prepared hot water, gave the monks foot and back rubs, and settled down to watch over them. Having cultivated loving kindness and made a good foundation the monks began seeking insight. At the end of the three months all of them had attained the highest fruit and become Arahants, and they celebrated the full and pure end of rains ceremony [pavāranā].
 PTS SnA i.193ff.
 bhikkhu means 'beggar'. Monk literally means 'alone' from Greek monos. Neither beggar nor monk quite capture the sense, but together they get closer.
 pañcamattāni bhikkhusatāni – literally 'five measures of a hundred beggar-monks'.
 The description of the place shows a distinct influence of Sanskrit compositions with the use of very long compound adjectives: "… nīla-kācamaṇi-sannibha-silā-talaṃ sītala-ghana-cchāya-nīla-vana-saṇḍa-maṇḍitaṃ muttā-tala-rajata-paṭṭa-sadisa-vālukā-kiṇṇa-bhūmi-bhāgaṃ suci-sāta-sītala-jalāsaya-parivāritaṃ pabbatam-addasaṃsu."
 I'm guessing here from yāmagaṇḍikaṃ koṭṭetvā: yāma could also be 'restraint'; gaṇḍikaṃ is a block of wood, and koṭṭetvā is a gerund from koṭṭeti 'to beat'. PED sv. yāma has a doubtful reading 'to beat the block of restraint'; or allow relating it to Yāma, king of the underworld. However, organised monks on retreat would have marked the periods of the day and night, and banging on a wooden block is an excellent way of doing this, and is in fact used today, i.e. I read PED yāma2 'a watch of the night'.
 'tree spirits' translates rukkhadevatā – these seem to be nature spirits, rather than celestial devas.
 Sādhu means 'good, virtuous; approval, ascent.
 In the suttas the events would have been repeated verbatim, but by contrast here we just get "they told the Blessed one all about it" te bhagavato sabbaṃ ārocesuṃ.
 sakala-jambudīpa literally 'all of the rose-apple island.'
 catuppādapīṭhakaṭṭhānamattampi – I'm not entirely certain of this reading.
 The āsavas 'influxes, cankers, taints' are kāmāsava 'sense desire', bhavāsava 'existence', diṭṭhāsava 'views', avijjāsava 'ignorance'. A list of three āsava leaves out diṭṭhāsava.
 Sayadaw says these are the waxing and waning days of the 5th, 8th, 14th and 15th days in the month. The monks were mostly practicing alone in cabins during this time, but came together for these periods of teaching. See Sayadaw, Mahasi. Brahmavihara Dhamma. [ca. 1983, trans. Min Swe (Min Kyaw Thu)] http://www.buddhanet.net/brahmaviharas/index.htm
 ussāretha literally 'pile up', i.e. chant repeatedly
 see also note 5. This is a very awkward sentence to translate: "Imañca suttaṃ māsassa aṭṭhasu dhammassavanadivasesu gaṇḍiṃ ākoṭetvā ussāretha, dhammakathaṃ karotha, sākacchatha, anumodatha, idameva kammaṭṭhānaṃ āsevatha, bhāvetha, bahulīkarotha."
 padakkhinaṃ katvā is literally 'making the right hand', i.e. keeping the ritually pure right hand towards the object of veneration rather than the impure left hand. The left hand is impure because it is used to clean the anus after defecating. See also: Ritual Purity or Rank Superstition.
image: Himalaya's Bhutan. From www.bhutanmajestictravel.com.