Skip to content

Commit 2863f0e

Browse files
authored
Merge pull request #119 from Icinga/feature/mssql_connector
Feature: Adds MSSQL connector
2 parents 212e318 + bab6778 commit 2863f0e

File tree

6 files changed

+234
-1
lines changed

6 files changed

+234
-1
lines changed

lib/icinga/exception/Exit-IcingaThrowException.psm1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function Exit-IcingaThrowException()
55
[string]$StringPattern,
66
[string]$CustomMessage,
77
[string]$ExceptionThrown,
8-
[ValidateSet('Permission', 'Input', 'Configuration', 'Unhandled', 'Custom')]
8+
[ValidateSet('Permission', 'Input', 'Configuration', 'Connection', 'Unhandled', 'Custom')]
99
[string]$ExceptionType = 'Unhandled',
1010
[switch]$Force
1111
);
@@ -36,6 +36,10 @@ function Exit-IcingaThrowException()
3636
$ExceptionTypeString = 'Invalid Configuration';
3737
$ExceptionMessageLib = $IcingaExceptions.Configuration;
3838
};
39+
'Connection' {
40+
$ExceptionTypeString = 'Connection error';
41+
$ExceptionMessageLib = $IcingaExceptions.Connection;
42+
};
3943
'Unhandled' {
4044
$ExceptionTypeString = 'Unhandled';
4145
};

lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
ConversionUnitMissing = 'Unable to parse input value. You have to add an unit to your input value. Example: "10GB". Allowed units are: "B, KB, MB, GB, TB, PB, KiB, MiB, GiB, TiB, PiB".';
2020
CimClassNameUnknown = 'The provided class name you try to fetch with Get-CimInstance is not known on this system.';
2121
WmiObjectClassUnknown = 'The provided class name you try to fetch with Get-WmiObject is not known on this system.';
22+
MSSQLCredentialHandling = 'The connection to MSSQL was not possbible because your login credential was not correct.';
23+
MSSQLCommandMissing = 'Failed to build a SQL query'
2224
};
2325

2426
[hashtable]$Configuration = @{
@@ -30,6 +32,10 @@
3032
PerfCounterCategoryMissing = 'The specified Performance Counter category was not found on this system. This could either be a configuration error on your local Windows machine or a wrong usage of the plugin. Please check on different Windows machines if this issue persis. In case it only occurs on certain machines it is likely that the counter is simply not present and the plugin can not be processed.';
3133
}
3234

35+
[hashtable]$Connection = @{
36+
MSSQLConnectionError = 'Could not open a connection to SQL Server. This failure may be caused by the fact that under the default settings SQL Server does not allow remote connections or the host is unreachable.';
37+
}
38+
3339
<#
3440
# Once we defined a new enum hashtable above, simply add it to this list
3541
# to make it available within the entire module.
@@ -41,6 +47,7 @@
4147
Permission = $Permission;
4248
Inputs = $Inputs;
4349
Configuration = $Configuration;
50+
Connection = $Connection;
4451
}
4552

4653
Export-ModuleMember -Variable @( 'IcingaExceptions' );
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<#
2+
.SYNOPSIS
3+
Closes a open connection to a MSSQL server
4+
.DESCRIPTION
5+
This Cmdlet will close an open connection to a MSSQL server.
6+
.FUNCTIONALITY
7+
Closes an open connection to a MSSQL server.
8+
.EXAMPLE
9+
PS>Close-IcingaMSSQLConnection $OpenMSSQLConnection;
10+
.INPUTS
11+
System.Data.SqlClient.SqlConnection
12+
.OUTPUTS
13+
.LINK
14+
https://github.com/Icinga/icinga-powershell-framework
15+
#>
16+
function Close-IcingaMSSQLConnection()
17+
{
18+
param (
19+
[System.Data.SqlClient.SqlConnection]$SqlConnection = $null
20+
);
21+
22+
if ($null -eq $SqlConnection) {
23+
return;
24+
}
25+
26+
Write-IcingaDebugMessage `
27+
-Message 'Closing client connection for endpoint {0}' `
28+
-Objects $SqlConnection;
29+
30+
$SqlConnection.Close();
31+
$SqlConnection.Dispose();
32+
$SqlConnection = $null;
33+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<#
2+
.SYNOPSIS
3+
Builds a SQL query
4+
.DESCRIPTION
5+
This Cmdlet will build a SQL query
6+
and returns it as an string.
7+
.FUNCTIONALITY
8+
Build a SQL query
9+
.EXAMPLE
10+
PS>New-IcingaMSSQLCommand -SqlConnection $SqlConnection -SqlQuery "SELECT object_name FROM sys.dm_os_performance_counters";
11+
.PARAMETER SqlConnection
12+
An open SQL connection object e.g. $SqlConnection = Open-IcingaMSSQLConnection -IntegratedSecurity;
13+
.PARAMETER SqlQuery
14+
A SQL query as string.
15+
.INPUTS
16+
System.Data.SqlClient.SqlConnection
17+
System.String
18+
.OUTPUTS
19+
System.Data.SqlClient.SqlCommand
20+
.LINK
21+
https://github.com/Icinga/icinga-powershell-framework
22+
#>
23+
function New-IcingaMSSQLCommand()
24+
{
25+
param (
26+
[System.Data.SqlClient.SqlConnection]$SqlConnection = $null,
27+
[string]$SqlQuery = $null
28+
);
29+
30+
$SqlCommand = New-Object System.Data.SqlClient.SqlCommand;
31+
$SqlCommand.Connection = $SqlConnection;
32+
33+
if ($null -eq $SqlCommand.Connection) {
34+
Exit-IcingaThrowException -ExceptionType 'Input' `
35+
-ExceptionThrown $IcingaExceptions.Inputs.MSSQLCommandMissing `
36+
-CustomMessage 'It seems the -SqlConnection is empty or invalid' `
37+
-Force;
38+
}
39+
40+
$SqlCommand.CommandText = $SqlQuery;
41+
42+
return $SqlCommand;
43+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<#
2+
.SYNOPSIS
3+
Opens a connection to a MSSQL server
4+
.DESCRIPTION
5+
This Cmdlet will open a connection to a MSSQL server
6+
and returns that connection object.
7+
.FUNCTIONALITY
8+
Opens a connection to a MSSQL server
9+
.EXAMPLE
10+
PS>Open-IcingaMSSQLConnection -IntegratedSecurity -Address localhost;
11+
.EXAMPLE
12+
PS>Open-IcingaMSSQLConnection -Username Exampleuser -Password (ConvertTo-IcingaSecureString 'examplePassword') -Address 123.125.123.2;
13+
.PARAMETER Username
14+
The username for connecting to the MSSQL database
15+
.PARAMETER Password
16+
The password for connecting to the MSSQL database as secure string
17+
.PARAMETER Address
18+
The IP address or FQDN to the MSSQL server to connect to (default: localhost)
19+
.PARAMETER Port
20+
The port of the MSSQL server/instance to connect to with the provided credentials (default: 1433)
21+
.PARAMETER SqlDatabase
22+
The name of a specific database to connect to. Leave empty to connect "globaly"
23+
.PARAMETER IntegratedSecurity
24+
Allows this plugin to use the credentials of the current PowerShell session inherited by
25+
the user the PowerShell is running with. If this is set and the user the PowerShell is
26+
running with can access to the MSSQL database you will not require to provide username
27+
and password
28+
.OUTPUTS
29+
System.Data.SqlClient.SqlConnection
30+
.LINK
31+
https://github.com/Icinga/icinga-powershell-framework
32+
#>
33+
function Open-IcingaMSSQLConnection()
34+
{
35+
param (
36+
[string]$Username,
37+
[securestring]$Password,
38+
[string]$Address = "localhost",
39+
[int]$Port = 1433,
40+
[string]$SqlDatabase,
41+
[switch]$IntegratedSecurity = $FALSE
42+
);
43+
44+
if ($IntegratedSecurity -eq $FALSE) {
45+
if ([string]::IsNullOrEmpty($Username)) {
46+
Exit-IcingaThrowException `
47+
-ExceptionType 'Input' `
48+
-ExceptionThrown $IcingaExceptions.Inputs.MSSQLCredentialHandling `
49+
-CustomMessage '-Username not set and -IntegratedSecurity is false' `
50+
-Force;
51+
} elseif ($null -eq $Password) {
52+
Exit-IcingaThrowException `
53+
-ExceptionType 'Input' `
54+
-ExceptionThrown $IcingaExceptions.Inputs.MSSQLCredentialHandling `
55+
-CustomMessage '-Password not set and -IntegratedSecurity is false' `
56+
-Force;
57+
}
58+
59+
$Password.MakeReadOnly();
60+
$SqlCredential = New-Object System.Data.SqlClient.SqlCredential($Username, $Password);
61+
}
62+
63+
try {
64+
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
65+
$SqlConnection.ConnectionString = "Server=$Address,$Port;";
66+
67+
if ($null -ne $SqlDatabase) {
68+
$SqlConnection.ConnectionString += "Database=$SqlDatabase;";
69+
}
70+
71+
if ($IntegratedSecurity -eq $TRUE) {
72+
$SqlConnection.ConnectionString += "Integrated Security=True;";
73+
}
74+
75+
$SqlConnection.Credential = $SqlCredential;
76+
77+
Write-IcingaDebugMessage `
78+
-Message 'Open client connection for endpoint {0}' `
79+
-Objects $SqlConnection;
80+
81+
$SqlConnection.Open();
82+
} catch {
83+
Exit-IcingaThrowException `
84+
-InputString $_.Exception.Message `
85+
-StringPattern $Username `
86+
-ExceptionType 'Input' `
87+
-ExceptionThrown $IcingaExceptions.Inputs.MSSQLCredentialHandling;
88+
89+
Exit-IcingaThrowException `
90+
-InputString $_.Exception.Message `
91+
-StringPattern 'error: 40' `
92+
-ExceptionType 'Connection' `
93+
-ExceptionThrown $IcingaExceptions.Connection.MSSQLConnectionError;
94+
95+
Exit-IcingaThrowException `
96+
-InputString $_.Exception.Message `
97+
-StringPattern 'error: 0' `
98+
-ExceptionType 'Connection' `
99+
-ExceptionThrown $IcingaExceptions.Connection.MSSQLConnectionError;
100+
101+
Exit-IcingaThrowException `
102+
-InputString $_.Exception.Message `
103+
-StringPattern 'error: 25' `
104+
-ExceptionType 'Connection' `
105+
-ExceptionThrown $IcingaExceptions.Connection.MSSQLConnectionError;
106+
# Last resort
107+
Exit-IcingaThrowException `
108+
-InputString $_.Exception.Message `
109+
-ExceptionType 'Custom' `
110+
-Force;
111+
}
112+
113+
return $SqlConnection;
114+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<#
2+
.SYNOPSIS
3+
Executes a SQL query
4+
.DESCRIPTION
5+
This Cmdlet will send a SQL query to a given database and
6+
execute the query and returns the output.
7+
.FUNCTIONALITY
8+
Executes a SQL query
9+
.EXAMPLE
10+
PS> Send-IcingaMSSQLCommand -SqlCommand $SqlCommand;
11+
.PARAMETER SqlCommand
12+
The SQL query which will be executed, e.g. $SqlCommand = New-IcingaMSSQLCommand
13+
.INPUTS
14+
System.Data.SqlClient.SqlCommand
15+
.OUTPUTS
16+
System.Data.DataSet
17+
.LINK
18+
https://github.com/Icinga/icinga-powershell-framework
19+
#>
20+
function Send-IcingaMSSQLCommand()
21+
{
22+
param (
23+
[System.Data.SqlClient.SqlCommand]$SqlCommand = $null
24+
);
25+
26+
$Adapter = New-Object System.Data.SqlClient.SqlDataAdapter $SqlCommand;
27+
28+
$Data = New-Object System.Data.DataSet;
29+
$Adapter.Fill($Data) | Out-Null;
30+
31+
return $Data.Tables;
32+
}

0 commit comments

Comments
 (0)