Nantes Université

Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 527f1e11 rédigé par Jean-Francois GUILLOU's avatar Jean-Francois GUILLOU
Parcourir les fichiers

Added homes scraping

parent fcc51db4
Aucune branche associée trouvée
Étiquettes 0.2.0
Aucune requête de fusion associée trouvée
Pipeline #66562 réussi
......@@ -55,6 +55,7 @@ func (c *CephMount) ensureMount() error {
return nil
}
// GetFolderSize reads path and extracts it's size
func (c *CephMount) GetFolderSize(path string) (float64, error) {
err := c.ensureMount()
if err != nil {
......@@ -80,3 +81,45 @@ func (c *CephMount) GetFolderSize(path string) (float64, error) {
return float64(entry.Statx().Size), nil
}
// GetFoldersSize reads path and extracts every subfolders size
func (c *CephMount) GetFoldersSize(path string) (map[string]float64, error) {
err := c.ensureMount()
if err != nil {
return nil, err
}
if !strings.HasPrefix(path, "/") {
return nil, errors.New("bogus path")
}
dir, err := c.MountInfo.OpenDir(path)
if err != nil {
return nil, err
}
defer dir.Close()
folders := make(map[string]float64)
statx := cephfs.StatxSize
flags := cephfs.AtStatxDontSync
for {
entry, err := dir.ReadDirPlus(statx, flags)
if err != nil {
return nil, err
}
// nil entry means we reached the end of the folder
if entry == nil {
break
}
// Skip files, parent folder and hidden folders
if entry.DType() != cephfs.DTypeDir || strings.HasPrefix(entry.Name(), ".") {
continue
}
folders[entry.Name()] = float64(entry.Statx().Size)
}
return folders, nil
}
package main
import (
"encoding/json"
"errors"
"net/http"
"strings"
)
type Folder struct {
Name string
Path string
Size float64
}
type Config struct {
Shares []Folder
}
func getFolders() ([]Folder, error) {
config := &Config{}
err := getJson(*foldersApi, config)
if err != nil {
return nil, err
}
if len(config.Shares) == 0 {
return nil, errors.New("no folders found")
}
var folders []Folder
for _, folder := range config.Shares {
if !strings.Contains(folder.Path, *foldersPrefix) {
continue
}
folder.Path = strings.TrimPrefix(folder.Path, *foldersPrefix)
// log.Debug().Str("path", folder.Path).Send()
folders = append(folders, folder)
}
return folders, 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)
}
......@@ -18,30 +18,37 @@ func NewFoldersizeCollector() *FoldersizeCollector {
),
size: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "size"),
"Folder size", []string{"name"}, nil,
"Folder size", []string{"name", "domain", "type"}, nil,
),
}
}
func (c *FoldersizeCollector) Collect(ch chan<- prometheus.Metric) {
folders, err := getFolders()
folders, globs, err := getFolders()
if err != nil {
log.Fatal().Err(err).Msg("Failed to Collect folders list")
return
}
for i, folder := range folders {
ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, 1)
for _, folder := range folders {
size, err := ceph.GetFolderSize(folder.Path)
if err != nil {
log.Fatal().Err(err).Str("path", folder.Path).Msg("Failed to get folder size")
return
log.Warn().Err(err).Str("path", folder.Path).Msg("Failed to get folder size")
}
folders[i].Size = size
ch <- prometheus.MustNewConstMetric(c.size, prometheus.GaugeValue, size, folder.Name, folder.Domain, folder.Type)
}
ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, 1)
for _, folder := range folders {
ch <- prometheus.MustNewConstMetric(c.size, prometheus.GaugeValue, folder.Size, folder.Name)
for _, glob := range globs {
folders, err := ceph.GetFoldersSize(glob.Path)
if err != nil {
log.Fatal().Err(err).Str("path", glob.Path).Msg("Failed to get glob size")
}
for name, size := range folders {
ch <- prometheus.MustNewConstMetric(c.size, prometheus.GaugeValue, size, name, glob.Domain, glob.Type)
}
}
}
......
package main
import (
"encoding/json"
"errors"
"net/http"
"regexp"
"strings"
)
type Folder struct {
Name string
Path string
Domain string
Type 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"`
}
type Home struct {
Path string `json:"path"`
}
var domainRegex = regexp.MustCompile("(?i)dom:([a-z]+)")
// 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])
}
folders = append(folders, Folder{
Name: folder.Name,
Path: strings.TrimPrefix(folder.Path, *foldersPrefix),
Domain: domain,
Type: "share",
})
}
var globs []Folder
globs = append(globs, Folder{
Name: "home",
Path: strings.TrimSuffix(strings.TrimPrefix(config.Homes.Path, *foldersPrefix), "%u"),
Type: "home",
})
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