Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #68 from jcooklin/fb/updates-plugin-lib
Browse files Browse the repository at this point in the history
Fixes #67 - use snap-plugin-lib-go
  • Loading branch information
jcooklin authored May 23, 2017
2 parents 992884a + a8aa848 commit f4274aa
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 285 deletions.
67 changes: 41 additions & 26 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package: github.com/intelsdi-x/snap-plugin-collector-psutil
import:
- package: github.com/intelsdi-x/snap
version: ^1.0.0
subpackages:
- control/plugin
- control/plugin/cpolicy
- core
- package: github.com/intelsdi-x/snap-plugin-lib-go
- package: github.com/shirou/gopsutil
subpackages:
- cpu
Expand Down
17 changes: 8 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ limitations under the License.

package main

// Import the snap plugin library
import (
"os"
// Import the snap plugin library
"github.com/intelsdi-x/snap/control/plugin"
// Import our collector plugin implementation
"github.com/intelsdi-x/snap-plugin-collector-psutil/psutil"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
)

const (
pluginName = "psutil"
pluginVersion = 11
)

// plugin bootstrap
func main() {
plugin.Start(
psutil.Meta(),
psutil.NewPsutilCollector(), // CollectorPlugin interface
os.Args[1],
)
plugin.StartCollector(psutil.NewPsutilCollector(), pluginName, pluginVersion)
}
51 changes: 26 additions & 25 deletions psutil/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
"runtime"
"time"

"github.com/intelsdi-x/snap/control/plugin"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
"github.com/shirou/gopsutil/cpu"
)

Expand Down Expand Up @@ -75,8 +74,9 @@ var cpuLabels = map[string]label{
},
}

func cpuTimes(nss []core.Namespace) ([]plugin.MetricType, error) {
func cpuTimes(nss []plugin.Namespace) ([]plugin.Metric, error) {
// gather metrics per each cpu
defer timeSpent(time.Now(), "cpuTimes")
timesCPUs, err := cpu.Times(true)
if err != nil {
return nil, err
Expand All @@ -88,7 +88,7 @@ func cpuTimes(nss []core.Namespace) ([]plugin.MetricType, error) {
return nil, err
}

results := []plugin.MetricType{}
results := []plugin.Metric{}

for _, ns := range nss {
// set requested metric name from last namespace element
Expand All @@ -98,19 +98,19 @@ func cpuTimes(nss []core.Namespace) ([]plugin.MetricType, error) {
for _, timesCPU := range timesCPUs {
// prepare namespace copy to update value
// this will allow to keep namespace as dynamic (name != "")
dyn := make([]core.NamespaceElement, len(ns))
dyn := make([]plugin.NamespaceElement, len(ns))
copy(dyn, ns)
dyn[3].Value = timesCPU.CPU
// get requested metric value
val, err := getCPUTimeValue(&timesCPU, metricName)
if err != nil {
return nil, err
}
metric := plugin.MetricType{
Namespace_: dyn,
Data_: val,
Timestamp_: time.Now(),
Unit_: cpuLabels[metricName].unit,
metric := plugin.Metric{
Namespace: dyn,
Data: val,
Timestamp: time.Now(),
Unit: cpuLabels[metricName].unit,
}
results = append(results, metric)
}
Expand All @@ -126,11 +126,11 @@ func cpuTimes(nss []core.Namespace) ([]plugin.MetricType, error) {
if err != nil {
return nil, err
}
metric := plugin.MetricType{
Namespace_: ns,
Data_: val,
Timestamp_: time.Now(),
Unit_: cpuLabels[metricName].unit,
metric := plugin.Metric{
Namespace: ns,
Data: val,
Timestamp: time.Now(),
Unit: cpuLabels[metricName].unit,
}
results = append(results, metric)
}
Expand Down Expand Up @@ -177,21 +177,22 @@ func getCPUTimeValue(stat *cpu.TimesStat, name string) (float64, error) {
}
}

func getCPUTimesMetricTypes() ([]plugin.MetricType, error) {
func getCPUTimesMetricTypes() ([]plugin.Metric, error) {
defer timeSpent(time.Now(), "getCPUTimesMetricTypes")
//passing true to CPUTimes indicates per CPU
mts := []plugin.MetricType{}
mts := []plugin.Metric{}
switch runtime.GOOS {
case "linux", "darwin":
for k, label := range cpuLabels {
mts = append(mts, plugin.MetricType{
Namespace_: core.NewNamespace("intel", "psutil", "cpu").AddDynamicElement("cpu_id", "physical cpu id").AddStaticElement(k),
Description_: label.description,
Unit_: label.unit,
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "cpu").AddDynamicElement("cpu_id", "physical cpu id").AddStaticElement(k),
Description: label.description,
Unit: label.unit,
})
mts = append(mts, plugin.MetricType{
Namespace_: core.NewNamespace("intel", "psutil", "cpu", "cpu-total").AddStaticElement(k),
Description_: label.description,
Unit_: label.unit,
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "cpu", "cpu-total").AddStaticElement(k),
Description: label.description,
Unit: label.unit,
})
}
default:
Expand Down
80 changes: 41 additions & 39 deletions psutil/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,25 @@ import (

"github.com/shirou/gopsutil/disk"

"github.com/intelsdi-x/snap/control/plugin"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap-plugin-lib-go/v1/plugin"
)

func getPSUtilDiskUsage(path string) (*disk.UsageStat, error) {
defer timeSpent(time.Now(), "getPSUtilDiskUsage")
disk_usage, err := disk.Usage(path)
if err != nil {
return nil, err
}
return disk_usage, nil
}

func getDiskUsageMetrics(nss []core.Namespace, mounts []string) ([]plugin.MetricType, error) {
func getDiskUsageMetrics(nss []plugin.Namespace, mounts []string) ([]plugin.Metric, error) {
defer timeSpent(time.Now(), "getDiskUsageMetrics")
t := time.Now()
var paths []disk.PartitionStat
metrics := []plugin.MetricType{}
metrics := []plugin.Metric{}
namespaces := map[string][]string{}
requested := map[string]core.Namespace{}
requested := map[string]plugin.Namespace{}
for _, ns := range nss {
namespaces[ns.Strings()[len(ns.Strings())-1]] = ns.Strings()
requested[ns.Strings()[len(ns.Strings())-1]] = ns
Expand Down Expand Up @@ -81,73 +82,74 @@ func getDiskUsageMetrics(nss []core.Namespace, mounts []string) ([]plugin.Metric
tags["device"] = path.Device
for _, namespace := range namespaces {
if strings.Contains(strings.Join(namespace, "|"), "total") {
nspace := make([]core.NamespaceElement, len(requested["total"]))
nspace := make([]plugin.NamespaceElement, len(requested["total"]))
copy(nspace, requested["total"])
nspace[3].Value = path.Mountpoint
metrics = append(metrics, plugin.MetricType{
Namespace_: nspace,
Data_: data.Total,
Tags_: tags,
Timestamp_: t,
metrics = append(metrics, plugin.Metric{
Namespace: nspace,
Data: data.Total,
Tags: tags,
Timestamp: t,
})
}
if strings.Contains(strings.Join(namespace, "|"), "used") {
nspace := make([]core.NamespaceElement, len(requested["used"]))
nspace := make([]plugin.NamespaceElement, len(requested["used"]))
copy(nspace, requested["used"])
nspace[3].Value = path.Mountpoint
metrics = append(metrics, plugin.MetricType{
Namespace_: nspace,
Data_: data.Used,
Tags_: tags,
Timestamp_: t,
metrics = append(metrics, plugin.Metric{
Namespace: nspace,
Data: data.Used,
Tags: tags,
Timestamp: t,
})
}
if strings.Contains(strings.Join(namespace, "|"), "free") {
nspace := make([]core.NamespaceElement, len(requested["free"]))
nspace := make([]plugin.NamespaceElement, len(requested["free"]))
copy(nspace, requested["free"])
nspace[3].Value = path.Mountpoint
metrics = append(metrics, plugin.MetricType{
Namespace_: nspace,
Data_: data.Free,
Tags_: tags,
Timestamp_: t,
metrics = append(metrics, plugin.Metric{
Namespace: nspace,
Data: data.Free,
Tags: tags,
Timestamp: t,
})
}
if strings.Contains(strings.Join(namespace, "|"), "percent") {
nspace := make([]core.NamespaceElement, len(requested["percent"]))
nspace := make([]plugin.NamespaceElement, len(requested["percent"]))
copy(nspace, requested["percent"])
nspace[3].Value = path.Mountpoint
metrics = append(metrics, plugin.MetricType{
Namespace_: nspace,
Data_: data.UsedPercent,
Tags_: tags,
Timestamp_: t,
metrics = append(metrics, plugin.Metric{
Namespace: nspace,
Data: data.UsedPercent,
Tags: tags,
Timestamp: t,
})
}
}
}
return metrics, nil
}

func getDiskUsageMetricTypes() []plugin.MetricType {
var mts []plugin.MetricType
mts = append(mts, plugin.MetricType{
Namespace_: core.NewNamespace("intel", "psutil", "disk").
func getDiskUsageMetricTypes() []plugin.Metric {
defer timeSpent(time.Now(), "getDiskUsageMetricTypes")
var mts []plugin.Metric
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "disk").
AddDynamicElement("mount_point", "Mount Point").
AddStaticElement("total"),
})
mts = append(mts, plugin.MetricType{
Namespace_: core.NewNamespace("intel", "psutil", "disk").
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "disk").
AddDynamicElement("mount_point", "Mount Point").
AddStaticElement("used"),
})
mts = append(mts, plugin.MetricType{
Namespace_: core.NewNamespace("intel", "psutil", "disk").
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "disk").
AddDynamicElement("mount_point", "Mount Point").
AddStaticElement("free"),
})
mts = append(mts, plugin.MetricType{
Namespace_: core.NewNamespace("intel", "psutil", "disk").
mts = append(mts, plugin.Metric{
Namespace: plugin.NewNamespace("intel", "psutil", "disk").
AddDynamicElement("mount_point", "Mount Point").
AddStaticElement("percent"),
})
Expand Down
Loading

0 comments on commit f4274aa

Please sign in to comment.