Beispiel #1
1
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
}
Beispiel #2
0
func queryIOCounters(iface net.Interface, freq time.Duration, qio QueryIO) {

	for {
		var row syscall.MibIfRow

		select {
		case <-time.After(freq):
			row.Index = uint32(iface.Index)

			err := syscall.GetIfEntry(&row)
			if err != nil {
				log.Fatal(err)
			}

			ioc := &IOCounters{
				Name:        iface.Name,
				BytesSent:   sysmon.Size(row.OutOctets),
				BytesRecv:   sysmon.Size(row.InOctets),
				PacketsSent: row.OutUcastPkts,
				PacketsRecv: row.InUcastPkts,
				Errin:       row.InErrors,
				Errout:      row.OutErrors,
				Dropin:      row.InDiscards,
				Dropout:     row.OutDiscards,
			}
			qio.IOCounterChan <- ioc
		case <-qio.quit:
			// we have received a signal to stop
			return
		}
	}
}
Beispiel #3
0
func queryIOCounters(name string, freq time.Duration, qio QueryIO) {
	h, err := syscall.Open(name, syscall.O_RDONLY, 0)
	if err != nil {
		log.Fatal(err)
	}

	for {
		select {
		case <-time.After(freq):
			diskPerf, err := win32.IoctlDiskPerformance(h)

			if err != nil {
				log.Fatal(err)
			}

			ioc := &IOCounters{
				Name:       name,
				ReadCount:  diskPerf.ReadCount,
				WriteCount: diskPerf.WriteCount,
				ReadBytes:  sysmon.Size(diskPerf.BytesRead),
				WriteBytes: sysmon.Size(diskPerf.BytesWritten),
				ReadTime:   toDuration(diskPerf.ReadTime),
				WriteTime:  toDuration(diskPerf.WriteTime),
				IoTime:     toTime(diskPerf.QueryTime),
			}
			qio.IOCounterChan <- ioc
		case <-qio.quit:
			// we have received a signal to stop
			syscall.CloseHandle(h)
			return
		}
	}
}
Beispiel #4
0
func (p Process) ioCounters() (*IOCounters, error) {
	wioc, err := win32.GetProcessIoCounters(syscall.Handle(p.handle))
	if err != nil {
		return nil, err
	}

	return &IOCounters{
		ReadCount:  wioc.ReadOperationCount,
		WriteCount: wioc.WriteOperationCount,
		OtherCount: wioc.OtherOperationCount,
		ReadBytes:  sysmon.Size(wioc.ReadTransferCount),
		WriteBytes: sysmon.Size(wioc.WriteTransferCount),
		OtherBytes: sysmon.Size(wioc.OtherTransferCount),
	}, nil
}
Beispiel #5
0
func allVolumes() ([]Volume, error) {
	var ret []Volume
	var dst []win32.Win32_Volume

	q := wmi.CreateQuery(&dst, "")

	c := &wmi.Client{
		NonePtrZero:        true,
		PtrNil:             false,
		AllowMissingFields: true,
	}

	if err := c.Query(q, &dst); err != nil {
		return ret, err
	}

	for _, v := range dst {

		vol := Volume{
			Device: Device{
				DeviceId:    v.DeviceID,
				Name:        v.Name,
				Caption:     v.Caption,
				Description: v.Description,
			},
			BlockSize:    v.BlockSize,
			Capacity:     sysmon.Size(v.Capacity),
			Mount:        v.DriveLetter,
			DriveType:    v.DriveType,
			FileSystem:   v.FileSystem,
			FreeSpace:    sysmon.Size(v.FreeSpace),
			Label:        v.Label,
			SerialNumber: v.SerialNumber,
		}

		ret = append(ret, vol)
	}

	return ret, nil
}
Beispiel #6
0
func allUsage() ([]Usage, error) {
	var ret []Usage
	var dst []win32.Win32_Volume

	q := wmi.CreateQuery(&dst, "")

	c := &wmi.Client{
		NonePtrZero:        true,
		PtrNil:             false,
		AllowMissingFields: true,
	}

	if err := c.Query(q, &dst); err != nil {
		return ret, err
	}

	for _, v := range dst {

		u := Usage{
			Device: Device{
				DeviceId:    v.DeviceID,
				Name:        v.Name,
				Caption:     v.Caption,
				Description: v.Description,
			},
			Total:       sysmon.Size(v.Capacity),
			Free:        sysmon.Size(v.FreeSpace),
			Used:        sysmon.Size(v.Capacity - v.FreeSpace),
			UsedPercent: float64(v.Capacity-v.FreeSpace) / float64(v.Capacity) * 100,
		}

		ret = append(ret, u)
	}

	return ret, nil
}
Beispiel #7
0
func (p Process) memoryInfo() (*MemoryCounters, error) {
	pmc, err := win32.GetProcessMemoryInfo(syscall.Handle(p.handle))
	if err != nil {
		return nil, err
	}

	return &MemoryCounters{
		PageFaultCount:             pmc.PageFaultCount,
		PeakWorkingSetSize:         sysmon.Size(pmc.PeakWorkingSetSize),
		WorkingSetSize:             sysmon.Size(pmc.WorkingSetSize),
		QuotaPeakPagedPoolUsage:    sysmon.Size(pmc.QuotaPeakPagedPoolUsage),
		QuotaPagedPoolUsage:        sysmon.Size(pmc.QuotaPagedPoolUsage),
		QuotaPeakNonPagedPoolUsage: sysmon.Size(pmc.QuotaPeakNonPagedPoolUsage),
		QuotaNonPagedPoolUsage:     sysmon.Size(pmc.QuotaNonPagedPoolUsage),
		PagefileUsage:              sysmon.Size(pmc.PagefileUsage),
		PeakPagefileUsage:          sysmon.Size(pmc.PeakPagefileUsage),
	}, nil
}
Beispiel #8
0
func allDrives() ([]DiskDrive, error) {
	var ret []DiskDrive
	var dst []win32.Win32_DiskDrive

	q := wmi.CreateQuery(&dst, "")

	c := &wmi.Client{
		NonePtrZero:        true,
		PtrNil:             false,
		AllowMissingFields: true,
	}

	if err := c.Query(q, &dst); err != nil {
		return ret, err
	}

	for _, d := range dst {

		disk := DiskDrive{
			Device: Device{
				DeviceId:    d.DeviceID,
				Name:        d.Name,
				Caption:     d.Caption,
				Description: d.Description,
			},
			BytesPerSector: d.BytesPerSector,
			Partitions:     d.Partitions,
			Model:          d.Model,
			Size:           sysmon.Size(d.Size),
			Index:          d.Index,
			MediaType:      d.MediaType,
			SerialNumber:   d.SerialNumber,
			Status:         d.Status,
		}

		ret = append(ret, disk)
	}

	return ret, nil
}
Beispiel #9
0
func (p Process) modules() ([]*Module, error) {
	var ret []*Module

	snapshot, err := win32.CreateToolhelp32Snapshot(win32.TH32CS_SNAPMODULE, p.Pid)
	if err != nil {
		return ret, err
	}
	defer syscall.CloseHandle(snapshot)

	var modEntry win32.ModuleEntry32
	modEntry.Size = uint32(unsafe.Sizeof(modEntry))

	if err := win32.Module32First(snapshot, &modEntry); err != nil {
		return ret, err
	}

	for {
		m := &Module{
			ProcessID: modEntry.ProcessID,
			BaseAddr:  modEntry.BaseAddr,
			BaseSize:  sysmon.Size(modEntry.BaseSize),
			Handle:    modEntry.Handle,
			Name:      syscall.UTF16ToString(modEntry.ModuleName[:]),
			ExePath:   syscall.UTF16ToString(modEntry.ExePath[:]),
		}
		ret = append(ret, m)

		err = win32.Module32Next(snapshot, &modEntry)
		if err != nil {
			if err == syscall.ERROR_NO_MORE_FILES {
				break
			}
			return ret, err
		}
	}
	return ret[1:], nil
}