Skip to content

Commit

Permalink
init commit of users metricset
Browse files Browse the repository at this point in the history
  • Loading branch information
fearful-symmetry committed Feb 18, 2020
1 parent 9a9aab5 commit 961801d
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 2 deletions.
27 changes: 27 additions & 0 deletions metricbeat/docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -35921,6 +35921,33 @@ format: duration

--

[float]
=== users

Logged-in user data



*`system.users.path`*::
+
--
Dbus object path


type: keyword

--

*`system.users.sessions`*::
+
--
sessions associated with the user


type: keyword

--

[[exported-fields-tomcat]]
== Tomcat fields

Expand Down
4 changes: 4 additions & 0 deletions metricbeat/docs/modules/system.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ The following metricsets are available:

* <<metricbeat-metricset-system-uptime,uptime>>

* <<metricbeat-metricset-system-users,users>>

include::system/core.asciidoc[]

include::system/cpu.asciidoc[]
Expand Down Expand Up @@ -308,3 +310,5 @@ include::system/socket_summary.asciidoc[]

include::system/uptime.asciidoc[]

include::system/users.asciidoc[]

23 changes: 23 additions & 0 deletions metricbeat/docs/modules/system/users.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
////
This file is generated! See scripts/mage/docs_collector.go
////

[[metricbeat-metricset-system-users]]
=== System users metricset

beta[]

include::../../../module/system/users/_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/users/_meta/data.json[]
----
3 changes: 2 additions & 1 deletion metricbeat/docs/modules_list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ This file is generated! See scripts/mage/docs_collector.go
|<<metricbeat-module-statsd,Statsd>> beta[] |image:./images/icon-no.png[No prebuilt dashboards] |
.1+| .1+| |<<metricbeat-metricset-statsd-server,server>> beta[]
|<<metricbeat-module-system,System>> |image:./images/icon-yes.png[Prebuilt dashboards are available] |
.17+| .17+| |<<metricbeat-metricset-system-core,core>>
.18+| .18+| |<<metricbeat-metricset-system-core,core>>
|<<metricbeat-metricset-system-cpu,cpu>>
|<<metricbeat-metricset-system-diskio,diskio>>
|<<metricbeat-metricset-system-entropy,entropy>>
Expand All @@ -219,6 +219,7 @@ This file is generated! See scripts/mage/docs_collector.go
|<<metricbeat-metricset-system-socket,socket>>
|<<metricbeat-metricset-system-socket_summary,socket_summary>>
|<<metricbeat-metricset-system-uptime,uptime>>
|<<metricbeat-metricset-system-users,users>> beta[]
|<<metricbeat-module-tomcat,Tomcat>> beta[] |image:./images/icon-yes.png[Prebuilt dashboards are available] |
.4+| .4+| |<<metricbeat-metricset-tomcat-cache,cache>> beta[]
|<<metricbeat-metricset-tomcat-memory,memory>> beta[]
Expand Down
1 change: 1 addition & 0 deletions metricbeat/include/list_common.go

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

2 changes: 1 addition & 1 deletion metricbeat/module/system/fields.go

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions metricbeat/module/system/users/_meta/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"@timestamp":"2016-05-23T08:05:34.853Z",
"beat":{
"hostname":"beathost",
"name":"beathost"
},
"metricset":{
"host":"localhost",
"module":"system",
"name":"users",
"rtt":44269
},
"system":{
"users":{
"example": "users"
}
},
"type":"metricsets"
}
11 changes: 11 additions & 0 deletions metricbeat/module/system/users/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
The system/users metricset reports logged in users and associated sessions via dbus and logind.

This metricset is available on:

- Linux
[float]
=== Configuration

There are no configuration options for this metricset.
16 changes: 16 additions & 0 deletions metricbeat/module/system/users/_meta/fields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- name: users
type: group
release: beta
description: >
Logged-in user data
fields:
- name: path
type: keyword
description: >
Dbus object path
- name: sessions
type: keyword
description: >
sessions associated with the user
104 changes: 104 additions & 0 deletions metricbeat/module/system/users/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package users

import (
"github.com/coreos/go-systemd/login1"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/metricbeat/mb"
"github.com/pkg/errors"
)

type userInfo struct {
UID uint32
User string
Path string
Sessions []login1.Session
}

// init registers the MetricSet with the central registry as soon as the program
// starts. The New function will be called later to instantiate an instance of
// the MetricSet for each host defined in the module's configuration. After the
// MetricSet has been created then Fetch will begin to be called periodically.
func init() {
mb.Registry.MustAddMetricSet("system", "users", New)
}

// MetricSet holds any configuration or state information. It must implement
// the mb.MetricSet interface. And this is best achieved by embedding
// mb.BaseMetricSet because it implements all of the required mb.MetricSet
// interface methods except for Fetch.
type MetricSet struct {
mb.BaseMetricSet
counter int
conn *login1.Conn
}

// New creates a new instance of the MetricSet. New is responsible for unpacking
// any MetricSet specific configuration options if there are any.
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
cfgwarn.Beta("The system users metricset is beta.")

config := struct{}{}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}

conn, err := login1.New()
if err != nil {
return nil, errors.Wrap(err, "error connecting to dbus")
}

return &MetricSet{
BaseMetricSet: base,
counter: 1,
conn: conn,
}, nil
}

// Fetch methods implements the data gathering and data conversion to the right
// format. It publishes the event which is then forwarded to the output. In case
// of an error set the Error field of mb.Event or simply call report.Error().
func (m *MetricSet) Fetch(report mb.ReporterV2) error {
users, err := m.conn.ListUsers()
if err != nil {
return errors.Wrap(err, "error listing users")
}
sessions, err := m.conn.ListSessions()
if err != nil {
return errors.Wrap(err, "error listing sessions")
}

eventMapping(users, sessions, report)

return nil
}

// eventMapping iterates through the lists of users and sessions, combining the two
func eventMapping(users []login1.User, sessions []login1.Session, report mb.ReporterV2) error {
sessionList := []string{}
for _, user := range users {
for _, session := range sessions {
if session.UID == user.UID {
sessionList = append(sessionList, session.ID)
}
}
reported := report.Event(mb.Event{
RootFields: common.MapStr{
"user": common.MapStr{
"name": user.Name,
"id": user.UID,
},
},
MetricSetFields: common.MapStr{
"path": user.Path,
"sessions": sessionList,
},
},
)
//if the channel is closed and metricbeat is shutting down, just return
if !reported {
break
}
}
return nil
}

0 comments on commit 961801d

Please sign in to comment.