feat(collectors) basic implementation of boring collectors; sender via http(s) to hard coded api route
This commit is contained in:
58
internal/collectors/disk/disk.go
Normal file
58
internal/collectors/disk/disk.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package disk
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
|
||||
"bugs.watch/agent/internal/aggregate"
|
||||
)
|
||||
|
||||
type Stats struct {
|
||||
TotalBytes uint64
|
||||
UsedBytes uint64
|
||||
}
|
||||
|
||||
// Read returns disk usage statistics for the root filesystem.
|
||||
func Read() (Stats, error) {
|
||||
var fs syscall.Statfs_t
|
||||
|
||||
if err := syscall.Statfs("/", &fs); err != nil {
|
||||
return Stats{}, err
|
||||
}
|
||||
|
||||
total := fs.Blocks * uint64(fs.Bsize)
|
||||
free := fs.Bavail * uint64(fs.Bsize)
|
||||
used := total - free
|
||||
|
||||
return Stats{
|
||||
TotalBytes: total,
|
||||
UsedBytes: used,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UsedPercent returns disk usage as a percentage.
|
||||
func UsedPercent(stats Stats) float64 {
|
||||
if stats.TotalBytes == 0 {
|
||||
return 0
|
||||
}
|
||||
return (float64(stats.UsedBytes) / float64(stats.TotalBytes)) * 100.0
|
||||
}
|
||||
|
||||
// Collector implements the collectors.Collector interface for disk usage.
|
||||
type Collector struct{}
|
||||
|
||||
func New() *Collector {
|
||||
return &Collector{}
|
||||
}
|
||||
|
||||
func (c *Collector) Name() string {
|
||||
return "disk"
|
||||
}
|
||||
|
||||
func (c *Collector) Collect(window *aggregate.Window) {
|
||||
stats, err := Read()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
window.AddDisk(UsedPercent(stats))
|
||||
}
|
||||
Reference in New Issue
Block a user