For example: All bacteria prodcur Flagellin that can be detected by the plant to elicit a immune response. How does the plant 'know' which bacteria beneficial or virulent and when to orchestrate an immune response?
Well immune responses in plants I think depend largely on signaling molecules produced by the associated microbe.. This defines or trigers the particular plant response.. Should it be defence response if the microbe in question is a pathogen or beneficial one
It depends partly on how conserved the flagellin flg22 peptide in a particular bacteria is to that of the Pseudomonas syringae flg22. (e.g. in most gamma proteobacteria, it is well-conserved; in alphaproteobacteria, such as Rhizobia, it is poorly conserved. See Felix et al., 1999. Plants have a sensitive perception system for the most conserved domain of bacterial flagellin. Plant Journal 18:265-276.) That determines how well it elicits a response through plant FLS2 receptor. It also depends on what other factors a particular bacterial species might have that might suppress a defense response.
there are signalling pathways of plant hormone for the recognition of beneficial microbes or non-beneficial microbes. for example, non-beneficial microbe will activate gene encoding Salicylic Acid (SA) corresponding for SAR (systemic acquire resistance), while beneficial microbes will activate gene encoding jasmonic acid (JA) and ethylene (ET) corresponding to ISR (induce systemic resistance).