-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-filezillaLOG.ps1
110 lines (97 loc) · 3.48 KB
/
check-filezillaLOG.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
<#
.SYNOPSIS
Goes through the FileZilla log file to check if file is uploaded and messages a person for each file.
.DESCRIPTION
Run the script after you have logged in and keep it running in the background
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
None
.OUTPUTS
E-mail/slack(in code)/print in prompt.
.NOTES
Version: 2.0
Author: Kjetil Grun
Creation Date: 27.06.2016
Purpose/Change: Send a message to me everytime someone uploads something to FileZilla Server
Powershell Version: 5
.EXAMPLE
<Example goes here. Repeat this attribute for more than one example>
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Log location
$fzdir = 'C:\Program Files (x86)\FileZilla Server\Logs\'
$line = 0
$lastfile = ''
$counter = 0
#-----------------------------------------------------------[Functions]------------------------------------------------------------
function reset-linecount {
write-host "checking if the file is new"
if($lastfile -like '' -or $fzlog -notlike $lastfile) {
Write-Host 'New file. Reset line counter'
$line = 0
return $line
}
}
function set-lastfile {
$lastfile = $fzlog
return $lastfile
}
function find-logfilesize {
Write-Host 'Checking logfile'
Write-Host "Number of lines when last checked $line"
# Finding the line number of the file
$currlines = Get-Content $fzdir$fzlog | Measure-Object -Line
Write-Host 'Number of lines in file: ' $currlines.Lines
return $currlines
}
function check-lineschanged {
# Checking if number of lines has changed
if($currlines.Lines -gt $line -and ((gc $fzdir$fzlog) -gt $Null)) {
# If number of lines is more than last time - check the newest lines
(Get-Content (Get-Item $fzdir$fzlog))[$line .. $currlines.Lines]| foreach {
$word = $_.split(' ')
# For loop through the line as it seems different powershell versions has different ways of counting the list. It should not be less than 7 words into the sentence.
for($i=0; $i -le 6; $i++) {
# Finding the word STOR which is the status code for stored file in FileZilla and adding it to an list
if($word[$i] -like 'STOR') {
$msglist +=($word[$i+1]+" is uploaded by IP"+$word[$i-1]+" to site: "+$word[$i-2]+"`n")
}
}
}
}
# Setting the value of the line numbers and filename for the next check.
return $msglist
}
function set-lineno {
$line = $currlines.lines
return $line
}
function send-message {
if($msglist -notlike $null){
#$postSlackMessage = @{token="notoken4ugetyerown";channel='compile-bot';as_user="false";icon_url="icon url";text=$msglist;username="jian-yang"}
#Invoke-RestMethod -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage
write-host $msglist
}
# Sleeping for X seconds so not to check all the time
Start-Sleep -s 5
}
while ($true) {
# Finding last changed logfile
$fzlog = Get-ChildItem $fzdir | Where-Object { ! $_.PSIsContainer } | Sort-Object lastwriteTime | Select-Object -last 1
# Resetting line counter if new file
if($lastfile -like '' -or $fzlog -notlike $lastfile) {
Write-Host 'New file. Reset line counter'
$line = 0
}
# Doing the functions
$lastfile = set-lastfile
$currlines = find-logfilesize
$msglist = check-lineschanged
# Setting the line number for the next recursion and sending the array
$line = set-lineno
write-host $line
send-message
# Clearing the array
clear-variable -Name "msglist"
}