Skip to content

Commit

Permalink
add uname metric
Browse files Browse the repository at this point in the history
  • Loading branch information
identw committed May 22, 2024
1 parent 6da5c3a commit 9a3a4a7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
}
listenAddr := fmt.Sprintf("%s:%s", host, port)

metrics.RegistrMetrics()
metrics.RegistrMetrics(cfg)

if cfg.FileHashCollector.Enabled {
go func() {
Expand Down Expand Up @@ -135,6 +135,13 @@ func main() {
} else {
metrics.UpdateHostnameChecksumMetrics(hostnameChecksum)
}

// uname checksum
if unameChecksum, err := system.UnameChecksum(); err != nil {
fmt.Fprintf(os.Stderr, "Error getting uname: %v\n", err)
} else {
metrics.UpdateUnameChecksumMetrics(unameChecksum)
}

// hostname
if hostname, err := os.Hostname(); err != nil {
Expand All @@ -155,7 +162,9 @@ func main() {
fmt.Fprintf(os.Stderr, "Error getting count of login users: %v\n", err)
} else {
metrics.UpdateLoginUsersCountMetrics(countLoginUsers)
}
}


time.Sleep(60 * time.Second)
}
}()
Expand Down
56 changes: 43 additions & 13 deletions pkg/metrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"
"sync"

"github.com/orangeAppsRu/custom-exporter/pkg/config"
"github.com/orangeAppsRu/custom-exporter/pkg/filehash"
"github.com/orangeAppsRu/custom-exporter/pkg/network"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -74,6 +75,13 @@ var (
[]string{"hostname"},
)

unameChecksumGauge = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "uname_checksum",
Help: "Checksum of uname",
},
)

uptimeSecondsCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "uptime_seconds",
Expand Down Expand Up @@ -113,28 +121,44 @@ var (
processRunningStatusMutex sync.Mutex
hostnameChecksumMutex sync.Mutex
hostnameMutex sync.Mutex
unameChecksumMutex sync.Mutex
uptimeSecondsMutex sync.Mutex
countLoginUsersMutex sync.Mutex
puppetCatalogLastCompileTimestampMutex sync.Mutex
puppetCatalogLastCompileStatusMutex sync.Mutex
)

func RegistrMetrics() {
func RegistrMetrics(cfg config.Config) {
prometheus.DefaultRegisterer.Unregister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
prometheus.DefaultRegisterer.Unregister(collectors.NewGoCollector())

prometheus.MustRegister(hashGauge)
prometheus.MustRegister(networkTargetGauge)
prometheus.MustRegister(processCountGauge)
prometheus.MustRegister(processCpuTimeCounter)
prometheus.MustRegister(processMemoryResidentGauge)
prometheus.MustRegister(processRunningStatusGauge)
prometheus.MustRegister(hostnameChecksumGauge)
prometheus.MustRegister(hostnameGauge)
prometheus.MustRegister(uptimeSecondsCounter)
prometheus.MustRegister(countLoginUsersGauge)
prometheus.MustRegister(puppetCatalogLastCompileTimestampGauge)
prometheus.MustRegister(puppetCatalogLastCompileStatusGauge)
if cfg.FileHashCollector.Enabled {
prometheus.MustRegister(hashGauge)
}

if cfg.PortCollector.Enabled {
prometheus.MustRegister(networkTargetGauge)
}

if cfg.ProcessCollector.Enabled {
prometheus.MustRegister(processCountGauge)
prometheus.MustRegister(processCpuTimeCounter)
prometheus.MustRegister(processMemoryResidentGauge)
prometheus.MustRegister(processRunningStatusGauge)
}

if cfg.SystemCollector.Enabled {
prometheus.MustRegister(hostnameChecksumGauge)
prometheus.MustRegister(hostnameGauge)
prometheus.MustRegister(unameChecksumGauge)
prometheus.MustRegister(uptimeSecondsCounter)
prometheus.MustRegister(countLoginUsersGauge)
}

if cfg.PuppetCollector.Enabled {
prometheus.MustRegister(puppetCatalogLastCompileTimestampGauge)
prometheus.MustRegister(puppetCatalogLastCompileStatusGauge)
}
}

func UpdateFileHashMetrics(filesWithHash []filehash.FileHash) {
Expand Down Expand Up @@ -201,6 +225,12 @@ func UpdateHostnameMetrics(hostname string) {
hostnameMutex.Unlock()
}

func UpdateUnameChecksumMetrics(checksum float64) {
unameChecksumMutex.Lock()
unameChecksumGauge.Set(checksum)
unameChecksumMutex.Unlock()
}

func UpdateUptimeSecondsMetrics(uptime float64) {
uptimeSecondsMutex.Lock()
uptimeSecondsCounter.Add(uptime)
Expand Down
12 changes: 10 additions & 2 deletions pkg/system/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"strconv"
"strings"
// "encoding/binary"
// "encoding/json"
"os/exec"
)

Expand Down Expand Up @@ -62,3 +60,13 @@ func CountLoginUsers() (int, error) {
return numUsers, nil
}

func UnameChecksum() (float64, error) {
output, err := exec.Command("uname", "-a").Output()
if err != nil {
return 0, fmt.Errorf("failed to execute 'uname -a': %w", err)
}
tablePolynomial := crc32.MakeTable(crc32.IEEE)
hash := crc32.Checksum([]byte(output), tablePolynomial)

return float64(hash), nil
}

0 comments on commit 9a3a4a7

Please sign in to comment.