"And there was a famine in the days of David three years, year after year; and David sought the face of the LORD" etc. (2 Samuel 21:1). David said: Go out and see, perhaps there are among you people who worship idolatry, for the heavens are not held back from bringing down dew and rain except on account of the sin of Israel, that they worship idolatry. This is what is written: "Take heed to yourselves, lest your heart be deceived" etc., "and the anger of the LORD be kindled against you, and He shut up the heavens" etc. (Deuteronomy 11:16-17). They went out and searched and did not find. He said: Perhaps there is sexual immorality among you, as it is written: "[And you have polluted the land with your harlotries] etc.; therefore the showers have been withheld, and there has been no latter rain; yet you had a harlot's forehead" etc. (Jeremiah 3:2-3). They went out and searched and did not find. He said to them: Perhaps there is bloodshed among you, as it is written: "So you shall not pollute the land" etc., "[for blood, it pollutes]" etc. (Numbers 35:33). They went out and searched and did not find. He said to them: Perhaps you vow and do not fulfill vows, as it is written: "Clouds and wind, but no rain, is a man who boasts of a gift falsely" (Proverbs 25:14). They went out and searched and did not find. He said to them: Perhaps you do not tithe your tithes properly, for on account of the sin of tithes the rains are withheld, as it is written: "Bring all the tithe" etc. (Malachi 3:10). They went out and searched and did not find. From this point on David said: The matter depends on none but me. The Holy One, blessed be He, said to him: David, you are spared; rather, it is "because of Saul" (2 Samuel 21:1), that you did not perform kindness with him, "and his bloody house, because he put to death the Gibeonites" (ibid.). At that moment David said: Is it on account of the converts that the Holy One, blessed be He, has done thus to my people? The Holy One, blessed be He, said to him: If you push away those who are far, you will end up pushing away those who are near. Go out and learn from Joshua your master, that at the time when the Gibeonites said, "Come up to us quickly, and save us, and help us" etc. (Joshua 10:6), at that moment Joshua said: Is it on account of these converts that we trouble the community? The Holy One, blessed be He, said to him: If you push away those who are far, you will end up pushing away those who are near. Go out and learn from where your stock is [it is not from converts]. This is what is written: "And to Joseph in the land of Egypt were born, whom Asenath the daughter of Poti-phera bore to him" etc. (Genesis 46:20), and it is written below: "Of the tribe of Ephraim, Hoshea the son of Nun" (Numbers 13:8). Immediately, "And David said [to the Gibeonites], What shall I do" etc.; "let seven men of his sons be given to us" etc.; "But the king spared Mephibosheth" etc. (2 Samuel 21:3,6,7). Because he was a man of Torah, he prayed for him; they passed him before the altar and it did not seize him. This is what is written: "I will call to God Most High, to the God who fulfills it for me" (Psalms 57:3), for the Holy One, blessed be He, agreed with his hand.
David Investigates the Three-Year Famine and the Gibeonites
Midrash Shmuel 28:5
ויהי רעב בימי דוד שלש שנים שנה אחרי שנה ויבקש דוד את פני ה' וגו' (שמואל ב' כ"א א'), אמר דוד צאו וראו שמא יש בכם בני אדם שהם עובדים עבודה זרה, שאין השמים נעצרים מלהוריד טל ומטר אלא בעון ישראל שהם עובדין עבודה זרה, הדא הוא דכתיב השמרו לכם פן יפתה לבבכם וגו' וחרה אף ה' בכם ועצר את השמים וגו' (דברים י"א י"ז), יצאו ובקשו ולא מצאו, אמר שמא גילוי עריות ביניכם, דכתיב [ותחניפי ארץ בתזנותיך וגו'] וימנעו רביבים ומלקוש לא היה ומצח אשה זונה היה לך וגו' (ירמי' ג' ב' ג'), יצאו ובקשו ולא מצאו, אמר להם שמא שפיכות דמים ביניכם, דכתיב ולא תחניפו את הארץ וגו' [כי הדם הוא יחניף וגו'] (במדבר ל"ה ל"ג), יצאו ובקשו ולא מצאו, אמר להם שמא נודרין ולא משלמין נדרים, דכתיב נשיאים ורוח וגשם אין איש מתהלל במתת שקר (משלי כ"ה י"ד), יצאו ובקשו ולא מצאו, אמר להם שמא אין מעשרין מעשרותיהן כתיקנן שבעון מעשרות הגשמים נעצרים, דכתיב הביאו את כל המעשר וגו' (מלאכי ג' י'), יצאו ובקשו ולא מצאו, ומכאן ואילך אמר דוד אין הדבר תלוי אלא בי, אמר לו הקדוש ברוך הוא דוד מוצל את, אלא (על) [אל] שאול (שמואל ב' שם), שלא עשיתם חסד עמו, (ועל) [ואל] בית הדמים על אשר המית את הגבעונים (שם שם), באותה שעה אמר דוד וכי בשביל הגרים עשה הקדוש ברוך הוא לעמי כך, אמר לו הקדוש ברוך הוא אם תרחיק את הרחוקים סופך לרחק את הקרובים, צא ולמד מיהושע רבך שבשעה שאמרו הגבעונים עלה אלינו מהרה והושיעה לנו ועזרנו וגו' (יהושע י' ו'), באותה שעה אמר יהושע וכי בשביל הגרים הללו אנו מטריחים על הציבור, אמר לו הקדוש ברוך הוא אם מרחק אתה הרחוקים סופך לרחק את הקרובים, צא ולמד מהיכן הוא [מטעך לא מן הגרים], הדא הוא דכתיב ויולד ליוסף בארץ מצרים אשר ילדה לו אסנת בת פוטיפרע וגו' (בראשית מ"ו כ), וכתיב למטה אפרים הושע בן נון (במדבר י"ג ח'), מיד ויאמר דוד [אל הגבעונים] ומה אעשה וגו', יותן לנו שבעה אנשים מבניו וגו', ויחמול המלך על מפיבושת וגו' (שמואל ב' כ"א ג' ו' ז'), על שהיה בן תורה נתפלל עליו העבירו לפני המזבח ולא קלטו, היינו דכתיב אקרא לאלהים עליון לאל גומר עלי (תהלים נ"ז ג'), שהסכים הקב"ה על ידו.