forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add windows service name lookup to procstat input (influxdata#4811)
- Loading branch information
Showing
4 changed files
with
87 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// +build !windows | ||
|
||
package procstat | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func queryPidWithWinServiceName(winServiceName string) (uint32, error) { | ||
return 0, fmt.Errorf("os not support win_service option") | ||
} |
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,49 @@ | ||
// +build windows | ||
|
||
package procstat | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
"golang.org/x/sys/windows/svc/mgr" | ||
) | ||
|
||
func getService(name string) (*mgr.Service, error) { | ||
m, err := mgr.Connect() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer m.Disconnect() | ||
|
||
srv, err := m.OpenService(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return srv, nil | ||
} | ||
|
||
func queryPidWithWinServiceName(winServiceName string) (uint32, error) { | ||
|
||
srv, err := getService(winServiceName) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
var p *windows.SERVICE_STATUS_PROCESS | ||
var bytesNeeded uint32 | ||
var buf []byte | ||
|
||
if err := windows.QueryServiceStatusEx(srv.Handle, windows.SC_STATUS_PROCESS_INFO, nil, 0, &bytesNeeded); err != windows.ERROR_INSUFFICIENT_BUFFER { | ||
return 0, err | ||
} | ||
|
||
buf = make([]byte, bytesNeeded) | ||
p = (*windows.SERVICE_STATUS_PROCESS)(unsafe.Pointer(&buf[0])) | ||
if err := windows.QueryServiceStatusEx(srv.Handle, windows.SC_STATUS_PROCESS_INFO, &buf[0], uint32(len(buf)), &bytesNeeded); err != nil { | ||
return 0, err | ||
} | ||
|
||
return p.ProcessId, nil | ||
} |