Skip to content

Commit

Permalink
Merge pull request #8 from cloudradar-monitoring/perf_counters_windows
Browse files Browse the repository at this point in the history
Windows: Perfcounters & Free memory
  • Loading branch information
soupdiver authored Oct 22, 2018
2 parents dc50cc4 + 801ad00 commit 797b6ec
Show file tree
Hide file tree
Showing 8 changed files with 1,189 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mem.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !windows

package cagent

import (
Expand Down
56 changes: 56 additions & 0 deletions mem_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// +build windows

package cagent

import (
"context"
"errors"
"strings"
"time"

"github.com/shirou/gopsutil/mem"
log "github.com/sirupsen/logrus"
"github.com/cloudradar-monitoring/cagent/perfcounters"
)

const memGetTimeout = time.Second * 10
var watcher = perfcounters.Watcher()

func (ca *Cagent) MemResults() (MeasurementsMap, error) {
results := MeasurementsMap{}

var errs []string
ctx, cancel := context.WithTimeout(context.Background(), memGetTimeout)
defer cancel()

memStat, err := mem.VirtualMemoryWithContext(ctx)

results["free_B"] = nil
results["shared_B"] = nil
results["buff_B"] = nil

if err != nil {
log.Errorf("[MEM] Failed to get virtual memory stat: %s", err.Error())
errs = append(errs, err.Error())
results["total_B"] = nil
results["used_B"] = nil
results["available_B"] = nil
} else {
results["total_B"] = memStat.Total
results["used_B"] = memStat.Used
results["available_B"] = memStat.Available
}

free, err := watcher.Query(`\Memory\Free & Zero Page List Bytes`, "*")
if err != nil {
log.Errorf("[MEM] Failed to get free memory: %s", err.Error())
} else {
results["free_B"] = free
}

if len(errs) == 0 {
return results, nil
}

return results, errors.New("MEM: " + strings.Join(errs, "; "))
}
73 changes: 73 additions & 0 deletions perfcounters/kernel32.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2010 The win Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The names of the authors may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// This is the official list of 'win' authors for copyright purposes.
//
// Alexander Neumann <[email protected]>
// Joseph Watson <[email protected]>
// Kevin Pors <[email protected]>

// +build windows

package perfcounters

import (
"syscall"
)

type SYSTEMTIME struct {
wYear uint16
wMonth uint16
wDayOfWeek uint16
wDay uint16
wHour uint16
wMinute uint16
wSecond uint16
wMilliseconds uint16
}

type FILETIME struct {
dwLowDateTime uint32
dwHighDateTime uint32
}

var (
// Library
libkrnDll *syscall.DLL

// Functions
krn_FileTimeToSystemTime *syscall.Proc
krn_FileTimeToLocalFileTime *syscall.Proc
krn_LocalFileTimeToFileTime *syscall.Proc
krn_WideCharToMultiByte *syscall.Proc
)

func init() {
libkrnDll = syscall.MustLoadDLL("Kernel32.dll")

krn_FileTimeToSystemTime = libkrnDll.MustFindProc("FileTimeToSystemTime")
krn_FileTimeToLocalFileTime = libkrnDll.MustFindProc("FileTimeToLocalFileTime")
krn_LocalFileTimeToFileTime = libkrnDll.MustFindProc("LocalFileTimeToFileTime")
krn_WideCharToMultiByte = libkrnDll.MustFindProc("WideCharToMultiByte")
}
Loading

0 comments on commit 797b6ec

Please sign in to comment.