コード例 #1
0
ファイル: cpuusage.go プロジェクト: lucasgalton/sycub-agent
// NewCPUUsageGenerator is set up windows api
func NewCPUUsageGenerator() (*CPUUsageGenerator, error) {
	g := &CPUUsageGenerator{0, nil}

	var err error
	g.query, err = windows.CreateQuery()
	if err != nil {
		cpuUsageLogger.Criticalf(err.Error())
		return nil, err
	}
	var counter *windows.CounterInfo

	counter, err = windows.CreateCounter(g.query, "cpu.user.percentage", `\Processor(_Total)\% User Time`)
	if err != nil {
		cpuUsageLogger.Criticalf(err.Error())
		return nil, err
	}
	g.counters = append(g.counters, counter)

	counter, err = windows.CreateCounter(g.query, "cpu.system.percentage", `\Processor(_Total)\% Privileged Time`)
	if err != nil {
		cpuUsageLogger.Criticalf(err.Error())
		return nil, err
	}
	g.counters = append(g.counters, counter)

	counter, err = windows.CreateCounter(g.query, "cpu.idle.percentage", `\Processor(_Total)\% Idle Time`)
	if err != nil {
		cpuUsageLogger.Criticalf(err.Error())
		return nil, err
	}
	g.counters = append(g.counters, counter)
	return g, nil
}
コード例 #2
0
// NewProcessorQueueLengthGenerator is set up windows api
func NewProcessorQueueLengthGenerator() (*ProcessorQueueLengthGenerator, error) {
	g := &ProcessorQueueLengthGenerator{0, nil}

	var err error
	g.query, err = windows.CreateQuery()
	if err != nil {
		processorQueueLengthLogger.Criticalf(err.Error())
		return nil, err
	}

	counter, err := windows.CreateCounter(g.query, "processor_queue_length", `\System\Processor Queue Length`)
	if err != nil {
		processorQueueLengthLogger.Criticalf(err.Error())
		return nil, err
	}
	g.counters = append(g.counters, counter)
	return g, nil
}
コード例 #3
0
ファイル: interface.go プロジェクト: lucasgalton/sycub-agent
// NewInterfaceGenerator XXX
func NewInterfaceGenerator(interval time.Duration) (*InterfaceGenerator, error) {
	g := &InterfaceGenerator{interval, 0, nil}

	var err error
	g.query, err = windows.CreateQuery()
	if err != nil {
		interfaceLogger.Criticalf(err.Error())
		return nil, err
	}

	ifs, err := net.Interfaces()
	if err != nil {
		interfaceLogger.Criticalf(err.Error())
		return nil, err
	}

	ai, err := windows.GetAdapterList()
	if err != nil {
		interfaceLogger.Criticalf(err.Error())
		return nil, err
	}

	for _, ifi := range ifs {
		for ; ai != nil; ai = ai.Next {
			if ifi.Index == int(ai.Index) {
				name := windows.BytePtrToString(&ai.Description[0])
				name = strings.Replace(name, "(", "[", -1)
				name = strings.Replace(name, ")", "]", -1)
				name = strings.Replace(name, "#", "_", -1)
				var counter *windows.CounterInfo

				counter, err = windows.CreateCounter(
					g.query,
					fmt.Sprintf(`interface.nic%d.rxBytes.delta`, ifi.Index),
					fmt.Sprintf(`\Network Interface(%s)\Bytes Received/sec`, name))
				if err != nil {
					interfaceLogger.Criticalf(err.Error())
					return nil, err
				}
				g.counters = append(g.counters, counter)
				counter, err = windows.CreateCounter(
					g.query,
					fmt.Sprintf(`interface.nic%d.txBytes.delta`, ifi.Index),
					fmt.Sprintf(`\Network Interface(%s)\Bytes Sent/sec`, name))
				if err != nil {
					interfaceLogger.Criticalf(err.Error())
					return nil, err
				}
				g.counters = append(g.counters, counter)
			}
		}
	}

	r, _, err := windows.PdhCollectQueryData.Call(uintptr(g.query))
	if r != 0 && err != nil {
		if r == windows.PDH_NO_DATA {
			interfaceLogger.Infof("this metric has not data. ")
			return nil, err
		}
		interfaceLogger.Criticalf(err.Error())
		return nil, err
	}

	return g, nil
}
コード例 #4
0
ファイル: disk.go プロジェクト: lucasgalton/sycub-agent
// NewDiskGenerator XXX
func NewDiskGenerator(interval time.Duration) (*DiskGenerator, error) {
	g := &DiskGenerator{interval, 0, nil}

	var err error
	g.query, err = windows.CreateQuery()
	if err != nil {
		diskLogger.Criticalf(err.Error())
		return nil, err
	}

	drivebuf := make([]byte, 256)
	r, _, err := windows.GetLogicalDriveStrings.Call(
		uintptr(len(drivebuf)),
		uintptr(unsafe.Pointer(&drivebuf[0])))

	if r == 0 {
		diskLogger.Criticalf(err.Error())
		return nil, err
	}

	for _, v := range drivebuf {
		if v >= 65 && v <= 90 {
			drive := string(v)
			r, _, err = windows.GetDriveType.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(drive + `:\`))))
			if r != windows.DRIVE_FIXED {
				continue
			}
			var counter *windows.CounterInfo

			counter, err = windows.CreateCounter(
				g.query,
				fmt.Sprintf(`disk.%s.reads.delta`, drive),
				fmt.Sprintf(`\PhysicalDisk(0 %s:)\Disk Reads/sec`, drive))
			if err != nil {
				diskLogger.Criticalf(err.Error())
				return nil, err
			}
			g.counters = append(g.counters, counter)

			counter, err = windows.CreateCounter(
				g.query,
				fmt.Sprintf(`disk.%s.writes.delta`, drive),
				fmt.Sprintf(`\PhysicalDisk(0 %s:)\Disk Writes/sec`, drive))
			if err != nil {
				diskLogger.Criticalf(err.Error())
				return nil, err
			}
			g.counters = append(g.counters, counter)
		}
	}

	r, _, err = windows.PdhCollectQueryData.Call(uintptr(g.query))
	if r != 0 && err != nil {
		if r == windows.PDH_NO_DATA {
			diskLogger.Infof("this metric has not data. ")
			return nil, err
		}
		diskLogger.Criticalf(err.Error())
		return nil, err
	}

	return g, nil
}