From aa54ee5f73762ae63a8a6d3907a77024d1badd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Zelenkov=C3=A1?= Date: Sat, 4 Jan 2025 09:25:02 +0100 Subject: [PATCH] Refactor config and get rid of some dependencies --- Dockerfile | 2 +- README.md | 56 +++++++++---------------- cmd/cmd.go | 98 ------------------------------------------- config/config.go | 50 ---------------------- config/config_test.go | 90 --------------------------------------- go.mod | 18 -------- go.sum | 52 ----------------------- log/logger.go | 9 ++-- main.go | 94 ++++++++++++++++++++++++++++++++++++++++- 9 files changed, 118 insertions(+), 351 deletions(-) delete mode 100644 cmd/cmd.go delete mode 100644 config/config.go delete mode 100644 config/config_test.go diff --git a/Dockerfile b/Dockerfile index 0ff3b1a..f298b8b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Builder image -FROM golang:1.23.4 as builder +FROM golang:1.23.4 AS builder WORKDIR /workspace ENV GO111MODULE=on diff --git a/README.md b/README.md index b5536b5..cdaf81b 100644 --- a/README.md +++ b/README.md @@ -1,50 +1,34 @@ -# IPsec Prometheus Exporter +# IPSec Prometheus Exporter -_The IPsec Prometheus exporter subscribes to the strongSwan via Vici API and exposes [Security Associations](https://github.com/strongswan/strongswan/blob/master/src/libcharon/plugins/vici/README.md#list-sa) (SAs) metrics._ +_The IPSec Prometheus exporter subscribes to the strongSwan via Vici API and exposes [Security Associations](https://github.com/strongswan/strongswan/blob/master/src/libcharon/plugins/vici/README.md#list-sa) (SAs) metrics._ Collected metrics (together with application metrics) are exposed on `/metrics` endpoint. Prometheus target is then configured with this endpoint and port e.g. `http://localhost:8079/metrics`. ## Configuration -IPsec Prometheus exporter configuration yaml file is optional. If not provided, the default values are used. +IPSec Prometheus exporter is configured via command-line arguments. If not provided, the default values are used. -### Config file +### Command-line arguments If the default value match with your choice you can omit it. -```yaml -# Logger configuration -logging: - # logging level - default: INFO - level: DEBUG - -# HTTP server configuration -server: - # server port - default: 8079 - port: 8080 - -# Vici configuration -vici: - # Vici network scheme - default: tcp - network: "udp" - # Vici host is the ip-address or hostname. - # Default values for hostname is "localhost". - # IPv6 is supported. Use host in format of "[fd12:3456:789a::1]". - host: "127.0.0.1" - # Vici port - default: 4502 - port: 30123 +``` +Options and default values: +--server-port=8079 Application port where the collected metrics are available +--log-level=info Logging level (debug, info, warn, error) +--vici-network=tcp Vici network scheme (tcp, udp, unix) +--vici-address=localhost:4502 IP address or hostname with a port or unix socket path + IPv6 is supported. Use address in format of "[fd12:3456:789a::1]:4502" ``` ## Value Definition - - -| Metric | Value | Description | -|--------|-------|-------------| -| strongswan_*_status | 0 | The tunnel is installed and is up and running. | -| strongswan_*_status | 1 | The connection is established. | -| strongswan_*_status | 2 | The tunnel or connection is down. | -| strongswan_*_status | 3 | The tunnel or connection status is not recognized. | +| Metric | Value | Description | +|---------------------|-------|----------------------------------------------------| +| strongswan_*_status | 0 | The tunnel is installed and is up and running. | +| strongswan_*_status | 1 | The connection is established. | +| strongswan_*_status | 2 | The tunnel or connection is down. | +| strongswan_*_status | 3 | The tunnel or connection status is not recognized. | ## Build & Run To build the binary run: @@ -52,13 +36,13 @@ To build the binary run: make build ``` -Run the binary with optional `config` parameter provided: +Run the binary with optional arguments provided: ```bash -./ipsec-prometheus-exporter [--config=] +./ipsec-prometheus-exporter [--server-port=8079] [--log-level=info] [--vici-network=tcp] [--vici-address=localhost:4502] ``` ## Docker image Public docker image is available for multiple platforms: https://hub.docker.com/r/torilabs/ipsec-prometheus-exporter ``` -docker run -it -p 8079:8079 -v $(pwd)/my-config.yaml:/config.yaml --rm torilabs/ipsec-prometheus-exporter:latest +docker run -it -p 8079:8079 --rm torilabs/ipsec-prometheus-exporter:latest --server-port=8079 ``` diff --git a/cmd/cmd.go b/cmd/cmd.go deleted file mode 100644 index f683fdb..0000000 --- a/cmd/cmd.go +++ /dev/null @@ -1,98 +0,0 @@ -package cmd - -import ( - "fmt" - "net/http" - "os" - "os/signal" - "syscall" - - "github.com/etherlabsio/healthcheck/v2" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "github.com/strongswan/govici/vici" - "github.com/torilabs/ipsec-prometheus-exporter/config" - "github.com/torilabs/ipsec-prometheus-exporter/log" - "github.com/torilabs/ipsec-prometheus-exporter/strongswan" - "go.uber.org/zap" -) - -var ( - cfgPath string - cfg config.Configuration -) - -func Execute() error { - return rootCmd.Execute() -} - -var rootCmd = &cobra.Command{ - Use: "ipsec-prometheus-exporter", - DisableAutoGenTag: true, - Short: "IPsec exporter for Prometheus.", - Long: "IPsec Prometheus Exporter exports Strongswan metrics.", - SilenceErrors: true, - SilenceUsage: true, - PreRunE: func(cmd *cobra.Command, _ []string) (err error) { - if err = viper.BindPFlags(cmd.Flags()); err != nil { - return - } - viper.SetConfigFile(cfgPath) - - if cfg, err = config.Parse(); err != nil { - return err - } - return nil - }, - RunE: func(*cobra.Command, []string) error { - if err := log.Setup(cfg); err != nil { - return err - } - defer log.Logger.Sync() - - sigs := make(chan os.Signal, 1) - signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - - checkers := make([]healthcheck.Option, 0) - - viciClientFn := func() (strongswan.ViciClient, error) { - s, err := vici.NewSession(vici.WithAddr(cfg.Vici.Network, fmt.Sprintf("%s:%d", cfg.Vici.Host, cfg.Vici.Port))) - if err != nil { - log.Logger.Warnf("Error connecting to Vici API: %s", err) - } - return s, err - } - cl := strongswan.NewCollector(viciClientFn) - checkers = append(checkers, healthcheck.WithChecker("vici", cl)) - if err := prometheus.Register(cl); err != nil { - return err - } - startServer(checkers) - - // wait for program to terminate - <-sigs - - // shutdown - log.Logger.Info("Shutting down the service.") - - return nil - }, -} - -func init() { - rootCmd.PersistentFlags().StringVar(&cfgPath, "config", "", "Path to the config file.") -} - -func startServer(checkers []healthcheck.Option) { - log.Logger.Infof("Starting admin server on port '%v'.", cfg.Server.Port) - - go func() { - http.Handle("/healthcheck", healthcheck.Handler(checkers...)) - http.Handle("/metrics", promhttp.Handler()) - if err := http.ListenAndServe(fmt.Sprintf(":%d", cfg.Server.Port), nil); err != nil && err != http.ErrServerClosed { - log.Logger.With(zap.Error(err)).Fatalf("Failed to start admin server.") - } - }() -} diff --git a/config/config.go b/config/config.go deleted file mode 100644 index bec6c4b..0000000 --- a/config/config.go +++ /dev/null @@ -1,50 +0,0 @@ -package config - -import ( - "github.com/pkg/errors" - "github.com/spf13/viper" -) - -type Logger struct { - Level string -} - -type Server struct { - Port int -} - -type Vici struct { - Network string - Host string - Port int -} - -type Configuration struct { - Logging Logger - Server Server - Vici Vici -} - -func Parse() (cfg Configuration, err error) { - if viper.ConfigFileUsed() != "" { - if err := viper.ReadInConfig(); err != nil { - return cfg, errors.Wrap(err, "failed to read configuration") - } - } - - setDefaults() - - if err := viper.Unmarshal(&cfg); err != nil { - return cfg, errors.Wrap(err, "failed to deserialize config") - } - - return cfg, nil -} - -func setDefaults() { - viper.SetDefault("logging.level", "info") - viper.SetDefault("server.port", 8079) - viper.SetDefault("vici.network", "tcp") - viper.SetDefault("vici.host", "localhost") - viper.SetDefault("vici.port", 4502) -} diff --git a/config/config_test.go b/config/config_test.go deleted file mode 100644 index 7be7bf5..0000000 --- a/config/config_test.go +++ /dev/null @@ -1,90 +0,0 @@ -package config - -import ( - "io/fs" - "os" - "reflect" - "testing" - - "github.com/spf13/viper" -) - -func TestParse(t *testing.T) { - tests := []struct { - name string - rawCfg string - wantCfg Configuration - wantErr bool - }{ - { - name: "default configuration", - wantCfg: Configuration{ - Logging: Logger{ - Level: "info", - }, - Server: Server{ - Port: 8079, - }, - Vici: Vici{ - Network: "tcp", - Host: "localhost", - Port: 4502, - }, - }, - }, - { - name: "full configuration", - rawCfg: `# Logger configuration -logging: - level: DEBUG -server: - port: 8077 -vici: - network: udp - host: 1.2.3.4 - port: 8080 -`, - wantCfg: Configuration{ - Logging: Logger{ - Level: "DEBUG", - }, - Server: Server{ - Port: 8077, - }, - Vici: Vici{ - Network: "udp", - Host: "1.2.3.4", - Port: 8080, - }, - }, - }, - { - name: "invalid configuration", - rawCfg: `sth wrong`, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - file, err := os.CreateTemp("/tmp", "ipsec-prometheus-exporter-*.yaml") - if err != nil { - t.Fatal(err) - } - defer os.Remove(file.Name()) - if err := os.WriteFile(file.Name(), []byte(tt.rawCfg), fs.ModePerm); err != nil { - t.Fatal(err) - } - viper.Reset() - viper.SetConfigFile(file.Name()) - - gotCfg, err := Parse() - if (err != nil) != tt.wantErr { - t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotCfg, tt.wantCfg) { - t.Errorf("Parse() gotCfg = %v, want %v", gotCfg, tt.wantCfg) - } - }) - } -} diff --git a/go.mod b/go.mod index c3b4bc5..7135daf 100644 --- a/go.mod +++ b/go.mod @@ -8,8 +8,6 @@ require ( github.com/etherlabsio/healthcheck/v2 v2.0.0 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.20.5 - github.com/spf13/cobra v1.8.1 - github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.10.0 github.com/strongswan/govici v0.7.0 go.uber.org/zap v1.27.0 @@ -19,31 +17,15 @@ require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/fsnotify/fsnotify v1.7.0 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect - github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/kylelemons/godebug v1.1.0 // indirect - github.com/magiconair/properties v1.8.7 // indirect - github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect - github.com/sagikazarmark/locafero v0.4.0 // indirect - github.com/sagikazarmark/slog-shim v0.1.0 // indirect - github.com/sourcegraph/conc v0.3.0 // indirect - github.com/spf13/afero v1.11.0 // indirect - github.com/spf13/cast v1.6.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect - github.com/subosito/gotenv v1.6.0 // indirect go.uber.org/multierr v1.10.0 // indirect - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/sys v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect google.golang.org/protobuf v1.34.2 // indirect - gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index fa3549d..be1ae7b 100644 --- a/go.sum +++ b/go.sum @@ -2,23 +2,12 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/etherlabsio/healthcheck/v2 v2.0.0 h1:oKq8cbpwM/yNGPXf2Sff6MIjVUjx/pGYFydWzeK2MpA= github.com/etherlabsio/healthcheck/v2 v2.0.0/go.mod h1:huNVOjKzu6FI1eaO1CGD3ZjhrmPWf5Obu/pzpI6/wog= -github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= -github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= -github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= -github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= -github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -27,17 +16,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= -github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= -github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= -github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= -github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y= @@ -50,56 +32,22 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= -github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= -github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= -github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= -github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= -github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= -github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= -github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= -github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= -github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= -github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= -github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= -github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= -github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/strongswan/govici v0.7.0 h1:m0BaL5hY+2khnQZYDaKJkd4Ji1jFrNzS0fg+GQS7QnE= github.com/strongswan/govici v0.7.0/go.mod h1:WvC3Lo9kEzjxUb5xNe2B4NczQpa7+cZMIy2x8eZLzSE= -github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= -github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= -gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/log/logger.go b/log/logger.go index 4077457..140a856 100644 --- a/log/logger.go +++ b/log/logger.go @@ -1,7 +1,6 @@ package log import ( - "github.com/torilabs/ipsec-prometheus-exporter/config" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) @@ -16,10 +15,12 @@ func init() { Logger = logger.Sugar() } -func Setup(cfg config.Configuration) error { +func Setup(rawLevel string) error { + if rawLevel == "" { + rawLevel = "info" + } var level zapcore.Level - - if err := level.Set(cfg.Logging.Level); err != nil { + if err := level.Set(rawLevel); err != nil { return err } diff --git a/main.go b/main.go index abc1702..a304f41 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,103 @@ package main import ( - "github.com/torilabs/ipsec-prometheus-exporter/cmd" + "context" + "errors" + "flag" + "fmt" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/etherlabsio/healthcheck/v2" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/strongswan/govici/vici" "github.com/torilabs/ipsec-prometheus-exporter/log" + "github.com/torilabs/ipsec-prometheus-exporter/strongswan" "go.uber.org/zap" ) +const ( + gracefulShutdownWait = time.Second * 60 + requestTimeout = time.Second * 30 + readHeaderTimeout = time.Second * 30 +) + +var ( + serverPort = flag.Uint("server-port", 8079, "Server port") + logLevel = flag.String("log-level", "info", "Log level") + viciNetwork = flag.String("vici-network", "tcp", "Vici network (tcp, udp or unix)") + viciAddr = flag.String("vici-address", "localhost:4502", "Vici host and port or unix socket path") +) + func main() { - if err := cmd.Execute(); err != nil { + if err := run(); err != nil { log.Logger.With(zap.Error(err)).Error("Terminating the service.") } } + +func run() (err error) { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + + flag.Parse() + + if err := log.Setup(*logLevel); err != nil { + return err + } + defer log.Logger.Sync() + + viciClientFn := func() (strongswan.ViciClient, error) { + s, err := vici.NewSession(vici.WithAddr(*viciNetwork, *viciAddr)) + if err != nil { + log.Logger.Warnf("Error connecting to Vici API: %s", err) + } + return s, err + } + cl := strongswan.NewCollector(viciClientFn) + + checkers := make([]healthcheck.Option, 0) + checkers = append(checkers, healthcheck.WithChecker("vici", cl)) + if err := prometheus.Register(cl); err != nil { + return err + } + stopFn := startServer(checkers) + defer stopFn() + + // wait for program to terminate + <-sigs + + return nil +} + +func startServer(checkers []healthcheck.Option) func() { + mux := http.DefaultServeMux + mux.Handle("/healthcheck", http.TimeoutHandler(healthcheck.Handler(checkers...), requestTimeout, "request timeout")) + mux.Handle("/metrics", http.TimeoutHandler(promhttp.Handler(), requestTimeout, "request timeout")) + + s := &http.Server{ + Addr: fmt.Sprintf(":%d", *serverPort), + Handler: mux, + ReadHeaderTimeout: readHeaderTimeout, + } + + go func() { + log.Logger.Infof("Starting admin server on port '%v'.", *serverPort) + if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Logger.With(zap.Error(err)).Fatalf("Failed to start admin server.") + } + }() + + return func() { + ctx, cancel := context.WithTimeout(context.Background(), gracefulShutdownWait) + defer cancel() + err := s.Shutdown(ctx) + if err != nil { + log.Logger.With(zap.Error(err)).Warn("Error occurred during shutting down servers.") + } + log.Logger.Info("Admin server successfully shutdown.") + } +}