From 31ec5180006b5b26e1fdf04d991a94d8f41670ea Mon Sep 17 00:00:00 2001 From: Pierre Gimalac Date: Mon, 24 Apr 2023 09:41:39 +0200 Subject: [PATCH] Fix various style-related linters (#170) * style: apply gofmt (darwin) * style: add copyright in each go file * style: add comments for packages, exported functions, exported types (mostly darwin) * style: fix receiver naming lint warning * style: fix unused argument lint warning (darwin) * style: fix unnecessary conversion lint warning * style: fix miscellaneous linter warnings, eg. error format specifier, invalid argument for function, typo * style: fix capitalized error message lint warning * style: fix typo warn lint * style: add missing comments on exported fields, const and packages (windows) * style: fix naming lint warnings (windows) * style: fix unused types / functions / fields and unnecessary cast lint warnings (windows) * style: add missing comments on exported fields, const and packages (windows continued) * style: fix naming lint warnings (windows, continued) * style: fix unused types / functions / fields and unnecessary cast lint warnings (windows continued) * style: fix naming lint warnings (windows, continued) * style: fix unnecessary cast lint warnings (windows continued) * style: fix naming lint warnings (windows, continued) * Update filesystem/filesystem.go Co-authored-by: Srdjan Grubor * style: fix var-naming lint warning about Ip,Cpu,Json,Id capitalization * style: fix indent --------- Co-authored-by: Srdjan Grubor --- cpu/cpu.go | 22 ++++- cpu/cpu_darwin.go | 8 +- cpu/cpu_linux_arm64.go | 11 ++- cpu/cpu_linux_default.go | 11 ++- cpu/cpu_windows.go | 97 +++++++++++++++--- cpu/cpu_windows_386.go | 10 ++ cpu/cpu_windows_amd64.go | 44 ++++++--- cpu/lscpu_linux_arm64.go | 25 +++-- cpu/util_linux.go | 5 + cpu/util_linux_test.go | 5 + filesystem/filesystem.go | 7 +- filesystem/filesystem_common.go | 15 ++- filesystem/filesystem_nix_test.go | 29 +++--- filesystem/filesystem_windows.go | 27 +++-- gohai.go | 11 +++ gohai_test.go | 9 +- log.go | 26 ++++- make.go | 7 +- memory/memory.go | 14 ++- memory/memory_darwin.go | 5 + memory/memory_linux.go | 5 + memory/memory_windows.go | 16 ++- network/network.go | 8 +- network/network_common.go | 23 +++-- platform/platform.go | 12 ++- platform/platform_android.go | 13 ++- platform/platform_common.go | 9 +- platform/platform_darwin.go | 5 + platform/platform_linux.go | 5 + platform/platform_nix.go | 10 +- platform/platform_windows.go | 136 +++++++++++++++++--------- platform/platform_windows_386.go | 21 +++- platform/platform_windows_amd64.go | 22 ++++- processes/gops/gops.go | 8 +- processes/gops/process_group.go | 24 ++++- processes/gops/process_group_test.go | 9 +- processes/gops/process_info.go | 18 ++-- processes/gops/process_info_darwin.go | 7 +- processes/gops/process_info_linux.go | 5 + processes/processes.go | 12 ++- processes/processes_common.go | 15 ++- processes/processes_windows.go | 7 +- utils/cmd.go | 6 ++ utils/dict_helpers.go | 7 +- 44 files changed, 615 insertions(+), 176 deletions(-) diff --git a/cpu/cpu.go b/cpu/cpu.go index 64eabfa..cbd2070 100644 --- a/cpu/cpu.go +++ b/cpu/cpu.go @@ -1,3 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +// Package cpu regroups collecting information about the CPU package cpu import ( @@ -9,6 +15,8 @@ import ( ) // Cpu holds metadata about the host CPU +// +//nolint:revive type Cpu struct { // VendorId the CPU vendor ID VendorId string @@ -43,20 +51,24 @@ type Cpu struct { const name = "cpu" -func (self *Cpu) Name() string { +// Name returns the name of the package +func (cpu *Cpu) Name() string { return name } -func (self *Cpu) Collect() (result interface{}, err error) { - result, err = getCpuInfo() +// Collect collects the CPU information. +// Returns an object which can be converted to a JSON or an error if nothing could be collected. +// Tries to collect as much information as possible. +func (cpu *Cpu) Collect() (result interface{}, err error) { + result, err = getCPUInfo() return } -// Get returns a Cpu struct already initialized, a list of warnings and an error. The method will try to collect as much +// Get returns a CPU struct already initialized, a list of warnings and an error. The method will try to collect as much // metadata as possible, an error is returned if nothing could be collected. The list of warnings contains errors if // some metadata could not be collected. func Get() (*Cpu, []string, error) { - cpuInfo, err := getCpuInfo() + cpuInfo, err := getCPUInfo() if err != nil { return nil, nil, err } diff --git a/cpu/cpu_darwin.go b/cpu/cpu_darwin.go index ae44ad2..87e45e3 100644 --- a/cpu/cpu_darwin.go +++ b/cpu/cpu_darwin.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package cpu import ( @@ -17,8 +22,7 @@ var cpuMap = map[string]string{ "machdep.cpu.stepping": "stepping", } -func getCpuInfo() (cpuInfo map[string]string, err error) { - +func getCPUInfo() (cpuInfo map[string]string, err error) { cpuInfo = make(map[string]string) for option, key := range cpuMap { diff --git a/cpu/cpu_linux_arm64.go b/cpu/cpu_linux_arm64.go index 3b29b0f..13e62ff 100644 --- a/cpu/cpu_linux_arm64.go +++ b/cpu/cpu_linux_arm64.go @@ -1,5 +1,10 @@ -// +build linux -// +build arm64 +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +//go:build linux && arm64 +// +build linux,arm64 package cpu @@ -18,7 +23,7 @@ import ( // nodeNRegex recognizes directories named `nodeNN` var nodeNRegex = regexp.MustCompile("^node[0-9]+$") -func getCpuInfo() (cpuInfo map[string]string, err error) { +func getCPUInfo() (cpuInfo map[string]string, err error) { cpuInfo = make(map[string]string) procCpu, err := readProcCpuInfo() diff --git a/cpu/cpu_linux_default.go b/cpu/cpu_linux_default.go index 91af875..5f029bc 100644 --- a/cpu/cpu_linux_default.go +++ b/cpu/cpu_linux_default.go @@ -1,5 +1,10 @@ -// +build linux -// +build !arm64 +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +//go:build linux && !arm64 +// +build linux,!arm64 package cpu @@ -28,7 +33,7 @@ var perPhysicalProcValues = []string{ "cpu_logical_processors", } -func getCpuInfo() (cpuInfo map[string]string, err error) { +func getCPUInfo() (cpuInfo map[string]string, err error) { lines, err := readProcFile() if err != nil { return diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 84cfda0..5b7c052 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package cpu import ( @@ -11,17 +16,20 @@ import ( "golang.org/x/sys/windows/registry" ) -var getCpuInfo = GetCpuInfo - -// Values that need to be multiplied by the number of physical processors -var perPhysicalProcValues = []string{ - "cpu_cores", - "cpu_logical_processors", -} +var getCPUInfo = GetCpuInfo +// ERROR_INSUFFICIENT_BUFFER is the error number associated with the +// "insufficient buffer size" error +// +//nolint:revive const ERROR_INSUFFICIENT_BUFFER syscall.Errno = 122 + const registryHive = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0" +// CACHE_DESCRIPTOR contains cache related information +// see https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-cache_descriptor +// +//nolint:unused,revive type CACHE_DESCRIPTOR struct { Level uint8 Associativity uint8 @@ -29,6 +37,12 @@ type CACHE_DESCRIPTOR struct { Size uint32 cacheType uint32 } + +// SYSTEM_LOGICAL_PROCESSOR_INFORMATION describes the relationship +// between the specified processor set. +// see https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-system_logical_processor_information +// +//nolint:unused,revive type SYSTEM_LOGICAL_PROCESSOR_INFORMATION struct { ProcessorMask uintptr Relationship int // enum (int) @@ -39,16 +53,32 @@ type SYSTEM_LOGICAL_PROCESSOR_INFORMATION struct { //.const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_SIZE = 32 +// GROUP_AFFINITY represents a processor group-specific affinity, +// such as the affinity of a thread. +// see https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-group_affinity +// +//nolint:revive type GROUP_AFFINITY struct { Mask uintptr Group uint16 Reserved [3]uint16 } + +// NUMA_NODE_RELATIONSHIP represents information about a NUMA node +// in a processor group. +// see https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-numa_node_relationship +// +//nolint:revive type NUMA_NODE_RELATIONSHIP struct { NodeNumber uint32 Reserved [20]uint8 GroupMask GROUP_AFFINITY } + +// CACHE_RELATIONSHIP describes cache attributes. +// see https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-cache_relationship +// +//nolint:revive type CACHE_RELATIONSHIP struct { Level uint8 Associativity uint8 @@ -59,18 +89,34 @@ type CACHE_RELATIONSHIP struct { GroupMask GROUP_AFFINITY } +// PROCESSOR_GROUP_INFO represents the number and affinity of processors +// in a processor group. +// see https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-processor_group_info +// +//nolint:revive type PROCESSOR_GROUP_INFO struct { MaximumProcessorCount uint8 ActiveProcessorCount uint8 Reserved [38]uint8 ActiveProcessorMask uintptr } + +// GROUP_RELATIONSHIP represents information about processor groups. +// see https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-group_relationship +// +//nolint:revive type GROUP_RELATIONSHIP struct { MaximumGroupCount uint16 ActiveGroupCount uint16 Reserved [20]uint8 // variable size array of PROCESSOR_GROUP_INFO } + +// PROCESSOR_RELATIONSHIP represents information about affinity +// within a processor group. +// see https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-processor_relationship +// +//nolint:unused,revive type PROCESSOR_RELATIONSHIP struct { Flags uint8 EfficiencyClass uint8 @@ -79,6 +125,11 @@ type PROCESSOR_RELATIONSHIP struct { // what follows is an array of zero or more GROUP_AFFINITY structures } +// SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX contains information about +// the relationships of logical processors and related hardware. +// https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-system_logical_processor_information_ex +// +//nolint:revive type SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX struct { Relationship int Size uint32 @@ -89,12 +140,31 @@ type SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX struct { // GROUP_RELATIONSHIP } -const RelationProcessorCore = 0 -const RelationNumaNode = 1 -const RelationCache = 2 -const RelationProcessorPackage = 3 -const RelationGroup = 4 +// see https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getlogicalprocessorinformationex +const ( + // RelationProcessorCore retrieves information about logical processors + // that share a single processor core. + RelationProcessorCore = 0 + // RelationNumaNode retrieves information about logical processors + // that are part of the same NUMA node. + RelationNumaNode = 1 + // RelationCache retrieves information about logical processors + // that share a cache. + RelationCache = 2 + // RelationProcessorPackage retrieves information about logical processors + // that share a physical package. + RelationProcessorPackage = 3 + // RelationGroup retrieves information about logical processors + // that share a processor group. + RelationGroup = 4 +) +// SYSTEM_INFO contains information about the current computer system. +// This includes the architecture and type of the processor, the number +// of processors in the system, the page size, and other such information. +// see https://learn.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info +// +//nolint:revive type SYSTEM_INFO struct { wProcessorArchitecture uint16 wReserved uint16 @@ -109,6 +179,9 @@ type SYSTEM_INFO struct { wProcessorRevision uint16 } +// CPU_INFO contains information about cpu, eg. number of cores, cache size +// +//nolint:revive type CPU_INFO struct { numaNodeCount int // number of NUMA nodes pkgcount int // number of packages (physical CPUS) diff --git a/cpu/cpu_windows_386.go b/cpu/cpu_windows_386.go index d054e39..f21f7ed 100644 --- a/cpu/cpu_windows_386.go +++ b/cpu/cpu_windows_386.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package cpu import ( @@ -6,11 +11,16 @@ import ( "unsafe" ) +// SYSTEM_LOGICAL_PROCESSOR_INFORMATION_SIZE is the size of +// SYSTEM_LOGICAL_PROCESSOR_INFORMATION struct +// +//nolint:revive const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_SIZE = 24 func getSystemLogicalProcessorInformationSize() int { return SYSTEM_LOGICAL_PROCESSOR_INFORMATION_SIZE } + func byteArrayToProcessorStruct(data []byte) (info SYSTEM_LOGICAL_PROCESSOR_INFORMATION) { info.ProcessorMask = uintptr(binary.LittleEndian.Uint32(data)) info.Relationship = int(binary.LittleEndian.Uint32(data[4:])) diff --git a/cpu/cpu_windows_amd64.go b/cpu/cpu_windows_amd64.go index 8230aff..189e565 100644 --- a/cpu/cpu_windows_amd64.go +++ b/cpu/cpu_windows_amd64.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package cpu import ( @@ -6,11 +11,18 @@ import ( "unsafe" ) +// SYSTEM_LOGICAL_PROCESSOR_INFORMATION_SIZE is the size of +// SYSTEM_LOGICAL_PROCESSOR_INFORMATION struct +// +//nolint:revive const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_SIZE = 32 +//nolint:unused func getSystemLogicalProcessorInformationSize() int { return SYSTEM_LOGICAL_PROCESSOR_INFORMATION_SIZE } + +//nolint:unused func byteArrayToProcessorStruct(data []byte) (info SYSTEM_LOGICAL_PROCESSOR_INFORMATION) { info.ProcessorMask = uintptr(binary.LittleEndian.Uint64(data)) info.Relationship = int(binary.LittleEndian.Uint64(data[8:])) @@ -21,7 +33,7 @@ func byteArrayToProcessorStruct(data []byte) (info SYSTEM_LOGICAL_PROCESSOR_INFO func byteArrayToGroupAffinity(data []byte) (affinity GROUP_AFFINITY, consumed uint32, err error) { err = nil affinity.Mask = uintptr(binary.LittleEndian.Uint64(data)) - affinity.Group = uint16(binary.LittleEndian.Uint16(data[8:])) + affinity.Group = binary.LittleEndian.Uint16(data[8:]) // can skip the reserved, but count it consumed = 16 return @@ -30,7 +42,7 @@ func byteArrayToGroupAffinity(data []byte) (affinity GROUP_AFFINITY, consumed ui func byteArrayToProcessorInformationExStruct(data []byte) (info SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, consumed uint32, err error) { err = nil info.Relationship = int(binary.LittleEndian.Uint32(data)) - info.Size = uint32(binary.LittleEndian.Uint32(data[4:])) + info.Size = binary.LittleEndian.Uint32(data[4:]) consumed = 8 return @@ -38,8 +50,8 @@ func byteArrayToProcessorInformationExStruct(data []byte) (info SYSTEM_LOGICAL_P func byteArrayToProcessorRelationshipStruct(data []byte) (proc PROCESSOR_RELATIONSHIP, groupMask []GROUP_AFFINITY, consumed uint32, err error) { err = nil - proc.Flags = uint8(data[0]) - proc.EfficiencyClass = uint8(data[1]) + proc.Flags = data[0] + proc.EfficiencyClass = data[1] proc.GroupCount = uint16(binary.LittleEndian.Uint32(data[22:])) consumed = 24 if proc.GroupCount != 0 { @@ -61,7 +73,7 @@ func byteArrayToProcessorRelationshipStruct(data []byte) (proc PROCESSOR_RELATIO } func byteArrayToNumaNode(data []byte) (numa NUMA_NODE_RELATIONSHIP, consumed uint32, err error) { - numa.NodeNumber = uint32(binary.LittleEndian.Uint32(data)) + numa.NodeNumber = binary.LittleEndian.Uint32(data) // skip 20 bytes of reserved consumed = 24 aff, used, err := byteArrayToGroupAffinity(data[consumed:]) @@ -71,10 +83,10 @@ func byteArrayToNumaNode(data []byte) (numa NUMA_NODE_RELATIONSHIP, consumed uin } func byteArrayToRelationCache(data []byte) (cache CACHE_RELATIONSHIP, consumed uint32, err error) { - cache.Level = uint8(data[0]) - cache.Associativity = uint8(data[1]) - cache.LineSize = uint16(binary.LittleEndian.Uint16(data[2:])) - cache.CacheSize = uint32(binary.LittleEndian.Uint32(data[4:])) + cache.Level = data[0] + cache.Associativity = data[1] + cache.LineSize = binary.LittleEndian.Uint16(data[2:]) + cache.CacheSize = binary.LittleEndian.Uint32(data[4:]) cache.CacheType = int(binary.LittleEndian.Uint32(data[8:])) // skip 20 bytes consumed = 32 @@ -86,16 +98,16 @@ func byteArrayToRelationCache(data []byte) (cache CACHE_RELATIONSHIP, consumed u } func byteArrayToRelationGroup(data []byte) (group GROUP_RELATIONSHIP, gi []PROCESSOR_GROUP_INFO, consumed uint32, err error) { - group.MaximumGroupCount = uint16(binary.LittleEndian.Uint16(data)) - group.ActiveGroupCount = uint16(binary.LittleEndian.Uint16(data[4:])) + group.MaximumGroupCount = binary.LittleEndian.Uint16(data) + group.ActiveGroupCount = binary.LittleEndian.Uint16(data[4:]) consumed = 24 if group.ActiveGroupCount > 0 { groups := make([]PROCESSOR_GROUP_INFO, group.ActiveGroupCount) for i := uint16(0); i < group.ActiveGroupCount; i++ { - groups[i].MaximumProcessorCount = uint8(data[consumed]) - consumed += 1 - groups[i].ActiveProcessorCount = uint8(data[consumed]) - consumed += 1 + groups[i].MaximumProcessorCount = data[consumed] + consumed++ + groups[i].ActiveProcessorCount = data[consumed] + consumed++ consumed += 38 // reserved groups[i].ActiveProcessorMask = uintptr(binary.LittleEndian.Uint64(data[consumed:])) consumed += 8 @@ -108,7 +120,7 @@ func computeCoresAndProcessors() (CPU_INFO, error) { var cpuInfo CPU_INFO var mod = syscall.NewLazyDLL("kernel32.dll") var getProcInfo = mod.NewProc("GetLogicalProcessorInformationEx") - var buflen uint32 = 0 + var buflen uint32 // first, figure out how much we need status, _, callErr := getProcInfo.Call(uintptr(0xFFFF), // all relationships. diff --git a/cpu/lscpu_linux_arm64.go b/cpu/lscpu_linux_arm64.go index fb98b1d..8c35b98 100644 --- a/cpu/lscpu_linux_arm64.go +++ b/cpu/lscpu_linux_arm64.go @@ -1,7 +1,7 @@ // Code generated by cpu/from-lscpu-arm.py; DO NOT EDIT. -// +build linux -// +build arm64 +//go:build linux && arm64 +// +build linux,arm64 package cpu @@ -14,7 +14,9 @@ type hwImpl struct { } // hwVariant is based on hw_implementer in -// https://github.com/util-linux/util-linux/blob/master/sys-utils/lscpu-arm.c +// +// https://github.com/util-linux/util-linux/blob/master/sys-utils/lscpu-arm.c +// // See from-lscpu-arm.py to regenerate this value. var hwVariant = map[uint64]hwImpl{ 0x41: hwImpl{ @@ -84,7 +86,7 @@ var hwVariant = map[uint64]hwImpl{ 0x42: hwImpl{ name: "Broadcom", parts: map[uint64]string{ - 0x0f: "Brahma B15", + 0x0f: "Brahma B15", 0x100: "Brahma B53", 0x516: "ThunderX2", }, @@ -119,14 +121,12 @@ var hwVariant = map[uint64]hwImpl{ }, }, 0x49: hwImpl{ - name: "Infineon", - parts: map[uint64]string{ - }, + name: "Infineon", + parts: map[uint64]string{}, }, 0x4d: hwImpl{ - name: "Motorola/Freescale", - parts: map[uint64]string{ - }, + name: "Motorola/Freescale", + parts: map[uint64]string{}, }, 0x4e: hwImpl{ name: "NVIDIA", @@ -231,8 +231,7 @@ var hwVariant = map[uint64]hwImpl{ }, }, 0xc0: hwImpl{ - name: "Ampere", - parts: map[uint64]string{ - }, + name: "Ampere", + parts: map[uint64]string{}, }, } diff --git a/cpu/util_linux.go b/cpu/util_linux.go index fdc6141..85b0a28 100644 --- a/cpu/util_linux.go +++ b/cpu/util_linux.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package cpu import ( diff --git a/cpu/util_linux_test.go b/cpu/util_linux_test.go index c1b7faa..93abdfe 100644 --- a/cpu/util_linux_test.go +++ b/cpu/util_linux_test.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package cpu import ( diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go index ff157db..ba56979 100644 --- a/filesystem/filesystem.go +++ b/filesystem/filesystem.go @@ -1,6 +1,12 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + //go:build linux || darwin // +build linux darwin +// Package filesystem returns information about available filesystems. package filesystem import ( @@ -17,7 +23,6 @@ var dfOptions = []string{"-l", "-k"} var dfTimeout = 2 * time.Second func getFileSystemInfo() (interface{}, error) { - ctx, cancel := context.WithTimeout(context.Background(), dfTimeout) defer cancel() diff --git a/filesystem/filesystem_common.go b/filesystem/filesystem_common.go index 47170c5..7029e68 100644 --- a/filesystem/filesystem_common.go +++ b/filesystem/filesystem_common.go @@ -1,14 +1,25 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +// Package filesystem regroups collecting information about the filesystem package filesystem +// FileSystem is the Collector type of the filesystem package. type FileSystem struct{} const name = "filesystem" -func (self *FileSystem) Name() string { +// Name returns the name of the package +func (filesystem *FileSystem) Name() string { return name } -func (self *FileSystem) Collect() (result interface{}, err error) { +// Collect collects the filesystem information. +// Returns an object which can be converted to a JSON or an error if nothing could be collected. +// Tries to collect as much information as possible. +func (filesystem *FileSystem) Collect() (result interface{}, err error) { result, err = getFileSystemInfo() return } diff --git a/filesystem/filesystem_nix_test.go b/filesystem/filesystem_nix_test.go index 54c3dd9..9e233ab 100644 --- a/filesystem/filesystem_nix_test.go +++ b/filesystem/filesystem_nix_test.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + //go:build linux || darwin // +build linux darwin @@ -46,12 +51,12 @@ func TestOldMacosDf(t *testing.T) { out, err := getFileSystemInfo() require.NoError(t, err) - require.Equal(t, []interface{}([]interface{}{ + require.Equal(t, []interface{}{ map[string]string{"kb_size": "975093952", "mounted_on": "/", "name": "/dev/disk0s2"}, map[string]string{"kb_size": "368", "mounted_on": "/dev", "name": "devfs"}, map[string]string{"kb_size": "0", "mounted_on": "/net", "name": "map -hosts"}, map[string]string{"kb_size": "0", "mounted_on": "/Volumes/Large", "name": "map -static"}, - }), out) + }, out) } func TestDfLinux(t *testing.T) { @@ -64,11 +69,11 @@ func TestDfLinux(t *testing.T) { out, err := getFileSystemInfo() require.NoError(t, err) - require.Equal(t, []interface{}([]interface{}{ + require.Equal(t, []interface{}{ map[string]string{"kb_size": "16197480", "mounted_on": "/", "name": "/dev/root"}, map[string]string{"kb_size": "15381564", "mounted_on": "/dev", "name": "devtmpfs"}, map[string]string{"kb_size": "15388388", "mounted_on": "/dev/shm", "name": "tmpfs"}, - }), out) + }, out) } func TestDfMac(t *testing.T) { @@ -80,10 +85,10 @@ func TestDfMac(t *testing.T) { out, err := getFileSystemInfo() require.NoError(t, err) - require.Equal(t, []interface{}([]interface{}{ + require.Equal(t, []interface{}{ map[string]string{"kb_size": "488245288", "mounted_on": "/", "name": "/dev/disk1s1s1"}, map[string]string{"kb_size": "488245288", "mounted_on": "/System/Volumes/VM", "name": "/dev/disk1s5"}, - }), out) + }, out) } func TestDfWithVolumeSpaces(t *testing.T) { @@ -95,10 +100,10 @@ func TestDfWithVolumeSpaces(t *testing.T) { out, err := getFileSystemInfo() require.NoError(t, err) - require.Equal(t, []interface{}([]interface{}{ + require.Equal(t, []interface{}{ map[string]string{"kb_size": "367616", "mounted_on": "/Volumes/Firefox", "name": "/dev/disk4s3"}, map[string]string{"kb_size": "307200", "mounted_on": "/Volumes/MySQL Workbench community-8.0.30", "name": "/dev/disk5"}, - }), out) + }, out) } func TestDfWithErrors(t *testing.T) { @@ -111,10 +116,10 @@ func TestDfWithErrors(t *testing.T) { out, err := getFileSystemInfo() require.NoError(t, err) - require.Equal(t, []interface{}([]interface{}{ + require.Equal(t, []interface{}{ map[string]string{"kb_size": "367616", "mounted_on": "/Volumes/Firefox", "name": "/dev/disk4s3"}, map[string]string{"kb_size": "307200", "mounted_on": "/Volumes/MySQL Workbench community-8.0.30", "name": "/dev/disk5"}, - }), out) + }, out) } func TestFaileDfWithData(t *testing.T) { @@ -123,9 +128,9 @@ func TestFaileDfWithData(t *testing.T) { out, err := getFileSystemInfo() require.NoError(t, err) - require.Equal(t, []interface{}([]interface{}{ + require.Equal(t, []interface{}{ map[string]string{"kb_size": "488245288", "mounted_on": "/", "name": "/dev/disk1s1s1"}, - }), out) + }, out) } func TestGetFileSystemInfo(t *testing.T) { diff --git a/filesystem/filesystem_windows.go b/filesystem/filesystem_windows.go index 53324a8..596d756 100644 --- a/filesystem/filesystem_windows.go +++ b/filesystem/filesystem_windows.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package filesystem import ( @@ -6,14 +11,18 @@ import ( "unsafe" ) +// Handle represents a pointer used by FindFirstVolumeW and similar functions type Handle uintptr -const InvalidHandle Handle = Handle(^Handle(0)) -const ERROR_moreData syscall.Errno = 234 +// InvalidHandle is the value returned in case of error +const InvalidHandle Handle = ^Handle(0) + +// ERRORMoreData is the error returned when the size is not big enough +const ERRORMoreData syscall.Errno = 234 // this would probably go in a common utilities rather than here -func convert_windows_string_list(winput []uint16) []string { +func convertWindowsStringList(winput []uint16) []string { var retstrings []string var rsindex = 0 @@ -33,7 +42,7 @@ func convert_windows_string_list(winput []uint16) []string { } // as would this -func convert_windows_string(winput []uint16) string { +func convertWindowsString(winput []uint16) string { var retstring string for i := 0; i < len(winput); i++ { if winput[i] == 0 { @@ -80,7 +89,7 @@ func getMountPoints(vol string) []string { 2, uintptr(unsafe.Pointer(&objlistsize))) - if status != 0 || errno != ERROR_moreData { + if status != 0 || errno != ERRORMoreData { // unexpected return retval } @@ -93,7 +102,7 @@ func getMountPoints(vol string) []string { if status == 0 { return retval } - return convert_windows_string_list(buf) + return convertWindowsStringList(buf) } @@ -108,7 +117,7 @@ func getFileSystemInfo() (interface{}, error) { var sz int32 = 512 fh, _, _ := findFirst.Call(uintptr(unsafe.Pointer(&buf[0])), uintptr(sz)) - var findHandle Handle = Handle(fh) + var findHandle = Handle(fh) var fileSystemInfo []interface{} if findHandle != InvalidHandle { @@ -117,7 +126,7 @@ func getFileSystemInfo() (interface{}, error) { defer findClose.Call(fh) moreData := true for moreData { - outstring := convert_windows_string(buf) + outstring := convertWindowsString(buf) sz, _ := getDiskSize(outstring) var capacity string if 0 == sz { @@ -136,7 +145,7 @@ func getFileSystemInfo() (interface{}, error) { "mounted_on": mountName, } fileSystemInfo = append(fileSystemInfo, iface) - status, _, _ := findNext.Call(uintptr(fh), + status, _, _ := findNext.Call(fh, uintptr(unsafe.Pointer(&buf[0])), uintptr(sz)) if 0 == status { diff --git a/gohai.go b/gohai.go index a9d8530..c7f9d93 100644 --- a/gohai.go +++ b/gohai.go @@ -1,3 +1,10 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +// Package main contains the binary related functions, +// eg. cli parameters package main import ( @@ -21,11 +28,13 @@ import ( "github.com/DataDog/gohai/processes" ) +// Collector represents a group of information which can be collected type Collector interface { Name() string Collect() (interface{}, error) } +// SelectedCollectors represents a set of collector names type SelectedCollectors map[string]struct{} var collectors = []Collector{ @@ -52,6 +61,7 @@ var ( goVersion string ) +// Collect fills the result map with the collector information under their name key func Collect() (result map[string]interface{}, err error) { result = make(map[string]interface{}) @@ -112,6 +122,7 @@ func (sc *SelectedCollectors) String() string { return fmt.Sprint(collectorSlice) } +// Set adds the given comma-separated list of collector names to the selected set. func (sc *SelectedCollectors) Set(value string) error { for _, collectorName := range strings.Split(value, ",") { (*sc)[collectorName] = struct{}{} diff --git a/gohai_test.go b/gohai_test.go index 6c8b36f..155ec96 100644 --- a/gohai_test.go +++ b/gohai_test.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package main import ( @@ -88,11 +93,11 @@ func TestGohaiSerialization(t *testing.T) { assert.NoError(t, err) - gohaiJson, err := json.Marshal(gohai) + gohaiJSON, err := json.Marshal(gohai) assert.NoError(t, err) var payload gohaiPayload - assert.NoError(t, json.Unmarshal(gohaiJson, &payload)) + assert.NoError(t, json.Unmarshal(gohaiJSON, &payload)) assert.NotEmpty(t, payload.CPU.CPUCores) assert.NotEmpty(t, payload.CPU.CPULogicalProcessors) diff --git a/log.go b/log.go index c0e33cd..275726b 100644 --- a/log.go +++ b/log.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package main import ( @@ -17,20 +22,33 @@ const baseStdErrLogConfig = ` ` +// StdErrReceiver is a dummy receiver used to log to stderr instead of stdout. +// See seelog.CustomReceiver. type StdErrReceiver struct{} -// Implement seelog.CustomReceiver to log to stderr instead of stdout -func (sr *StdErrReceiver) ReceiveMessage(message string, level log.LogLevel, context log.LogContextInterface) error { - fmt.Fprintf(os.Stderr, message) +// ReceiveMessage is called when the custom receiver gets seelog message from +// a parent dispatcher. +// See seelog.CustomReceiver. +func (sr *StdErrReceiver) ReceiveMessage(message string, _ log.LogLevel, _ log.LogContextInterface) error { + fmt.Fprint(os.Stderr, message) return nil } -func (sr *StdErrReceiver) AfterParse(initArgs log.CustomReceiverInitArgs) error { +// AfterParse is called immediately after your custom receiver is instantiated by +// the xml config parser. +// See seelog.CustomReceiver. +func (sr *StdErrReceiver) AfterParse(_ log.CustomReceiverInitArgs) error { return nil } +// Flush is called when the custom receiver gets a 'flush' directive from a +// parent receiver. +// See seelog.CustomReceiver. func (sr *StdErrReceiver) Flush() {} +// Close is called when the custom receiver gets a 'close' directive from a +// parent receiver. +// See seelog.CustomReceiver. func (sr *StdErrReceiver) Close() error { return nil } diff --git a/make.go b/make.go index bc6d82c..1eec1a6 100644 --- a/make.go +++ b/make.go @@ -1,6 +1,11 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +//go:build ignore // +build ignore -// Builds Gohai with version information package main import ( diff --git a/memory/memory.go b/memory/memory.go index 8cccdbf..2f4f19f 100644 --- a/memory/memory.go +++ b/memory/memory.go @@ -1,3 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +// Package memory regroups collecting information about the memory package memory // Memory holds memory metadata about the host @@ -10,11 +16,15 @@ type Memory struct { const name = "memory" -func (self *Memory) Name() string { +// Name returns the name of the package +func (memory *Memory) Name() string { return name } -func (self *Memory) Collect() (result interface{}, err error) { +// Collect collects the Memory information. +// Returns an object which can be converted to a JSON or an error if nothing could be collected. +// Tries to collect as much information as possible. +func (memory *Memory) Collect() (result interface{}, err error) { result, err = getMemoryInfo() return } diff --git a/memory/memory_darwin.go b/memory/memory_darwin.go index 0803723..0e87f39 100644 --- a/memory/memory_darwin.go +++ b/memory/memory_darwin.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package memory import ( diff --git a/memory/memory_linux.go b/memory/memory_linux.go index 089d8da..693fcf5 100644 --- a/memory/memory_linux.go +++ b/memory/memory_linux.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package memory import ( diff --git a/memory/memory_windows.go b/memory/memory_windows.go index 908238e..09c65e6 100644 --- a/memory/memory_windows.go +++ b/memory/memory_windows.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package memory import ( @@ -6,6 +11,9 @@ import ( "unsafe" ) +// MEMORYSTATUSEX is the type of the struct expected by GlobalMemoryStatusEx +// +//nolint:revive type MEMORYSTATUSEX struct { dwLength uint32 // size of this structure dwMemoryLoad uint32 // number 0-100 estimating %age of memory in use @@ -32,13 +40,13 @@ func getMemoryInfoByte() (mem uint64, swap uint64, warnings []string, err error) var mod = syscall.NewLazyDLL("kernel32.dll") var getMem = mod.NewProc("GlobalMemoryStatusEx") - var mem_struct MEMORYSTATUSEX + var memStruct MEMORYSTATUSEX - mem_struct.dwLength = uint32(unsafe.Sizeof(mem_struct)) + memStruct.dwLength = uint32(unsafe.Sizeof(memStruct)) - status, _, err := getMem.Call(uintptr(unsafe.Pointer(&mem_struct))) + status, _, err := getMem.Call(uintptr(unsafe.Pointer(&memStruct))) if status != 0 { - mem = mem_struct.ulTotalPhys + mem = memStruct.ulTotalPhys err = nil } return diff --git a/network/network.go b/network/network.go index ce81c30..59b8234 100644 --- a/network/network.go +++ b/network/network.go @@ -1,3 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +// Package network regroups collecting information about the network interfaces package network func getNetworkInfo() (networkInfo map[string]interface{}, err error) { @@ -9,7 +15,7 @@ func getNetworkInfo() (networkInfo map[string]interface{}, err error) { } networkInfo["macaddress"] = macaddress - ipAddress, err := externalIpAddress() + ipAddress, err := externalIPAddress() if err != nil { return networkInfo, err } diff --git a/network/network_common.go b/network/network_common.go index 1247852..ba0580f 100644 --- a/network/network_common.go +++ b/network/network_common.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package network import ( @@ -8,6 +13,8 @@ import ( ) // Network holds network metadata about the host +// +//nolint:revive type Network struct { // IpAddress is the ipv4 address for the host IpAddress string @@ -22,11 +29,15 @@ type Network struct { const name = "network" -func (self *Network) Name() string { +// Name returns the name of the package +func (network *Network) Name() string { return name } -func (self *Network) Collect() (result interface{}, err error) { +// Collect collects the Network information. +// Returns an object which can be converted to a JSON or an error if nothing could be collected. +// Tries to collect as much information as possible. +func (network *Network) Collect() (result interface{}, err error) { result, err = getNetworkInfo() if err != nil { return @@ -104,8 +115,6 @@ func getMultiNetworkInfo() (multiNetworkInfo []map[string]interface{}, err error return multiNetworkInfo, err } -type Ipv6Address struct{} - func externalIpv6Address() (string, error) { ifaces, err := net.Interfaces() @@ -148,9 +157,7 @@ func externalIpv6Address() (string, error) { return "", nil } -type IpAddress struct{} - -func externalIpAddress() (string, error) { +func externalIPAddress() (string, error) { ifaces, err := net.Interfaces() if err != nil { @@ -188,8 +195,6 @@ func externalIpAddress() (string, error) { return "", errors.New("not connected to the network") } -type MacAddress struct{} - func macAddress() (string, error) { ifaces, err := net.Interfaces() diff --git a/platform/platform.go b/platform/platform.go index d1dc5ed..568b38a 100644 --- a/platform/platform.go +++ b/platform/platform.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + //go:build !android // +build !android @@ -10,7 +15,10 @@ import ( "github.com/DataDog/gohai/utils" ) -func (self *Platform) Collect() (result interface{}, err error) { +// Collect collects the Platform information. +// Returns an object which can be converted to a JSON or an error if nothing could be collected. +// Tries to collect as much information as possible. +func (platform *Platform) Collect() (result interface{}, err error) { result, _, err = getPlatformInfo() return } @@ -55,7 +63,7 @@ func getPlatformInfo() (platformInfo map[string]string, warnings []string, err e platformInfo = map[string]string{} } - platformInfo["goV"] = strings.Replace(runtime.Version(), "go", "", -1) + platformInfo["goV"] = strings.ReplaceAll(runtime.Version(), "go", "") platformInfo["GOOS"] = runtime.GOOS platformInfo["GOOARCH"] = runtime.GOARCH diff --git a/platform/platform_android.go b/platform/platform_android.go index 6e89e50..ee1ed42 100644 --- a/platform/platform_android.go +++ b/platform/platform_android.go @@ -1,12 +1,23 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + //go:build android // +build android package platform -func (self *Platform) Collect() (interface{}, error) { +// Collects the Platform information. +// Returns an object which can be converted to a JSON or an error if nothing could be collected. +// Tries to collect as much information as possible. +func (platform *Platform) Collect() (interface{}, error) { return nil, nil } +// Get returns a Platform struct already initialized, a list of warnings and an error. The method will try to collect as much +// metadata as possible, an error is returned if nothing could be collected. The list of warnings contains errors if +// some metadata could not be collected. func Get() (*Platform, []string, error) { return nil, nil, nil } diff --git a/platform/platform_common.go b/platform/platform_common.go index 6448c51..159985e 100644 --- a/platform/platform_common.go +++ b/platform/platform_common.go @@ -1,3 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +// Package platform regroups collecting information about the platform package platform // Platform holds metadata about the host @@ -33,6 +39,7 @@ type Platform struct { const name = "platform" -func (self *Platform) Name() string { +// Name returns the name of the package +func (platform *Platform) Name() string { return name } diff --git a/platform/platform_darwin.go b/platform/platform_darwin.go index a9e7a6e..187892a 100644 --- a/platform/platform_darwin.go +++ b/platform/platform_darwin.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package platform import ( diff --git a/platform/platform_linux.go b/platform/platform_linux.go index d39ad8f..827c502 100644 --- a/platform/platform_linux.go +++ b/platform/platform_linux.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package platform import "strings" diff --git a/platform/platform_nix.go b/platform/platform_nix.go index 5c8c260..2330a3c 100644 --- a/platform/platform_nix.go +++ b/platform/platform_nix.go @@ -1,16 +1,20 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + //go:build linux || darwin // +build linux darwin package platform import ( - "fmt" "os/exec" "regexp" "strings" ) -// GetArchInfo() returns basic host architecture information +// GetArchInfo returns basic host architecture information func GetArchInfo() (archInfo map[string]string, err error) { archInfo = map[string]string{} @@ -18,7 +22,7 @@ func GetArchInfo() (archInfo map[string]string, err error) { if err != nil { return nil, err } - line := fmt.Sprintf("%s", out) + line := string(out) values := regexp.MustCompile(" +").Split(line, 7) updateArchInfo(archInfo, values) diff --git a/platform/platform_windows.go b/platform/platform_windows.go index 447f373..f2ab53f 100644 --- a/platform/platform_windows.go +++ b/platform/platform_windows.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package platform import ( @@ -13,8 +18,11 @@ import ( "golang.org/x/sys/windows/registry" ) +// OSVERSIONINFOEXW contains operating system version information. // From winnt.h (see https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-osversioninfoexw) // This is used by https://docs.microsoft.com/en-us/windows/win32/devnotes/rtlgetversion +// +//nolint:revive type OSVERSIONINFOEXW struct { dwOSVersionInfoSize uint32 dwMajorVersion uint32 @@ -30,51 +38,91 @@ type OSVERSIONINFOEXW struct { } var ( - modNetapi32 = windows.NewLazyDLL("Netapi32.dll") - procNetServerGetInfo = modNetapi32.NewProc("NetServerGetInfo") - procNetApiBufferFree = modNetapi32.NewProc("NetApiBufferFree") - ntdll = windows.NewLazyDLL("Ntdll.dll") - procRtlGetVersion = ntdll.NewProc("RtlGetVersion") - winbrand = windows.NewLazyDLL("winbrand.dll") - kernel32 = windows.NewLazyDLL("kernel32.dll") - procIsWow64Process2 = kernel32.NewProc("IsWow64Process2") - ERROR_SUCESS syscall.Errno = 0 + modNetapi32 = windows.NewLazyDLL("Netapi32.dll") + procNetServerGetInfo = modNetapi32.NewProc("NetServerGetInfo") + procNetAPIBufferFree = modNetapi32.NewProc("NetApiBufferFree") + ntdll = windows.NewLazyDLL("Ntdll.dll") + procRtlGetVersion = ntdll.NewProc("RtlGetVersion") + winbrand = windows.NewLazyDLL("winbrand.dll") + kernel32 = windows.NewLazyDLL("kernel32.dll") + procIsWow64Process2 = kernel32.NewProc("IsWow64Process2") + + // ERROR_SUCCESS is the error returned in case of success + // + //nolint:revive + ERROR_SUCCESS syscall.Errno ) +// see https://learn.microsoft.com/en-us/windows/win32/api/lmserver/nf-lmserver-netserverenum +// +//nolint:revive const ( - SV_TYPE_WORKSTATION = uint32(0x00000001) - SV_TYPE_SERVER = uint32(0x00000002) - SV_TYPE_SQLSERVER = uint32(0x00000004) - SV_TYPE_DOMAIN_CTRL = uint32(0x00000008) - SV_TYPE_DOMAIN_BAKCTRL = uint32(0x00000010) - SV_TYPE_TIME_SOURCE = uint32(0x00000020) - SV_TYPE_AFP = uint32(0x00000040) - SV_TYPE_NOVELL = uint32(0x00000080) - SV_TYPE_DOMAIN_MEMBER = uint32(0x00000100) - SV_TYPE_PRINTQ_SERVER = uint32(0x00000200) - SV_TYPE_DIALIN_SERVER = uint32(0x00000400) - SV_TYPE_XENIX_SERVER = uint32(0x00000800) - SV_TYPE_SERVER_UNIX = SV_TYPE_XENIX_SERVER - SV_TYPE_NT = uint32(0x00001000) - SV_TYPE_WFW = uint32(0x00002000) - SV_TYPE_SERVER_MFPN = uint32(0x00004000) - SV_TYPE_SERVER_NT = uint32(0x00008000) + // SV_TYPE_WORKSTATION is for all workstations. + SV_TYPE_WORKSTATION = uint32(0x00000001) + // SV_TYPE_SERVER is for all computers that run the Server service. + SV_TYPE_SERVER = uint32(0x00000002) + // SV_TYPE_SQLSERVER is for any server that runs an instance of Microsoft SQL Server. + SV_TYPE_SQLSERVER = uint32(0x00000004) + // SV_TYPE_DOMAIN_CTRL is for a server that is primary domain controller. + SV_TYPE_DOMAIN_CTRL = uint32(0x00000008) + // SV_TYPE_DOMAIN_BAKCTRL is for any server that is a backup domain controller. + SV_TYPE_DOMAIN_BAKCTRL = uint32(0x00000010) + // SV_TYPE_TIME_SOURCE is for any server that runs the Timesource service. + SV_TYPE_TIME_SOURCE = uint32(0x00000020) + // SV_TYPE_AFP is for any server that runs the Apple Filing Protocol (AFP) file service. + SV_TYPE_AFP = uint32(0x00000040) + // SV_TYPE_NOVELL is for any server that is a Novell server. + SV_TYPE_NOVELL = uint32(0x00000080) + // SV_TYPE_DOMAIN_MEMBER is for any computer that is LAN Manager 2.x domain member. + SV_TYPE_DOMAIN_MEMBER = uint32(0x00000100) + // SV_TYPE_PRINTQ_SERVER is for any computer that shares a print queue. + SV_TYPE_PRINTQ_SERVER = uint32(0x00000200) + // SV_TYPE_DIALIN_SERVER is for any server that runs a dial-in service. + SV_TYPE_DIALIN_SERVER = uint32(0x00000400) + // SV_TYPE_XENIX_SERVER is for any server that is a Xenix server. + SV_TYPE_XENIX_SERVER = uint32(0x00000800) + // SV_TYPE_SERVER_UNIX is for any server that is a UNIX server. This is the same as the SV_TYPE_XENIX_SERVER. + SV_TYPE_SERVER_UNIX = SV_TYPE_XENIX_SERVER + // SV_TYPE_NT is for a workstation or server. + SV_TYPE_NT = uint32(0x00001000) + // SV_TYPE_WFW is for any computer that runs Windows for Workgroups. + SV_TYPE_WFW = uint32(0x00002000) + // SV_TYPE_SERVER_MFPN is for any server that runs the Microsoft File and Print for NetWare service. + SV_TYPE_SERVER_MFPN = uint32(0x00004000) + // SV_TYPE_SERVER_NT is for any server that is not a domain controller. + SV_TYPE_SERVER_NT = uint32(0x00008000) + // SV_TYPE_POTENTIAL_BROWSER is for any computer that can run the browser service. SV_TYPE_POTENTIAL_BROWSER = uint32(0x00010000) - SV_TYPE_BACKUP_BROWSER = uint32(0x00020000) - SV_TYPE_MASTER_BROWSER = uint32(0x00040000) - SV_TYPE_DOMAIN_MASTER = uint32(0x00080000) - SV_TYPE_SERVER_OSF = uint32(0x00100000) - SV_TYPE_SERVER_VMS = uint32(0x00200000) - SV_TYPE_WINDOWS = uint32(0x00400000) /* Windows95 and above */ - SV_TYPE_DFS = uint32(0x00800000) /* Root of a DFS tree */ - SV_TYPE_CLUSTER_NT = uint32(0x01000000) /* NT Cluster */ - SV_TYPE_TERMINALSERVER = uint32(0x02000000) /* Terminal Server(Hydra) */ - SV_TYPE_CLUSTER_VS_NT = uint32(0x04000000) /* NT Cluster Virtual Server Name */ - SV_TYPE_DCE = uint32(0x10000000) /* IBM DSS (Directory and Security Services) or equivalent */ - SV_TYPE_ALTERNATE_XPORT = uint32(0x20000000) /* return list for alternate transport */ - SV_TYPE_LOCAL_LIST_ONLY = uint32(0x40000000) /* Return local list only */ - SV_TYPE_DOMAIN_ENUM = uint32(0x80000000) - SV_TYPE_ALL = uint32(0xFFFFFFFF) /* handy for NetServerEnum2 */ + // SV_TYPE_BACKUP_BROWSER is for a computer that runs a browser service as backup. + SV_TYPE_BACKUP_BROWSER = uint32(0x00020000) + // SV_TYPE_MASTER_BROWSER is for a computer that runs the master browser service. + SV_TYPE_MASTER_BROWSER = uint32(0x00040000) + // SV_TYPE_DOMAIN_MASTER is for a computer that runs the domain master browser. + SV_TYPE_DOMAIN_MASTER = uint32(0x00080000) + // SV_TYPE_SERVER_OSF is for a computer that runs OSF/1. + SV_TYPE_SERVER_OSF = uint32(0x00100000) + // SV_TYPE_SERVER_VMS is for a computer that runs Open Virtual Memory System (VMS). + SV_TYPE_SERVER_VMS = uint32(0x00200000) + // SV_TYPE_WINDOWS is for a computer that runs Windows. + SV_TYPE_WINDOWS = uint32(0x00400000) /* Windows95 and above */ + // SV_TYPE_DFS is for a computer that is the root of Distributed File System (DFS) tree. + SV_TYPE_DFS = uint32(0x00800000) + // SV_TYPE_CLUSTER_NT is for server clusters available in the domain. + SV_TYPE_CLUSTER_NT = uint32(0x01000000) + // SV_TYPE_TERMINALSERVER is for a server running the Terminal Server service. + SV_TYPE_TERMINALSERVER = uint32(0x02000000) + // SV_TYPE_CLUSTER_VS_NT is for cluster virtual servers available in the domain. + SV_TYPE_CLUSTER_VS_NT = uint32(0x04000000) + // SV_TYPE_DCE is for a computer that runs IBM Directory and Security Services (DSS) or equivalent. + SV_TYPE_DCE = uint32(0x10000000) + // SV_TYPE_ALTERNATE_XPORT is for a computer that over an alternate transport. + SV_TYPE_ALTERNATE_XPORT = uint32(0x20000000) + // SV_TYPE_LOCAL_LIST_ONLY is for any computer maintained in a list by the browser. See the following Remarks section. + SV_TYPE_LOCAL_LIST_ONLY = uint32(0x40000000) + // SV_TYPE_DOMAIN_ENUM is for the primary domain. + SV_TYPE_DOMAIN_ENUM = uint32(0x80000000) + // SV_TYPE_ALL is for all servers. This is a convenience that will return all possible servers + SV_TYPE_ALL = uint32(0xFFFFFFFF) /* handy for NetServerEnum2 */ ) const ( @@ -133,7 +181,7 @@ func netServerGetInfo() (si SERVER_INFO_101, err error) { } // ignore free errors //nolint:errcheck - defer procNetApiBufferFree.Call(uintptr(unsafe.Pointer(outdata))) + defer procNetAPIBufferFree.Call(uintptr(unsafe.Pointer(outdata))) return platGetServerInfo(outdata), nil } @@ -146,7 +194,7 @@ func fetchOsDescription() (string, error) { // Encode the string "%WINDOWS_LONG%" to UTF-16 and append a null byte for the Windows API magicString := utf16.Encode([]rune("%WINDOWS_LONG%" + "\x00")) os, _, err := procBrandingFormatString.Call(uintptr(unsafe.Pointer(&magicString[0]))) - if err == ERROR_SUCESS { + if err == ERROR_SUCCESS { // ignore free errors //nolint:errcheck defer syscall.LocalFree(syscall.Handle(os)) @@ -246,7 +294,7 @@ func getNativeArchInfo() string { return nativearch } -// GetArchInfo() returns basic host architecture information +// GetArchInfo returns basic host architecture information func GetArchInfo() (systemInfo map[string]string, err error) { systemInfo = map[string]string{} diff --git a/platform/platform_windows_386.go b/platform/platform_windows_386.go index 0ff8a74..75d4f24 100644 --- a/platform/platform_windows_386.go +++ b/platform/platform_windows_386.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package platform import ( @@ -5,6 +10,10 @@ import ( "unsafe" ) +// WKSTA_INFO_100 contains platform-specific information +// see https://learn.microsoft.com/en-us/windows/win32/api/lmwksta/ns-lmwksta-wksta_info_100 +// +//nolint:revive type WKSTA_INFO_100 struct { wki100_platform_id uint32 wki100_computername string @@ -13,6 +22,10 @@ type WKSTA_INFO_100 struct { wki100_ver_minor uint32 } +// SERVER_INFO_101 contains server-specific information +// see https://learn.microsoft.com/en-us/windows/win32/api/lmserver/ns-lmserver-server_info_101 +// +//nolint:revive type SERVER_INFO_101 struct { sv101_platform_id uint32 sv101_name string @@ -34,12 +47,14 @@ func byteArrayToWksaInfo(data []byte) (info WKSTA_INFO_100) { // ... and again here for the lan group name. //stringptr = (*[]byte)(unsafe.Pointer(uintptr(binary.LittleEndian.Uint64(data[8:])))) - //info.wki100_langroup = convert_windows_string(stringptr) + //info.wki100_langroup = convertWindowsString(stringptr) info.wki100_ver_major = binary.LittleEndian.Uint32(data[12:]) info.wki100_ver_minor = binary.LittleEndian.Uint32(data[16:]) return } + +//nolint:unused func platGetVersion(outdata *byte) (maj uint64, min uint64, err error) { var info WKSTA_INFO_100 var dataptr []byte @@ -57,14 +72,14 @@ func platGetServerInfo(data *byte) (si101 SERVER_INFO_101) { si101.sv101_platform_id = binary.LittleEndian.Uint32(outdata) //stringptr := (*[]uint16)(unsafe.Pointer(uintptr(binary.LittleEndian.Uint64(outdata[4:])))) - //si101.sv101_name = convert_windows_string(*stringptr) + //si101.sv101_name = convertWindowsString(*stringptr) si101.sv101_version_major = binary.LittleEndian.Uint32(outdata[8:]) si101.sv101_version_minor = binary.LittleEndian.Uint32(outdata[12:]) si101.sv101_type = binary.LittleEndian.Uint32(outdata[16:]) //stringptr = (*[]uint16)(unsafe.Pointer(uintptr(binary.LittleEndian.Uint32(outdata[20:])))) - //si101.sv101_comment = convert_windows_string(*stringptr) + //si101.sv101_comment = convertWindowsString(*stringptr) return } diff --git a/platform/platform_windows_amd64.go b/platform/platform_windows_amd64.go index 75d6aa4..59aa9f6 100644 --- a/platform/platform_windows_amd64.go +++ b/platform/platform_windows_amd64.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package platform import ( @@ -5,6 +10,10 @@ import ( "unsafe" ) +// WKSTA_INFO_100 contains platform-specific information +// see https://learn.microsoft.com/en-us/windows/win32/api/lmwksta/ns-lmwksta-wksta_info_100 +// +//nolint:revive,unused type WKSTA_INFO_100 struct { wki100_platform_id uint32 wki100_computername string @@ -13,6 +22,10 @@ type WKSTA_INFO_100 struct { wki100_ver_minor uint32 } +// SERVER_INFO_101 contains server-specific information +// see https://learn.microsoft.com/en-us/windows/win32/api/lmserver/ns-lmserver-server_info_101 +// +//nolint:revive,unused type SERVER_INFO_101 struct { sv101_platform_id uint32 sv101_name string @@ -22,6 +35,7 @@ type SERVER_INFO_101 struct { sv101_comment string } +//nolint:unused func byteArrayToWksaInfo(data []byte) (info WKSTA_INFO_100) { info.wki100_platform_id = binary.LittleEndian.Uint32(data) @@ -37,12 +51,14 @@ func byteArrayToWksaInfo(data []byte) (info WKSTA_INFO_100) { // ... and again here for the lan group name. //stringptr = (*[]byte)(unsafe.Pointer(uintptr(binary.LittleEndian.Uint64(data[16:])))) - //info.wki100_langroup = convert_windows_string(stringptr) + //info.wki100_langroup = convertWindowsString(stringptr) info.wki100_ver_major = binary.LittleEndian.Uint32(data[24:]) info.wki100_ver_minor = binary.LittleEndian.Uint32(data[28:]) return } + +//nolint:unused func platGetVersion(outdata *byte) (maj uint64, min uint64, err error) { var info WKSTA_INFO_100 var dataptr []byte @@ -61,7 +77,7 @@ func platGetServerInfo(data *byte) (si101 SERVER_INFO_101) { // due to 64 bit packing, skip 8 bytes to get to the name string //stringptr := *(*[]uint16)(unsafe.Pointer(uintptr(binary.LittleEndian.Uint64(outdata[8:])))) - //si101.sv101_name = convert_windows_string(stringptr) + //si101.sv101_name = convertWindowsString(stringptr) si101.sv101_version_major = binary.LittleEndian.Uint32(outdata[16:]) si101.sv101_version_minor = binary.LittleEndian.Uint32(outdata[20:]) @@ -69,6 +85,6 @@ func platGetServerInfo(data *byte) (si101 SERVER_INFO_101) { // again skip 4 more for byte packing, so start at 32 //stringptr = (*[]uint16)(unsafe.Pointer(uintptr(binary.LittleEndian.Uint32(outdata[32:])))) - //si101.sv101_comment = convert_windows_string(*stringptr) + //si101.sv101_comment = convertWindowsString(*stringptr) return } diff --git a/processes/gops/gops.go b/processes/gops/gops.go index 9cd9371..f0f3d55 100644 --- a/processes/gops/gops.go +++ b/processes/gops/gops.go @@ -1,3 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +//go:build linux || darwin // +build linux darwin package gops @@ -14,7 +20,7 @@ func minInt(x, y int) int { return y } -// Return an ordered slice of the process groups that use the most RSS +// TopRSSProcessGroups returns an ordered slice of the process groups that use the most RSS func TopRSSProcessGroups(limit int) (ProcessNameGroups, error) { procs, err := GetProcesses() if err != nil { diff --git a/processes/gops/process_group.go b/processes/gops/process_group.go index 5cf47b8..bbd38b9 100644 --- a/processes/gops/process_group.go +++ b/processes/gops/process_group.go @@ -1,3 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +//go:build linux || darwin // +build linux darwin package gops @@ -6,7 +12,7 @@ import ( "sort" ) -// Represents a group of processes, grouped by name +// ProcessNameGroup represents a group of processes, grouped by name type ProcessNameGroup struct { pids []int32 rss uint64 @@ -16,29 +22,35 @@ type ProcessNameGroup struct { usernames map[string]bool } +// ProcessNameGroups represents a list of ProcessNameGroup. type ProcessNameGroups []*ProcessNameGroup +// Pids returns the list of pids in the group. func (pg *ProcessNameGroup) Pids() []int32 { return pg.pids } +// Name returns the name of the group. func (pg *ProcessNameGroup) Name() string { return pg.name } +// RSS returns the RSS used by the group. func (pg *ProcessNameGroup) RSS() uint64 { return pg.rss } +// PctMem returns the percentage of memory used by the group. func (pg *ProcessNameGroup) PctMem() float64 { return pg.pctMem } +// VMS returns the vms of the group. func (pg *ProcessNameGroup) VMS() uint64 { return pg.vms } -// Return a slice of the usernames, sorted alphabetically +// Usernames returns a slice of the usernames, sorted alphabetically func (pg *ProcessNameGroup) Usernames() []string { var usernameStringSlice sort.StringSlice for username := range pg.usernames { @@ -50,6 +62,7 @@ func (pg *ProcessNameGroup) Usernames() []string { return []string(usernameStringSlice) } +// NewProcessNameGroup returns a new empty ProcessNameGroup func NewProcessNameGroup() *ProcessNameGroup { processNameGroup := new(ProcessNameGroup) processNameGroup.usernames = make(map[string]bool) @@ -57,7 +70,7 @@ func NewProcessNameGroup() *ProcessNameGroup { return processNameGroup } -// Group the processInfos by name and return a slice of ProcessNameGroup +// GroupByName groups the processInfos by name and return a slice of ProcessNameGroup func GroupByName(processInfos []*ProcessInfo) ProcessNameGroups { groupIndexByName := make(map[string]int) processNameGroups := make(ProcessNameGroups, 0, 10) @@ -85,19 +98,22 @@ func (pg *ProcessNameGroup) add(p *ProcessInfo) { pg.usernames[p.Username] = true } -// Sort slices of process groups +// Len returns the number of groups func (s ProcessNameGroups) Len() int { return len(s) } +// Swap swaps processes at index i and j func (s ProcessNameGroups) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +// ByRSSDesc is used to sort groups by decreasing RSS. type ByRSSDesc struct { ProcessNameGroups } +// Less returns whether the group at index i uses more RSS than the one at index j. func (s ByRSSDesc) Less(i, j int) bool { return s.ProcessNameGroups[i].RSS() > s.ProcessNameGroups[j].RSS() } diff --git a/processes/gops/process_group_test.go b/processes/gops/process_group_test.go index 4c278ed..33304ec 100644 --- a/processes/gops/process_group_test.go +++ b/processes/gops/process_group_test.go @@ -1,10 +1,17 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +//go:build linux || darwin // +build linux darwin package gops import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestUsernames(t *testing.T) { diff --git a/processes/gops/process_info.go b/processes/gops/process_info.go index dd50f37..08d4fdd 100644 --- a/processes/gops/process_info.go +++ b/processes/gops/process_info.go @@ -1,7 +1,12 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + //go:build linux || darwin // +build linux darwin -// Extract the information on running processes from gopsutil +// Package gops extracts the information on running processes from gopsutil package gops import ( @@ -16,6 +21,7 @@ import ( "github.com/shirou/gopsutil/v3/process" ) +// ProcessInfo contains information about a single process type ProcessInfo struct { PID int32 PPID int32 @@ -26,20 +32,20 @@ type ProcessInfo struct { Username string } -// Return a slice of all the processes that are running +// GetProcesses returns a slice of all the processes that are running func GetProcesses() ([]*ProcessInfo, error) { processInfos := make([]*ProcessInfo, 0, 10) virtMemStat, err := mem.VirtualMemory() if err != nil { - err = fmt.Errorf("Error fetching system memory stats: %s", err) + err = fmt.Errorf("error fetching system memory stats: %w", err) return nil, err } totalMem := float64(virtMemStat.Total) pids, err := process.Pids() if err != nil { - err = fmt.Errorf("Error fetching PIDs: %s", err) + err = fmt.Errorf("error fetching PIDs: %w", err) return nil, err } @@ -47,12 +53,12 @@ func GetProcesses() ([]*ProcessInfo, error) { p, err := process.NewProcess(pid) if err != nil { // an error can occur here only if the process has disappeared, - log.Debugf("Process with pid %d disappeared while scanning: %s", pid, err) + log.Debugf("Process with pid %d disappeared while scanning: %w", pid, err) continue } processInfo, err := newProcessInfo(p, totalMem) if err != nil { - log.Debugf("Error fetching info for pid %d: %s", pid, err) + log.Debugf("Error fetching info for pid %d: %w", pid, err) continue } diff --git a/processes/gops/process_info_darwin.go b/processes/gops/process_info_darwin.go index 8b52e70..16d0eaa 100644 --- a/processes/gops/process_info_darwin.go +++ b/processes/gops/process_info_darwin.go @@ -1,4 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package gops // Do nothing for now -func postProcess(processInfos []*ProcessInfo) {} +func postProcess(_ []*ProcessInfo) {} diff --git a/processes/gops/process_info_linux.go b/processes/gops/process_info_linux.go index f3511b0..33b86c5 100644 --- a/processes/gops/process_info_linux.go +++ b/processes/gops/process_info_linux.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package gops // Build a hash pid -> ppid diff --git a/processes/processes.go b/processes/processes.go index cd88e24..f1ba213 100644 --- a/processes/processes.go +++ b/processes/processes.go @@ -1,5 +1,12 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +//go:build linux || darwin // +build linux darwin +// Package processes regroups collecting information about existing processes package processes import ( @@ -9,9 +16,12 @@ import ( "github.com/DataDog/gohai/processes/gops" ) +// ProcessField is an untyped representation of a process group, +// compatible with the legacy "processes" resource check. type ProcessField [7]interface{} -// Return a JSON payload that's compatible with the legacy "processes" resource check +// getProcesses return a JSON payload which is compatible with +// the legacy "processes" resource check func getProcesses(limit int) ([]interface{}, error) { processGroups, err := gops.TopRSSProcessGroups(limit) if err != nil { diff --git a/processes/processes_common.go b/processes/processes_common.go index 6fdbf5b..1896ba1 100644 --- a/processes/processes_common.go +++ b/processes/processes_common.go @@ -1,3 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +// Package processes regroups collecting information about running processes. package processes import "flag" @@ -6,6 +12,7 @@ var options struct { limit int } +// Processes is the Collector type of the processes package. type Processes struct{} const name = "processes" @@ -14,11 +21,15 @@ func init() { flag.IntVar(&options.limit, name+"-limit", 20, "Number of process groups to return") } -func (self *Processes) Name() string { +// Name returns the name of the package +func (processes *Processes) Name() string { return name } -func (self *Processes) Collect() (result interface{}, err error) { +// Collect collects the processes information. +// Returns an object which can be converted to a JSON or an error if nothing could be collected. +// Tries to collect as much information as possible. +func (processes *Processes) Collect() (result interface{}, err error) { // even if getProcesses returns nil, simply assigning to result // will have a non-nil return, because it has a valid inner // type (more info here: https://golang.org/doc/faq#nil_error ) diff --git a/processes/processes_windows.go b/processes/processes_windows.go index 51e81d6..8694919 100644 --- a/processes/processes_windows.go +++ b/processes/processes_windows.go @@ -1,7 +1,12 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package processes import "errors" -func getProcesses(limit int) ([]interface{}, error) { +func getProcesses(_ int) ([]interface{}, error) { return nil, errors.New("Not implemented on Windows") } diff --git a/utils/cmd.go b/utils/cmd.go index 262fc0b..8fa8e37 100644 --- a/utils/cmd.go +++ b/utils/cmd.go @@ -1,3 +1,9 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + +// Package utils contains helper functions used across the project package utils import ( diff --git a/utils/dict_helpers.go b/utils/dict_helpers.go index 521ebea..b6b686c 100644 --- a/utils/dict_helpers.go +++ b/utils/dict_helpers.go @@ -1,3 +1,8 @@ +// This file is licensed under the MIT License. +// This product includes software developed at Datadog (https://www.datadoghq.com/). +// Copyright © 2015 Kentaro Kuribayashi +// Copyright 2014-present Datadog, Inc. + package utils import ( @@ -41,7 +46,7 @@ func GetUint64(dict map[string]string, key string, warnings *[]string) uint64 { // GetFloat64 returns the dict[key] or an empty uint64 if the entry doesn't exists func GetFloat64(dict map[string]string, key string, warnings *[]string) float64 { if v, ok := dict[key]; ok { - num, err := strconv.ParseFloat(v, 10) + num, err := strconv.ParseFloat(v, 64) if err == nil { return num }