66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
import "github.com/hpcloud/tail"
|
|
|
|
const rectest string = "UPLOAD"
|
|
|
|
type FtpStr struct {
|
|
wday string
|
|
month string
|
|
day string
|
|
time string
|
|
year string
|
|
pid string
|
|
user string
|
|
/*res string
|
|
act string
|
|
tromt string
|
|
fromip string
|
|
fname string
|
|
fdim string
|
|
txspeed string*/
|
|
}
|
|
|
|
func getTimestamp(tsfmt string) string {
|
|
t := time.Now()
|
|
f := "%04d%02d%02d%02d%02d%02d"
|
|
if tsfmt == "log" {
|
|
f = "%04d/%02d/%02d %02d:%02d:%02d"
|
|
}
|
|
return fmt.Sprintf(f, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
|
|
}
|
|
|
|
func MySplit(r rune) bool {
|
|
return r == '[' || r == ']' || r == ' ' || r == '"' || r == ','
|
|
}
|
|
|
|
func main() {
|
|
pgm := os.Args[0]
|
|
filePtr := flag.String("file", "/var/log/vsftpd.log", "ftp log file")
|
|
//userPtr := flag.String("user", "asega", "ftp sender user")
|
|
flag.Parse()
|
|
fmt.Println(getTimestamp("log"), ">>", path.Base(pgm))
|
|
fmt.Println(getTimestamp("log"), ">>", path.Dir(pgm))
|
|
fmt.Println(getTimestamp("log"), ">>", *filePtr)
|
|
|
|
t, _ := tail.TailFile(*filePtr, tail.Config{Follow: true})
|
|
for line := range t.Lines {
|
|
ftpfield := strings.FieldsFunc(line.Text, MySplit)
|
|
ftp := FtpStr{ftpfield[0], ftpfield[1], ftpfield[2], ftpfield[3], ftpfield[4], ftpfield[6], ftpfield[7]}
|
|
//ftpfield[10], ftpfield[11], ftpfield[12], ftpfield[13], ftpfield[14], ftpfield[15]}
|
|
if ftp.user == "asega" && ftp.{
|
|
fmt.Printf("%q\n", ftpfield)
|
|
}
|
|
}
|
|
|
|
}
|