This repository has been archived by the owner on Jan 31, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ioan Popovici
committed
Nov 30, 2018
1 parent
83c675b
commit 3f596a9
Showing
22 changed files
with
1,779 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Export-CMDBToTelegraf release history | ||
|
||
## 1.0 - 2018-11-30 | ||
|
||
### First version | ||
|
||
* Exports data from the SCCM database and formats it for telegraf. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
|
||
#region Function Format-Telegraf | ||
Function Format-Telegraf { | ||
<# | ||
.SYNOPSIS | ||
Formats input object for telegraf. | ||
.DESCRIPTION | ||
Formats input object for telegraf format. | ||
.PARAMETER InputObject | ||
Specifies the InputObject. | ||
.PARAMETER Tags | ||
Specifies the tags to attach. | ||
.PARAMETER AddTimeStamp | ||
Specifies if a unix time stamp will be added to each row. Defaut is: $false. | ||
.EXAMPLE | ||
Format-Telegraf -InputObject 'SomeInputObject' | ||
.NOTES | ||
This is an internal script function and should typically not be called directly. | ||
.LINK | ||
https://SCCM.Zone | ||
.LINK | ||
https://SCCM.Zone/Git | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter(Mandatory = $false, ValueFromPipeline = $true, Position = 0)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('Obj')] | ||
[psobject]$InputObject, | ||
[Parameter(Mandatory = $false, Position = 1)] | ||
[Alias('Tags')] | ||
[string]$TelegrafTags, | ||
[Parameter(Mandatory = $false, Position = 2)] | ||
[Alias('TStamp')] | ||
[switch]$AddTimeStamp | ||
) | ||
Begin { | ||
|
||
## Initialize result variable | ||
[psobject]$Result = @() | ||
} | ||
Process { | ||
Try { | ||
|
||
## Get input members | ||
If ($InputObject) { | ||
[string[]]$Headers = ($InputObject | Get-Member | Where-Object -Property 'MemberType' -eq 'Property').Name | ||
} | ||
Else { $Headers = $null } | ||
|
||
## Format object | ||
ForEach ($Row in $InputObject) { | ||
# Initialize format variables for every new iteration | ||
[string]$FormatRowProps = $null | ||
[string]$FormatRow = $null | ||
# Get row data using object headers and format for telegraf | ||
ForEach ($Header in $Headers) { | ||
$FormatRowProps = -join ($Header, '=', $($Row.$Header), ',') | ||
$FormatRow = -join ($FormatRow, $FormatRowProps) | ||
} | ||
# Add telegraf tags and remove last ',' from the string | ||
If ($FormatRow) { | ||
# Add tags if needed | ||
If ($TelegrafTags) { $FormatRow = -join ($TelegrafTags, ' ', $FormatRow) } | ||
# Remove last ',' from the string | ||
$FormatRow = $FormatRow -replace (".$") | ||
# Add Unix time stamp (UTC) | ||
If ($AddTimeStamp) { | ||
[string]$UnixTimeStamp = $(([DateTimeOffset](Get-Date)).ToUnixTimeSeconds()) | ||
$FormatRow = -join ($FormatRow, ' ', $UnixTimeStamp) | ||
} | ||
} | ||
# Add row to result object | ||
$Result += $FormatRow | ||
} | ||
} | ||
Catch { | ||
Write-Error -Message "Formating Error: `n $_.ErrorMessage" | ||
} | ||
Finally { | ||
|
||
## Output result | ||
Write-Output -InputObject $($Result | Format-Table -HideTableHeaders) | ||
} | ||
} | ||
End { | ||
} | ||
} | ||
#endregion |
88 changes: 88 additions & 0 deletions
88
Grafana/Export-CMDBToTelegraf/Cmdlets/Get-CMDBClientInfo.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#region Function Get-CMDBClientInfo | ||
Function Get-CMDBClientInfo { | ||
<# | ||
.SYNOPSIS | ||
Gets client information. | ||
.DESCRIPTION | ||
Gets client information from the SCCM database. | ||
.PARAMETER Server | ||
Specifies the server name. | ||
.PARAMETER Database | ||
Specifies the database name. | ||
.PARAMETER LastnDays | ||
Specifies the how many days in the past to query. Default is: '12'. | ||
.EXAMPLE | ||
Get-CMDBClientInfo -Server 'SomeServer' -Database 'CM_SomeSiteCode' -LastnDays '12' | ||
.NOTES | ||
This is an internal script function and should typically not be called directly. | ||
.LINK | ||
https://SCCM.Zone | ||
.LINK | ||
https://SCCM.Zone/Git | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter(Mandatory = $true, Position = 0)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('Srv')] | ||
[string]$Server, | ||
[Parameter(Mandatory = $true, Position = 1)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('Dbs')] | ||
[string]$Database, | ||
[Parameter(Mandatory = $false, Position = 2)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('Days')] | ||
[string]$LastnDays = '12' | ||
) | ||
Begin { | ||
|
||
## Add '-' to LastnDays | ||
[string]$LastnDays = -join ('-', $LastnDays) | ||
|
||
## Query definition | ||
[string]$Query = | ||
" | ||
DECLARE @Date DATETIME = DATEADD(hh, $LastnDays, GETDATE()); | ||
SELECT | ||
CMG_Update_Scan = ( | ||
SELECT COUNT(DISTINCT ResourceID) | ||
FROM v_UpdateScanStatus | ||
WHERE LastScanTime > @Date | ||
AND LastScanPackageLocation LIKE '%cmg%' | ||
GROUP BY LastScanPackageLocation | ||
) | ||
, CMG_Clients = ( | ||
SELECT COUNT(DISTINCT Name) | ||
FROM v_CombinedDeviceResources | ||
WHERE CNIsOnInternet = 1 | ||
AND CNIsOnline = 1 | ||
AND CNAccessMP LIKE '%cmg%' | ||
) | ||
, MP_Clients = ( | ||
SELECT COUNT(DISTINCT Name) | ||
FROM v_CombinedDeviceResources | ||
WHERE CNIsOnInternet = 0 | ||
AND CNIsOnline = 1 | ||
); | ||
" | ||
} | ||
Process { | ||
Try { | ||
|
||
## Run SQL query | ||
[psobject]$Result = Invoke-Sqlcmd -Query $Query -Server $Server -Database $Database -ErrorAction 'Stop' | ||
} | ||
Catch { | ||
Write-Error -Message "Query Error: `n $_.ErrorMessage" | ||
} | ||
Finally { | ||
|
||
## Output result | ||
Write-Output -InputObject $Result | ||
} | ||
} | ||
End { | ||
} | ||
} | ||
#endregion |
78 changes: 78 additions & 0 deletions
78
Grafana/Export-CMDBToTelegraf/Cmdlets/Get-CMDBDeviceInfo.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#region Function Get-CMDBDeviceInfo | ||
Function Get-CMDBDeviceInfo { | ||
<# | ||
.SYNOPSIS | ||
Gets device information. | ||
.DESCRIPTION | ||
Gets device information from the SCCM database. | ||
.PARAMETER Server | ||
Specifies the server name. | ||
.PARAMETER Database | ||
Specifies the database name. | ||
.PARAMETER CollectionID | ||
Specifies the CollectionID to query. | ||
.EXAMPLE | ||
Get-CMDBDeviceInfo -Server 'SomeServer' -Database 'CM_SomeSiteCode' -CollectionID 'SomeCollectionID' | ||
.NOTES | ||
This is an internal script function and should typically not be called directly. | ||
.LINK | ||
https://SCCM.Zone | ||
.LINK | ||
https://SCCM.Zone/Git | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter(Mandatory = $true, Position = 0)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('Srv')] | ||
[string]$Server, | ||
[Parameter(Mandatory = $true, Position = 1)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('Dbs')] | ||
[string]$Database, | ||
[Parameter(Mandatory = $true, Position = 2)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('CID')] | ||
[string]$CollectionID | ||
) | ||
Begin { | ||
|
||
## Query definition | ||
[string]$Query = | ||
" | ||
DECLARE @CollectionID NVARCHAR(16)= '$CollectionID'; | ||
DECLARE @UserSIDs NVARCHAR(16)= 'Disabled'; | ||
SELECT | ||
Client_Version = Systems.Client_Version0 | ||
, Count = COUNT(*) | ||
FROM fn_rbac_R_System(@UserSIDs) AS SYS | ||
LEFT JOIN fn_rbac_FullCollectionMembership(@UserSIDs) AS CollectionMembership ON CollectionMembership.ResourceID = Systems.ResourceID | ||
WHERE Systems.Client0 = 1 | ||
AND CollectionMembership.CollectionID = @CollectionID | ||
GROUP BY | ||
Systems.Client_Version0 | ||
, Systems.Client_Type0 | ||
ORDER BY | ||
Systems.Client_Version0 | ||
, Systems.Client_Type0 | ||
" | ||
} | ||
Process { | ||
Try { | ||
|
||
## Run SQL query | ||
[psobject]$Result = Invoke-Sqlcmd -Query $Query -Server $Server -Database $Database -ErrorAction 'Stop' | ||
} | ||
Catch { | ||
Write-Error -Message "Query Error: `n $_.ErrorMessage" | ||
} | ||
Finally { | ||
|
||
## Output result | ||
Write-Output -InputObject $Result | ||
} | ||
} | ||
End { | ||
} | ||
} | ||
#endregion |
112 changes: 112 additions & 0 deletions
112
Grafana/Export-CMDBToTelegraf/Cmdlets/Get-CMDBDistributionInfo.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#region Function Get-CMDBDistributionInfo | ||
Function Get-CMDBDistributionInfo { | ||
<# | ||
.SYNOPSIS | ||
Gets package distribution information. | ||
.DESCRIPTION | ||
Gets package distribution information from the SCCM database. | ||
.PARAMETER Server | ||
Specifies the server name. | ||
.PARAMETER Database | ||
Specifies the database name. | ||
.EXAMPLE | ||
Get-CMDBDistributionInfo -Server 'SomeServer' -Database 'CM_SomeSiteCode' | ||
.NOTES | ||
This is an internal script function and should typically not be called directly. | ||
.LINK | ||
https://SCCM.Zone | ||
.LINK | ||
https://SCCM.Zone/Git | ||
#> | ||
[CmdletBinding()] | ||
Param ( | ||
[Parameter(Mandatory = $true, Position = 0)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('Srv')] | ||
[string]$Server, | ||
[Parameter(Mandatory = $true, Position = 1)] | ||
[ValidateNotNullorEmpty()] | ||
[Alias('Dbs')] | ||
[string]$Database | ||
) | ||
Begin { | ||
|
||
## Query definition | ||
[string]$Query = | ||
" | ||
USE $Database; | ||
WITH CTE AS ( | ||
SELECT | ||
DP_Name = ( | ||
UPPER(SUBSTRING(PkgDistribution.ServerNALPath, 13, CHARINDEX('.', PkgDistribution.ServerNALPath) - 13)) | ||
) | ||
, Not_Installed = ( | ||
COUNT( | ||
CASE | ||
WHEN PkgDistribution.State NOT IN ('0', '3', '6') THEN '*' | ||
ELSE 'Unknown' | ||
END | ||
) | ||
) | ||
, Error = ( | ||
COUNT( | ||
CASE | ||
WHEN PkgDistribution.State IN('3', '6') THEN '*' | ||
END | ||
) | ||
) | ||
, Status = ( | ||
CASE | ||
WHEN PkgDistribution.State = '0' THEN '1' --'OK' | ||
WHEN PkgDistribution.State NOT IN ('0', '3', '6') THEN '2' --'In_Progress' | ||
WHEN PkgDistribution.State IN ('3', '6') THEN '3' --'Error' | ||
END | ||
) | ||
FROM dbo.v_PackageStatusDistPointsSumm AS PkgDistribution | ||
, dbo.SMSPackages AS Packages | ||
WHERE Packages.PackageType != 4 | ||
AND (Packages.PkgID = PkgDistribution.PackageID) | ||
GROUP BY | ||
PkgDistribution.ServerNALPath, | ||
PkgDistribution.State | ||
) | ||
SELECT | ||
PKG_Not_Installed = SUM(Not_Installed) | ||
, PKG_Error = SUM(Error) | ||
, DP_OK = ( | ||
SELECT COUNT(DP_Name) | ||
FROM CTE | ||
WHERE Status = '1' | ||
) | ||
, DP_In_Progress = ( | ||
SELECT COUNT(DP_Name) | ||
FROM CTE | ||
WHERE Status = '2' | ||
) | ||
, DP_Error = ( | ||
SELECT COUNT(DP_Name) | ||
FROM CTE | ||
WHERE Status = '3' | ||
) | ||
FROM CTE; | ||
" | ||
} | ||
Process { | ||
Try { | ||
|
||
## Run SQL query | ||
[psobject]$Result = Invoke-Sqlcmd -Query $Query -Server $Server -Database $Database -ErrorAction 'Stop' | ||
} | ||
Catch { | ||
Write-Error -Message "Query Error: `n $_.ErrorMessage" | ||
} | ||
Finally { | ||
|
||
## Output result | ||
Write-Output -InputObject $Result | ||
} | ||
} | ||
End { | ||
} | ||
} | ||
#endregion |
Oops, something went wrong.