-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (101 loc) · 3.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"runtime"
"time"
"github.com/StackExchange/wmi"
)
// https://docs.microsoft.com/en-us/previous-versions/windows/desktop/eventlogprov/win32-ntlogevent
type Win32_NTLogEvent struct {
Category uint16
CategoryString string
ComputerName string
Data []uint8
EventCode uint16
EventIdentifier uint32
EventType uint8
InsertionStrings []string
Logfile string
Message string
RecordNumber uint32
SourceName string
TimeGenerated time.Time
TimeWritten time.Time
Type string
User string
}
func main() {
// This initialization prevents a memory leak on WMF 5+. See
// https://github.com/martinlindhe/wmi_exporter/issues/77 and linked issues
// for details.
// https://github.com/StackExchange/wmi/issues/27#issuecomment-309578576
// https://github.com/bosun-monitor/bosun/pull/2028/files#diff-a6d7a21df96534b54447f5b1a8936f35e642cacad4a41f911e37a12dc2852e20R17-R23
//fmt.Println("Initializing SWbemServices")
s, err := wmi.InitializeSWbemServices(wmi.DefaultClient)
if err != nil {
return
}
wmi.DefaultClient.SWbemServicesClient = s
for {
checkLog()
PrintMemUsage()
time.Sleep(250 * time.Millisecond)
}
}
func checkLog() (map[string][]*Win32_NTLogEvent, error) {
logfiles := []string{
"Application",
"System",
}
now := time.Now().UTC()
now = now.Add((3600 * time.Second) * -1)
wmidate := now.Format("20060102150405.000000-070")
var dst []Win32_NTLogEvent
var sql string
//var eventBuffer map[string][]*Win32_NTLogEvent
eventBuffer := make(map[string][]*Win32_NTLogEvent)
for _, logfile := range logfiles {
sql = fmt.Sprintf("SELECT * FROM Win32_NTLogEvent WHERE Logfile='%v' AND TimeWritten >= '%v'", logfile, wmidate)
//fmt.Println(sql)
err := wmi.Query(sql, &dst)
if err != nil {
fmt.Println("Event Log error: ", err)
continue
}
for _, event := range dst {
// Resolve Memory Leak
eventBuffer[logfile] = append(eventBuffer[logfile], &Win32_NTLogEvent{
Category: event.Category,
CategoryString: event.CategoryString,
ComputerName: event.ComputerName,
Data: event.Data,
EventCode: event.EventCode,
EventIdentifier: event.EventIdentifier,
EventType: event.EventType,
InsertionStrings: event.InsertionStrings,
Logfile: event.Logfile,
Message: event.Message,
RecordNumber: event.RecordNumber,
SourceName: event.SourceName,
TimeGenerated: event.TimeGenerated,
TimeWritten: event.TimeWritten,
Type: event.Type,
User: event.User,
})
}
}
return eventBuffer, nil
}
// Credit to: https://golangcode.com/print-the-current-memory-usage/
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}