-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInstall-Fluentd.psm1
111 lines (92 loc) · 4.01 KB
/
Install-Fluentd.psm1
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
<#
.SYNOPSIS
This downlaods, installs and configures a fluentd windows agent via td-agent to forward logs to a centeralized server.
.PARAMETER Server
The FQDN or IP of the fluentd server to forward the packets to.
.PARAMETER Port
The port number of the server to specify (Defaults to 443)
.PARAMETER Tag
The tag to forward the logs with. (defaults to winevt.raw)
.PARAMETER Version
The version of td-agent to install. (Defaults to 4.0.1)
.EXAMPLE
Install-Fluentd -Server 192.168.1.40 -Servername fluentd-02 -Tag "it.winevt.raw" Port 7777
#>
function Install-Fluentd {
[CmdletBinding()]
param(
[Parameter()]
[string]$Server,
[string]$Servername,
[string]$Tag='winevt.raw',
[int]$Port=443,
[version]$Version='4.0.1'
)
# Set some more variables for the installation:
$StartDTM = (Get-Date)
$Vendor = "TreasureData"
$Product = "td-agent"
$PackageName = "td-agent-$Version$(if ($Version.Major -eq 3 ){Write-Output '-0'})-x64"
$InstallerType = "msi"
$Path = "C:\Windows\Installers\"
$Installer = "$packageName.$installerType"
$InstallerPath = "$Path$($Installer)"
# Build a URL with the version number and fail if it's obviously the wrong version
if ($Version.Major -ge 3 ){
$DownloadURL="http://packages.treasuredata.com.s3.amazonaws.com/$($Version.Major)/windows/td-agent-$Version$(if ($Version.Major -eq 3 ){Write-Output '-0'})-x64.msi"
}else{
Write-Error -Message "The version of td-agent does not exist." -Category InvalidArgument -ErrorAction Stop
}
# Fail if there's a bad response from the url we built
if ( $(Test-URL $DownloadURL) -eq $false ) {
$ErrorMessage = "Could not verify a download link for the specified version: $Version "
Write-Error -Message $ErrorMessage -Category ResourceUnavailable -ErrorAction Stop
}
# Error if td-agent is already installed.
$TDAgent = Get-Itemproperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' | Select-Object DisplayName, DisplayVersion, UninstallString, PSChildName | Where-Object { $_.DisplayName -imatch "td-agent" }
$ProductCode = $TDAgent.PSChildName
if ( $ProductCode -ne $null ){
Write-Error -Message "Another install was already found. Use Uninstall-Fluentd to remove it." -Category DeviceError -ErrorAction Stop
}
# Check if $Path exists if not create it
if (Test-Path -Path $Path -PathType Container)
{ Write-Verbose "$Path already exists" -Verbose }
else
{ New-Item -Path $Path -ItemType directory }
# Download the Installer
Write-Verbose "Downloading $Vendor $Product $Version" -Verbose
Invoke-WebRequest $DownloadURL -OutFile $InstallerPath
# Start the installation
Write-Verbose "Installing $Vendor $Product $Version" -Verbose
Start-Process msiexec -ArgumentList "/i $InstallerPath", "/qn" -Wait
# Remove the installer
rm -Force $InstallerPath
# Configure the Service
Set-FluentdConfig -Servername $(if ($ServerName){$ServerName}else{$Server}) -Server $Server -Tag $Tag -Port $Port
# Register the fluentd service and set it to automatically start.
Start-Process "C:\opt\td-agent\bin\fluentd" -ArgumentList "--reg-winsvc i" -Wait -Verb RunAs
Start-Process "C:\opt\td-agent\bin\fluentd" -ArgumentList "--reg-winsvc-fluentdopt '-c C:/opt/td-agent/etc/td-agent/td-agent.conf -o C:/opt/td-agent/td-agent.log'" -Wait -Verb RunAs
# Add windows_eventlog plugin
Start-Process "C:\opt\td-agent\bin\fluentd" -ArgumentList "fluent-gem install fluent-plugin-windows-eventlog" -Wait -Verb RunAs
Enable-Fluentd
}
function Test-URL {
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,Position=1)]
[string]$URL
)
# First we create the request.
$HTTP_Request = [System.Net.WebRequest]::Create($URL)
# We then get a response from the site.
$HTTP_Response = $HTTP_Request.GetResponse()
# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode
# Finally, we clean up the http request by closing it.
If ($HTTP_Response -eq $null) { } Else { $HTTP_Response.Close() }
If ($HTTP_Status -eq 200) {
return $true
} Else {
return $false
}
}