Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #9 from cloudfoundry-incubator/master

Add windows version
  • Loading branch information...
commit a4cb00834887e549c46e58cb5d5f4e53bd492f32 2 parents 3641d81 + 7f596b5
Max Brunsfeld maxbrunsfeld authored

Showing 3 changed files with 134 additions and 2 deletions. Show diff stats Hide diff stats

  1. +2 2 sigar_unix.go
  2. +100 0 sigar_windows.go
  3. +32 0 sigar_windows_test.go
4 sigar_unix.go
... ... @@ -1,9 +1,9 @@
1 1 // Copyright (c) 2012 VMware, Inc.
2 2
3   -package sigar
4   -
5 3 // +build darwin freebsd linux netbsd openbsd
6 4
  5 +package sigar
  6 +
7 7 import "syscall"
8 8
9 9 func (self *FileSystemUsage) Get(path string) error {
100 sigar_windows.go
... ... @@ -0,0 +1,100 @@
  1 +// Copyright (c) 2012 VMware, Inc.
  2 +
  3 +package sigar
  4 +
  5 +// #include <stdlib.h>
  6 +// #include <windows.h>
  7 +import "C"
  8 +
  9 +import (
  10 + "fmt"
  11 + "unsafe"
  12 +)
  13 +
  14 +func init() {
  15 +}
  16 +
  17 +func (self *LoadAverage) Get() error {
  18 + return nil
  19 +}
  20 +
  21 +func (self *Uptime) Get() error {
  22 + return nil
  23 +}
  24 +
  25 +func (self *Mem) Get() error {
  26 + var statex C.MEMORYSTATUSEX
  27 + statex.dwLength = C.DWORD(unsafe.Sizeof(statex))
  28 +
  29 + succeeded := C.GlobalMemoryStatusEx(&statex)
  30 + if succeeded == C.FALSE {
  31 + lastError := C.GetLastError()
  32 + return fmt.Errorf("GlobalMemoryStatusEx failed with error: %d", int(lastError))
  33 + }
  34 +
  35 + self.Total = uint64(statex.ullTotalPhys)
  36 + return nil
  37 +}
  38 +
  39 +func (self *Swap) Get() error {
  40 + return notImplemented()
  41 +}
  42 +
  43 +func (self *Cpu) Get() error {
  44 + return notImplemented()
  45 +}
  46 +
  47 +func (self *CpuList) Get() error {
  48 + return notImplemented()
  49 +}
  50 +
  51 +func (self *FileSystemList) Get() error {
  52 + return notImplemented()
  53 +}
  54 +
  55 +func (self *ProcList) Get() error {
  56 + return notImplemented()
  57 +}
  58 +
  59 +func (self *ProcState) Get(pid int) error {
  60 + return notImplemented()
  61 +}
  62 +
  63 +func (self *ProcMem) Get(pid int) error {
  64 + return notImplemented()
  65 +}
  66 +
  67 +func (self *ProcTime) Get(pid int) error {
  68 + return notImplemented()
  69 +}
  70 +
  71 +func (self *ProcArgs) Get(pid int) error {
  72 + return notImplemented()
  73 +}
  74 +
  75 +func (self *ProcExe) Get(pid int) error {
  76 + return notImplemented()
  77 +}
  78 +
  79 +func (self *FileSystemUsage) Get(path string) error {
  80 + var availableBytes C.ULARGE_INTEGER
  81 + var totalBytes C.ULARGE_INTEGER
  82 + var totalFreeBytes C.ULARGE_INTEGER
  83 +
  84 + pathChars := C.CString(path)
  85 + defer C.free(unsafe.Pointer(pathChars))
  86 +
  87 + succeeded := C.GetDiskFreeSpaceEx((*C.CHAR)(pathChars), &availableBytes, &totalBytes, &totalFreeBytes)
  88 + if succeeded == C.FALSE {
  89 + lastError := C.GetLastError()
  90 + return fmt.Errorf("GetDiskFreeSpaceEx failed with error: %d", int(lastError))
  91 + }
  92 +
  93 + self.Total = *(*uint64)(unsafe.Pointer(&totalBytes))
  94 + return nil
  95 +}
  96 +
  97 +func notImplemented() error {
  98 + panic("Not Implemented")
  99 + return nil
  100 +}
32 sigar_windows_test.go
... ... @@ -0,0 +1,32 @@
  1 +package sigar_test
  2 +
  3 +import (
  4 + "os"
  5 +
  6 + . "github.com/onsi/ginkgo"
  7 + . "github.com/onsi/gomega"
  8 +
  9 + sigar "github.com/cloudfoundry/gosigar"
  10 +)
  11 +
  12 +var _ = Describe("SigarWindows", func() {
  13 + Describe("Memory", func() {
  14 + It("gets the total memory", func() {
  15 + mem := sigar.Mem{}
  16 + err := mem.Get()
  17 +
  18 + Ω(err).ShouldNot(HaveOccurred())
  19 + Ω(mem.Total).Should(BeNumerically(">", 0))
  20 + })
  21 + })
  22 +
  23 + Describe("Disk", func() {
  24 + It("gets the total disk space", func() {
  25 + usage := sigar.FileSystemUsage{}
  26 + err := usage.Get(os.TempDir())
  27 +
  28 + Ω(err).ShouldNot(HaveOccurred())
  29 + Ω(usage.Total).Should(BeNumerically(">", 0))
  30 + })
  31 + })
  32 +})

0 comments on commit a4cb008

Please sign in to comment.
Something went wrong with that request. Please try again.