diff --git a/apps/exchange/2010/local/mode/activesyncmailbox.pm b/apps/exchange/2010/local/mode/activesyncmailbox.pm index f339ea2ad..c790dde0c 100644 --- a/apps/exchange/2010/local/mode/activesyncmailbox.pm +++ b/apps/exchange/2010/local/mode/activesyncmailbox.pm @@ -50,7 +50,10 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => - { + { + "remote-host:s" => { name => 'remote_host', }, + "remote-user:s" => { name => 'remote_user', }, + "remote-password:s" => { name => 'remote_password', }, "no-ps" => { name => 'no_ps', }, "timeout:s" => { name => 'timeout', default => 50 }, "command:s" => { name => 'command', default => 'powershell.exe' }, @@ -95,7 +98,10 @@ sub check_options { sub run { my ($self, %options) = @_; - my $ps = centreon::common::powershell::exchange::2010::activesyncmailbox::get_powershell(mailbox => $self->{option_results}->{mailbox}, + my $ps = centreon::common::powershell::exchange::2010::activesyncmailbox::get_powershell(remote_host => $self->{option_results}->{remote_host}, + remote_user => $self->{option_results}->{remote_user}, + remote_password => $self->{option_results}->{remote_password}, + mailbox => $self->{option_results}->{mailbox}, password => $self->{option_results}->{password}, no_ps => $self->{option_results}->{no_ps}, no_trust_ssl => $self->{option_results}->{no_trust_ssl} @@ -128,6 +134,18 @@ Check activesync to a mailbox. =over 8 +=item B<--remote-host> + +Open a session to the remote-host (fully qualified host name). --remote-user and --remote-password are optional + +=item B<--remote-user> + +Open a session to the remote-host with authentication. This also needs --remote-host and --remote-password. + +=item B<--remote-password> + +Open a session to the remote-host with authentication. This also needs --remote-user and --remote-host. + =item B<--timeout> Set timeout time for command execution (Default: 50 sec) diff --git a/apps/exchange/2010/local/mode/databases.pm b/apps/exchange/2010/local/mode/databases.pm index d57b6bf2c..ce88fa9bd 100644 --- a/apps/exchange/2010/local/mode/databases.pm +++ b/apps/exchange/2010/local/mode/databases.pm @@ -51,7 +51,10 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => - { + { + "remote-host:s" => { name => 'remote_host', }, + "remote-user:s" => { name => 'remote_user', }, + "remote-password:s" => { name => 'remote_password', }, "no-ps" => { name => 'no_ps', }, "no-mailflow" => { name => 'no_mailflow', }, "no-mapi" => { name => 'no_mapi', }, @@ -92,7 +95,10 @@ sub check_options { sub run { my ($self, %options) = @_; - my $ps = centreon::common::powershell::exchange::2010::databases::get_powershell(no_mailflow => $self->{option_results}->{no_mailflow}, + my $ps = centreon::common::powershell::exchange::2010::databases::get_powershell(remote_host => $self->{option_results}->{remote_host}, + remote_user => $self->{option_results}->{remote_user}, + remote_password => $self->{option_results}->{remote_password}, + no_mailflow => $self->{option_results}->{no_mailflow}, no_ps => $self->{option_results}->{no_ps}, no_mapi => $self->{option_results}->{no_mapi}, filter_database => $self->{option_results}->{ps_database_filter}, @@ -125,6 +131,18 @@ Check: Exchange Databases are Mounted, Mapi/Mailflow Connectivity to all databas =over 8 +=item B<--remote-host> + +Open a session to the remote-host (fully qualified host name). --remote-user and --remote-password are optional + +=item B<--remote-user> + +Open a session to the remote-host with authentication. This also needs --remote-host and --remote-password. + +=item B<--remote-password> + +Open a session to the remote-host with authentication. This also needs --remote-user and --remote-host. + =item B<--no-mailflow> Don't check mailflow connectivity. diff --git a/apps/exchange/2010/local/mode/imapmailbox.pm b/apps/exchange/2010/local/mode/imapmailbox.pm index d27853791..015abb824 100644 --- a/apps/exchange/2010/local/mode/imapmailbox.pm +++ b/apps/exchange/2010/local/mode/imapmailbox.pm @@ -50,7 +50,10 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => - { + { + "remote-host:s" => { name => 'remote_host', }, + "remote-user:s" => { name => 'remote_user', }, + "remote-password:s" => { name => 'remote_password', }, "no-ps" => { name => 'no_ps', }, "timeout:s" => { name => 'timeout', default => 50 }, "command:s" => { name => 'command', default => 'powershell.exe' }, @@ -94,10 +97,14 @@ sub check_options { sub run { my ($self, %options) = @_; - my $ps = centreon::common::powershell::exchange::2010::imapmailbox::get_powershell(mailbox => $self->{option_results}->{mailbox}, - password => $self->{option_results}->{password}, - no_ps => $self->{option_results}->{no_ps}, - ); + my $ps = centreon::common::powershell::exchange::2010::imapmailbox::get_powershell( + remote_host => $self->{option_results}->{remote_host}, + remote_user => $self->{option_results}->{remote_user}, + remote_password => $self->{option_results}->{remote_password}, + mailbox => $self->{option_results}->{mailbox}, + password => $self->{option_results}->{password}, + no_ps => $self->{option_results}->{no_ps}, + ); $self->{option_results}->{command_options} .= " " . $ps . " 2>&1"; my $stdout = centreon::plugins::misc::windows_execute(output => $self->{output}, timeout => $self->{option_results}->{timeout}, @@ -126,6 +133,18 @@ Check imap to a mailbox. =over 8 +=item B<--remote-host> + +Open a session to the remote-host (fully qualified host name). --remote-user and --remote-password are optional + +=item B<--remote-user> + +Open a session to the remote-host with authentication. This also needs --remote-host and --remote-password. + +=item B<--remote-password> + +Open a session to the remote-host with authentication. This also needs --remote-user and --remote-host. + =item B<--timeout> Set timeout time for command execution (Default: 50 sec) diff --git a/apps/exchange/2010/local/mode/mapimailbox.pm b/apps/exchange/2010/local/mode/mapimailbox.pm index 6f74610a2..1185d32f3 100644 --- a/apps/exchange/2010/local/mode/mapimailbox.pm +++ b/apps/exchange/2010/local/mode/mapimailbox.pm @@ -50,7 +50,10 @@ sub new { $self->{version} = '1.0'; $options{options}->add_options(arguments => - { + { + "remote-host:s" => { name => 'remote_host', }, + "remote-user:s" => { name => 'remote_user', }, + "remote-password:s" => { name => 'remote_password', }, "no-ps" => { name => 'no_ps', }, "timeout:s" => { name => 'timeout', default => 50 }, "command:s" => { name => 'command', default => 'powershell.exe' }, @@ -90,7 +93,11 @@ sub check_options { sub run { my ($self, %options) = @_; - my $ps = centreon::common::powershell::exchange::2010::mapimailbox::get_powershell(mailbox => $self->{option_results}->{mailbox}, + my $ps = centreon::common::powershell::exchange::2010::mapimailbox::get_powershell( + remote_host => $self->{option_results}->{remote_host}, + remote_user => $self->{option_results}->{remote_user}, + remote_password => $self->{option_results}->{remote_password}, + mailbox => $self->{option_results}->{mailbox}, no_ps => $self->{option_results}->{no_ps}, ); $self->{option_results}->{command_options} .= " " . $ps . " 2>&1"; @@ -121,6 +128,18 @@ Check mapi connection to a mailbox. =over 8 +=item B<--remote-host> + +Open a session to the remote-host (fully qualified host name). --remote-user and --remote-password are optional + +=item B<--remote-user> + +Open a session to the remote-host with authentication. This also needs --remote-host and --remote-password. + +=item B<--remote-password> + +Open a session to the remote-host with authentication. This also needs --remote-user and --remote-host. + =item B<--timeout> Set timeout time for command execution (Default: 50 sec) diff --git a/centreon/common/powershell/exchange/2010/activesyncmailbox.pm b/centreon/common/powershell/exchange/2010/activesyncmailbox.pm index 24f62654e..f5a9a4aed 100644 --- a/centreon/common/powershell/exchange/2010/activesyncmailbox.pm +++ b/centreon/common/powershell/exchange/2010/activesyncmailbox.pm @@ -38,6 +38,7 @@ package centreon::common::powershell::exchange::2010::activesyncmailbox; use strict; use warnings; use centreon::plugins::misc; +use centreon::common::powershell::exchange::2010::powershell; sub get_powershell { my (%options) = @_; @@ -47,29 +48,9 @@ sub get_powershell { return '' if ($no_ps == 1); - my $ps = ' -$culture = new-object "System.Globalization.CultureInfo" "en-us" -[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture - -If (@(Get-PSSnapin -Registered | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"} ).count -eq 1) { - If (@(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"} ).count -eq 0) { - Try { - Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP - } Catch { - Write-Host $Error[0].Exception - exit 1 - } - } else { - Write-Host "Snap-In no present or not registered" - exit 1 - } -} else { - Write-Host "Snap-In no present or not registered" - exit 1 -} -$ProgressPreference = "SilentlyContinue" - -# Check to make sure all databases are mounted + my $ps = centreon::common::powershell::exchange::2010::powershell::powershell_init(%options); + + $ps .= ' try { $ErrorActionPreference = "Stop" $username = "' . $options{mailbox} . '" diff --git a/centreon/common/powershell/exchange/2010/databases.pm b/centreon/common/powershell/exchange/2010/databases.pm index c6624d4bf..a2bd22f3b 100644 --- a/centreon/common/powershell/exchange/2010/databases.pm +++ b/centreon/common/powershell/exchange/2010/databases.pm @@ -38,6 +38,7 @@ package centreon::common::powershell::exchange::2010::databases; use strict; use warnings; use centreon::plugins::misc; +use centreon::common::powershell::exchange::2010::powershell; sub get_powershell { my (%options) = @_; @@ -48,28 +49,9 @@ sub get_powershell { return '' if ($no_ps == 1); - my $ps = ' -$culture = new-object "System.Globalization.CultureInfo" "en-us" -[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture - -If (@(Get-PSSnapin -Registered | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"} ).count -eq 1) { - If (@(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"} ).count -eq 0) { - Try { - Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP - } Catch { - Write-Host $Error[0].Exception - exit 1 - } - } else { - Write-Host "Snap-In no present or not registered" - exit 1 - } -} else { - Write-Host "Snap-In no present or not registered" - exit 1 -} -$ProgressPreference = "SilentlyContinue" - + my $ps = centreon::common::powershell::exchange::2010::powershell::powershell_init(%options); + + $ps .= ' # Check to make sure all databases are mounted try { $ErrorActionPreference = "Stop" diff --git a/centreon/common/powershell/exchange/2010/imapmailbox.pm b/centreon/common/powershell/exchange/2010/imapmailbox.pm index 43a85ae24..f5c78e9bb 100644 --- a/centreon/common/powershell/exchange/2010/imapmailbox.pm +++ b/centreon/common/powershell/exchange/2010/imapmailbox.pm @@ -38,6 +38,7 @@ package centreon::common::powershell::exchange::2010::imapmailbox; use strict; use warnings; use centreon::plugins::misc; +use centreon::common::powershell::exchange::2010::powershell; sub get_powershell { my (%options) = @_; @@ -46,29 +47,9 @@ sub get_powershell { return '' if ($no_ps == 1); - my $ps = ' -$culture = new-object "System.Globalization.CultureInfo" "en-us" -[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture - -If (@(Get-PSSnapin -Registered | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"} ).count -eq 1) { - If (@(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"} ).count -eq 0) { - Try { - Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP - } Catch { - Write-Host $Error[0].Exception - exit 1 - } - } else { - Write-Host "Snap-In no present or not registered" - exit 1 - } -} else { - Write-Host "Snap-In no present or not registered" - exit 1 -} -$ProgressPreference = "SilentlyContinue" - -# Check to make sure all databases are mounted + my $ps = centreon::common::powershell::exchange::2010::powershell::powershell_init(%options); + + $ps .= ' try { $ErrorActionPreference = "Stop" $username = "' . $options{mailbox} . '" diff --git a/centreon/common/powershell/exchange/2010/mapimailbox.pm b/centreon/common/powershell/exchange/2010/mapimailbox.pm index 55eef7c81..142440f7a 100644 --- a/centreon/common/powershell/exchange/2010/mapimailbox.pm +++ b/centreon/common/powershell/exchange/2010/mapimailbox.pm @@ -38,6 +38,7 @@ package centreon::common::powershell::exchange::2010::mapimailbox; use strict; use warnings; use centreon::plugins::misc; +use centreon::common::powershell::exchange::2010::powershell; sub get_powershell { my (%options) = @_; @@ -46,29 +47,9 @@ sub get_powershell { return '' if ($no_ps == 1); - my $ps = ' -$culture = new-object "System.Globalization.CultureInfo" "en-us" -[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture - -If (@(Get-PSSnapin -Registered | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"} ).count -eq 1) { - If (@(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.Exchange.Management.PowerShell.E2010"} ).count -eq 0) { - Try { - Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction STOP - } Catch { - Write-Host $Error[0].Exception - exit 1 - } - } else { - Write-Host "Snap-In no present or not registered" - exit 1 - } -} else { - Write-Host "Snap-In no present or not registered" - exit 1 -} -$ProgressPreference = "SilentlyContinue" - -# Check to make sure all databases are mounted + my $ps = centreon::common::powershell::exchange::2010::powershell::powershell_init(%options); + + $ps .= ' try { $ErrorActionPreference = "Stop" $mapi = test-mapiconnectivity -Identity "' . $options{mailbox} . '" diff --git a/centreon/common/powershell/exchange/2010/powershell.pm b/centreon/common/powershell/exchange/2010/powershell.pm new file mode 100644 index 000000000..914217ac1 --- /dev/null +++ b/centreon/common/powershell/exchange/2010/powershell.pm @@ -0,0 +1,147 @@ +################################################################################ +# Copyright 2005-2014 MERETHIS +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give MERETHIS +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of MERETHIS choice, provided that +# MERETHIS also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# For more information : contact@centreon.com +# Authors : Quentin Garnier +# Frank Holtz +# +#################################################################################### + +package centreon::common::powershell::exchange::2010::powershell; + +use strict; +use warnings; +use centreon::plugins::misc; + +# Generate Scipt to Load exchange extensions +#--remote-host --remote-user --remote-password +sub powershell_init { + my (%options) = @_; + # options: no_ps + my $no_ps = (defined($options{no_ps})) ? 1 : 0; + + return '' if ($no_ps == 1); + + my $ps = ' +$culture = new-object "System.Globalization.CultureInfo" "en-us" +[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture +'; + + if (!defined($options{remote_host})) { + $ps.=' + If (@(Get-PSSnapin -Registered | Where-Object {$_.Name -Match "Microsoft.Exchange.Management.PowerShell.E"} ).count -gt 0) { + If (@(Get-PSSnapin | Where-Object {$_.Name -Match "Microsoft.Exchange.Management.PowerShell.E"} ).count -eq 0) { + Try { + Get-PSSnapin -Registered | Where-Object {$_.Name -Match "Microsoft.Exchange.Management.PowerShell.E"} | Add-PSSnapin -ErrorAction STOP + } Catch { + Write-Host $Error[0].Exception + exit 1 + } + } else { + Write-Host "Snap-In no present or not registered" + exit 1 + } +} else { + Write-Host "Snap-In no present or not registered" + exit 1 +} +$ProgressPreference = "SilentlyContinue" +'; + } else { + # Find exchange installation path + my $exchangepath; + if (defined($ENV{ExchangeInstallPath})) { + # Windows Variable + $exchangepath = $ENV{ExchangeInstallPath}; + } else { + # No ENV -> look into registry (access via cygwin ssh session) + my $filename = "/proc/registry/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Session Manager/Environment/ExchangeInstallPath"; + if (-e $filename) { + open FILE, "$filename" or die "Couldn't open file: $!"; + $exchangepath = ; + $exchangepath =~ s/[\r|\n|\0].*//g; + close FILE; + } + } + + # No installation found + if (!defined($exchangepath)) { + print '$ENV{ExchangeInstallPath} is undefined. Please install PowerShell extensions for Exchange.'; + exit 1 + } + + $ps .= ' +# Open a session to the exchange +try +{ + $ErrorActionPreference = "Stop" + . "'.$exchangepath.'\bin\RemoteExchange.ps1" +} catch { + Write-Host $Error[0].Exception + exit 1 +} +try +{ + $ErrorActionPreference = "Stop"'; + + if (defined($options{remote_user}) && defined($options{remote_password})) { + # Replace / to \ + $options{remote_user}=~ s/\//\\/g; + $ps .= ' + $username = "'.centreon::plugins::misc::powershell_escape($options{remote_user}) . '" + $password = ConvertTo-SecureString "' . centreon::plugins::misc::powershell_escape($options{remote_password}) . '" -AsPlainText -Force + $UserCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password + Connect-ExchangeServer -ServerFqdn "'. centreon::plugins::misc::powershell_escape($options{remote_host}) .'" -UserName $UserCredential + '; + } else { + $ps .= ' + Connect-ExchangeServer -ServerFqdn "'. centreon::plugins::misc::powershell_escape($options{remote_host}) .'" + '; + } + $ps .= ' +} catch { + Write-Host $Error[0].Exception + exit 1 + } +'; + } + + return $ps; +} + +1; + +__END__ + +=head1 DESCRIPTION + +Powershell commands + +=cut \ No newline at end of file diff --git a/centreon/plugins/misc.pm b/centreon/plugins/misc.pm index 177dfa590..ad8335fd3 100644 --- a/centreon/plugins/misc.pm +++ b/centreon/plugins/misc.pm @@ -270,6 +270,15 @@ sub powershell_encoded { return $script; } +sub powershell_escape { + my ($value) = $_[0]; + $value =~ s/`/``/g; + $value =~ s/#/`#/g; + $value =~ s/'/`'/g; + $value =~ s/"/`"/g; + return $value; +} + sub minimal_version { my ($version_src, $version_dst) = @_;