Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider 3668caea rédigé par Jean-Francois GUILLOU's avatar Jean-Francois GUILLOU
Parcourir les fichiers

Remove samba api usage

parent 93db1bd7
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Pipeline #82947 réussi
......@@ -10,7 +10,13 @@ type FoldersizeCollector struct {
size *prometheus.Desc
}
func NewFoldersizeCollector() *FoldersizeCollector {
var foldersToScan []string
var rootsToScan []string
func NewFoldersizeCollector(folders []string, roots []string) *FoldersizeCollector {
foldersToScan = folders
rootsToScan = roots
return &FoldersizeCollector{
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
......@@ -18,36 +24,35 @@ func NewFoldersizeCollector() *FoldersizeCollector {
),
size: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "size"),
"Folder size", []string{"name", "type", "domain", "entity", "sub"}, nil,
"Folder size", []string{"name", "domain", "entity"}, nil,
),
}
}
func (c *FoldersizeCollector) Collect(ch chan<- prometheus.Metric) {
folders, globs, err := getFolders()
if err != nil {
log.Fatal().Err(err).Msg("Failed to Collect folders list")
return
}
ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, 1)
for _, folder := range folders {
size, err := ceph.GetFolderSize(folder.Path)
for _, folder := range foldersToScan {
size, err := ceph.GetFolderSize(folder)
if err != nil {
log.Warn().Err(err).Str("path", folder.Path).Msg("Failed to get folder size")
log.Warn().Err(err).Str("path", folder).Msg("Failed to get folder size")
}
ch <- prometheus.MustNewConstMetric(c.size, prometheus.GaugeValue, size, folder.Name, folder.Type, folder.Domain, folder.Entity, folder.Subshare)
fdomain := ""
fentity := ""
ch <- prometheus.MustNewConstMetric(c.size, prometheus.GaugeValue, size, folder, fdomain, fentity)
}
for _, glob := range globs {
folders, err := ceph.GetFoldersSize(glob.Path)
for _, folderRoot := range rootsToScan {
sizes, err := ceph.GetFoldersSize(folderRoot)
if err != nil {
log.Fatal().Err(err).Str("path", glob.Path).Msg("Failed to get glob size")
log.Warn().Err(err).Str("path", folderRoot).Msg("Failed to get folders size")
}
for name, size := range folders {
ch <- prometheus.MustNewConstMetric(c.size, prometheus.GaugeValue, size, name, glob.Type, glob.Domain, glob.Entity, glob.Subshare)
for name, size := range sizes {
fdomain := ""
fentity := ""
ch <- prometheus.MustNewConstMetric(c.size, prometheus.GaugeValue, size, name, fdomain, fentity)
}
}
}
......
......@@ -4,6 +4,7 @@ import (
"flag"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
......@@ -17,13 +18,13 @@ const (
)
var (
addr = flag.String("web.listen-address", ":9729", "The address to listen on for HTTP requests.")
endpoint = flag.String("web.endpoint", "/metrics", "Path under which to expose metrics.")
foldersApi = flag.String("folders.api", "http://127.0.0.1:8080", "Endpoint to fetch for folders JSON listing")
foldersPrefix = flag.String("folders.prefix", "", "Folders prefix to remove from path")
mon = flag.String("ceph.mon", "127.0.0.1", "Ceph monitor(s) names or IP")
key = flag.String("ceph.key", "", "Ceph key used to connect")
root = flag.String("ceph.root", "", "Cephfs root mount point")
addr = flag.String("web.listen-address", ":9729", "The address to listen on for HTTP requests.")
endpoint = flag.String("web.endpoint", "/metrics", "Path under which to expose metrics.")
scanFolders = flag.String("scan.folders", ".", "List of comma separated folders to scan")
scanRoots = flag.String("scan.roots", "", "List of comma separated folder roots to scan")
mon = flag.String("ceph.mon", "127.0.0.1", "Ceph monitor(s) names or IP")
key = flag.String("ceph.key", "", "Ceph key used to connect")
root = flag.String("ceph.root", "", "Cephfs root mount point")
)
var ceph *CephMount
......@@ -52,7 +53,10 @@ func main() {
log.Error().Err(err).Msg("Failed to Release")
}()
err = prometheus.Register(NewFoldersizeCollector())
folders := strings.Split(*scanFolders, ",")
roots := strings.Split(*scanRoots, ",")
err = prometheus.Register(NewFoldersizeCollector(folders, roots))
if err != nil {
log.Fatal().Err(err).Msg("Failed to register collector")
}
......
package main
import (
"encoding/json"
"errors"
"net/http"
"regexp"
"strings"
)
type Folder struct {
Name string
Path string
Domain string
Entity string
Type string
Subshare string
}
type SambaConfig struct {
Shares []Share `json:"shares"`
Homes Home `json:"homes"`
}
type Share struct {
Name string `json:"name"`
Path string `json:"path"`
Comment string `json:"comment"`
LimitedWrites bool `json:"limitedWrites"`
AccessGroups []string `json:"accessGroups"`
WriteGroups []string `json:"writeGroups"`
ForceGroup string `json:"forceGroup"`
Subshare bool `json:"subshare"`
}
type Home struct {
Path string `json:"path"`
}
var domainRegex = regexp.MustCompile(`(?i)dom:([a-z]+)`)
var entityRegex = regexp.MustCompile(`(?i)ent:([\w\-]+)`)
// Loads of black magic here
func getFolders() ([]Folder, []Folder, error) {
config := &SambaConfig{}
err := getJson(*foldersApi, config)
if err != nil {
return nil, nil, err
}
if len(config.Shares) == 0 {
return nil, nil, errors.New("no folders found")
}
var folders []Folder
for _, folder := range config.Shares {
if !strings.Contains(folder.Path, *foldersPrefix) {
continue
}
domain := "UNK"
if domainMatches := domainRegex.FindStringSubmatch(folder.Comment); domainMatches != nil {
domain = strings.ToUpper(domainMatches[1])
}
entity := ""
if entityMatches := entityRegex.FindStringSubmatch(folder.Comment); entityMatches != nil {
entity = strings.ToUpper(entityMatches[1])
}
subshare := "0"
if folder.Subshare {
subshare = "1"
}
folders = append(folders, Folder{
Name: folder.Name,
Path: strings.TrimPrefix(folder.Path, *foldersPrefix),
Domain: domain,
Entity: entity,
Type: "share",
Subshare: subshare,
})
}
var globs []Folder
globs = append(globs, Folder{
Name: "home",
Path: strings.TrimSuffix(strings.TrimPrefix(config.Homes.Path, *foldersPrefix), "%u"),
Type: "home",
Subshare: "0",
})
return folders, globs, nil
}
func getJson(url string, target interface{}) error {
r, err := http.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
return json.NewDecoder(r.Body).Decode(target)
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter