forked from tomtorggler/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioCodesCdr.ps1
207 lines (195 loc) · 6.79 KB
/
AudioCodesCdr.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
function Get-Cdr {
[cmdletbinding()]
param(
[Parameter(Mandatory,ValueFromPipeline)]
[System.IO.FileInfo]
$Path,
[Parameter(Mandatory)]
[ValidateSet("MEDIA","SBC")]
[string]$Type
)
process {
$Pattern = "\|CALL_[START|CONNECT|END]"
if($type -eq "MEDIA") {
$Pattern = "\|MEDIA_[START|UPDATE|END]"
}
if(Test-Path $Path) {
Write-Verbose "Get CDR from: $Path"
(Select-String -Pattern $Pattern -Path $Path | Select-Object -ExpandProperty line).TrimEnd() -replace "\|","," -replace "\s*,",","
}
}
}
function Split-Cdr {
[CmdletBinding()]
param(
[Parameter(Mandatory,ValueFromPipeline)]
[System.IO.FileInfo]
$Path,
[System.IO.FileInfo]
$OutputFolder = $env:temp
)
# create output folder if it does not exist
New-Item -Path $OutputFolder -ItemType Directory -ErrorAction SilentlyContinue
$MediaPath = Join-path -Path $OutputFolder -ChildPath media.txt
$SBCPath = Join-path -Path $OutputFolder -ChildPath sbc.txt
# delete existing target files if they exist
Remove-Item $MediaPath,$SBCPath -ErrorAction SilentlyContinue
foreach($f in $path) {
Get-Cdr -Path $f -Type SBC | Add-Content $SBCPath
Get-Cdr -Path $f -Type MEDIA | Add-Content $MediaPath
}
}
function Import-Cdr {
<#
.SYNOPSIS
Import CDR from file.
.DESCRIPTION
This function uses Import-Csv to import CDR information form a file.
.EXAMPLE
PS C:\> Import-Cdr -Path .\CDR-2018-10-03-08.log -Header $Header
This example imports Media CDRs.
.INPUTS
[system.io.fileinfo]
.OUTPUTS
[psobject]
.NOTES
Author: @torggler
#>
[CmdletBinding()]
param(
[Parameter(Mandatory,ValueFromPipeline)]
[System.IO.FileInfo]
$Path,
[Parameter()]
[string[]]$Header
)
process {
if(Test-Path $Path) {
Import-Csv -Path $Path -Delimiter "," -Header $Header | Select-Object *,@{n="Jitter";e={$_.RTPjitter -as [int]}},@{n="Delay";e={$_.RTPdelay -as [int]}} -ExcludeProperty RTPjitter,RTPdelay
}
}
}
function Get-CdrTitle {
<#
.SYNOPSIS
Extract CSV header from CDR file.
.DESCRIPTION
This function extracts the header information from a CDR file. This can later be used, to import the CDR using Import-Csv.
.EXAMPLE
PS C:\> Get-CdrTitle .\CDR-2018-10-03-08.log -Type SBC
This example extracts the header for SBCReport.
.EXAMPLE
PS C:\> Get-CdrTitle .\CDR-2018-10-03-08.log -Type MEDIA
This example extracts the header for MediaReport.
.INPUTS
[system.io.fileinfo]
.OUTPUTS
[string]
.NOTES
Author: @torggler
#>
[CmdletBinding()]
param(
[Parameter(Mandatory,ValueFromPipeline)]
[System.IO.FileInfo]
$Path,
[Parameter(Mandatory)]
[ValidateSet("MEDIA","SBC")]
[string]$Type
)
$Pattern = "SBCReportType"
if($type -eq "MEDIA") {
$Pattern = "MediaReportType"
}
if(Test-Path $Path) {
$title = Select-String -Path $path -Pattern $Pattern | Select-Object -ExpandProperty line | Select-Object -First 1
$out = $title -replace "^.*?\|","Timestamp|" -replace " ","" -split "\|" -replace "\(\w+\)","" -replace "SBCReportType","ReportType" -replace "MediaReportType","ReportType"
}
if($Type -eq "MEDIA" -and -not($out)){
$out = @('Timestamp','ReportType','SIPCallId','SessionId','Cid','MediaType','Coder','Intrv','LocalRtpIp','LocalRtpPort','RemoteRtpIp','RemoteRtpPort','InPackets','OutPackets','LocalPackLoss','RemotePackLoss','RTPdelay','RTPjitter','TxRTPssrc','RxRTPssrc','LocalRFactor','RemoteRFactor','LocalMosCQ','RemoteMosCQ','TxRTPIPDiffServ','LatchedRtpIp','LatchedRtpPort','LatchedT38Ip','LatchedT38Port','CoderTranscoding','LegId')
} elseif($Type -eq "SBC" -and -not($out)) {
$out = @('Timestamp','ReportType','EPTyp','SIPCallId','SessionId','Orig','SourceIp','SourcePort','DestIp','DestPort','TransportType','SrcURI','SrcURIBeforeMap','DstURI','DstURIBeforeMap','Durat','TrmSd','TrmReason','TrmReasonCategory','SetupTime','ConnectTime','ReleaseTime','RedirectReason','RedirectURINum','RedirectURINumBeforeMap','TxSigIPDiffServ','IPGroup','SrdId','SIPInterfaceId','ProxySetId','IpProfileId','MediaRealmId','DirectMedia','SIPTrmReason','SipTermDesc')
}
Write-Output $out
}
<#
function Get-BadStream {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[System.IO.FileInfo]
$Path,
[Parameter()]
[ValidateSet("Jitter","Delay","PackLoss")]
$Type = "Jitter",
[int]
$Count
)
process {
switch($Type){
'Jitter' { $fs = {$_.RTPjitter -gt 1} }
'Delay' { $fs = {$_.RTPdelay -gt 1} }
'PackLoss' { $fs = {$_.RemotePackLoss -gt 1 -or $_.LocalPackLoss -gt 1}}
}
if($count) {
### Add Header!
Import-Cdr -Path $Path | Where-Object -FilterScript $fs | Select-Object -First $Count
} else {
Import-Cdr -Path $Path | Where-Object -FilterScript $fs
}
}
}
#>
function Get-NrFromUri {
[CmdletBinding()]
param (
[Parameter(Mandatory,ValueFromPipeline)]
$InputObject
)
process {
foreach($i in $InputObject) {
$i -replace "[@|;].*$",""
}
}
}
function Get-DateFromTime {
[CmdletBinding()]
param (
[Parameter(Mandatory,ValueFromPipeline)]
$InputObject
)
process {
foreach($i in $InputObject) {
$J = $i -split "UTC"
Get-Date (($J[1] -replace "(\w{3})(\w{3})(\d{2})(\d{4})","`$1 `$2 `$3 `$4"),$J[0] -join " ")
}
}
}
function Get-HostFromPath {
[CmdletBinding()]
param (
[Parameter(Mandatory,ValueFromPipeline)]
[System.IO.FileInfo]
$InputObject
)
process {
# split the Path at \ and try to cast each part as [ipaddress]. Only returns valid casts.
$InputObject.DirectoryName -split "\\" | ForEach-Object { $_ -as [ipaddress] } | Select-Object -ExpandProperty IPAddressToString
}
}
function Get-MediaCdr{
[cmdletbinding()]
param($path)
$header = Get-CdrTitle -Path $path -Type MEDIA
$tempfile = Join-path -Path $env:temp -ChildPath "$((get-date).ticks).txt"
Get-Cdr $path -Type MEDIA | Set-Content $tempfile
Import-Cdr -Header $header -Path $tempfile
}
function Get-SbcCdr{
[cmdletbinding()]
param($path)
$header = Get-CdrTitle -Path $path -Type SBC
$tempfile = Join-path -Path $env:temp -ChildPath "$((get-date).ticks).txt"
Get-Cdr $path -Type SBC | Set-Content $tempfile
Import-Cdr -Header $header -Path $tempfile
}