-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-MSSQL.ps1
487 lines (468 loc) · 16.5 KB
/
Get-MSSQL.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
Import-Module SQLPS -WarningAction SilentlyContinue
$ErrorActionPreference= 'silentlycontinue'
###
# Read the ini file, if provided
###
function Get-IniFile {
param(
[parameter(Mandatory = $true)] [string] $filePath
)
$anonymous = "NoSection"
$ini = @{}
switch -regex -file $filePath
{
"^\[(.+)\]$" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
if (!($section))
{
$section = $anonymous
$ini[$section] = @{}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=\s*(.*)" # Key
{
if (!($section))
{
$section = $anonymous
$ini[$section] = @{}
}
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
##
# Read user config
##
function Get-UserConfig {
param(
[parameter(Mandatory = $false)] [string] $instance
)
$ini = ""
$mkconf=Get-Item -Path $ENV:MK_CONFDIR
if($instance.Length -gt 0)
{
$conf="$($mkconf.FullName)mssql_$instance.ini"
if(Get-Item -Path "$conf")
{
$ini = Get-IniFile -filePath "$conf"
}
}
else
{
$conf="$($mkconf.FullName)mssql.ini"
if(Get-Item -Path "$conf")
{
$ini = Get-IniFile -filePath "$conf"
}
}
return $ini
}
##
# Invoking SQL Queries based on the delivered (or not delivered) configuration
##
function Invoke-SqlcmdWithConfig{
param(
$instance,
$sql
)
$iniGlobal=Get-UserConfig
cd $instance.PSPath
$ini = Get-UserConfig -instance $instance.PSChildName
if($ini -like "")
{
$ini=$iniGlobal
}
$data=""
if($ini -like "")
{
$data = Invoke-Sqlcmd -Query "$sql" -WarningAction SilentlyContinue
}
else
{
$data = Invoke-Sqlcmd -Query "$sql" -Username $ini.auth.name -Password $ini.auth.password -WarningAction SilentlyContinue
}
return $data
}
##
# Creates an empty structure for the cmk output
##
function Get-Basestruct{
$basestruct=@{}
$basestruct["instance"]=@{}
$basestruct["instance"]["name"]="<<<mssql_instance:sep(124)>>>"
$basestruct["databases"]=@{}
$basestruct["databases"]["name"]="<<<mssql_databases:sep(124)>>>"
$basestruct["counters"]=@{}
$basestruct["counters"]["name"]="<<<mssql_counters:sep(124)>>>"
$basestruct["tablespaces"]=@{}
$basestruct["tablespaces"]["name"]="<<<mssql_tablespaces>>>"
$basestruct["blocked_sessions"]=@{}
$basestruct["blocked_sessions"]["name"]="<<<mssql_blocked_sessions:sep(124)>>>"
$basestruct["backup"]=@{}
$basestruct["backup"]["name"]="<<<mssql_backup:sep(124)>>>"
$basestruct["transactionlogs"]=@{}
$basestruct["transactionlogs"]["name"]="<<<mssql_transactionlogs:sep(124)>>>"
$basestruct["datafiles"]=@{}
$basestruct["datafiles"]["name"]="<<<mssql_datafiles:sep(124)>>>"
$basestruct["clusters"]=@{}
$basestruct["clusters"]["name"]="<<<mssql_cluster:sep(124)>>>"
$basestruct["connections"]=@{}
$basestruct["connections"]["name"]="<<<mssql_connections>>>"
return $basestruct
}
###
# Read the configuration string for the SQL server
###
function Get-ConfString{
param(
$instance
)
$conf=Invoke-SqlcmdWithConfig -instance $instance -sql "SELECT SERVERPROPERTY('edition') AS edition, SERVERPROPERTY('ProductVersion') as version"
return "$($conf.version.Trim())|$($conf.edition.trim())|$($instance.ClusterName)"
}
###
# Get the SQL server service state
###
function Get-StateString{
param(
$instanceName
)
if($instanceName -ne "MSSQLSERVER")
{
$instanceName = "MSSQL`$$instanceName"
}
$service=Get-Service $instanceName
if($service.Status -eq "Running")
{
$state=1
$errormsg=""
}
else
{
$state=0
$errormsg="Service $instanceName is in state $($service.Status)"
}
return "$state|$errormsg"
}
###
# Get the SQL Servers UTC time
###
function Get-UTCString{
param(
$instance
)
$data=Invoke-SqlcmdWithConfig -instance $instance -sql "SELECT GETUTCDATE() as utc_date"
return "None|$($data.utc_date.ToUniversalTime().ToString("M/d/yyyy hh:mm:ss tt"))"
}
##
# Returns the Counter name
##
function Get-CounterString{
param(
$counterRow
)
return "$($counterRow.counter_name)"
}
##
# Adds a counter to the structure
##
function Add-Counter{
param(
$dbstruct,
$counterdata
)
$objectName=$counterdata.object_name.trim().Replace(" ", "_").Replace("$", "_")
if(-not $dbstruct["counters"].ContainsKey("$objectName"))
{
$dbstruct["counters"]["$objectName"]=@{}
}
$counterName=$counterdata.counter_name.trim().Replace(" ", "_").Replace("$", "_").ToLower()
if(-not $dbstruct["counters"]["$objectName"].ContainsKey("$counterName"))
{
$dbstruct["counters"]["$objectName"]["$counterName"]=@{}
}
$instanceName=$counterData.instance_name.trim().Replace(" ", "_").Replace("$", "_")
if($instanceName -eq "") { $instanceName = "None" }
$dbstruct["counters"]["$objectName"]["$counterName"]["$instanceName"]=$counterdata.cntr_value.toString().trim()
}
##
# Adds the blocked sessions to the structure
##
function Add-BlockedSession{
param(
$dbstruct,
$blockedData
)
$session_id=$counterdata.session_id.toString().trim().Replace(" ", "_").Replace("$", "_")
if(-not $dbstruct["blocked_sessions"].ContainsKey("$session_id"))
{
$dbstruct["blocked_sessions"]["$session_id"]=@{}
}
$wait_duration_ms=$counterdata.wait_duration_ms.toString().trim().Replace(" ", "_").Replace("$", "_")
if(-not $dbstruct["blocked_sessions"]["$session_id"].ContainsKey("$wait_duration_ms"))
{
$dbstruct["session_id"]["$session_id"][$wait_duration_ms]=@{}
}
$blocking_session_id=$counterdata.blocking_session_id.toString().trim().Replace(" ", "_").Replace("$", "_")
if(-not $dbstruct["blocked_sessions"]["$session_id"][$wait_duration_ms].ContainsKey("$blocking_session_id"))
{
$dbstruct["blocked_sessions"]["$session_id"][$wait_duration_ms]["$wait_type"]["$blocking_session_id"]=$null
}
}
###
# Add the database sizes to the structure
# !!! The original script does not output any informations on not accessible (e.g. restoring) databases !!!
###
function Add-DatabaseSize{
param(
$dbstruct,
$instance,
$instanceName,
$dbname
)
$data=Invoke-SqlcmdWithConfig -instance $instance -sql "USE [$dbname]; EXEC sp_spaceused"# @oneresultset = 1"
if($data -notlike "")
{
$field="MSSQL_$instanceName $($dbname.Replace(" ", "_")) $($data[0].database_size.trim()) $($data[0].'unallocated space'.trim()) $($data[1].reserved.trim()) $($data[1].data.trim()) $($data[1].index_size.trim()) $($data[1].unused.trim())"
$dbstruct["tablespaces"]["$field"]=$null
}
}
##
# Writes the structure to a CMK readable format
##
function Write-DBStruct {
param(
$dbstruct
)
foreach($key in $dbstruct.Keys)
{
"$($dbstruct.$key.name)"
foreach($subkey in ($dbstruct.$key.Keys | Where-Object {$_ -notlike 'name'}))
{
if($dbstruct.$key.$subkey -ne $null)
{
Get-ValuesRecurse -sub $dbstruct.$key.$subkey -base "$subkey"
}
else
{
"$subkey"
}
}
}
}
###
# Help function to print a hastable recursively
###
function Get-ValuesRecurse {
param(
$sub,
$base
)
if($sub.GetType().Name -eq "Hashtable")
{
if($sub.Keys.Count -gt 0)
{
foreach($key in $sub.Keys)
{
if($sub.$key -eq $null)
{
"$base|$key"
}
else
{
Get-ValuesRecurse -sub $sub.$key -base "$base|$key"
}
}
}
else
{
if($sub.Value -eq $null)
{
"$base"
}
else
{
"$base|$sub"
}
}
}
else
{
"$base|$sub"
}
}
##
# Get the database backup informations
##
function Add-BackupInformation{
param(
$dbstruct,
$instance,
$instanceName,
$dbname
)
$sql=@"
USE [master];
DECLARE @HADRStatus sql_variant; DECLARE @SQLCommand nvarchar(max);
SET @HADRStatus = (SELECT SERVERPROPERTY ('IsHadrEnabled'));
IF (@HADRStatus IS NULL or @HADRStatus <> 1)
BEGIN
SET @SQLCommand = 'SELECT CONVERT(VARCHAR, DATEADD(s, DATEDIFF(s, ''19700101'', MAX(backup_finish_date)), ''19700101''), 120) AS last_backup_date, type, machine_name, ''True'' as is_primary_replica, ''1'' as is_local, '''' as replica_id FROM msdb.dbo.backupset WHERE database_name = ''DBNAMETEMPL'' AND machine_name = SERVERPROPERTY(''Machinename'') GROUP BY type, machine_name '
END
ELSE
BEGIN
SET @SQLCommand = 'SELECT CONVERT(VARCHAR, DATEADD(s, DATEDIFF(s, ''19700101'', MAX(b.backup_finish_date)), ''19700101''), 120) AS last_backup_date, b.type, b.machine_name, isnull(rep.is_primary_replica,0) as is_primary_replica, rep.is_local, isnull(convert(varchar(40), rep.replica_id), '''') AS replica_id FROM msdb.dbo.backupset b LEFT OUTER JOIN sys.databases db ON b.database_name = db.name LEFT OUTER JOIN sys.dm_hadr_database_replica_states rep ON db.database_id = rep.database_id WHERE database_name = ''DBNAMETEMPL'' AND (rep.is_local is null or rep.is_local = 1) AND (rep.is_primary_replica is null or rep.is_primary_replica = ''True'') and machine_name = SERVERPROPERTY(''Machinename'') GROUP BY type, rep.replica_id, rep.is_primary_replica, rep.is_local, b.database_name, b.machine_name, rep.synchronization_state, rep.synchronization_health'
END
EXEC (@SQLCommand)
"@.Replace("DBNAMETEMPL", "$dbname")
$backups=Invoke-SqlcmdWithConfig -instance $instance -sql $sql
foreach($backup in $backups)
{
if($backup.last_backup_date.toString() -ne "" -and ($backup.replica_id.toString().trim() -eq "" -or $backup.is_primary_replica.toString().Trim() -eq "True" ))
{
if(-not $dbstruct["backup"].ContainsKey("MSSQL_$instanceName"))
{
$dbstruct["backup"]["MSSQL_$instanceName"]=@{}
}
if(-not $dbstruct["backup"]["MSSQL_$instanceName"].ContainsKey("$($dbname.Replace(" ", "_"))"))
{
$dbstruct["backup"]["MSSQL_$instanceName"]["$($dbname.Replace(" ", "_"))"]=@{}
}
$dbstruct["backup"]["MSSQL_$instanceName"]["$($dbname.Replace(" ", "_"))"]["$($backup.last_backup_date.trim().replace(" ", "|"))|$($backup.type.trim())"]=$null
}
}
}
##
# Get the database datafiles
##
function Add-DataFiles{
param(
$type,
$dbstruct,
$instance,
$instanceName,
$dbname
)
if($type -like "datafiles")
{
$dbtype="ROWS"
}
else
{
$dbtype="LOG"
}
$sql=@"
USE [DBNAMETEMPL];
SELECT name, physical_name,
cast(max_size/128 as bigint) as MaxSize,
cast(size/128 as bigint) as AllocatedSize,
cast(FILEPROPERTY (name, 'spaceused')/128 as bigint) as UsedSize,
case when max_size = '-1' then '1' else '0' end as Unlimited
FROM sys.database_files WHERE type_desc = 'TYPETEMPL'
"@.Replace("DBNAMETEMPL", $dbname.Trim()).Replace("TYPETEMPL", $dbtype)
$files=Invoke-SqlcmdWithConfig -instance $instance -sql $sql
foreach($file in $files)
{
if(-not $dbstruct["$type"].ContainsKey("$instanceName"))
{
$dbstruct["$type"]["$instanceName"]=@{}
}
if(-not $dbstruct["$type"]["$instanceName"].ContainsKey("$($dbname.Replace(" ", "_"))"))
{
$dbstruct["$type"]["$instanceName"]["$($dbname.Replace(" ", "_"))"]=@{}
}
$field="$($file.name.trim().replace(" ", "_"))|$($file.physical_name.trim().replace(" ", "_"))|$($file.MaxSize)|$($file.AllocatedSize)|$($file.UsedSize)|$($file.Unlimited)"
$dbstruct["$type"]["$instanceName"]["$($dbname.Replace(" ", "_"))"][$field]=$null
}
}
##
# Add accessible databases
##
function Add-DataBase{
param(
$dbstruct,
$instance,
$instanceName,
$database
)
$sql=@"
SELECT
DATABASEPROPERTYEX(name, 'Status') AS Status,
DATABASEPROPERTYEX(name, 'Recovery') AS RecoveryModel,
DATABASEPROPERTYEX(name, 'IsAutoClose') AS AutoClose,
DATABASEPROPERTYEX(name, 'IsAutoShrink') AS AutoShrink
FROM master.dbo.sysdatabases WHERE name = 'DBNAMETEMPL'
"@.Replace("DBNAMETEMPL", $database.Name)
$databaseData=Invoke-SqlcmdWithConfig -instance $instance -sql $sql
if(-not $dbstruct["databases"].ContainsKey("$instanceName"))
{
$dbstruct["databases"]["$instanceName"]=@{}
}
$dbstruct["databases"]["$instanceName"]["$($database.Name.Trim().Replace(" ", "_"))"]="$($databaseData.Status)|$($databaseData.RecoveryModel)|$($databaseData.AutoClose)|$($databaseData.AutoShrink)"
}
##
# Add active DB connections
##
function Add-DBConnections{
param(
$dbstruct,
$instance,
$instanceName,
$database
)
$sql=@"
SELECT name AS DBName, ISNULL((SELECT COUNT(dbid) AS NumberOfConnections FROM
sys.sysprocesses WHERE dbid > 0 AND name = DB_NAME(dbid) GROUP BY dbid ),0) AS NumberOfConnections
FROM sys.databases WHERE name = 'DBNAMETEMPL'
"@.Replace("DBNAMETEMPL", $database.Name)
$databaseCons=Invoke-SqlcmdWithConfig -instance $instance -sql $sql
$dbstruct["connections"]["$($instanceName) $($database.Name.Replace(" ", "_")) $($databaseCons.NumberOfConnections)"]=$null
}
##
# Main function
##
$path = Get-Location
$dbstruct=Get-Basestruct
foreach($machine in Get-ChildItem -Path SQLSERVER:\SQL\)
{
foreach($instance in Get-ChildItem -Path $machine.PSPath)
{
$instanceName=$instance.ServiceName
$dbstruct["instance"]["MSSQL_$instanceName"]=@{}
$dbstruct["instance"]["MSSQL_$instanceName"]["config"]=Get-ConfString -instance $instance
$dbstruct["instance"]["MSSQL_$instanceName"]["state"]=Get-StateString -instanceName $instanceName
$dbstruct["counters"]["None"]=@{}
$dbstruct["counters"]["None"]["utc_time"]=Get-UTCString -instance $instance
foreach($counter in Invoke-SqlcmdWithConfig -instance $instance -sql "SELECT counter_name, object_name, instance_name, cntr_value FROM sys.dm_os_performance_counters WHERE object_name NOT LIKE '%Deprecated%'")
{
Add-Counter -dbstruct $dbstruct -counterdata $counter
}
foreach($session in Invoke-SqlcmdWithConfig -instance $instance -sql "SELECT session_id, wait_duration_ms, wait_type, blocking_session_id FROM sys.dm_os_waiting_tasks WHERE blocking_session_id <> 0")
{
Add-BlockedSession -dbstruct $dbstruct -blockedData $session
}
foreach($database in $instance.Databases)
{
Add-DatabaseSize -dbstruct $dbstruct -instance $instance -instanceName $instanceName -dbname $database.Name
Add-BackupInformation -dbstruct $dbstruct -instance $instance -instanceName $instanceName -dbname $database.Name
Add-DataFiles -type "datafiles" -dbstruct $dbstruct -instance $instance -instanceName $instanceName -dbname $database.Name
Add-DataFiles -type "transactionlogs" -dbstruct $dbstruct -instance $instance -instanceName $instanceName -dbname $database.Name
Add-DataBase -dbstruct $dbstruct -database $database -instance $instance -instanceName $instanceName
Add-DBConnections -dbstruct $dbstruct -database $database -instance $instance -instanceName $instanceName
}
}
}
Write-DBStruct -dbstruct $dbstruct
cd $path