PuppetAgent Plugin

Added PuppetAgent Plugin reads last_run_summary file
This commit is contained in:
Jonathan Cross
2015-10-16 07:45:35 -04:00
committed by Cameron Sparr
parent a1bad378d2
commit c3dad00c1b
22 changed files with 9942 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ import (
_ "github.com/influxdb/telegraf/plugins/postgresql"
_ "github.com/influxdb/telegraf/plugins/procstat"
_ "github.com/influxdb/telegraf/plugins/prometheus"
_ "github.com/influxdb/telegraf/plugins/puppetagent"
_ "github.com/influxdb/telegraf/plugins/rabbitmq"
_ "github.com/influxdb/telegraf/plugins/redis"
_ "github.com/influxdb/telegraf/plugins/rethinkdb"

View File

@@ -0,0 +1,132 @@
## Telegraf Plugin: PuppetAgent
#### Description
The puppetagent plugin collects variables outputted from the 'last_run_summary.yaml' file
usually located in `/var/lib/puppet/state/`
[PuppetAgent Runs](https://puppetlabs.com/blog/puppet-monitoring-how-to-monitor-the-success-or-failure-of-puppet-runs).
```
cat /var/lib/puppet/state/last_run_summary.yaml
---
events:
failure: 0
total: 0
success: 0
resources:
failed: 0
scheduled: 0
changed: 0
skipped: 0
total: 109
failed_to_restart: 0
restarted: 0
out_of_sync: 0
changes:
total: 0
time:
user: 0.004331
schedule: 0.001123
filebucket: 0.000353
file: 0.441472
exec: 0.508123
anchor: 0.000555
yumrepo: 0.006989
ssh_authorized_key: 0.000764
service: 1.807795
package: 1.325788
total: 8.85354707064819
config_retrieval: 4.75567007064819
last_run: 1444936531
cron: 0.000584
version:
config: 1444936521
puppet: "3.7.5"
```
```
jcross@pit-devops-02 ~ >sudo ./telegraf_linux_amd64 -filter puppetagent -config tele.conf -test
* Plugin: puppetagent, Collection 1
> [] puppetagent_events_failure value=0
> [] puppetagent_events_total value=0
> [] puppetagent_events_success value=0
> [] puppetagent_resources_failed value=0
> [] puppetagent_resources_scheduled value=0
> [] puppetagent_resources_changed value=0
> [] puppetagent_resources_skipped value=0
> [] puppetagent_resources_total value=109
> [] puppetagent_resources_failedtorestart value=0
> [] puppetagent_resources_restarted value=0
> [] puppetagent_resources_outofsync value=0
> [] puppetagent_changes_total value=0
> [] puppetagent_time_user value=0.00393
> [] puppetagent_time_schedule value=0.001234
> [] puppetagent_time_filebucket value=0.000244
> [] puppetagent_time_file value=0.587734
> [] puppetagent_time_exec value=0.389584
> [] puppetagent_time_anchor value=0.000399
> [] puppetagent_time_sshauthorizedkey value=0.000655
> [] puppetagent_time_service value=0
> [] puppetagent_time_package value=1.297537
> [] puppetagent_time_total value=9.45297606225586
> [] puppetagent_time_configretrieval value=5.89822006225586
> [] puppetagent_time_lastrun value=1444940131
> [] puppetagent_time_cron value=0.000646
> [] puppetagent_version_config value=1444940121
> [] puppetagent_version_puppet value=3.7.5
```
## Measurements:
#### PuppetAgent int64 measurements:
Meta:
- units: int64
- tags: ``
Measurement names:
- puppetagent_events_failure
- puppetagent_events_total
- puppetagent_events_success
- puppetagent_resources_failed
- puppetagent_resources_scheduled
- puppetagent_resources_changed
- puppetagent_resources_skipped
- puppetagent_resources_total
- puppetagent_resources_failedtorestart
- puppetagent_resources_restarted
- puppetagent_resources_outofsync
- puppetagent_changes_total
- puppetagent_time_service
- puppetagent_time_lastrun
- puppetagent_version_config
#### PuppetAgent float64 measurements:
Meta:
- units: float64
- tags: ``
Measurement names:
- puppetagent_time_user
- puppetagent_time_schedule
- puppetagent_time_filebucket
- puppetagent_time_file
- puppetagent_time_exec
- puppetagent_time_anchor
- puppetagent_time_sshauthorizedkey
- puppetagent_time_package
- puppetagent_time_total
- puppetagent_time_configretrieval
- puppetagent_time_lastrun
- puppetagent_time_cron
- puppetagent_version_config
#### PuppetAgent string measurements:
Meta:
- units: string
- tags: ``
Measurement names:
- puppetagent_version_puppet

View File

@@ -0,0 +1,34 @@
---
events:
failure: 0
total: 0
success: 0
resources:
failed: 0
scheduled: 0
changed: 0
skipped: 0
total: 109
failed_to_restart: 0
restarted: 0
out_of_sync: 0
changes:
total: 0
time:
user: 0.004331
schedule: 0.001123
filebucket: 0.000353
file: 0.441472
exec: 0.508123
anchor: 0.000555
yumrepo: 0.006989
ssh_authorized_key: 0.000764
service: 1.807795
package: 1.325788
total: 8.85354707064819
config_retrieval: 4.75567007064819
last_run: 1444936531
cron: 0.000584
version:
config: 1444936521
puppet: "3.7.5"

View File

@@ -0,0 +1,135 @@
package puppetagent
import (
"fmt"
"github.com/influxdb/telegraf/plugins"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"reflect"
"strings"
)
// PuppetAgent is a PuppetAgent plugin
type PuppetAgent struct {
Location string
}
var sampleConfig = `
#Location of puppet last run summary file
location = "/var/lib/puppet/state/last_run_summary.yaml"
`
type State struct {
Events event
Resources resource
Changes change
Time time
Version version
}
type event struct {
Failure int64 `yaml:"failure"`
Total int64 `yaml:"total"`
Success int64 `yaml:"success"`
}
type resource struct {
Failed int64 `yaml:"failed"`
Scheduled int64 `yaml:"scheduled"`
Changed int64 `yaml:"changed"`
Skipped int64 `yaml:"skipped"`
Total int64 `yaml:"total"`
FailedToRestart int64 `yaml:"failed_to_restart"`
Restarted int64 `yaml:"restarted"`
OutOfSync int64 `yaml:"out_of_sync"`
}
type change struct {
Total int64 `yaml:"total"`
}
type time struct {
User float64 `yaml:"user"`
Schedule float64 `yaml:"schedule"`
FileBucket float64 `yaml:"filebucket"`
File float64 `yaml:"file"`
Exec float64 `yaml:"exec"`
Anchor float64 `yaml:"anchor"`
SSHAuthorizedKey float64 `yaml:"ssh_authorized_key"`
Service float64 `yaml:"server"`
Package float64 `yaml:"package"`
Total float64 `yaml:"total"`
ConfigRetrieval float64 `yaml:"config_retrieval"`
LastRun int64 `yaml:"last_run"`
Cron float64 `yaml:"cron"`
}
type version struct {
Config int64 `yaml:"config"`
Puppet string `yaml:"puppet"`
}
// SampleConfig returns sample configuration message
func (pa *PuppetAgent) SampleConfig() string {
return sampleConfig
}
// Description returns description of PuppetAgent plugin
func (pa *PuppetAgent) Description() string {
return `Reads last_run_summary.yaml file and converts to measurments`
}
// Gather reads stats from all configured servers accumulates stats
func (pa *PuppetAgent) Gather(acc plugins.Accumulator) error {
if len(pa.Location) == 0 {
pa.Location = "/var/lib/puppet/state/last_run_summary.yaml"
}
if _, err := os.Stat(pa.Location); err != nil {
return fmt.Errorf("%s", err)
}
fh, err := ioutil.ReadFile(pa.Location)
if err != nil {
return fmt.Errorf("%s", err)
}
var puppetState State
err = yaml.Unmarshal(fh, &puppetState)
if err != nil {
return fmt.Errorf("%s", err)
}
structPrinter(&puppetState, acc)
return nil
}
func structPrinter(s *State, acc plugins.Accumulator) {
e := reflect.ValueOf(s).Elem()
for tLevelFNum := 0; tLevelFNum < e.NumField(); tLevelFNum++ {
name := e.Type().Field(tLevelFNum).Name
nameNumField := e.FieldByName(name).NumField()
for sLevelFNum := 0; sLevelFNum < nameNumField; sLevelFNum++ {
sName := e.FieldByName(name).Type().Field(sLevelFNum).Name
sValue := e.FieldByName(name).Field(sLevelFNum).Interface()
lname := strings.ToLower(name)
lsName := strings.ToLower(sName)
acc.Add(fmt.Sprintf("%s_%s", lname, lsName), sValue, nil)
}
}
}
func init() {
plugins.Add("puppetagent", func() plugins.Plugin {
return &PuppetAgent{}
})
}

View File

@@ -0,0 +1,45 @@
package puppetagent
import (
"github.com/influxdb/telegraf/testutil"
"github.com/stretchr/testify/assert"
"testing"
)
func TestGather(t *testing.T) {
var acc testutil.Accumulator
pa := PuppetAgent{
Location: "last_run_summary.yaml",
}
pa.Gather(&acc)
assert.True(t, acc.HasIntValue("events_failure"))
assert.True(t, acc.HasIntValue("events_total"))
assert.True(t, acc.HasIntValue("events_success"))
assert.True(t, acc.HasIntValue("resources_failed"))
assert.True(t, acc.HasIntValue("resources_scheduled"))
assert.True(t, acc.HasIntValue("resources_changed"))
assert.True(t, acc.HasIntValue("resources_skipped"))
assert.True(t, acc.HasIntValue("resources_total"))
assert.True(t, acc.HasIntValue("resources_failedtorestart"))
assert.True(t, acc.HasIntValue("resources_restarted"))
assert.True(t, acc.HasIntValue("resources_outofsync"))
assert.True(t, acc.HasIntValue("changes_total"))
assert.True(t, acc.HasIntValue("time_lastrun"))
assert.True(t, acc.HasIntValue("version_config"))
assert.True(t, acc.HasFloatValue("time_user"))
assert.True(t, acc.HasFloatValue("time_schedule"))
assert.True(t, acc.HasFloatValue("time_filebucket"))
assert.True(t, acc.HasFloatValue("time_file"))
assert.True(t, acc.HasFloatValue("time_exec"))
assert.True(t, acc.HasFloatValue("time_anchor"))
assert.True(t, acc.HasFloatValue("time_sshauthorizedkey"))
assert.True(t, acc.HasFloatValue("time_service"))
assert.True(t, acc.HasFloatValue("time_package"))
assert.True(t, acc.HasFloatValue("time_total"))
assert.True(t, acc.HasFloatValue("time_configretrieval"))
assert.True(t, acc.HasFloatValue("time_cron"))
}