diff --git a/common.go b/common.go index 00c2d11..d90d8e8 100644 --- a/common.go +++ b/common.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" "os" "github.com/prometheus/client_golang/prometheus" @@ -56,6 +55,7 @@ type reportScraper struct { info map[string]string data gaugeValuesBySectionsMap puppetVersion string + catalogVersion string configTimestamp float64 } @@ -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, + ) } } @@ -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 } @@ -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 @@ -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() @@ -237,7 +257,7 @@ func readFile(filename string) ([]byte, error) { return nil, ¬FoundError{&readError{filename, err}} } - content, err := ioutil.ReadFile(filename) + content, err := os.ReadFile(filename) if err != nil { return nil, &readError{filename, err} } diff --git a/full_report.go b/full_report.go index 476757f..34aebd3 100644 --- a/full_report.go +++ b/full_report.go @@ -3,6 +3,7 @@ package main import ( "hash/fnv" "strconv" + "time" "github.com/prometheus/client_golang/prometheus" ) @@ -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) @@ -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 diff --git a/main.go b/main.go index 7afa8dd..db896d0 100644 --- a/main.go +++ b/main.go @@ -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" diff --git a/summary_report.go b/summary_report.go index bd3c6e7..41f065e 100644 --- a/summary_report.go +++ b/summary_report.go @@ -23,7 +23,15 @@ 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 { @@ -31,7 +39,10 @@ func (r *summaryReportScraper) UnmarshalYAML(unmarshal func(interface{}) error) } 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)