<commit_before>package collectors

import (
	"github.com/StackExchange/tcollector/opentsdb"
	"github.com/StackExchange/wmi"
	"regexp"
)

func init() {
	collectors = append(collectors, c_cpu_windows)
	collectors = append(collectors, c_network_windows)
	collectors = append(collectors, c_physical_disk_windows)
	collectors = append(collectors, c_simple_mem_windows)
}

const CPU_QUERY = `
	SELECT Name, PercentPrivilegedTime, PercentInterruptTime, PercentUserTime
	FROM Win32_PerfRawData_PerfOS_Processor
	WHERE Name <> '_Total'
`

//KMB Moving Excludes to Go, we didn't notice the WHERE changing performance much and a regex
//is easier than building the WHERE string
const NETWORK_QUERY = `
	SELECT Name, BytesReceivedPerSec, BytesSentPerSec,
		PacketsReceivedPerSec, PacketsSentPerSec,
		PacketsOutboundDiscarded, PacketsOutboundErrors,
		PacketsReceivedDiscarded, PacketsReceivedErrors
	FROM Win32_PerfRawData_Tcpip_NetworkInterface
`
const INTERFACE_EXCLUSIONS = `isatap|Teredo`

const PHYSICAL_DISK_QUERY = `
	SELECT Name, AvgDisksecPerRead, AvgDisksecPerWrite,
		AvgDiskReadQueueLength, AvgDiskWriteQueueLength, 
		DiskReadBytesPersec, DiskReadsPersec,
		DiskWriteBytesPersec, DiskWritesPersec, 
		SplitIOPerSec, PercentDiskReadTime, PercentDiskWriteTime
	FROM Win32_PerfRawData_PerfDisk_PhysicalDisk
`

//Memory Needs to be expanded upon, Should be deeper in Utilization (What is Cache etc) 
//as well as Saturation (i.e. Paging Activity). Lot of that is in Win32_PerfRawData_PerfOS_Memory

//Win32_Operating_System's units are KBytes
const SIMPLE_MEMORY_QUERY =  `
	SELECT FreePhysicalMemory, FreeVirtualMemory,
	TotalVisibleMemorySize, TotalVirtualMemorySize
	FROM Win32_OperatingSystem
`

func c_cpu_windows() opentsdb.MultiDataPoint {
	var dst []wmi.Win32_PerfRawData_PerfOS_Processor
	err := wmi.Query(CPU_QUERY, &dst)
	if err != nil {
		l.Println("cpu:", err)
		return nil
	}
	var md opentsdb.MultiDataPoint
	for _, v := range dst {
		Add(&md, "cpu.time", v.PercentPrivilegedTime, opentsdb.TagSet{"cpu": v.Name, "type": "privileged"})
		Add(&md, "cpu.time", v.PercentInterruptTime, opentsdb.TagSet{"cpu": v.Name, "type": "interrupt"})
		Add(&md, "cpu.time", v.PercentUserTime, opentsdb.TagSet{"cpu": v.Name, "type": "user"})
	}
	return md
}

func c_simple_mem_windows() opentsdb.MultiDataPoint {
	var dst []wmi.Win32_OperatingSystem
	err := wmi.Query(SIMPLE_MEMORY_QUERY, &dst)
	if err != nil {
		l.Println("simple_mem:", err)
		return nil
	}
	var md opentsdb.MultiDataPoint
	for _, v := range dst {
		Add(&md, "mem.virtual.total", v.TotalVirtualMemorySize * 1024, opentsdb.TagSet{})
		Add(&md, "mem.virtual.free", v.FreeVirtualMemory * 1024, opentsdb.TagSet{})
		Add(&md, "mem.physical.total", v.TotalVisibleMemorySize * 1024, opentsdb.TagSet{})
		Add(&md, "mem.physical.free", v.FreePhysicalMemory * 1024, opentsdb.TagSet{})
	}
	return md
}

func c_physical_disk_windows() opentsdb.MultiDataPoint {
	var dst []wmi.Win32_PerfRawData_PerfDisk_PhysicalDisk
	err := wmi.Query(PHYSICAL_DISK_QUERY, &dst)
	if err != nil {
		l.Println("disk_physical:", err)
		return nil
	}
	var md opentsdb.MultiDataPoint
	for _, v := range dst {
		Add(&md, "disk.physical.duration", v.AvgDiskSecPerRead, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.duration", v.AvgDiskSecPerWrite, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.queue", v.AvgDiskReadQueueLength, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.queue", v.AvgDiskWriteQueueLength, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.ops", v.DiskReadsPerSec, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.ops", v.DiskWritesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.bytes", v.DiskReadBytesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.bytes", v.DiskWriteBytesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.percenttime", v.PercentDiskReadTime, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.percenttime", v.PercentDiskWriteTime, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.spltio", v.SplitIOPerSec, opentsdb.TagSet{"disk": v.Name})
	}<commit_msg>cmd/scollector: Disk space utilization via wmi<commit_after>package collectors

import (
	"github.com/StackExchange/tcollector/opentsdb"
	"github.com/StackExchange/wmi"
	"regexp"
)

func init() {
	collectors = append(collectors, c_cpu_windows)
	collectors = append(collectors, c_network_windows)
	collectors = append(collectors, c_physical_disk_windows)
	collectors = append(collectors, c_simple_mem_windows)
	collectors = append(collectors, c_disk_space_windows)
}

const CPU_QUERY = `
	SELECT Name, PercentPrivilegedTime, PercentInterruptTime, PercentUserTime
	FROM Win32_PerfRawData_PerfOS_Processor
	WHERE Name <> '_Total'
`

//KMB Moving Excludes to Go, we didn't notice the WHERE changing performance much and a regex
//is easier than building the WHERE string
const NETWORK_QUERY = `
	SELECT Name, BytesReceivedPerSec, BytesSentPerSec,
		PacketsReceivedPerSec, PacketsSentPerSec,
		PacketsOutboundDiscarded, PacketsOutboundErrors,
		PacketsReceivedDiscarded, PacketsReceivedErrors
	FROM Win32_PerfRawData_Tcpip_NetworkInterface
`
const INTERFACE_EXCLUSIONS = `isatap|Teredo`

const PHYSICAL_DISK_QUERY = `
	SELECT Name, AvgDisksecPerRead, AvgDisksecPerWrite,
		AvgDiskReadQueueLength, AvgDiskWriteQueueLength, 
		DiskReadBytesPersec, DiskReadsPersec,
		DiskWriteBytesPersec, DiskWritesPersec, 
		SplitIOPerSec, PercentDiskReadTime, PercentDiskWriteTime
	FROM Win32_PerfRawData_PerfDisk_PhysicalDisk
	WHERE Name <> '_Total'
`

//Similar Breakdowns exist as to physical, but for now just using this for the space utilization 
const DISKSPACE_QUERY = `
	SELECT Name, FreeMegaBytes, PercentFreeSpace
	FROM Win32_PerfRawData_PerfDisk_LogicalDisk
	WHERE Name <> '_Total'
`

//Memory Needs to be expanded upon, Should be deeper in Utilization (What is Cache etc) 
//as well as Saturation (i.e. Paging Activity). Lot of that is in Win32_PerfRawData_PerfOS_Memory
//Win32_Operating_System's units are KBytes

const SIMPLE_MEMORY_QUERY =  `
	SELECT FreePhysicalMemory, FreeVirtualMemory,
	TotalVisibleMemorySize, TotalVirtualMemorySize
	FROM Win32_OperatingSystem
`

func c_cpu_windows() opentsdb.MultiDataPoint {
	var dst []wmi.Win32_PerfRawData_PerfOS_Processor
	err := wmi.Query(CPU_QUERY, &dst)
	if err != nil {
		l.Println("cpu:", err)
		return nil
	}
	var md opentsdb.MultiDataPoint
	for _, v := range dst {
		Add(&md, "cpu.time", v.PercentPrivilegedTime, opentsdb.TagSet{"cpu": v.Name, "type": "privileged"})
		Add(&md, "cpu.time", v.PercentInterruptTime, opentsdb.TagSet{"cpu": v.Name, "type": "interrupt"})
		Add(&md, "cpu.time", v.PercentUserTime, opentsdb.TagSet{"cpu": v.Name, "type": "user"})
	}
	return md
}

func c_simple_mem_windows() opentsdb.MultiDataPoint {
	var dst []wmi.Win32_OperatingSystem
	err := wmi.Query(SIMPLE_MEMORY_QUERY, &dst)
	if err != nil {
		l.Println("simple_mem:", err)
		return nil
	}
	var md opentsdb.MultiDataPoint
	for _, v := range dst {
		Add(&md, "mem.virtual.total", v.TotalVirtualMemorySize * 1024, opentsdb.TagSet{})
		Add(&md, "mem.virtual.free", v.FreeVirtualMemory * 1024, opentsdb.TagSet{})
		Add(&md, "mem.physical.total", v.TotalVisibleMemorySize * 1024, opentsdb.TagSet{})
		Add(&md, "mem.physical.free", v.FreePhysicalMemory * 1024, opentsdb.TagSet{})
	}
	return md
}

func c_disk_space_windows() opentsdb.MultiDataPoint {
	var dst []wmi.Win32_PerfRawData_PerfDisk_LogicalDisk
	err := wmi.Query(DISKSPACE_QUERY, &dst)
	if err != nil {
		l.Println("simple_mem:", err)
		return nil
	}
	var md opentsdb.MultiDataPoint
	for _, v := range dst {
		Add(&md, "disk.logical.free_bytes", v.FreeMegabytes * 1048576, opentsdb.TagSet{"partition": v.Name})
		Add(&md, "disk.logical.percent_free", v.PercentFreeSpace, opentsdb.TagSet{"partition": v.Name})
	}
	return md
}

func c_physical_disk_windows() opentsdb.MultiDataPoint {
	var dst []wmi.Win32_PerfRawData_PerfDisk_PhysicalDisk
	err := wmi.Query(PHYSICAL_DISK_QUERY, &dst)
	if err != nil {
		l.Println("disk_physical:", err)
		return nil
	}
	var md opentsdb.MultiDataPoint
	for _, v := range dst {
		Add(&md, "disk.physical.duration", v.AvgDiskSecPerRead, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.duration", v.AvgDiskSecPerWrite, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.queue", v.AvgDiskReadQueueLength, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.queue", v.AvgDiskWriteQueueLength, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.ops", v.DiskReadsPerSec, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.ops", v.DiskWritesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.bytes", v.DiskReadBytesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.bytes", v.DiskWriteBytesPerSec, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.percenttime", v.PercentDiskReadTime, opentsdb.TagSet{"disk": v.Name, "type": "read"})
		Add(&md, "disk.physical.percenttime", v.PercentDiskWriteTime, opentsdb.TagSet{"disk": v.Name, "type": "write"})
		Add(&md, "disk.physical.spltio", v.SplitIOPerSec, opentsdb.TagSet{"disk": v.Name})
	}
{'fraction_non_alphanumeric': 0.13332629578802913, 'fraction_numerical': 0.008339491185474507, 'mean_word_length': 6.028189910979228, 'pattern_counts': {'":': 56, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5203635', 'n_tokens_mistral': 3655, 'n_tokens_neox': 3406, 'n_words': 712}