-
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
New windows service metricset #5332
Merged
andrewkroh
merged 60 commits into
elastic:master
from
martinscholz83:windows-service-metricset
Nov 2, 2017
Merged
Changes from all commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
1ef849c
Create new metricset
martinscholz83 d414d86
Add Close function for handler
martinscholz83 b9878d7
Add description
martinscholz83 d7d0aa1
make update
martinscholz83 a5bd839
Add service start types
martinscholz83 65ea2f7
Add service access rights
martinscholz83 a0c7f13
Add OpenService function
martinscholz83 d5be9ed
Add more funtions
martinscholz83 8fea616
Add errors
martinscholz83 6fd5d02
Query service config
martinscholz83 eb3ecd2
Correct return type for QueryServiceConfig
martinscholz83 73ef011
Remove services config
martinscholz83 3fe19b7
Add process access rights
martinscholz83 51f64db
Add process sycall functions
martinscholz83 ff57f2b
remove process functions
martinscholz83 df94026
Share run.go for all windows metricsets
martinscholz83 ae9de49
Cleanup code
martinscholz83 6ade0fc
Break into smaller functions
martinscholz83 49430ff
Refactor getting uptime
martinscholz83 e3b71ce
Calculate service uptime
martinscholz83 78f4a34
Add TestData integration test
martinscholz83 8ffd2c6
Remove unnecessary types
martinscholz83 7dff37d
Add SeDebugPrivilege helper function
martinscholz83 ed94168
Add fields
martinscholz83 239d774
Change uptime format
martinscholz83 8f0dcc6
Move uptime logic into helper
martinscholz83 89f6ce8
Change return type for service uptime
martinscholz83 076e376
Change uptime to time.Duration
martinscholz83 b0dde4e
Refactor getServiceStates
martinscholz83 999e2b5
Write data.json
martinscholz83 088389f
Refactor uptime calculating
martinscholz83 63df59e
Remove new lines
martinscholz83 75958f9
Remove commented out code
martinscholz83 57a47a3
Call privilege func once
martinscholz83 570d828
make update
martinscholz83 2f98979
make fmt
martinscholz83 8577fc4
Reorder imports
martinscholz83 8654ee8
Add miising return value for CheckAndEnableSeDebugPrivilege()
martinscholz83 fa8ef48
remove named parameter
martinscholz83 120c8f8
Format fields
martinscholz83 2a172bb
Remove uptime conversion in helper func
martinscholz83 e534732
Set oxford comma for lists
martinscholz83 e504e6f
Only add uptime to event if its not 0
martinscholz83 884212e
Fix adding uptime to event
martinscholz83 0489cf8
Update data.json
martinscholz83 63d735b
Update Makefile
martinscholz83 4ff7381
Fix formatting
martinscholz83 12b78b2
make fmt
martinscholz83 55751ae
make update
martinscholz83 4eb1120
Build only on windows
martinscholz83 99fbfd1
Add integration test
martinscholz83 563f487
Refactor getServiceStates
andrewkroh 9d83f1e
Fix const Running
martinscholz83 aecd50b
Rename service metricset
martinscholz83 e9409e8
Rename json output
martinscholz83 17ac114
make update
martinscholz83 cb4cf55
go generate
martinscholz83 e743b68
go test -data
martinscholz83 5ca5ebe
Fix typo
martinscholz83 b98d69b
make update
martinscholz83 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-windows-service]] | ||
include::../../../module/windows/service/_meta/docs.asciidoc[] | ||
|
||
|
||
==== Fields | ||
|
||
For a description of each field in the metricset, see the | ||
<<exported-fields-windows,exported fields>> section. | ||
|
||
Here is an example document generated by this metricset: | ||
|
||
[source,json] | ||
---- | ||
include::../../../module/windows/service/_meta/data.json[] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package helper | ||
|
||
import ( | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/gosigar/sys/windows" | ||
|
||
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
var once sync.Once | ||
|
||
// errMissingSeDebugPrivilege indicates that the SeDebugPrivilege is not | ||
// present in the process's token. This is distinct from disabled. The token | ||
// would be missing if the user does not have "Debug programs" rights. By | ||
// default, only administrators and LocalSystem accounts have the privileges to | ||
// debug programs. | ||
var errMissingSeDebugPrivilege = errors.New("Metricbeat is running without " + | ||
"SeDebugPrivilege, a Windows privilege that allows it to collect metrics " + | ||
"from other processes. The user running Metricbeat may not have the " + | ||
"appropriate privileges or the security policy disallows it.") | ||
|
||
// enableSeDebugPrivilege enables the SeDebugPrivilege if it is present in | ||
// the process's token. | ||
func enableSeDebugPrivilege() error { | ||
self, err := syscall.GetCurrentProcess() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var token syscall.Token | ||
err = syscall.OpenProcessToken(self, syscall.TOKEN_QUERY|syscall.TOKEN_ADJUST_PRIVILEGES, &token) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = windows.EnableTokenPrivileges(token, windows.SeDebugPrivilege); err != nil { | ||
return errors.Wrap(err, "EnableTokenPrivileges failed") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CheckAndEnableSeDebugPrivilege checks if the process's token has the | ||
// SeDebugPrivilege and enables it if it is disabled. | ||
func CheckAndEnableSeDebugPrivilege() error { | ||
var err error | ||
once.Do(func() { | ||
err = checkAndEnableSeDebugPrivilege() | ||
}) | ||
return err | ||
} | ||
|
||
func checkAndEnableSeDebugPrivilege() error { | ||
info, err := windows.GetDebugInfo() | ||
if err != nil { | ||
return errors.Wrap(err, "GetDebugInfo failed") | ||
} | ||
logp.Info("Metricbeat process and system info: %v", info) | ||
|
||
seDebug, found := info.ProcessPrivs[windows.SeDebugPrivilege] | ||
if !found { | ||
return errMissingSeDebugPrivilege | ||
} | ||
|
||
if seDebug.Enabled { | ||
logp.Info("SeDebugPrivilege is enabled. %v", seDebug) | ||
return nil | ||
} | ||
|
||
if err = enableSeDebugPrivilege(); err != nil { | ||
logp.Warn("Failure while attempting to enable SeDebugPrivilege. %v", err) | ||
} | ||
|
||
info, err = windows.GetDebugInfo() | ||
if err != nil { | ||
return errors.Wrap(err, "GetDebugInfo failed") | ||
} | ||
|
||
seDebug, found = info.ProcessPrivs[windows.SeDebugPrivilege] | ||
if !found { | ||
return errMissingSeDebugPrivilege | ||
} | ||
|
||
if !seDebug.Enabled { | ||
return errors.Errorf("Metricbeat failed to enable the "+ | ||
"SeDebugPrivilege, a Windows privilege that allows it to collect "+ | ||
"metrics from other processes. %v", seDebug) | ||
} | ||
|
||
logp.Info("SeDebugPrivilege is now enabled. %v", seDebug) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,12 @@ | ||
package system | ||
|
||
import ( | ||
"syscall" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/elastic/beats/libbeat/logp" | ||
"github.com/elastic/gosigar/sys/windows" | ||
"github.com/elastic/beats/metricbeat/helper" | ||
) | ||
|
||
// errMissingSeDebugPrivilege indicates that the SeDebugPrivilege is not | ||
// present in the process's token. This is distinct from disabled. The token | ||
// would be missing if the user does not have "Debug programs" rights. By | ||
// default, only administrators and LocalSystem accounts have the privileges to | ||
// debug programs. | ||
var errMissingSeDebugPrivilege = errors.New("Metricbeat is running without " + | ||
"SeDebugPrivilege, a Windows privilege that allows it to collect metrics " + | ||
"from other processes. The user running Metricbeat may not have the " + | ||
"appropriate privileges or the security policy disallows it.") | ||
|
||
func initModule() { | ||
if err := checkAndEnableSeDebugPrivilege(); err != nil { | ||
if err := helper.CheckAndEnableSeDebugPrivilege(); err != nil { | ||
logp.Warn("%v", err) | ||
} | ||
} | ||
|
||
// checkAndEnableSeDebugPrivilege checks if the process's token has the | ||
// SeDebugPrivilege and enables it if it is disabled. | ||
func checkAndEnableSeDebugPrivilege() error { | ||
info, err := windows.GetDebugInfo() | ||
if err != nil { | ||
return errors.Wrap(err, "GetDebugInfo failed") | ||
} | ||
logp.Info("Metricbeat process and system info: %v", info) | ||
|
||
seDebug, found := info.ProcessPrivs[windows.SeDebugPrivilege] | ||
if !found { | ||
return errMissingSeDebugPrivilege | ||
} | ||
|
||
if seDebug.Enabled { | ||
logp.Info("SeDebugPrivilege is enabled. %v", seDebug) | ||
return nil | ||
} | ||
|
||
if err = enableSeDebugPrivilege(); err != nil { | ||
logp.Warn("Failure while attempting to enable SeDebugPrivilege. %v", err) | ||
} | ||
|
||
info, err = windows.GetDebugInfo() | ||
if err != nil { | ||
return errors.Wrap(err, "GetDebugInfo failed") | ||
} | ||
|
||
seDebug, found = info.ProcessPrivs[windows.SeDebugPrivilege] | ||
if !found { | ||
return errMissingSeDebugPrivilege | ||
} | ||
|
||
if !seDebug.Enabled { | ||
return errors.Errorf("Metricbeat failed to enable the "+ | ||
"SeDebugPrivilege, a Windows privilege that allows it to collect "+ | ||
"metrics from other processes. %v", seDebug) | ||
} | ||
|
||
logp.Info("SeDebugPrivilege is now enabled. %v", seDebug) | ||
return nil | ||
} | ||
|
||
// enableSeDebugPrivilege enables the SeDebugPrivilege if it is present in | ||
// the process's token. | ||
func enableSeDebugPrivilege() error { | ||
self, err := syscall.GetCurrentProcess() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var token syscall.Token | ||
err = syscall.OpenProcessToken(self, syscall.TOKEN_QUERY|syscall.TOKEN_ADJUST_PRIVILEGES, &token) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = windows.EnableTokenPrivileges(token, windows.SeDebugPrivilege); err != nil { | ||
return errors.Wrap(err, "EnableTokenPrivileges failed") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"@timestamp": "2017-10-12T08:05:34.853Z", | ||
"beat": { | ||
"hostname": "host.example.com", | ||
"name": "host.example.com" | ||
}, | ||
"metricset": { | ||
"module": "windows", | ||
"name": "service", | ||
"rtt": 115 | ||
}, | ||
"windows": { | ||
"service": { | ||
"display_name": "AllJoyn-Routerdienst", | ||
"name": "AJRouter", | ||
"start_type": "ServiceDemandStart", | ||
"state": "ServiceStopped" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
=== windows service MetricSet | ||
|
||
The `service` metricset of the Windows module reads the status for Windows | ||
Services. | ||
|
||
[float] | ||
=== Configuration | ||
|
||
[source,yaml] | ||
---- | ||
- module: windows | ||
metricsets: ["service"] | ||
period: 10s | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
- name: service | ||
type: group | ||
description: > | ||
`service` contains the status for windows services. | ||
fields: | ||
- name: uptime.ms | ||
type: long | ||
format: duration | ||
input_format: milliseconds | ||
description: > | ||
`uptime` contains the service uptime metric. | ||
|
||
- name: name | ||
type: keyword | ||
description: > | ||
The service name. | ||
|
||
- name: display_name | ||
type: keyword | ||
description: > | ||
The display name of the service. | ||
|
||
- name: start_type | ||
type: keyword | ||
description: > | ||
The start type of the service. | ||
The possible values are `ServiceAutoStart`, `ServiceBootStart`, `ServiceDemandStart`, `ServiceDisabled`, and `ServiceSystemStart`. | ||
|
||
- name: state | ||
type: keyword | ||
description: > | ||
The actual state of the service. | ||
The possible values are `ServiceContinuePending`, `ServicePausePending`, `ServicePaused`, `ServiceRunning`, `ServiceStartPending`, | ||
`ServiceStopPending`, and `ServiceStopped`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm thinking this should be
checkAndEnableSeDebugPrivilege()
and a separate method namedCheckAndEnableSeDebugPrivilege
exists that callscheckAndEnableSeDebugPrivilege()
only once by usingsync.Once
.This way only the system module or the windows module will do the initialization, but not both.