Skip to content
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
merged 60 commits into from
Nov 2, 2017
Merged
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
1ef849c
Create new metricset
martinscholz83 Sep 20, 2017
d414d86
Add Close function for handler
martinscholz83 Sep 26, 2017
b9878d7
Add description
martinscholz83 Sep 27, 2017
d7d0aa1
make update
martinscholz83 Sep 27, 2017
a5bd839
Add service start types
martinscholz83 Oct 2, 2017
65ea2f7
Add service access rights
martinscholz83 Oct 2, 2017
a0c7f13
Add OpenService function
martinscholz83 Oct 2, 2017
d5be9ed
Add more funtions
martinscholz83 Oct 2, 2017
8fea616
Add errors
martinscholz83 Oct 2, 2017
6fd5d02
Query service config
martinscholz83 Oct 2, 2017
eb3ecd2
Correct return type for QueryServiceConfig
martinscholz83 Oct 5, 2017
73ef011
Remove services config
martinscholz83 Oct 5, 2017
3fe19b7
Add process access rights
martinscholz83 Oct 5, 2017
51f64db
Add process sycall functions
martinscholz83 Oct 5, 2017
ff57f2b
remove process functions
martinscholz83 Oct 5, 2017
df94026
Share run.go for all windows metricsets
martinscholz83 Oct 20, 2017
ae9de49
Cleanup code
martinscholz83 Oct 20, 2017
6ade0fc
Break into smaller functions
martinscholz83 Oct 20, 2017
49430ff
Refactor getting uptime
martinscholz83 Oct 20, 2017
e3b71ce
Calculate service uptime
martinscholz83 Oct 20, 2017
78f4a34
Add TestData integration test
martinscholz83 Oct 23, 2017
8ffd2c6
Remove unnecessary types
martinscholz83 Oct 24, 2017
7dff37d
Add SeDebugPrivilege helper function
martinscholz83 Oct 24, 2017
ed94168
Add fields
martinscholz83 Oct 24, 2017
239d774
Change uptime format
martinscholz83 Oct 24, 2017
8f0dcc6
Move uptime logic into helper
martinscholz83 Oct 24, 2017
89f6ce8
Change return type for service uptime
martinscholz83 Oct 24, 2017
076e376
Change uptime to time.Duration
martinscholz83 Oct 24, 2017
b0dde4e
Refactor getServiceStates
martinscholz83 Oct 24, 2017
999e2b5
Write data.json
martinscholz83 Oct 24, 2017
088389f
Refactor uptime calculating
martinscholz83 Oct 25, 2017
63df59e
Remove new lines
martinscholz83 Oct 25, 2017
75958f9
Remove commented out code
martinscholz83 Oct 25, 2017
57a47a3
Call privilege func once
martinscholz83 Oct 25, 2017
570d828
make update
martinscholz83 Oct 25, 2017
2f98979
make fmt
martinscholz83 Oct 25, 2017
8577fc4
Reorder imports
martinscholz83 Oct 25, 2017
8654ee8
Add miising return value for CheckAndEnableSeDebugPrivilege()
martinscholz83 Oct 25, 2017
fa8ef48
remove named parameter
martinscholz83 Oct 25, 2017
120c8f8
Format fields
martinscholz83 Oct 25, 2017
2a172bb
Remove uptime conversion in helper func
martinscholz83 Oct 25, 2017
e534732
Set oxford comma for lists
martinscholz83 Oct 25, 2017
e504e6f
Only add uptime to event if its not 0
martinscholz83 Oct 25, 2017
884212e
Fix adding uptime to event
martinscholz83 Oct 25, 2017
0489cf8
Update data.json
martinscholz83 Oct 25, 2017
63d735b
Update Makefile
martinscholz83 Oct 26, 2017
4ff7381
Fix formatting
martinscholz83 Oct 26, 2017
12b78b2
make fmt
martinscholz83 Oct 27, 2017
55751ae
make update
martinscholz83 Oct 27, 2017
4eb1120
Build only on windows
martinscholz83 Oct 27, 2017
99fbfd1
Add integration test
martinscholz83 Oct 27, 2017
563f487
Refactor getServiceStates
andrewkroh Oct 27, 2017
9d83f1e
Fix const Running
martinscholz83 Nov 2, 2017
aecd50b
Rename service metricset
martinscholz83 Nov 2, 2017
e9409e8
Rename json output
martinscholz83 Nov 2, 2017
17ac114
make update
martinscholz83 Nov 2, 2017
cb4cf55
go generate
martinscholz83 Nov 2, 2017
e743b68
go test -data
martinscholz83 Nov 2, 2017
5ca5ebe
Fix typo
martinscholz83 Nov 2, 2017
b98d69b
make update
martinscholz83 Nov 2, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor getting uptime
  • Loading branch information
martinscholz83 committed Oct 26, 2017
commit 49430ff1718df924de36cbcb23cb0a7db6045b74
43 changes: 11 additions & 32 deletions metricbeat/module/windows/services/services_windows.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ import (
"unicode/utf16"
"unsafe"

gosigar "github.com/elastic/gosigar/sys/windows"
gosigar "github.com/elastic/gosigar"
"github.com/pkg/errors"
"golang.org/x/sys/windows"

@@ -71,6 +71,7 @@ type ServiceStatus struct {
ServiceName string
CurrentState string
StartType string
Uptime uint64
}

type ServiceReader struct {
@@ -139,7 +140,7 @@ func getServiceStates(handle ServiceDatabaseHandle, state ServiceEnumState) ([]S
}
bufSize += bytesNeeded
servicesBuffer := make([]byte, bytesNeeded)
lastOffset = uintptr(len(servicesBuffer)) - 1
lastOffset = uintptr(len(servicesBuffer))

// This loop should not repeat more then two times
for {
@@ -193,9 +194,11 @@ func getServiceStates(handle ServiceDatabaseHandle, state ServiceEnumState) ([]S

//Get uptime for service
if ServiceState(serviceTemp.ServiceStatusProcess.DwCurrentState) != ServiceStopped {
if err := getServiceUptime(serviceTemp.ServiceStatusProcess.DwProcessId); err != nil {
processTime, err := getServiceUptime(serviceTemp.ServiceStatusProcess.DwProcessId)
if err != nil {
return nil, err
}
service.Uptime = processTime.Total
}

services = append(services, service)
@@ -250,39 +253,15 @@ func getDetailedServiceInfo(handle ServiceDatabaseHandle, serviceName string, ac
return nil
}

func getServiceUptime(processId uint32) error {
var processCreationTime syscall.Filetime
var processExitTime syscall.Filetime
var processKernelTime syscall.Filetime
var processUserTime syscall.Filetime
func getServiceUptime(processId uint32) (gosigar.ProcTime, error) {
var processCreationTime gosigar.ProcTime

// Enable SeDebugPrivilege to opne processes

var token syscall.Token

currentProcess, err := syscall.GetCurrentProcess()
err := processCreationTime.Get(int(processId))
if err != nil {
return err
}

if err := syscall.OpenProcessToken(currentProcess, syscall.TOKEN_ADJUST_PRIVILEGES, &token); err != nil {
return err
}

if err := gosigar.EnableTokenPrivileges(token, gosigar.SeDebugPrivilege); err != nil {
return err
return processCreationTime, err
}

processHandle, err := syscall.OpenProcess(uint32(ProcessAllAccess), false, processId)
if err != nil {
return err
}

if err := syscall.GetProcessTimes(processHandle, &processCreationTime, &processExitTime, &processKernelTime, &processUserTime); err != nil {
return err
}

return nil
return processCreationTime, nil
}

func (reader *ServiceReader) Close() error {