Migrate main to pkg.
This commit is contained in:
		
							parent
							
								
									ac2f685db0
								
							
						
					
					
						commit
						18e7e20941
					
				
							
								
								
									
										9
									
								
								Makefile
								
								
								
								
							
							
						
						
									
										9
									
								
								Makefile
								
								
								
								
							|  | @ -6,6 +6,7 @@ URL:=https://github.com/davidnewhall/$(BINARY) | ||||||
| MAINT=David Newhall II <david at sleepers dot pro> | MAINT=David Newhall II <david at sleepers dot pro> | ||||||
| DESC=This daemon polls a Unifi controller at a short interval and stores the collected metric data in an Influx Database. | DESC=This daemon polls a Unifi controller at a short interval and stores the collected metric data in an Influx Database. | ||||||
| PACKAGE:=./cmd/$(BINARY) | PACKAGE:=./cmd/$(BINARY) | ||||||
|  | LIBRARY:=./pkg/$(BINARY) | ||||||
| ifeq ($(VERSION),) | ifeq ($(VERSION),) | ||||||
| 	VERSION:=$(shell git tag -l --merged | tail -n1 | tr -d v||echo development) | 	VERSION:=$(shell git tag -l --merged | tail -n1 | tr -d v||echo development) | ||||||
| endif | endif | ||||||
|  | @ -60,17 +61,17 @@ README.html: md2roff | ||||||
| 
 | 
 | ||||||
| build: $(BINARY) | build: $(BINARY) | ||||||
| $(BINARY): | $(BINARY): | ||||||
| 	go build -o $(BINARY) -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) | 	go build -o $(BINARY) -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE) | ||||||
| 
 | 
 | ||||||
| linux: $(BINARY).linux | linux: $(BINARY).linux | ||||||
| $(BINARY).linux: | $(BINARY).linux: | ||||||
| 	# Building linux binary. | 	# Building linux binary. | ||||||
| 	GOOS=linux go build -o $(BINARY).linux -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) | 	GOOS=linux go build -o $(BINARY).linux -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE) | ||||||
| 
 | 
 | ||||||
| macos: $(BINARY).macos | macos: $(BINARY).macos | ||||||
| $(BINARY).macos: | $(BINARY).macos: | ||||||
| 	# Building darwin binary. | 	# Building darwin binary. | ||||||
| 	GOOS=darwin go build -o $(BINARY).macos -ldflags "-w -s -X main.Version=$(VERSION)" $(PACKAGE) | 	GOOS=darwin go build -o $(BINARY).macos -ldflags "-w -s -X github.com/davidnewhall/unifi-poller/pkg/unifi-poller.Version=$(VERSION)" $(PACKAGE) | ||||||
| 
 | 
 | ||||||
| # Packages
 | # Packages
 | ||||||
| 
 | 
 | ||||||
|  | @ -166,7 +167,7 @@ $(BINARY).rb: v$(VERSION).tar.gz.sha256 | ||||||
| # Run code tests and lint.
 | # Run code tests and lint.
 | ||||||
| test: lint | test: lint | ||||||
| 	# Testing. | 	# Testing. | ||||||
| 	go test -race -covermode=atomic $(PACKAGE) | 	go test -race -covermode=atomic $(LIBRARY) | ||||||
| lint: | lint: | ||||||
| 	# Checking lint. | 	# Checking lint. | ||||||
| 	golangci-lint run $(GOLANGCI_LINT_ARGS) | 	golangci-lint run $(GOLANGCI_LINT_ARGS) | ||||||
|  |  | ||||||
|  | @ -2,21 +2,16 @@ package main | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" |  | ||||||
| 	"log" | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
| 
 | 
 | ||||||
| 	"github.com/golift/unifi" | 	unifipoller "github.com/davidnewhall/unifi-poller/pkg/unifi-poller" | ||||||
| 	influx "github.com/influxdata/influxdb1-client/v2" |  | ||||||
| 	"github.com/naoina/toml" |  | ||||||
| 	"github.com/pkg/errors" |  | ||||||
| 	flag "github.com/spf13/pflag" |  | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
| 	u := &UnifiPoller{} | 	u := &unifipoller.UnifiPoller{} | ||||||
| 	if u.ParseFlags(os.Args[1:]); u.ShowVer { | 	if u.ParseFlags(os.Args[1:]); u.ShowVer { | ||||||
| 		fmt.Printf("unifi-poller v%s\n", Version) | 		fmt.Printf("unifi-poller v%s\n", unifipoller.Version) | ||||||
| 		return // don't run anything else.
 | 		return // don't run anything else.
 | ||||||
| 	} | 	} | ||||||
| 	if err := u.GetConfig(); err != nil { | 	if err := u.GetConfig(); err != nil { | ||||||
|  | @ -27,99 +22,3 @@ func main() { | ||||||
| 		log.Fatalln("[ERROR]", err) | 		log.Fatalln("[ERROR]", err) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 |  | ||||||
| // ParseFlags runs the parser.
 |  | ||||||
| func (u *UnifiPoller) ParseFlags(args []string) { |  | ||||||
| 	u.Flag = flag.NewFlagSet("unifi-poller", flag.ExitOnError) |  | ||||||
| 	u.Flag.Usage = func() { |  | ||||||
| 		fmt.Println("Usage: unifi-poller [--config=filepath] [--version]") |  | ||||||
| 		u.Flag.PrintDefaults() |  | ||||||
| 	} |  | ||||||
| 	u.Flag.StringVarP(&u.DumpJSON, "dumpjson", "j", "", |  | ||||||
| 		"This debug option prints the json payload for a device and exits.") |  | ||||||
| 	u.Flag.StringVarP(&u.ConfigFile, "config", "c", defaultConfFile, "Poller Config File (TOML Format)") |  | ||||||
| 	u.Flag.BoolVarP(&u.ShowVer, "version", "v", false, "Print the version and exit") |  | ||||||
| 	_ = u.Flag.Parse(args) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // GetConfig parses and returns our configuration data.
 |  | ||||||
| func (u *UnifiPoller) GetConfig() error { |  | ||||||
| 	// Preload our defaults.
 |  | ||||||
| 	u.Config = &Config{ |  | ||||||
| 		InfluxURL:  defaultInfxURL, |  | ||||||
| 		InfluxUser: defaultInfxUser, |  | ||||||
| 		InfluxPass: defaultInfxPass, |  | ||||||
| 		InfluxDB:   defaultInfxDb, |  | ||||||
| 		UnifiUser:  defaultUnifUser, |  | ||||||
| 		UnifiPass:  os.Getenv("UNIFI_PASSWORD"), |  | ||||||
| 		UnifiBase:  defaultUnifURL, |  | ||||||
| 		Interval:   Dur{value: defaultInterval}, |  | ||||||
| 		Sites:      []string{"default"}, |  | ||||||
| 	} |  | ||||||
| 	if buf, err := ioutil.ReadFile(u.ConfigFile); err != nil { |  | ||||||
| 		return err |  | ||||||
| 		// This is where the defaults in the config variable are overwritten.
 |  | ||||||
| 	} else if err := toml.Unmarshal(buf, u.Config); err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	if u.DumpJSON != "" { |  | ||||||
| 		u.Quiet = true |  | ||||||
| 	} |  | ||||||
| 	u.Config.Logf("Loaded Configuration: %s", u.ConfigFile) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // Run invokes all the application logic and routines.
 |  | ||||||
| func (u *UnifiPoller) Run() (err error) { |  | ||||||
| 	if u.DumpJSON != "" { |  | ||||||
| 		return u.DumpJSONPayload() |  | ||||||
| 	} |  | ||||||
| 	if log.SetFlags(0); u.Debug { |  | ||||||
| 		log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate) |  | ||||||
| 		log.Println("[DEBUG] Debug Logging Enabled") |  | ||||||
| 	} |  | ||||||
| 	log.Printf("[INFO] Unifi-Poller v%v Starting Up! PID: %d", Version, os.Getpid()) |  | ||||||
| 
 |  | ||||||
| 	if err = u.GetUnifi(); err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	if err = u.GetInfluxDB(); err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	u.PollController() |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // GetInfluxDB returns an influxdb interface.
 |  | ||||||
| func (u *UnifiPoller) GetInfluxDB() (err error) { |  | ||||||
| 	u.Client, err = influx.NewHTTPClient(influx.HTTPConfig{ |  | ||||||
| 		Addr:     u.InfluxURL, |  | ||||||
| 		Username: u.InfluxUser, |  | ||||||
| 		Password: u.InfluxPass, |  | ||||||
| 	}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return errors.Wrap(err, "influxdb") |  | ||||||
| 	} |  | ||||||
| 	u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.InfluxURL, u.InfluxUser) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| // GetUnifi returns a Unifi controller interface.
 |  | ||||||
| func (u *UnifiPoller) GetUnifi() (err error) { |  | ||||||
| 	// Create an authenticated session to the Unifi Controller.
 |  | ||||||
| 	u.Unifi, err = unifi.NewUnifi(u.UnifiUser, u.UnifiPass, u.UnifiBase, u.VerifySSL) |  | ||||||
| 	if err != nil { |  | ||||||
| 		return errors.Wrap(err, "unifi controller") |  | ||||||
| 	} |  | ||||||
| 	u.Unifi.ErrorLog = log.Printf // Log all errors.
 |  | ||||||
| 	// Doing it this way allows debug error logs (line numbers, etc)
 |  | ||||||
| 	if u.Debug && !u.Quiet { |  | ||||||
| 		u.Unifi.DebugLog = log.Printf // Log debug messages.
 |  | ||||||
| 	} |  | ||||||
| 	u.Logf("Authenticated to Unifi Controller at %s as user %s", u.UnifiBase, u.UnifiUser) |  | ||||||
| 	if err = u.CheckSites(); err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 	u.Logf("Polling Unifi Controller Sites: %v", u.Sites) |  | ||||||
| 	return nil |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| package main | package unifipoller | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"time" | 	"time" | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| package main | package unifipoller | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"log" | 	"log" | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| package main | package unifipoller | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| package main | package unifipoller | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"log" | 	"log" | ||||||
|  | @ -0,0 +1,110 @@ | ||||||
|  | package unifipoller | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"io/ioutil" | ||||||
|  | 	"log" | ||||||
|  | 	"os" | ||||||
|  | 
 | ||||||
|  | 	"github.com/golift/unifi" | ||||||
|  | 	influx "github.com/influxdata/influxdb1-client/v2" | ||||||
|  | 	"github.com/naoina/toml" | ||||||
|  | 	"github.com/pkg/errors" | ||||||
|  | 	flag "github.com/spf13/pflag" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // ParseFlags runs the parser.
 | ||||||
|  | func (u *UnifiPoller) ParseFlags(args []string) { | ||||||
|  | 	u.Flag = flag.NewFlagSet("unifi-poller", flag.ExitOnError) | ||||||
|  | 	u.Flag.Usage = func() { | ||||||
|  | 		fmt.Println("Usage: unifi-poller [--config=filepath] [--version]") | ||||||
|  | 		u.Flag.PrintDefaults() | ||||||
|  | 	} | ||||||
|  | 	u.Flag.StringVarP(&u.DumpJSON, "dumpjson", "j", "", | ||||||
|  | 		"This debug option prints the json payload for a device and exits.") | ||||||
|  | 	u.Flag.StringVarP(&u.ConfigFile, "config", "c", defaultConfFile, "Poller Config File (TOML Format)") | ||||||
|  | 	u.Flag.BoolVarP(&u.ShowVer, "version", "v", false, "Print the version and exit") | ||||||
|  | 	_ = u.Flag.Parse(args) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // GetConfig parses and returns our configuration data.
 | ||||||
|  | func (u *UnifiPoller) GetConfig() error { | ||||||
|  | 	// Preload our defaults.
 | ||||||
|  | 	u.Config = &Config{ | ||||||
|  | 		InfluxURL:  defaultInfxURL, | ||||||
|  | 		InfluxUser: defaultInfxUser, | ||||||
|  | 		InfluxPass: defaultInfxPass, | ||||||
|  | 		InfluxDB:   defaultInfxDb, | ||||||
|  | 		UnifiUser:  defaultUnifUser, | ||||||
|  | 		UnifiPass:  os.Getenv("UNIFI_PASSWORD"), | ||||||
|  | 		UnifiBase:  defaultUnifURL, | ||||||
|  | 		Interval:   Dur{value: defaultInterval}, | ||||||
|  | 		Sites:      []string{"default"}, | ||||||
|  | 	} | ||||||
|  | 	if buf, err := ioutil.ReadFile(u.ConfigFile); err != nil { | ||||||
|  | 		return err | ||||||
|  | 		// This is where the defaults in the config variable are overwritten.
 | ||||||
|  | 	} else if err := toml.Unmarshal(buf, u.Config); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if u.DumpJSON != "" { | ||||||
|  | 		u.Quiet = true | ||||||
|  | 	} | ||||||
|  | 	u.Config.Logf("Loaded Configuration: %s", u.ConfigFile) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Run invokes all the application logic and routines.
 | ||||||
|  | func (u *UnifiPoller) Run() (err error) { | ||||||
|  | 	if u.DumpJSON != "" { | ||||||
|  | 		return u.DumpJSONPayload() | ||||||
|  | 	} | ||||||
|  | 	if log.SetFlags(0); u.Debug { | ||||||
|  | 		log.SetFlags(log.Lshortfile | log.Lmicroseconds | log.Ldate) | ||||||
|  | 		log.Println("[DEBUG] Debug Logging Enabled") | ||||||
|  | 	} | ||||||
|  | 	log.Printf("[INFO] Unifi-Poller v%v Starting Up! PID: %d", Version, os.Getpid()) | ||||||
|  | 
 | ||||||
|  | 	if err = u.GetUnifi(); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	if err = u.GetInfluxDB(); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	u.PollController() | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // GetInfluxDB returns an influxdb interface.
 | ||||||
|  | func (u *UnifiPoller) GetInfluxDB() (err error) { | ||||||
|  | 	u.Client, err = influx.NewHTTPClient(influx.HTTPConfig{ | ||||||
|  | 		Addr:     u.InfluxURL, | ||||||
|  | 		Username: u.InfluxUser, | ||||||
|  | 		Password: u.InfluxPass, | ||||||
|  | 	}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return errors.Wrap(err, "influxdb") | ||||||
|  | 	} | ||||||
|  | 	u.Logf("Logging Measurements to InfluxDB at %s as user %s", u.InfluxURL, u.InfluxUser) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // GetUnifi returns a Unifi controller interface.
 | ||||||
|  | func (u *UnifiPoller) GetUnifi() (err error) { | ||||||
|  | 	// Create an authenticated session to the Unifi Controller.
 | ||||||
|  | 	u.Unifi, err = unifi.NewUnifi(u.UnifiUser, u.UnifiPass, u.UnifiBase, u.VerifySSL) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return errors.Wrap(err, "unifi controller") | ||||||
|  | 	} | ||||||
|  | 	u.Unifi.ErrorLog = log.Printf // Log all errors.
 | ||||||
|  | 	// Doing it this way allows debug error logs (line numbers, etc)
 | ||||||
|  | 	if u.Debug && !u.Quiet { | ||||||
|  | 		u.Unifi.DebugLog = log.Printf // Log debug messages.
 | ||||||
|  | 	} | ||||||
|  | 	u.Logf("Authenticated to Unifi Controller at %s as user %s", u.UnifiBase, u.UnifiUser) | ||||||
|  | 	if err = u.CheckSites(); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	u.Logf("Polling Unifi Controller Sites: %v", u.Sites) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue