linter defeated!
This commit is contained in:
parent
5ca21e635d
commit
2294202009
|
|
@ -7,11 +7,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Connect(params mytypes.Params) (_ mytypes.CephConnection, err error) {
|
func Connect(params mytypes.Params) (_ mytypes.CephConnection, err error) {
|
||||||
var cephConn mytypes.CephConnection = mytypes.CephConnection{}
|
var cephConn = mytypes.CephConnection{}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("Error in func connect() %w", err)
|
err = fmt.Errorf("error in func connect() %w", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,17 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"rbd_exporter/mytypes"
|
|
||||||
"rbd_exporter/metrics"
|
|
||||||
"rbd_exporter/logger"
|
|
||||||
"rbd_exporter/connection"
|
"rbd_exporter/connection"
|
||||||
|
"rbd_exporter/logger"
|
||||||
|
"rbd_exporter/metrics"
|
||||||
|
"rbd_exporter/mytypes"
|
||||||
|
"time"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var params mytypes.Params
|
|
||||||
var mainlogger *zap.SugaredLogger
|
|
||||||
|
|
||||||
// Here I initialize logger
|
// Here I initialize logger
|
||||||
func loggerInit() *zap.SugaredLogger {
|
func loggerInit() *zap.SugaredLogger {
|
||||||
|
|
@ -28,17 +26,11 @@ func loggerInit() *zap.SugaredLogger {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("Logger set up failed: %v", err))
|
panic(fmt.Sprintf("Logger set up failed: %v", err))
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
err := logger.Sync()
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("couldn't sinc logger")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return logger.Sugar()
|
return logger.Sugar()
|
||||||
}
|
}
|
||||||
|
|
||||||
// This func runs even before main()
|
func globalInit() (mainlogger *zap.SugaredLogger,params mytypes.Params){
|
||||||
func init() {
|
|
||||||
configFile := flag.String("config", "/etc/ceph/ceph.conf", "placement of ceph config file")
|
configFile := flag.String("config", "/etc/ceph/ceph.conf", "placement of ceph config file")
|
||||||
_keyring := flag.String("keyring", "/etc/ceph/ceph.client.admin.keyring", "placement of ceph keyring file")
|
_keyring := flag.String("keyring", "/etc/ceph/ceph.client.admin.keyring", "placement of ceph keyring file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
@ -50,33 +42,54 @@ func init() {
|
||||||
logger.SetLogger(mainlogger)
|
logger.SetLogger(mainlogger)
|
||||||
mainlogger.Info("Setting up logger is complete successfully")
|
mainlogger.Info("Setting up logger is complete successfully")
|
||||||
mainlogger.Info("Registering prom metrics")
|
mainlogger.Info("Registering prom metrics")
|
||||||
|
|
||||||
|
return mainlogger,params
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
mainlogger,params := globalInit()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
err := logger.Logger.Sync()
|
||||||
|
if err != nil {
|
||||||
|
logger.Logger.Error("couldn't sync logger")
|
||||||
|
}
|
||||||
|
}()
|
||||||
cephConn, err := connection.Connect(params)
|
cephConn, err := connection.Connect(params)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
logger.Logger.Error(err)
|
||||||
if wrapped := errors.Unwrap(err); wrapped != nil {
|
if wrapped := errors.Unwrap(err); wrapped != nil {
|
||||||
fmt.Println(wrapped)
|
logger.Logger.Error(wrapped)
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer cephConn.Conn.Shutdown()
|
defer cephConn.Conn.Shutdown()
|
||||||
mainlogger.Info("Successfully connected to a cluster")
|
mainlogger.Info("Successfully connected to a cluster")
|
||||||
|
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
||||||
|
|
||||||
|
server := http.Server{
|
||||||
|
Addr: ":9040",
|
||||||
|
ReadTimeout: 10 * time.Second,
|
||||||
|
Handler: http.DefaultServeMux,
|
||||||
|
}
|
||||||
|
|
||||||
// HTTP runs in separate thread cuz it blocks further execution of main
|
// HTTP runs in separate thread cuz it blocks further execution of main
|
||||||
go func() {
|
go func() {
|
||||||
mainlogger.Info("Starting http server")
|
mainlogger.Info("Starting http server")
|
||||||
// Here I check for errors if HTTP fails
|
// Here I check for errors if HTTP fails
|
||||||
if err := http.ListenAndServe(":9040", nil); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
mainlogger.Fatalf("HTTP server failed to start %v", err)
|
mainlogger.Fatalf("HTTP server failed to start %v", err)
|
||||||
}
|
}
|
||||||
mainlogger.Info("HTTP server started")
|
mainlogger.Info("HTTP server started")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// if err := server.Shutdown(); err != nil {
|
||||||
|
// logger.Logger.Errorf("https server shutdown failed",err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
go metrics.GetMetrics(cephConn)
|
go metrics.GetMetrics(cephConn)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,14 @@ import (
|
||||||
func GetMetrics(cephConn mytypes.CephConnector) {
|
func GetMetrics(cephConn mytypes.CephConnector) {
|
||||||
metrics := InitMetrics()
|
metrics := InitMetrics()
|
||||||
prometheus.MustRegister(
|
prometheus.MustRegister(
|
||||||
metrics.Total_rbd_requested_size_per_pool,
|
metrics.TotalRbdRequestedSizePerPool,
|
||||||
)
|
)
|
||||||
|
|
||||||
ticker := time.NewTicker(2 * time.Second)
|
ticker := time.NewTicker(2 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
var result []mytypes.Pool = []mytypes.Pool{}
|
var result = []mytypes.Pool{}
|
||||||
poolList, err := cephConn.ListPools()
|
poolList, err := cephConn.ListPools()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Logger.Error("Cannot get list of pools")
|
logger.Logger.Error("Cannot get list of pools")
|
||||||
|
|
@ -32,7 +32,7 @@ func GetMetrics(cephConn mytypes.CephConnector) {
|
||||||
result = append(result, x)
|
result = append(result, x)
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics.Total_rbd_requested_size_per_pool.Reset()
|
metrics.TotalRbdRequestedSizePerPool.Reset()
|
||||||
for _,v := range result {
|
for _,v := range result {
|
||||||
if !v.HasRBD {continue}
|
if !v.HasRBD {continue}
|
||||||
FillMetrics(v,metrics)
|
FillMetrics(v,metrics)
|
||||||
|
|
@ -45,7 +45,7 @@ func GetMetrics(cephConn mytypes.CephConnector) {
|
||||||
|
|
||||||
func InitMetrics() *mytypes.Metrics {
|
func InitMetrics() *mytypes.Metrics {
|
||||||
m := &mytypes.Metrics{
|
m := &mytypes.Metrics{
|
||||||
Total_rbd_requested_size_per_pool: prometheus.NewGaugeVec(
|
TotalRbdRequestedSizePerPool : prometheus.NewGaugeVec(
|
||||||
prometheus.GaugeOpts{
|
prometheus.GaugeOpts{
|
||||||
Name: "total_rbd_requested_size_per_pool",
|
Name: "total_rbd_requested_size_per_pool",
|
||||||
Help: "total size of all requested RBDs in a specific pool",
|
Help: "total size of all requested RBDs in a specific pool",
|
||||||
|
|
@ -69,11 +69,11 @@ func FillMetrics(pool mytypes.Pool,metrics *mytypes.Metrics) {
|
||||||
|
|
||||||
logger.Logger.Debugf("Processing pool %s",pool.Name)
|
logger.Logger.Debugf("Processing pool %s",pool.Name)
|
||||||
for _,v := range pool.RBDlist {
|
for _,v := range pool.RBDlist {
|
||||||
totalSizePerPool += uint64(v.GetSize())
|
totalSizePerPool += v.GetSize()
|
||||||
}
|
}
|
||||||
logger.Logger.Debugf("Total size of RBDs in pool %s is %d",pool.Name,totalSizePerPool)
|
logger.Logger.Debugf("Total size of RBDs in pool %s is %d",pool.Name,totalSizePerPool)
|
||||||
|
|
||||||
metrics.Total_rbd_requested_size_per_pool.WithLabelValues(pool.Name,
|
metrics.TotalRbdRequestedSizePerPool.WithLabelValues(pool.Name,
|
||||||
).Set(
|
).Set(
|
||||||
float64(totalSizePerPool))
|
float64(totalSizePerPool))
|
||||||
}
|
}
|
||||||
|
|
@ -83,7 +83,7 @@ func (riw RBDImageWrapper) Stat() (*rbd.ImageInfo,error) {
|
||||||
|
|
||||||
type IRBD interface {
|
type IRBD interface {
|
||||||
GetName() string
|
GetName() string
|
||||||
GetSize() int64
|
GetSize() uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
type RBDUsage struct {
|
type RBDUsage struct {
|
||||||
|
|
@ -99,8 +99,8 @@ func (r RBD) GetName() string {
|
||||||
return r.Name
|
return r.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r RBD) GetSize() int64 {
|
func (r RBD) GetSize() uint64 {
|
||||||
return int64(r.Size)
|
return r.Size
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
|
|
@ -110,6 +110,6 @@ type Pool struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Metrics struct {
|
type Metrics struct {
|
||||||
Total_rbd_requested_size_per_pool *prometheus.GaugeVec
|
TotalRbdRequestedSizePerPool *prometheus.GaugeVec
|
||||||
Total_rbd_requested_size prometheus.Gauge
|
// Total_rbd_requested_size prometheus.Gauge
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func PoolFactory(cephConn mytypes.CephConnector, poolName string) (mytypes.Pool, error) {
|
func PoolFactory(cephConn mytypes.CephConnector, poolName string) (mytypes.Pool, error) {
|
||||||
var rbdlist []mytypes.IRBD = []mytypes.IRBD{}
|
var rbdlist = []mytypes.IRBD{}
|
||||||
|
|
||||||
ioctx, err := cephConn.OpenIOContext(poolName)
|
ioctx, err := cephConn.OpenIOContext(poolName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -16,36 +16,29 @@ func PoolFactory(cephConn mytypes.CephConnector, poolName string) (mytypes.Pool,
|
||||||
|
|
||||||
defer ioctx.Destroy()
|
defer ioctx.Destroy()
|
||||||
|
|
||||||
imageList, err := ioctx.GetImageNames()
|
imageNames, err := ioctx.GetImageNames()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mytypes.Pool{}, fmt.Errorf("couldn't get list of rbds %w", err)
|
return mytypes.Pool{}, fmt.Errorf("couldn't get list of rbds %w", err)
|
||||||
}
|
}
|
||||||
for _, rbdname := range imageList {
|
for _, rbdName := range imageNames {
|
||||||
stat, err := RBDFacroty(ioctx, rbdname)
|
stat, err := RBDFacroty(ioctx, rbdName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("coundn't get stat from disk %s %w", rbdname, err)
|
logger.Logger.Errorf("coundn't get stat from disk %s %w", rbdName, err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
rbdlist = append(rbdlist, stat)
|
rbdlist = append(rbdlist, stat)
|
||||||
}
|
}
|
||||||
return mytypes.Pool{
|
return mytypes.Pool{
|
||||||
Name: poolName,
|
Name: poolName,
|
||||||
HasRBD: len(imageList) != 0,
|
HasRBD: len(imageNames) != 0,
|
||||||
RBDlist: rbdlist,
|
RBDlist: rbdlist,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func RBDFacroty(ioctx mytypes.IOContexter, rbdname string) (mytypes.RBD, error) {
|
func RBDFacroty(ioctx mytypes.IOContexter, rbdName string) (mytypes.RBD, error) {
|
||||||
defer func() {
|
image := ioctx.GetImage(rbdName)
|
||||||
if v := recover(); v != nil {
|
|
||||||
logger.Logger.Errorf("no such RBD exists (probably just deleted)")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
image := ioctx.GetImage(rbdname)
|
|
||||||
err := image.Open()
|
err := image.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
logger.Logger.Errorf("Couldn't open RBD %s",rbdName)
|
||||||
// logger.Logger.Errorf("Shit happened in RBDFactory")
|
// logger.Logger.Errorf("Shit happened in RBDFactory")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,11 +47,11 @@ func RBDFacroty(ioctx mytypes.IOContexter, rbdname string) (mytypes.RBD, error)
|
||||||
|
|
||||||
info,err := image.Stat()
|
info,err := image.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mytypes.RBD{},fmt.Errorf("couldn't get stats for image %s %w",rbdname,err)
|
return mytypes.RBD{},fmt.Errorf("couldn't get stats for image %s %w",rbdName,err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mytypes.RBD{
|
return mytypes.RBD{
|
||||||
Name: rbdname,
|
Name: rbdName,
|
||||||
ImageInfo: *info,
|
ImageInfo: *info,
|
||||||
},nil
|
},nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ func TestRBDFactory(t *testing.T) {
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, rbdName, result.Name)
|
assert.Equal(t, rbdName, result.Name)
|
||||||
assert.Equal(t, expectedImageInfo.Size, result.ImageInfo.Size)
|
assert.Equal(t, expectedImageInfo.Size, result.Size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user