In a desperate bid to save her rocky marriage, SHARON OSBOURNE has agreed to take back her madman husband OZZY – but ONLY if the aging rocker submits to random drug and alcohol testing.

The couple, who've been married for more than three decades, started living apart after Ozzy suffered a booze and drug relapse. When Sharon issued her ultimatum for him to either agree to the random testing or take a hike, a contrite Ozzy immediately said yes.

"Ozzy will do anything Sharon asks as long as she'll take him back," said a source. "He can't bear to live without her.

"But she's warned him, no more sneaking around behind her back doing drugs or drinking.

"Any slip-ups And she's out of there. And to make sure he stays on the straight and narrow, he's agreed that Sharon can give him random drug and alcohol tests."

As The ENQUIRER recently reported, Sharon, 60, walked out on her 31-year marriage to the heavy metal rocker because she was terrified he was killing himself with his out-of-control substance abuse.

On April 15, the 64-year-old Black Sabbath frontman posted on Facebook that he'd suffered an 18-month relapse after 10 years of clean living. At the time of his status update, a remorseful Ozzy claimed that he'd been sober again for six weeks.

"I was in a very dark place and was an a****** to the people I love most, my family," he wrote.

He also went on to insist that he and Sharon were not planning to divorce and that he was "just trying to be a better person."

During the relapse, "Ozzy was up to his old ways, gobbling down a bunch of pills and then chasing them with bottles of booze, beer, brandy — you name it" as he battled depression over bandmate TONY IOMMI's battle against cancer and son JACK's diagnosis with multiple sclerosis, said a source.

At the time of Ozzy's Facebook mea culpa, he and Sharon had already been living apart for more than a month — and she was spotted out without her wedding ring.

"Sharon told Ozzy that it's all well and good for him to be sober for a month and a half, but she wouldn't go back to him until he's on the wagon for at least three months," said the source.

And on "The Talk," Sharon made it clear that if Ozzy didn't stay sober, he'd be taken to the hospital "to have my foot removed from his ass!"

Now, it appears the wedding ring is back on — and so is the marriage.

The two were recently spotted on the red carpet at an event while delighted daughter KELLY looked on and having a cozy lunch at the Beverly Hills Hotel.

Meanwhile, assistants were seen moving clothes and boxes from Ozzy's rented Beverly Hills mansion into Sharon's nearby digs.

"Sharon was blindsided by Ozzy's relapse," added the source. "Now, she's not taking any chances.”