Because the web servers that are usually managed are VM servers, in order to save hard disk space, the hard disk space allocated to the virtual machine is generally small, with only 8G. Because it cannot save many logs, it is necessary to transfer each WEB log to a server with a larger hard disk every day, and then use NBU centralized backup. This program mainly uses the Go language to realize the automatic upload of web logs through FTP. It uses the traversal log directory and the third-party pure go library "/jlaffaye/ftp", and the log VM's local storage path format is /var/log/weblog/month/.
// uploadlog /* 1. This program mainly implements the use of uploading web logs under Linux. 2. The method is uploadlog logfile_dir The program only uploads log files at the current point in time. */ package main import ( "fmt" ftp "/jlaffaye/ftp" "log" "net" "os" "path/filepath" "strconv" "strings" "time" ) func main() { ("Hello World!") if len() != 2 { ("Usage:" + ([0]) + " log_dir ") (1) } //logFileName is the log to be analyzed logFileName, _, _ := getLogFileName() serverIp := getLocalIpAddr() //serverName, _ := () ((90) * ) dir := [1] (dir, func(path string, f , err error) error { if f == nil { return err } if () { return nil } if () == logFileName { (path) //pathFields' function is to parse the log path into a data, so that the log domain name can be obtained. Note that if it is a Linux system, use "/" pathFields := (path, "\") var domainName string if len(pathFields) > 3 { domainName = pathFields[len(pathFields)-3] } (()) ftpUploadFile("ftp-server-ip:21", "logftpuser", "ftp-password", path, domainName, serverIp+"_"+logFileName) (()) } return nil }) } func getLogFileName() (string, string, string) { MonthTOstr := map[string]string{"January": "01", "February": "02", "March": "03", "April": "04", "May": "05", "June": "06", "July": "07", "August": "08", "September": "09", "October": "10", "November": "11", "December": "12"} timenow := () year, month, day := () //monthStr := () hour, _, _ := () yearStr := ((year), "20") //Remove the previous four-digit years, such as "20" in "2014" dayStr, hourStr := (day), (hour) if day < 10 { dayStr = "0" + dayStr } if hour < 10 { hourStr = "0" + hourStr } fileName := "ex" + yearStr + MonthTOstr[()] + dayStr + hourStr + ".log" logDay := yearStr + MonthTOstr[()] + dayStr logMonth := yearStr + MonthTOstr[()] //monthSrt := (()) //(fileName, logDay) return fileName, logDay, logMonth //(fileName) } func getLocalIpAddr() string { // Just use a legal IP here, the port is free, even if it is not opened. Maybe because of using UDP, if you use TCP, there will be problems if the peer does not open it. conn, err := ("udp", "192.168.8.51:80") if err != nil { //(()) return "127.0.0.1" } defer () //(().String()) //conn. //((().String(), ":")[0]) return (().String(), ":")[0] } func ftpUploadFile(ftpserver, ftpuser, pw, localFile, remoteSavePath, saveName string) { ftp, err := (ftpserver) if err != nil { (err) } err = (ftpuser, pw) if err != nil { (err) } //Note that it is pub/log, and cannot start with "/". ("pub/log") dir, err := () (dir) (remoteSavePath) (remoteSavePath) dir, _ = () (dir) file, err := (localFile) if err != nil { (err) } defer () err = (saveName, file) if err != nil { (err) } () () ("success upload file:", localFile) }
The above is the entire content of this article, I hope you like it.