-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add system uptime metricset #4887
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-metricset-system-uptime]] | ||
include::../../../module/system/uptime/_meta/docs.asciidoc[] | ||
|
||
|
||
==== Fields | ||
|
||
For a description of each field in the metricset, see the | ||
<<exported-fields-system,exported fields>> section. | ||
|
||
Here is an example document generated by this metricset: | ||
|
||
[source,json] | ||
---- | ||
include::../../../module/system/uptime/_meta/data.json[] | ||
---- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"@timestamp": "2016-05-23T08:05:34.853Z", | ||
"@metadata": { | ||
"beat": "noindex", | ||
"type": "doc" | ||
}, | ||
"system": { | ||
"uptime": { | ||
"duration": { | ||
"ms": 695499821 | ||
} | ||
} | ||
}, | ||
"metricset": { | ||
"module": "system", | ||
"name": "uptime", | ||
"rtt": 115 | ||
}, | ||
"beat": { | ||
"name": "host.example.com", | ||
"hostname": "host.example.com" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
=== System Uptime Metricset | ||
|
||
The System `uptime` metricset provides the uptime of the host operating system. | ||
|
||
This metricset is available on: | ||
|
||
- Darwin | ||
- Linux | ||
- OpenBSD | ||
- Windows |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
- name: uptime | ||
type: group | ||
description: > | ||
`uptime` contains the operating system uptime metric. | ||
fields: | ||
- name: duration.ms | ||
type: long | ||
format: duration | ||
input_format: milliseconds | ||
description: > | ||
The OS uptime in milliseconds. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package uptime reports the system's uptime. | ||
package uptime |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// +build darwin linux openbsd windows | ||
|
||
package uptime | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/metricbeat/mb/parse" | ||
sigar "github.com/elastic/gosigar" | ||
) | ||
|
||
func init() { | ||
if err := mb.Registry.AddMetricSet("system", "uptime", New, parse.EmptyHostParser); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// MetricSet for fetching an OS uptime metric. | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
} | ||
|
||
// New is a mb.MetricSetFactory that returns a new MetricSet. | ||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
return &MetricSet{base}, nil | ||
} | ||
|
||
// Fetch fetches the uptime metric from the OS. | ||
func (m *MetricSet) Fetch() (common.MapStr, error) { | ||
var uptime sigar.Uptime | ||
if err := uptime.Get(); err != nil { | ||
return nil, errors.Wrap(err, "failed to get uptime") | ||
} | ||
|
||
return common.MapStr{ | ||
"duration": common.MapStr{ | ||
"ms": int64(uptime.Length * 1000), | ||
}, | ||
}, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build darwin linux openbsd windows | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andrewkroh was there any reason to exclude freebsd here? sigar has the implementation for this. // cc @kaiyan-sheng @ruflin There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can’t think of any reason. It probably should be tagged as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, what is the difference between |
||
|
||
package uptime | ||
|
||
import ( | ||
"testing" | ||
|
||
mbtest "github.com/elastic/beats/metricbeat/mb/testing" | ||
) | ||
|
||
func TestData(t *testing.T) { | ||
f := mbtest.NewEventFetcher(t, getConfig()) | ||
|
||
uptime, err := f.Fetch() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
event := mbtest.CreateFullEvent(f, uptime) | ||
mbtest.WriteEventToDataJSON(t, event) | ||
} | ||
|
||
func getConfig() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"module": "system", | ||
"metricsets": []string{"uptime"}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea!