By Mark Hosenball

WASHINGTON (Reuters) - Recent attacks on civilians in the U.S. and Europe have exposed a gap in the intelligence community’s efforts to track suspected extremists and prevent mass killings, a half dozen American, British and French counterterrorism officials told Reuters.

The attacks have a common theme of being carried out by actors with an apparent history of mental illness - but few if any direct links to extremist groups, the officials told Reuters.

From both a legal and a strategic perspective, counterterrorism investigators globally are focused on plots by established violent groups with known ideologies, such as Islamic State. In the U.S., laws designed to protect citizens from intrusive government spying can limit investigations of individuals unless they have provable ties to foreign terror groups.

Counterterrorism officials told Reuters that the assailants in a recent spate of mass killings all had histories of apparent mental illness. They included the mass shooting at a gay nightclub in Orlando, Florida; the murder of a British parliamentarian in Northern England; the killings of police in Baton Rouge, Louisiana and Dallas, Texas; the Bastille Day truck attack in Nice, France; and Friday’s mass shooting at a German shopping mall.

The counterterrorism officials in the U.S. and Europe spoke on condition they and their organizations remained anonymous.

On Saturday, Munich police chief Hubertus Andrae said the Munich mall gunman, identified in news reports as Ali David Sonboly, had undergone psychiatric treatment before the attack and was obsessed with mass killings. He had no criminal record, and had no known connections to extremist groups.

The German-Iranian 18-year-old, a local resident, shot and killed nine people after opening fire near Olympia shopping mall.

The tactics in such attacks contrast sharply with the attacks in Paris last November and Brussels in March, which were carried out by groups of militants with direct links to Islamic State.

Existing systems for collecting intelligence on extremists are not set up to identify individuals with a history of mental illness who come into contact with people or propaganda that could incite them to engage in violence, the intelligence officials told Reuters.

In the attack in Orlando, the perpetrator had viewed online jihadist propaganda, possibly produced by the Islamic State, the investigators said. But subsequent probes turned up no evidence the Orlando shooter, Omar Mateen, had any significant connections with Islamic State or any other militant organizations.

French investigators have arrested five alleged accomplices in the Nice attacks, but they have so far found no evidence that the attack was directed by foreign militants, according to a U.S. counterterrorism official and a French official.

The case of Mateen perhaps best exemplifies the difficulty in detecting and preventing attacks by single perpetrators with a history of mental illness.

Federal officials have acknowledged that, for about 10 months in 2013 and 2014, the FBI investigated Mateen after he allegedly boasted to co-workers about supposed connections to Al Qaeda and other militant groups. While he was under investigation, the FBI placed Mateen's name in three government databases, one of which is intended to trigger additional scrutiny if an individual passes through airport or border checkpoints.

But having found no evidence that Mateen had any real connections to militants, the FBI closed its investigation and his name was removed from the databases, two U.S. intelligence officials told Reuters.

The U.S. officials said those decisions were made to comply with laws designed to limit invasive government surveillance on all Americans. Neither the CIA nor National Counterterrorism Center are allowed to collect and retain information on American suspects who have no provable links to international terrorist groups, the two U.S. officials said.

About three weeks before he carried out the massacre in Orlando, Mateen told an acquaintance that he was worn out from staying up all night to research psychiatric medication, the acquaintance, who asked not to be named, told Reuters. Mateen also told this person that he was worried that he had slipped into psychosis.

U.S. officials also said they are investigating the role mental health issues may have played in the shooting of police in Baton Rouge and Dallas. In both attacks, the shooters had displayed signs of apparent mental illness and extreme views before their rampages.

A lawyer who represented the Dallas shooter, Micah Xavier Johnson, told the Associated Press than when Johnson was accused of sexual harassment by a female soldier, Johnson’s victim suggested in a court filing that Johnson should “receive mental help.”

Baton Rouge shooter Gavin Long told friends and relatives that he suffered from post-traumatic stress disorder, a source involved in the investigation told CNN.

"When someone with mental health issues snaps, there usually is some external stimulus that also is involved and provides a sort of organizing framework for the violent act," noted Paul Pillar, a former senior CIA analyst. "Identifying those who might commit such acts and doing something to prevent the attacks will always be very difficult.”

The shooting rampage on Dallas police officers, for instance, followed disputed shootings of black men by police officers in Minnesota and Louisiana. The Dallas suspect told police during a standoff that he had aimed to kill white police officers to avenge the deaths of black men shot by officers nationwide.

A soldier who served in Afghanistan with Johnson, however, said his Dallas attack reflected illness more than ideology.

"It's not racism,” Heather Brooks wrote on Facebook, the Dallas Morning News reported. “It was mental sickness, unchecked and untreated.”

The Baton Rouge suspect posted online videos saying that only violent resistance would stop the shootings of black Americans by police.

Before the latest round of deadly attacks in Munich, Nice, Baton Rouge, Dallas and Orlando, officials say U.S. and European spy agencies were overwhelmed trying to identify and track militants who received direct training from militant groups, notably Syria-based Islamic State.

Given their preoccupation with Islamic militants - including ISIL, Yemen-based Al Qaeda in the Arabian Peninsula and Somalia-based Al Shabaab – spy agencies such as the CIA, FBI and U.S. National Counterterrorism Center (NCTC) and Britain's MI5 only have the manpower to track limited numbers of individuals at any one time, counterterrorism officials told Reuters.

Britain's MI5, for instance, is deeply involved in trying to keep track of Britons and British-based foreigners involved with Islamic militants, but does not collect or retain data on right-wing extremists in the U.K.

Counterterrorism officials therefore were unaware of Thomas Mair, a man with a history of apparent mental illness who shot British Labour Party parliamentarian Jo Cox in June - even though his contacts with a U.S. neo-Nazi group brought him to the attention of the Southern Poverty Law Center, a U.S. group that monitors extremists.

Organized terror groups are difficult enough to track and stop, but the self-radicalized individuals with a history of mental illness represent an entirely new variety of terror, said former CIA and National Security Agency Director Gen. Michael Hayden. 

“I'm near petrified, too,” he said. “This phenomena, as I see it, allows the truly troubled and the truly dangerous to reach for a broader cause that gives meaning to their alienation."

(This version of the story was refiled to fix the capitalization in paragraph 28)

(Reporting By Mark Hosenball; Editing by David Rohde and Brian Thevenot)