func allPartitions() ([]Partition, error) { var ret []Partition var dst []win32.Win32_DiskPartition q := wmi.CreateQuery(&dst, "") if err := wmi.Query(q, &dst); err != nil { return ret, err } for _, p := range dst { part := Partition{ Device: Device{ DeviceId: p.DeviceID, Name: p.Name, Caption: p.Caption, Description: p.Description, }, BlockSize: p.BlockSize, BootPartition: p.BootPartition, NumberOfBlocks: p.NumberOfBlocks, PrimaryPartition: p.PrimaryPartition, Size: sysmon.Size(p.Size), Index: p.DiskIndex, Type: p.Type, } ret = append(ret, part) } return ret, nil }
func getInfo() ([]Info, error) { var ret []Info var dst []win32.Win32_Processor q := wmi.CreateQuery(&dst, "") if err := wmi.Query(q, &dst); err != nil { return ret, err } fmt.Println("dst size: ", len(dst)) var procID string for i, p := range dst { procID = "" if p.ProcessorId != nil { procID = *p.ProcessorId } cpu := Info{ CPU: int32(i), Family: p.Family.String(), VendorId: p.Manufacturer, ModelName: p.Name, Cores: int32(p.NumberOfLogicalProcessors), PhysicalId: procID, Mhz: float64(p.MaxClockSpeed), Flags: []string{}, } ret = append(ret, cpu) } return ret, nil }
func Info() ([]InfoStat, error) { var ret []InfoStat var dst []Win32_Processor q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { return ret, err } var procID string for i, l := range dst { procID = "" if l.ProcessorID != nil { procID = *l.ProcessorID } cpu := InfoStat{ CPU: int32(i), Family: fmt.Sprintf("%d", l.Family), VendorID: l.Manufacturer, ModelName: l.Name, Cores: int32(l.NumberOfLogicalProcessors), PhysicalID: procID, Mhz: float64(l.MaxClockSpeed), Flags: []string{}, } ret = append(ret, cpu) } return ret, nil }
func UpdateCpuStat() error { cpuStatLock.Lock() defer cpuStatLock.Unlock() err := wmi.Query(cpuStatQuery, &cpuStatHistory) if err != nil { return err } else { return nil } }
func UpdateProcStat() error { procStatLock.Lock() defer procStatLock.Unlock() err := wmi.Query(procStatQuery, &procStatHistory) if err != nil { return err } else { return nil } }
func UpdateMemStat() error { memStatLock.Lock() defer memStatLock.Unlock() err := wmi.Query(memStatQuery, &memStatHistory) if err != nil { return err } else { return nil } }
func c_meta_windows_version() (opentsdb.MultiDataPoint, error) { var md opentsdb.MultiDataPoint var dst []Win32_OperatingSystem q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { slog.Error(err) return md, err } var dstComputer []Win32_ComputerSystem q = wmi.CreateQuery(&dstComputer, "") err = wmi.Query(q, &dstComputer) if err != nil { slog.Error(err) return md, err } var dstBIOS []Win32_BIOS q = wmi.CreateQuery(&dstBIOS, "") err = wmi.Query(q, &dstBIOS) if err != nil { slog.Error(err) return md, err } for _, v := range dst { metadata.AddMeta("", nil, "version", v.Version, true) metadata.AddMeta("", nil, "versionCaption", v.Caption, true) } for _, v := range dstComputer { metadata.AddMeta("", nil, "manufacturer", v.Manufacturer, true) metadata.AddMeta("", nil, "model", v.Model, true) metadata.AddMeta("", nil, "memoryTotal", v.TotalPhysicalMemory, true) } for _, v := range dstBIOS { metadata.AddMeta("", nil, "serialNumber", v.SerialNumber, true) } return md, nil }
func UpdateIfStat() error { ifStatLock.Lock() defer ifStatLock.Unlock() ifStatHistory = ifStatCurrent err := wmi.Query(ifStatQuery, &ifStatCurrent) if err != nil { return err } else { return nil } }
func main() { var dst []Win32_NetworkAdapter q := wmi.CreateQuery(&dst, "WHERE NetConnectionID != null") err := wmi.Query(q, &dst) if err != nil { log.Fatal(err) } for i, v := range dst { fmt.Printf("[%d] : Name:%s NetConnectionID:%s\n", i, v.Name, v.NetConnectionID) } }
func getComputerSystemStats() (stats []Stat) { var dst []Win32_ComputerSystem q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { log.Fatalf("getComputerSystemStats: %s", err) } stats = append(stats, Stat{"mem.physical.total", strconv.FormatUint(dst[0].TotalPhysicalMemory, 10), time.Now().UTC()}) return }
func BootTime() (uint64, error) { now := time.Now() var dst []Win32_OperatingSystem q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { return 0, err } t := dst[0].LastBootUpTime.Local() return uint64(now.Sub(t).Seconds()), nil }
func getWin32OperatingSystem() (Win32_OperatingSystem, error) { var dst []Win32_OperatingSystem q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { return Win32_OperatingSystem{}, errors.Wrap(err, "wmi query for Win32_OperatingSystem failed") } if len(dst) != 1 { return Win32_OperatingSystem{}, errors.New("wmi query for Win32_OperatingSystem failed") } return dst[0], nil }
func GetOSInfo() (Win32_OperatingSystem, error) { var dst []Win32_OperatingSystem q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { return Win32_OperatingSystem{}, err } osInfo = &dst[0] return dst[0], nil }
func (p *Process) Exe() (string, error) { var dst []Win32_Process query := fmt.Sprintf("WHERE ProcessId = %d", p.Pid) q := wmi.CreateQuery(&dst, query) err := wmi.Query(q, &dst) if err != nil { return "", err } if len(dst) != 1 { return "", fmt.Errorf("could not get ExecutablePath") } return *dst[0].ExecutablePath, nil }
func (p *Process) Cmdline() (string, error) { var dst []Win32_Process query := fmt.Sprintf("WHERE ProcessId = %d", p.Pid) q := wmi.CreateQuery(&dst, query) err := wmi.Query(q, &dst) if err != nil { return "", err } if len(dst) != 1 { return "", fmt.Errorf("could not get CommandLine") } return *dst[0].CommandLine, nil }
func (p *Process) NumThreads() (int32, error) { var dst []Win32_Process query := fmt.Sprintf("WHERE ProcessId = %d", p.Pid) q := wmi.CreateQuery(&dst, query) err := wmi.Query(q, &dst) if err != nil { return 0, err } if len(dst) != 1 { return 0, fmt.Errorf("could not get ThreadCount") } return int32(dst[0].ThreadCount), nil }
// getWin32Process gets information about the process with the given process ID. // It uses a WMI query to get the information from the local system. func getWin32Process(pid int32) (Win32_Process, error) { var dst []Win32_Process query := fmt.Sprintf("WHERE ProcessId = %d", pid) q := wmi.CreateQuery(&dst, query) err := wmi.Query(q, &dst) if err != nil { return Win32_Process{}, fmt.Errorf("could not get Win32_Process %s: %v", query, err) } if len(dst) < 1 { return Win32_Process{}, fmt.Errorf("could not get Win32_Process %s: Process not found", query) } return dst[0], nil }
func GetWin32Proc(pid int32) ([]Win32_Process, error) { var dst []Win32_Process query := fmt.Sprintf("WHERE ProcessId = %d", pid) q := wmi.CreateQuery(&dst, query) err := wmi.Query(q, &dst) if err != nil { return []Win32_Process{}, fmt.Errorf("could not get win32Proc: %s", err) } if len(dst) != 1 { return []Win32_Process{}, fmt.Errorf("could not get win32Proc: empty") } return dst, nil }
func main() { err := termbox.Init() if err != nil { panic(err) } defer termbox.Close() event_queue := make(chan termbox.Event) go func() { for { event_queue <- termbox.PollEvent() } }() var dst []Win32_PerfFormattedData_Tcpip_NetworkInterface q := wmi.CreateQuery(&dst, `` /*`WHERE Name = "Realtek PCIe GBE Family Controller"`*/) var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() loop: for { select { case ev := <-event_queue: if ev.Type == termbox.EventKey && ev.Key == termbox.KeyEsc { break loop } default: var d []Win32_PerfFormattedData_Tcpip_NetworkInterface err := wmi.Query(q, &d) if err != nil { panic(err) } termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) y := 0 for _, entry := range d { print(0, y, fmt.Sprintf("%s: recv: %s sent: %s", entry.Name, humanize.Bytes(uint64(entry.BytesReceivedPerSec)), humanize.Bytes(uint64(entry.BytesSentPerSec)))) y++ } termbox.Flush() <-time.After(time.Millisecond * 500) } } }() wg.Wait() }
func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) { var ret []float64 var dst []Win32_Processor q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { return ret, err } for _, l := range dst { // use range but windows can only get one percent. ret = append(ret, float64(l.LoadPercentage)/100.0) } return ret, nil }
func (t *Win32_Process) GetWin32Process(args *string, reply *[]Win32_Process) error { fmt.Println(time.Now().Local(), "GetWin32Process CALL", *args) var dst []Win32_Process q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { log.Fatal("GetWin32Process.Query:", err) } *reply = dst fmt.Println(time.Now().Local(), "GetWin32Process RETURN") return nil }
func (t *Win32_Service) GetWin32Service(args *string, reply *[]Win32_Service) error { fmt.Println(time.Now().Local(), "GetWin32Service CALL", *args) var dst []Win32_Service q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { log.Fatal("GetWin32Service.Query", err) } *reply = dst fmt.Println(time.Now().Local(), "GetWin32Service RETURN") return nil }
func c_diskspace_windows() opentsdb.MultiDataPoint { var dst []Win32_PerfRawData_PerfDisk_LogicalDisk var q = wmi.CreateQuery(&dst, `WHERE Name <> '_Total'`) err := wmi.Query(q, &dst) if err != nil { l.Println("diskspace:", 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_simple_mem_windows() opentsdb.MultiDataPoint { var dst []Win32_OperatingSystem var q = wmi.CreateQuery(&dst, "") err := wmi.Query(q, &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, nil) Add(&md, "mem.virtual.free", v.FreeVirtualMemory*1024, nil) Add(&md, "mem.physical.total", v.TotalVisibleMemorySize*1024, nil) Add(&md, "mem.physical.free", v.FreePhysicalMemory*1024, nil) } return md }
func c_mssql_databases() opentsdb.MultiDataPoint { var dst []Win32_PerfRawData_MSSQLSERVER_SQLServerDatabases var q = wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { l.Println("sql_database:", err) return nil } var md opentsdb.MultiDataPoint for _, v := range dst { Add(&md, "mssql.active_transactions", v.ActiveTransactions, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.backup_restore_throughput", v.BackupPerRestoreThroughputPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.bulkcopy_rows", v.BulkCopyRowsPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.bulkcopy_throughput", v.BulkCopyThroughputPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.commit_table_entries", v.Committableentries, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.data_files_size", v.DataFilesSizeKB*1024, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.dbcc_logical_scan_bytes", v.DBCCLogicalScanBytesPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.group_commit_time", v.GroupCommitTimePersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_bytes_flushed", v.LogBytesFlushedPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_cache_hit_ratio", v.LogCacheHitRatio, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_cache_hit_ratio_base", v.LogCacheHitRatio_Base, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_cache_reads", v.LogCacheReadsPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_files_size", v.LogFilesSizeKB*1024, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_files_used_size", v.LogFilesUsedSizeKB*1024, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_flushes", v.LogFlushesPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_flush_waits", v.LogFlushWaitsPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_flush_wait_time", v.LogFlushWaitTime, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_flush_write_time_ms", v.LogFlushWriteTimems, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_growths", v.LogGrowths, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_pool_cache_misses", v.LogPoolCacheMissesPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_pool_disk_reads", v.LogPoolDiskReadsPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_pool_requests", v.LogPoolRequestsPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_shrinks", v.LogShrinks, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.log_truncations", v.LogTruncations, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.percent_log_used", v.PercentLogUsed, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.repl_pending_xacts", v.ReplPendingXacts, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.repl_trans_rate", v.ReplTransRate, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.shrink_data_movement_bytes", v.ShrinkDataMovementBytesPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.tracked_transactions", v.TrackedtransactionsPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.transactions", v.TransactionsPersec, opentsdb.TagSet{"db": v.Name}) Add(&md, "mssql.write_transactions", v.WriteTransactionsPersec, opentsdb.TagSet{"db": v.Name}) } return md }
func getDiskStats() (stats []Stat) { var dst []Win32_Volume q := wmi.CreateQuery(&dst, "Where DriveType=3 and NOT Label='System Reserved'") err := wmi.Query(q, &dst) if err != nil { log.Fatalf("getDiskStats: %s", err) } for _, v := range dst { keyPrefix := fmt.Sprintf("disk.%s", happyDriveName(v.Name)) percent := int64(100 * (float64(v.FreeSpace) / float64(v.Capacity))) stats = append(stats, Stat{fmt.Sprintf("%s.percent_used", keyPrefix), strconv.FormatInt(percent, 10), time.Now().UTC()}) stats = append(stats, Stat{fmt.Sprintf("%s.free", keyPrefix), strconv.FormatUint(v.FreeSpace, 10), time.Now().UTC()}) stats = append(stats, Stat{fmt.Sprintf("%s.avail", keyPrefix), strconv.FormatUint(v.Capacity, 10), time.Now().UTC()}) } return }
func c_cpu_info_windows() opentsdb.MultiDataPoint { var dst []Win32_Processor var q = wmi.CreateQuery(&dst, `WHERE Name <> '_Total'`) err := wmi.Query(q, &dst) if err != nil { l.Println("cpu_info:", err) return nil } var md opentsdb.MultiDataPoint for _, v := range dst { Add(&md, "cpu.clock", v.CurrentClockSpeed, opentsdb.TagSet{"cpu": v.Name}) Add(&md, "cpu.clock_max", v.MaxClockSpeed, opentsdb.TagSet{"cpu": v.Name}) Add(&md, "cpu.voltage", v.CurrentVoltage, opentsdb.TagSet{"cpu": v.Name}) Add(&md, "cpu.load", v.LoadPercentage, opentsdb.TagSet{"cpu": v.Name}) Add(&md, "cpu.cores_physical", v.NumberOfCores, opentsdb.TagSet{"cpu": v.Name}) Add(&md, "cpu.cores_logical", v.NumberOfLogicalProcessors, opentsdb.TagSet{"cpu": v.Name}) } return md }
func c_mssql_locks() opentsdb.MultiDataPoint { var dst []Win32_PerfRawData_MSSQLSERVER_SQLServerLocks var q = wmi.CreateQuery(&dst, `WHERE Name = 'Page' OR Name = 'Extent' OR Name = 'Object' or Name = 'Database'`) err := wmi.Query(q, &dst) if err != nil { l.Println("sql_locks:", err) return nil } var md opentsdb.MultiDataPoint for _, v := range dst { Add(&md, "mssql.lock_wait_time", v.AverageWaitTimems, opentsdb.TagSet{"type": v.Name}) Add(&md, "mssql.lock_requests", v.LockRequestsPersec, opentsdb.TagSet{"type": v.Name}) Add(&md, "mssql.lock_timeouts", v.LockTimeoutsPersec, opentsdb.TagSet{"type": v.Name}) Add(&md, "mssql.lock_timeouts0", v.LockTimeoutstimeout0Persec, opentsdb.TagSet{"type": v.Name}) Add(&md, "mssql.lock_waits", v.LockWaitsPersec, opentsdb.TagSet{"type": v.Name}) Add(&md, "mssql.deadlocks", v.NumberofDeadlocksPersec, opentsdb.TagSet{"type": v.Name}) } return md }
// A float representing the current system-wide CPU utilization as a percentage. func usagePercent() ([]float64, error) { var ret []float64 var dst []win32.Win32_Processor q := wmi.CreateQuery(&dst, "") if err := wmi.Query(q, &dst); err != nil { return ret, err } /* for _, p := range dst { // use range but windows can only get one percent. if p.LoadPercentage == nil { continue } ret = append(ret, float64(*p.LoadPercentage)) } */ return ret, nil }
func getOperatingSystemStats() (stats []Stat) { var dst []Win32_OperatingSystem q := wmi.CreateQuery(&dst, "") err := wmi.Query(q, &dst) if err != nil { log.Fatalf("getOperatingSystemStats: %s", err) } stats = append(stats, Stat{"mem.physical.free", strconv.FormatUint(dst[0].FreePhysicalMemory, 10), time.Now().UTC()}) stats = append(stats, Stat{"mem.virtual.free", strconv.FormatUint(dst[0].FreeVirtualMemory, 10), time.Now().UTC()}) stats = append(stats, Stat{"mem.virtual.total", strconv.FormatUint(dst[0].TotalVirtualMemorySize, 10), time.Now().UTC()}) stats = append(stats, Stat{"mem.visible.total", strconv.FormatUint(dst[0].TotalVisibleMemorySize, 10), time.Now().UTC()}) stats = append(stats, Stat{"processes.total", strconv.FormatUint(dst[0].NumberOfProcesses, 10), time.Now().UTC()}) stats = append(stats, Stat{ "uptime", strconv.FormatUint(uint64(time.Since(dst[0].LastBootUpTime).Seconds()), 10), time.Now().UTC(), }) return }