-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ricky Moorhouse <[email protected]>
- Loading branch information
1 parent
d4240ba
commit 6ae6338
Showing
19 changed files
with
2,532 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ coverage.xml | |
__pycache__/ | ||
local/ | ||
*.pyc | ||
secrets/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"net/http" | ||
"nets" | ||
"nets/analytics" | ||
"nets/apiconnect" | ||
"nets/consumption" | ||
"nets/datapower" | ||
"nets/manager" | ||
"os" | ||
"time" | ||
|
||
"github.com/IBM/alchemy-logging/src/go/alog" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
var ( | ||
promCounter = promauto.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "myapp_processed_ops_total", | ||
Help: "The total number of processed events", | ||
}, | ||
[]string{"collector"}) | ||
) | ||
|
||
var log = alog.UseChannel("trawler") | ||
|
||
type Config struct { | ||
Prometheus struct { | ||
Port string `yaml:"port"` | ||
Enabled bool `yaml:"enabled"` | ||
} `yaml:"prometheus"` | ||
Nets struct { | ||
APIConnect apiconnect.APIConnectNetConfig `yaml:"apiconnect"` | ||
Analytics analytics.AnalyticsNetConfig `yaml:"analytics"` | ||
Consumption consumption.ConsumptionNetConfig `yaml:"consumption"` | ||
DataPower datapower.DataPowerNetConfig `yaml:"datapower"` | ||
Manager manager.ManagerNetConfig `yaml:"manager"` | ||
} `yaml:"nets"` | ||
} | ||
|
||
type CertReloader struct { | ||
CertFile string // path to the x509 certificate for https | ||
KeyFile string // path to the x509 private key matching `CertFile` | ||
cachedCert *tls.Certificate | ||
cachedCertModTime time.Time | ||
} | ||
|
||
func ReadConfig() Config { | ||
var config Config | ||
|
||
config_path := os.Getenv("CONFIG_PATH") | ||
if config_path == "" { | ||
config_path = "config.yaml" | ||
} | ||
|
||
log.Log(alog.INFO, "Loading config from %s ", config_path) | ||
|
||
// Open YAML file | ||
file, err := os.Open(config_path) | ||
if err != nil { | ||
log.Log(alog.ERROR, err.Error()) | ||
} | ||
defer file.Close() | ||
|
||
// Decode YAML file to struct | ||
if file != nil { | ||
decoder := yaml.NewDecoder(file) | ||
if err := decoder.Decode(&config); err != nil { | ||
log.Log(alog.ERROR, err.Error()) | ||
|
||
} | ||
} | ||
|
||
return config | ||
} | ||
|
||
func enableNet(frequency int, newNet nets.BaseNet) { | ||
net_frequency := time.Duration(5) | ||
if frequency != 0 { | ||
log.Log(alog.INFO, "net is enabled at %ds frequency", frequency) | ||
net_frequency = time.Duration(frequency) | ||
} | ||
newNet.Frequency = net_frequency * time.Second | ||
//newNet.BackgroundFishing() | ||
} | ||
|
||
func frequency(configFrequency int) time.Duration { | ||
if configFrequency == 0 { | ||
// Default is 10 seconds | ||
return time.Duration(10) * time.Second | ||
} else { | ||
return time.Duration(configFrequency) * time.Second | ||
} | ||
} | ||
|
||
func main() { | ||
// Set up logging | ||
|
||
alog.Config(alog.INFO, alog.ChannelMap{ | ||
"trawler": alog.DEBUG, | ||
"apim": alog.INFO, | ||
"apic": alog.INFO, | ||
"a7s": alog.INFO, | ||
"dp": alog.INFO, | ||
}) | ||
// Read config file... | ||
config := ReadConfig() | ||
|
||
// Initialise appropriate nets... | ||
if config.Nets.APIConnect.Enabled { | ||
a := apiconnect.APIConnect{} | ||
a.Config = config.Nets.APIConnect | ||
a.Frequency = frequency(config.Nets.APIConnect.Frequency) | ||
log.Log(alog.INFO, "Enabled apiconnect net with %s frequency", a.Frequency) | ||
go a.BackgroundFishing() | ||
} | ||
|
||
// Analytics net | ||
if config.Nets.Analytics.Enabled { | ||
a7s := analytics.Analytics{} | ||
a7s.Config = config.Nets.Analytics | ||
a7s.Frequency = frequency(config.Nets.Analytics.Frequency) | ||
log.Log(alog.INFO, "Enabled analytics net with %s frequency", a7s.Frequency) | ||
go a7s.BackgroundFishing() | ||
} | ||
|
||
// Consumption health net | ||
if config.Nets.Consumption.Enabled { | ||
c := consumption.Consumption{} | ||
c.Config = config.Nets.Consumption | ||
c.Frequency = frequency(config.Nets.Consumption.Frequency) | ||
log.Log(alog.INFO, "Enabled consumption net with %s frequency", c.Frequency) | ||
c.Fish() | ||
go c.BackgroundFishing() | ||
} | ||
// Manager net | ||
if config.Nets.Manager.Enabled { | ||
apim := manager.Manager{} | ||
apim.Config = config.Nets.Manager | ||
apim.Frequency = frequency(config.Nets.Manager.Frequency) | ||
log.Log(alog.INFO, "Enabled management net with %s frequency", apim.Frequency) | ||
go apim.BackgroundFishing() | ||
} | ||
|
||
// DataPower net (TODO implement) | ||
if config.Nets.DataPower.Enabled { | ||
dp := datapower.DataPower{} | ||
dp.Config = config.Nets.DataPower | ||
dp.Frequency = frequency(config.Nets.DataPower.Frequency) | ||
log.Log(alog.INFO, "Enabled datapower net with %s frequency", dp.Frequency) | ||
go dp.BackgroundFishing() | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle("/metrics", promhttp.Handler()) | ||
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "ok") | ||
}) | ||
|
||
listenPort := "63512" | ||
if config.Prometheus.Port != "" { | ||
listenPort = config.Prometheus.Port | ||
} | ||
|
||
_, err := ListenAndServe(mux, listenPort) | ||
if err != nil { | ||
log.Log(alog.ERROR, "Server Failed to Listen") | ||
} | ||
|
||
} | ||
|
||
func ListenAndServe(mux *http.ServeMux, listenPort string) (*http.Server, error) { | ||
srv := &http.Server{} | ||
if os.Getenv("SECURE") != "true" { | ||
srv := &http.Server{ | ||
Addr: ":" + listenPort, | ||
Handler: mux, | ||
} | ||
log.Log(alog.INFO, "Listening insecurely on http://0.0.0.0:%s/metrics", listenPort) | ||
err := srv.ListenAndServe() | ||
if err != nil { | ||
log.Log(alog.FATAL, "failed to run insecure server: %s\n", err) | ||
return nil, err | ||
} | ||
|
||
} else { | ||
|
||
srv := &http.Server{ | ||
Addr: ":" + listenPort, | ||
Handler: mux, | ||
} | ||
certPath := os.Getenv("CERT_PATH") | ||
certReloader := CertReloader{ | ||
CertFile: certPath + "/tls.crt", | ||
KeyFile: certPath + "/tls.key", | ||
} | ||
|
||
caFile := certPath + "/ca.crt" | ||
|
||
var certPool *x509.CertPool = x509.NewCertPool() | ||
caBytes, err := os.ReadFile(caFile) | ||
if err != nil { | ||
log.Log(alog.FATAL, "failed loading caFile: %v", err) | ||
return nil, err | ||
} | ||
ok := certPool.AppendCertsFromPEM(caBytes) | ||
if !ok { | ||
log.Log(alog.FATAL, "could not parse certificate file: %v", caFile) | ||
return nil, err | ||
} | ||
|
||
tlsConfig := tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
GetCertificate: certReloader.GetCertificate, | ||
ClientAuth: tls.RequireAndVerifyClientCert, // pragma: allowlist secret | ||
ClientCAs: certPool, | ||
RootCAs: certPool, | ||
} | ||
srv.TLSConfig = &tlsConfig | ||
log.Log(alog.INFO, "Listening securely on https://0.0.0.0:%s/metrics", listenPort) | ||
err = srv.ListenAndServeTLS(certReloader.CertFile, certReloader.KeyFile) | ||
if err != nil { | ||
log.Log(alog.FATAL, "failed to run secure server: %s\n", err) | ||
return nil, err | ||
} | ||
} | ||
return srv, nil | ||
} | ||
|
||
// Implementation for tls.Config.GetCertificate useful when using | ||
// Kubernetes Secrets which update the filesystem at runtime. | ||
func (cr *CertReloader) GetCertificate(h *tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
stat, err := os.Stat(cr.KeyFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed checking key file modification time: %w", err) | ||
} | ||
|
||
if cr.cachedCert == nil || stat.ModTime().After(cr.cachedCertModTime) { | ||
log.Log(alog.INFO, "Re-loading certs from file as updated since cached time: %v", cr.cachedCertModTime) | ||
pair, err := tls.LoadX509KeyPair(cr.CertFile, cr.KeyFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed loading tls key pair: %w", err) | ||
} | ||
|
||
cr.cachedCert = &pair | ||
cr.cachedCertModTime = stat.ModTime() | ||
} | ||
|
||
return cr.cachedCert, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module rickymoorhouse/exporter | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/IBM/alchemy-logging/src/go v1.0.3 | ||
github.com/prometheus/client_golang v1.15.1 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect | ||
github.com/prometheus/client_model v0.4.0 // indirect | ||
github.com/prometheus/common v0.43.0 // indirect | ||
github.com/prometheus/procfs v0.9.0 // indirect | ||
github.com/stretchr/testify v1.8.1 // indirect | ||
golang.org/x/sys v0.8.0 // indirect | ||
google.golang.org/protobuf v1.30.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
github.com/IBM/alchemy-logging/src/go v1.0.3 h1:2QQ/I5cIoDfGooTEJzdzKZmWntHPIwNFIoo9ipFx0v4= | ||
github.com/IBM/alchemy-logging/src/go v1.0.3/go.mod h1:da1DJ0y3nkz/zKSyXGHkU8DC9lafh2S18W+KOvbqiFA= | ||
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.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= | ||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= | ||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= | ||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= | ||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= | ||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= | ||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= | ||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= | ||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= | ||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/prometheus/client_golang v1.15.1 h1:8tXpTmJbyH5lydzFPoxSIJ0J46jdh3tylbvM1xCv0LI= | ||
github.com/prometheus/client_golang v1.15.1/go.mod h1:e9yaBhRPU2pPNsZwE+JdQl0KEt1N9XgF6zxWmaC0xOk= | ||
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY= | ||
github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU= | ||
github.com/prometheus/common v0.43.0 h1:iq+BVjvYLei5f27wiuNiB1DN6DYQkp1c8Bx0Vykh5us= | ||
github.com/prometheus/common v0.43.0/go.mod h1:NCvr5cQIh3Y/gy73/RdVtC9r8xxrxwJnB+2lB3BxrFc= | ||
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= | ||
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= | ||
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/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/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
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.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= | ||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= | ||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= | ||
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= | ||
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= | ||
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/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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
go 1.21 | ||
|
||
use ( | ||
. | ||
./nets | ||
) |
Oops, something went wrong.