-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from madhanrm/monitor
Adding Monitor and Events
- Loading branch information
Showing
147 changed files
with
2,744 additions
and
21,729 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// +build windows | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package event | ||
|
||
import ( | ||
"github.com/microsoft/wmi/pkg/base/session" | ||
wmi "github.com/microsoft/wmi/pkg/wmiinstance" | ||
) | ||
|
||
type CallbackContext struct { | ||
callbackData string | ||
callback func(*wmi.WmiInstance, string) | ||
} | ||
|
||
// NewCallbackContext | ||
func NewCallbackContext(cb func(*wmi.WmiInstance, string), data string) *CallbackContext { | ||
return &CallbackContext{ | ||
callback: cb, | ||
callbackData: data, | ||
} | ||
} | ||
|
||
// Execute the callback | ||
func (cb *CallbackContext) Execute(instance *wmi.WmiInstance) { | ||
if cb.callback == nil { | ||
return | ||
} | ||
cb.callback(instance, cb.callbackData) | ||
} | ||
|
||
func onInstanceReady(ctx interface{}, wmiInstances []*wmi.WmiInstance) { | ||
context := ctx.(*CallbackContext) | ||
if context == nil { | ||
return | ||
} | ||
|
||
if len(wmiInstances) < 1 { | ||
return | ||
} | ||
|
||
context.Execute(wmiInstances[0]) | ||
} | ||
|
||
func onCompleted(ctx interface{}, wmiInstances []*wmi.WmiInstance) { | ||
// Not used | ||
} | ||
|
||
func onProgress(ctx interface{}, wmiInstances []*wmi.WmiInstance) { | ||
// Not used | ||
} | ||
|
||
func onInstancePut(ctx interface{}, wmiInstances []*wmi.WmiInstance) { | ||
// Not used | ||
} | ||
|
||
// RegisterWmiCallback | ||
func RegisterWmiCallback(context *CallbackContext, wmiNamespace, hostName, queryString string) (eventSink *wmi.WmiEventSink, err error) { | ||
wmiSession, err := session.GetSession(wmiNamespace, hostName, "", "", "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
eventSink, err = wmi.CreateWmiEventSink(wmiSession, context, onInstanceReady, onCompleted, onProgress, onInstancePut) | ||
if err != nil { | ||
return | ||
} | ||
defer func() { | ||
if err != nil { | ||
eventSink.Close() | ||
eventSink = nil | ||
} | ||
}() | ||
|
||
_, err = eventSink.Connect() | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, err = wmiSession.ExecNotificationQueryAsync(eventSink, queryString) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} |
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,101 @@ | ||
// +build windows | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package monitor | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/microsoft/wmi/pkg/base/event" | ||
"github.com/microsoft/wmi/pkg/base/query" | ||
"github.com/microsoft/wmi/pkg/constant" | ||
wmi "github.com/microsoft/wmi/pkg/wmiinstance" | ||
) | ||
|
||
// Monitor is a generic monitor to subscribe to Wmi Events based on a Query String | ||
type Monitor struct { | ||
mux sync.Mutex | ||
// eventSinks for each of the entities that are being monitored here | ||
// multiple event sinks can be setup for a single entity | ||
eventSinks map[string][]*wmi.WmiEventSink | ||
propertyNameToQuery string | ||
wmiNamespaceName string | ||
callbackContext interface{} | ||
callbackFunction func(interface{}, string) | ||
} | ||
|
||
func (c *Monitor) onModified(instance *wmi.WmiInstance, cbData string) { | ||
/* | ||
prop, err := instance.GetProperty(c.propertyNameToQuery) | ||
if err != nil { | ||
fmt.Printf("Err: %v - [%s]\n", err, c.propertyNameToQuery) | ||
return | ||
} | ||
propVal, ok := prop.(string) | ||
if !ok { | ||
return | ||
} | ||
*/ | ||
c.callbackFunction(c.callbackContext, cbData) | ||
return | ||
} | ||
|
||
// CreateMonitor createa a new Monitor | ||
func CreateMonitor(wmiNamespace string, callbackContext interface{}, | ||
callback func(interface{}, string)) *Monitor { | ||
return &Monitor{ | ||
wmiNamespaceName: wmiNamespace, | ||
eventSinks: map[string][]*wmi.WmiEventSink{}, | ||
callbackFunction: callback, | ||
callbackContext: callbackContext, | ||
} | ||
} | ||
|
||
// AddEntityWithFilter would add the entity to be monitored for changes | ||
func (c *Monitor) AddEntityWithFilter(entityName, wqlQueryString string, filters query.WmiQueryFilterCollection) (err error) { | ||
if _, ok := c.eventSinks[entityName]; ok { | ||
return nil // error | ||
} | ||
qString := fmt.Sprintf("%s %s", wqlQueryString, filters.String()) | ||
fmt.Printf("Event Query [%s]\n", qString) | ||
esink, err := c.getEventSink(entityName, qString) | ||
if err != nil { | ||
return | ||
} | ||
c.mux.Lock() | ||
defer c.mux.Unlock() | ||
c.eventSinks[entityName] = append(c.eventSinks[entityName], esink) | ||
return | ||
} | ||
|
||
// RemoveEntity to remove the entity being monitored for changes | ||
func (c *Monitor) RemoveEntity(entityName string) (err error) { | ||
if _, ok := c.eventSinks[entityName]; !ok { | ||
return nil // error NOT Found | ||
} | ||
c.mux.Lock() | ||
defer c.mux.Unlock() | ||
|
||
delete(c.eventSinks, entityName) | ||
return | ||
} | ||
|
||
// Close the monitor | ||
func (c *Monitor) Close() error { | ||
for k := range c.eventSinks { | ||
for _, s := range c.eventSinks[k] { | ||
s.Close() | ||
} | ||
delete(c.eventSinks, k) | ||
} | ||
return nil | ||
} | ||
|
||
// getEventSink | ||
func (c *Monitor) getEventSink(entityName, wqlQueryString string) (*wmi.WmiEventSink, error) { | ||
//qString := fmt.Sprintf("%s %s", queryString, additionalFilter) | ||
context := event.NewCallbackContext(c.onModified, entityName) | ||
return event.RegisterWmiCallback(context, c.wmiNamespaceName, constant.HostName, wqlQueryString) | ||
} |
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,17 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package constant | ||
|
||
type WMINamespace string | ||
|
||
const ( | ||
Virtualization WMINamespace = "root/virtualization/v2" | ||
CimV2 WMINamespace = "root/cimv2" | ||
StadardCimV2 WMINamespace = "root/standardcimv2" | ||
FailoverCluster WMINamespace = "root/mscluster" | ||
) | ||
|
||
const ( | ||
HostName string = "localhost" | ||
) |
Oops, something went wrong.