This commit is contained in:
wangjian 2022-08-07 20:49:02 +08:00
commit 46bc9dd6ff
6 changed files with 267 additions and 0 deletions

73
cmd/server.go Normal file
View File

@ -0,0 +1,73 @@
package cmd
import (
"file_monitoring/config"
"git.hpds.cc/Component/logging"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"go.uber.org/zap"
"log"
)
var (
ConfigFile string = "./config/config.yaml"
)
func Run() *cobra.Command {
//创建一个监控对象
watch, err := fsnotify.NewWatcher()
if err != nil {
//log.Fatal(err)
logging.L().Error("new watcher err", zap.Error(err))
}
defer watch.Close()
//添加要监控的对象,文件或文件夹
err = watch.Add(config.DefaultMonitoringDirectory)
if err != nil {
logging.L().Error("new watcher err", zap.Error(err))
}
//我们另启一个goroutine来处理监控对象的事件
go func() {
for {
select {
case ev := <-watch.Events:
{
//判断事件发生的类型如下5种
// Create 创建
// Write 写入
// Remove 删除
// Rename 重命名
// Chmod 修改权限
if ev.Op&fsnotify.Create == fsnotify.Create {
logging.L().Info("创建文件", zap.String("文件名", ev.Name))
//log.Println("创建文件 : ", ev.Name)
}
if ev.Op&fsnotify.Write == fsnotify.Write {
logging.L().Info("写入文件", zap.String("文件名", ev.Name))
//log.Println("写入文件 : ", ev.Name)
}
if ev.Op&fsnotify.Remove == fsnotify.Remove {
logging.L().Info("删除文件", zap.String("文件名", ev.Name))
//log.Println("删除文件 : ", ev.Name)
}
if ev.Op&fsnotify.Rename == fsnotify.Rename {
logging.L().Info("重命名文件", zap.String("文件名", ev.Name))
//log.Println("重命名文件 : ", ev.Name)
}
if ev.Op&fsnotify.Chmod == fsnotify.Chmod {
logging.L().Info("修改权限", zap.String("文件名", ev.Name))
//log.Println("修改权限 : ", ev.Name)
}
}
case err := <-watch.Errors:
{
log.Println("error : ", err)
return
}
}
}
}()
//循环
select {}
}

82
config/config.go Normal file
View File

@ -0,0 +1,82 @@
package config
import (
"git.hpds.cc/Component/logging"
"github.com/spf13/viper"
"os"
)
type Config struct {
MonitoringConf MonitoringDirectoryConfig
TransferConf TransferConfig
Log *logging.Logger
}
type MonitoringDirectoryConfig struct {
Path string `json:"path" yaml:"path" toml:"path"`
}
type TransferConfig struct {
Proto string `json:"proto" yaml:"proto" toml:"proto"`
Address string `json:"address" yaml:"address" toml:"address"`
Port int `json:"port" yaml:"port" toml:"port"`
}
var (
DefaultMonitoringDirectory = "./tmp/"
DefaultTransfer = TransferConfig{
Proto: "MQTT",
Address: "127.0.0.1",
Port: 6060,
}
)
// DefaultConfig return the default configuration.
// If config file is not provided, hpds_mqd will start with DefaultConfig.
func DefaultConfig() Config {
path, err := os.Getwd()
if err != nil {
panic(err)
}
vp := viper.New()
vp.AddConfigPath(".")
vp.AddConfigPath(path)
vp.AddConfigPath(path + "/cmd")
vp.AddConfigPath(path + "/config")
vp.SetConfigName("config")
vp.SetConfigType("yaml")
if err := vp.ReadInConfig(); err != nil {
panic(err)
}
DefaultMonitoringDirectory = vp.GetString("path")
DefaultTransfer = TransferConfig{
Proto: vp.GetString("transfer.proto"),
Address: vp.GetString("transfer.address"),
Port: vp.GetInt("transfer.port"),
}
c := Config{
MonitoringConf: MonitoringDirectoryConfig{Path: DefaultMonitoringDirectory},
TransferConf: DefaultTransfer,
Log: ParseLoggingConfig(vp),
}
return c
}
func ParseLoggingConfig(vp *viper.Viper) *logging.Logger {
logger := logging.NewLogger(
logging.SetPath(vp.GetString("log.path")),
logging.SetPrefix(vp.GetString("log.prefix")),
logging.SetDevelopment(vp.GetBool("log.development")),
logging.SetDebugFileSuffix(vp.GetString("log.debugFileSuffix")),
logging.SetWarnFileSuffix(vp.GetString("log.warnFileSuffix")),
logging.SetErrorFileSuffix(vp.GetString("log.errorFileSuffix")),
logging.SetInfoFileSuffix(vp.GetString("log.infoFileSuffix")),
logging.SetMaxAge(vp.GetInt("log.maxAge")),
logging.SetMaxBackups(vp.GetInt("log.maxBackups")),
logging.SetMaxSize(vp.GetInt("log.maxSize")),
logging.SetLevel(logging.LogLevel[vp.GetString("log.level")]),
)
return logger
}

31
config/config.yaml Normal file
View File

@ -0,0 +1,31 @@
path: "./tmp/"
transfer:
proto: "mqtt"
address: "127.0.0.1"
port: 6060
log:
# 日志级别debug/info/error/warn
level: debug # debug | info | warn | error
path : "./logs"
# 日志文件前缀
prefix : "hpds_mq"
# 是否为开发者模式
development : true
# debug日志文件后缀
debugFileSuffix : "debug.log"
# warn日志文件后缀
warnFileSuffix : "warn.log"
# error日志文件后缀
errorFileSuffix : "error.log"
# info日志文件后缀
infoFileSuffix : "info.log"
# 保存的最大天数
maxAge : 180
# 最多存在多少个切片文件
maxBackups : 3000
# 日日志文件大小M
maxSize : 100
# whether to dump MQTT packet in debug level
dump_packet: false

7
config/version.go Normal file
View File

@ -0,0 +1,7 @@
package config
// The git commit that was compiled. This will be filled in by the compiler.
var GitCommit string
// The main version number that is being run at the moment.
const Version = ""

33
go.mod Normal file
View File

@ -0,0 +1,33 @@
module file_monitoring
go 1.18
require (
git.hpds.cc/Component/logging v0.0.0-20220803091419-ba6a18ff2e88
github.com/fsnotify/fsnotify v1.5.4
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.0
go.uber.org/zap v1.21.0
)
require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)

41
main.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"file_monitoring/cmd"
"file_monitoring/config"
"fmt"
"net/http"
"os"
"github.com/spf13/cobra"
)
var (
rootCmd = &cobra.Command{
Use: "file_monitoring",
Long: "file_monitoring is a file monitoring and transfer tool",
Version: config.Version,
}
enablePprof bool
pprofAddr = "127.0.0.1:6060"
)
func init() {
config.DefaultConfig()
rootCmd.PersistentFlags().StringVarP(&cmd.ConfigFile, "config", "c", cmd.ConfigFile, "The configuration file path")
rootCmd.AddCommand(cmd.Run())
//rootCmd.AddCommand(cmd.NewReloadCommand())
}
func main() {
if enablePprof {
go func() {
http.ListenAndServe(pprofAddr, nil)
}()
}
if err := rootCmd.Execute(); err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
}