1 Star 0 Fork 38

秦X/remote-tail

forked from mylxsw/remote-tail 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 3.43 KB
一键复制 编辑 原始数据 按行查看 历史
管宜尧 提交于 2016-12-01 15:00 . update
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 string = getWelcomeMessage() + console.ColorfulText(console.TextMagenta, mossSep)
var filePath *string = flag.String("file", "", "-file=\"/home/data/logs/**/*.log\"")
var hostStr *string = flag.String("hosts", "", "-hosts=root@192.168.1.225,root@192.168.1.226")
var configFile *string = flag.String("conf", "", "-conf=example.toml")
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) (config command.Config) {
if configFile != "" {
if _, err := toml.DecodeFile(configFile, &config); err != nil {
log.Fatal(err)
}
} else {
var hosts []string = strings.Split(hostStr, ",")
config = command.Config{}
config.TailFile = filePath
config.Servers = make(map[string]command.Server, len(hosts))
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,
}
}
}
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)
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 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 {
fmt.Printf(
"%s %s %s",
console.ColorfulText(console.TextGreen, output.Host),
console.ColorfulText(console.TextYellow, "->"),
output.Content,
)
}
}()
} else {
fmt.Println(console.ColorfulText(console.TextRed, "No target host is available"))
}
wg.Wait()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/maoren/remote-tail.git
git@gitee.com:maoren/remote-tail.git
maoren
remote-tail
remote-tail
master

搜索帮助