Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from mmolnar/master
Browse files Browse the repository at this point in the history
Add puppet environment to agent info and add failed catalog metric
  • Loading branch information
graywolf-at-work authored Feb 8, 2023
2 parents a0daaa0 + 40514ca commit d5fdb11
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 28 deletions.
62 changes: 41 additions & 21 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -56,6 +55,7 @@ type reportScraper struct {
info map[string]string
data gaugeValuesBySectionsMap
puppetVersion string
catalogVersion string
configTimestamp float64
}

Expand Down Expand Up @@ -100,25 +100,23 @@ func (r *reportScraper) collectMetrics(ch chan<- prometheus.Metric, u puppetYaml

metricsBySections, err := r.processReport(u)

if metricsBySections != nil {
for section, metrics := range metricsBySections {
for metricName, metricValue := range metrics {
help, ok := knownMetricsDescriptionsBySection[section][metricName]
if !ok {
help = metricName
}

ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(r.namespace, section, metricName),
help,
nil,
nil,
),
prometheus.GaugeValue,
metricValue,
)
for section, metrics := range metricsBySections {
for metricName, metricValue := range metrics {
help, ok := knownMetricsDescriptionsBySection[section][metricName]
if !ok {
help = metricName
}

ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(r.namespace, section, metricName),
help,
nil,
nil,
),
prometheus.GaugeValue,
metricValue,
)
}
}

Expand Down Expand Up @@ -152,6 +150,22 @@ func (r *reportScraper) collectMetrics(ch chan<- prometheus.Metric, u puppetYaml
infoValues...,
)

catalogFailed := 0.0
if r.catalogVersion == "" {
catalogFailed = 1.0
}

ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
prometheus.BuildFQName(r.namespace, "catalog", "failed"),
"Bool value of puppet agent failing retrive catalog",
nil,
nil,
),
prometheus.GaugeValue,
catalogFailed,
)

return err
}

Expand All @@ -174,6 +188,10 @@ func (r *reportScraper) setConfigTimestamp(value float64) {
r.configTimestamp = value
}

func (r *reportScraper) setCatalogVersion(value string) {
r.catalogVersion = value
}

type parseError struct {
filename string
error
Expand All @@ -192,7 +210,9 @@ func (r *reportScraper) processDisabledLock() (bool, string) {
disabled = false
}

var d struct{ DisabledMessage string `json:"disabled_message"` }
var d struct {
DisabledMessage string `json:"disabled_message"`
}
if disabledLockContent != nil {
if err := json.Unmarshal(disabledLockContent, &d); err != nil {
return disabled, err.Error()
Expand Down Expand Up @@ -237,7 +257,7 @@ func readFile(filename string) ([]byte, error) {
return nil, &notFoundError{&readError{filename, err}}
}

content, err := ioutil.ReadFile(filename)
content, err := os.ReadFile(filename)
if err != nil {
return nil, &readError{filename, err}
}
Expand Down
21 changes: 18 additions & 3 deletions full_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"hash/fnv"
"strconv"
"time"

"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -33,8 +34,21 @@ func (r *fullReportScraper) UnmarshalYAML(unmarshal func(interface{}) error) err
return err
}

var configTimestamp float64
if t, err := time.Parse(time.RFC3339, report.Time); err == nil {
// Puppet 6 report uses RFC3339 format
configTimestamp = float64(t.Unix())
} else if t, err = time.Parse("2006-01-02 15:04:05 -07:00", report.Time); err == nil {
// Puppet 3 report uses something else
configTimestamp = float64(t.Unix())
} else {
// Fallback to what was used previously
configTimestamp = hash(report.ConfigurationVersion)
}

r.setPuppetVersion(report.PuppetVersion)
r.setConfigTimestamp(hash(report.ConfigurationVersion))
r.setConfigTimestamp(configTimestamp)
r.setCatalogVersion(report.ConfigurationVersion)

r.setInfo("environment", report.Environment)

Expand All @@ -49,9 +63,10 @@ func (r *fullReportScraper) UnmarshalYAML(unmarshal func(interface{}) error) err

type fullReport struct {
Metrics fullReportMetricsSections
PuppetVersion string `yaml:"puppet_version"`
ConfigurationVersion string `yaml:"configuration_version"`
PuppetVersion string `yaml:"puppet_version"`
ConfigurationVersion string `yaml:"configuration_version"`
Environment string
Time string
}

type fullReportMetricsSections map[string]fullReportMetricsSection
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ Usage:
The commands & flags are:
version print the version to stdout
`
version print the version to stdout`

var (
version = "n/a"
Expand Down
15 changes: 13 additions & 2 deletions summary_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@ func (r *summaryReportScraper) UnmarshalYAML(unmarshal func(interface{}) error)
var v struct {
Version struct {
Puppet string
Config float64
Config string
}
Application struct {
RunMode string `yaml:"run_mode"`
InitialEnvironment string `yaml:"initial_environment"`
ConvergedEnvironment string `yaml:"converged_environment"`
}
Time struct {
LastRun float64 `yaml:"last_run"`
}
}
if err := unmarshal(&v); err != nil {
return err
}

r.reportScraper.setPuppetVersion(v.Version.Puppet)
r.reportScraper.setConfigTimestamp(v.Version.Config)
r.reportScraper.setConfigTimestamp(v.Time.LastRun)
r.setCatalogVersion(v.Version.Config)

r.setInfo("environment", v.Application.ConvergedEnvironment)

var objmap map[string]gaugeValueMap
unmarshal(&objmap)
Expand Down

0 comments on commit d5fdb11

Please sign in to comment.