The go language implementation automatically uploadweb logs through the FTP library

  • 2020-05-27 05:55:03
  • OfStack

Because at ordinary times web are VM server management, in order to save disk space, 1, assign the virtual machine hard disk space is small, only 8 G, because, save not much log, so every day needs to transfer each WEB log to a hard disk is larger on the server, and then use NBU centralized backup, this program mainly use go language implementation implementation will upload FTP server automatically by the FTP web log, Using filepath. Walk traversal log directory and 3 Fang Chun go library "github. com/jlaffaye/ftp", and log VM local storage path format is/var/log/weblog/www domainName. com month / 20140616. access. log,


// uploadlog
/*
1. This procedure is mainly implemented linux The upload web Log use, 
2. The method of use is  uploadlog logfile_dir
 The application only uploads the log file at the current point in time, 
*/
package main
import (
  "fmt"
  ftp "github.com/jlaffaye/ftp"
  "log"
  "net"
  "os"
  "path/filepath"
  "strconv"
  "strings"
  "time"
)
func main() {
  fmt.Println("Hello World!")
  if len(os.Args) != 2 {
    log.Fatal("Usage:" + filepath.Base(os.Args[0]) + " log_dir ")
    os.Exit(1)
  }
  //logFileName Is the log to be analyzed 
  logFileName, _, _ := getLogFileName()
  serverIp := getLocalIpAddr()
  //serverName, _ := os.Hostname()
  time.Sleep(time.Duration(90) * time.Second)
  dir := os.Args[1]
  filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
    if f == nil {
      return err
    }
    if f.IsDir() {
      return nil
    }
    if f.Name() == logFileName {
      fmt.Println(path)
      //pathFields The role is to log path Parsed into 1 Data so that you can get the domain name of the log , Notice, if it is linux The system, use" / " 
      pathFields := strings.Split(path, "\")
      var domainName string
      if len(pathFields) > 3 {
        domainName = pathFields[len(pathFields)-3]
      }
      fmt.Println(time.Now())
      ftpUploadFile("ftp-server-ip:21", "logftpuser", "ftp-password", path, domainName, serverIp+"_"+logFileName)
      fmt.Println(time.Now())
    }
    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 := time.Now()
  year, month, day := timenow.Date()
  //monthStr := month.String()
  hour, _, _ := timenow.Clock()
  yearStr := strings.TrimLeft(strconv.Itoa(year), "20") // Get rid of the front 4 Digit year: "2014" Years of" 20 " 
  dayStr, hourStr := strconv.Itoa(day), strconv.Itoa(hour)
  if day < 10 {
    dayStr = "0" + dayStr
  }
  if hour < 10 {
    hourStr = "0" + hourStr
  }
  fileName := "ex" + yearStr + MonthTOstr[month.String()] + dayStr + hourStr + ".log"
  logDay := yearStr + MonthTOstr[month.String()] + dayStr
  logMonth := yearStr + MonthTOstr[month.String()]
  //monthSrt := strconv.Itoa(timenow.Month())
  //fmt.Println(fileName, logDay)
  return fileName, logDay, logMonth
  //fmt.Println(fileName)
}
func getLocalIpAddr() string {
  // Used here 1 A legal IP Ok, the port is free, even if it is not open, maybe because of use UDP If use TCP If you don't open the other end, you're going to have a problem 
  conn, err := net.Dial("udp", "192.168.8.51:80")
  if err != nil {
    //fmt.Println(err.Error())
    return "127.0.0.1"
  }
  defer conn.Close()
  //fmt.Println(conn.LocalAddr().String())
  //conn.
  //fmt.Println(strings.Split(conn.LocalAddr().String(), ":")[0])
  return strings.Split(conn.LocalAddr().String(), ":")[0]
}
func ftpUploadFile(ftpserver, ftpuser, pw, localFile, remoteSavePath, saveName string) {
  ftp, err := ftp.Connect(ftpserver)
  if err != nil {
    fmt.Println(err)
  }
  err = ftp.Login(ftpuser, pw)
  if err != nil {
    fmt.Println(err)
  }
  // Pay attention to is  pub/log Can't take" / "At the beginning 
  ftp.ChangeDir("pub/log")
  dir, err := ftp.CurrentDir()
  fmt.Println(dir)
  ftp.MakeDir(remoteSavePath)
  ftp.ChangeDir(remoteSavePath)
  dir, _ = ftp.CurrentDir()
  fmt.Println(dir)
  file, err := os.Open(localFile)
  if err != nil {
    fmt.Println(err)
  }
  defer file.Close()
  err = ftp.Stor(saveName, file)
  if err != nil {
    fmt.Println(err)
  }
  ftp.Logout()
  ftp.Quit()
  fmt.Println("success upload file:", localFile)
}

That's all for this article, I hope you enjoy it.


Related articles: