36 Star 107 Fork 38

mylxsw/remote-tail

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 4.61 KB
一键复制 编辑 原始数据 按行查看 历史
管宜尧 提交于 2019-06-27 15:58 +08:00 . bugfix
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"github.com/BurntSushi/toml"
"github.com/mylxsw/remote-tail/command"
"github.com/mylxsw/remote-tail/console"
)
var mossSep = ".--. --- .-- . .-. . -.. -... -.-- -- -.-- .-.. -..- ... .-- \n"
var welcomeMessage = getWelcomeMessage() + console.ColorfulText(console.TextMagenta, mossSep)
var filePath = flag.String("file", "", "-file=\"/var/log/*.log\"")
var hostStr = flag.String("hosts", "", "-hosts=root@192.168.1.101,root@192.168.1.102")
var configFile = flag.String("conf", "", "-conf=example.toml")
var tailFlags = flag.String("tail-flags", "--retry --follow=name", "flags for tail command, you can use -f instead if your server does't support `--retry --follow=name` flags")
var slient = flag.Bool("slient", false, "-slient=false")
var Version = ""
var GitCommit = ""
func usageAndExit(message string) {
if message != "" {
fmt.Fprintln(os.Stderr, message)
}
flag.Usage()
fmt.Fprint(os.Stderr, "\n")
os.Exit(1)
}
func printWelcomeMessage(config command.Config) {
fmt.Println(welcomeMessage)
for _, server := range config.Servers {
// If there is no tail_file for a service configuration, the global configuration is used
if server.TailFile == "" {
server.TailFile = config.TailFile
}
serverInfo := fmt.Sprintf("%s@%s:%s", server.User, server.Hostname, server.TailFile)
fmt.Println(console.ColorfulText(console.TextMagenta, serverInfo))
}
fmt.Printf("\n%s\n", console.ColorfulText(console.TextCyan, mossSep))
}
func parseConfig(filePath string, hostStr string, configFile string, slient bool, tailFlags string) (config command.Config) {
if configFile != "" {
if _, err := toml.DecodeFile(configFile, &config); err != nil {
log.Fatal(err)
}
} else {
hosts := strings.Split(hostStr, ",")
config = command.Config{}
config.TailFile = filePath
config.Servers = make(map[string]command.Server, len(hosts))
config.Slient = slient
config.TailFlags = tailFlags
for index, hostname := range hosts {
hostInfo := strings.Split(strings.Replace(hostname, ":", "@", -1), "@")
var port int
if len(hostInfo) > 2 {
port, _ = strconv.Atoi(hostInfo[2])
}
config.Servers["server_"+string(index)] = command.Server{
ServerName: "server_" + string(index),
Hostname: hostInfo[1],
User: hostInfo[0],
Port: port,
}
}
}
if config.TailFlags == "" {
config.TailFlags = "--retry --follow=name"
}
return
}
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, welcomeMessage)
fmt.Fprint(os.Stderr, "Options:\n\n")
flag.PrintDefaults()
}
flag.Parse()
if (*filePath == "" || *hostStr == "") && *configFile == "" {
usageAndExit("")
}
config := parseConfig(*filePath, *hostStr, *configFile, *slient, *tailFlags)
if !config.Slient {
printWelcomeMessage(config)
}
outputs := make(chan command.Message, 255)
var wg sync.WaitGroup
for _, server := range config.Servers {
wg.Add(1)
go func(server command.Server) {
defer func() {
if err := recover(); err != nil {
fmt.Printf(console.ColorfulText(console.TextRed, "Error: %s\n"), err)
}
}()
defer wg.Done()
// If there is no tail_file for a service configuration, the global configuration is used
if server.TailFile == "" {
server.TailFile = config.TailFile
}
if server.TailFlags == "" {
server.TailFlags = config.TailFlags
}
// If the service configuration does not have a port, the default value of 22 is used
if server.Port == 0 {
server.Port = 22
}
cmd := command.NewCommand(server)
cmd.Execute(outputs)
}(server)
}
if len(config.Servers) > 0 {
go func() {
for output := range outputs {
content := strings.Trim(output.Content, "\r\n")
// 去掉文件名称输出
if content == "" || (strings.HasPrefix(content, "==>") && strings.HasSuffix(content, "<==")) {
continue
}
if config.Slient {
fmt.Printf("%s -> %s\n", output.Host, content)
} else {
fmt.Printf(
"%s %s %s\n",
console.ColorfulText(console.TextGreen, output.Host),
console.ColorfulText(console.TextYellow, "->"),
content,
)
}
}
}()
} else {
fmt.Println(console.ColorfulText(console.TextRed, "No target host is available"))
}
wg.Wait()
}
func getWelcomeMessage() string {
return `
____ _ _____ _ _
| _ \ ___ _ __ ___ ___ | |_ __|_ _|_ _(_) |
| |_) / _ \ '_ ' _ \ / _ \| __/ _ \| |/ _' | | |
| _ < __/ | | | | | (_) | || __/| | (_| | | |
|_| \_\___|_| |_| |_|\___/ \__\___||_|\__,_|_|_|
Author: mylxsw
Homepage: github.com/mylxsw/remote-tail
Version: ` + Version + "(" + GitCommit + ")" + `
`
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/orionis/remote-tail.git
git@gitee.com:orionis/remote-tail.git
orionis
remote-tail
remote-tail
master

搜索帮助