From c7034357900029e05b58d099104b1ad443edac73 Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Tue, 28 Feb 2017 12:44:53 -0400 Subject: [PATCH] Fix all open go lint and vet issues --- collector/collector.go | 6 ++- collector/conntrack_linux.go | 3 +- collector/diskstats_linux.go | 3 +- collector/edac_linux.go | 3 +- collector/entropy_linux.go | 9 ++-- collector/filesystem_common.go | 3 +- collector/filesystem_freebsd.go | 8 ++-- collector/filesystem_linux.go | 6 +-- collector/ganglia/format.go | 8 +++- collector/gmond.go | 2 +- collector/hwmon_linux.go | 8 ++-- collector/infiniband_linux.go | 1 + collector/infiniband_linux_test.go | 4 +- collector/interrupts_common.go | 3 +- collector/ksmd_linux.go | 5 +-- collector/loadavg.go | 2 +- collector/logind_linux.go | 15 +++---- collector/logind_linux_test.go | 16 +++---- collector/mdadm_linux.go | 12 +++--- collector/megacli.go | 4 +- collector/meminfo_numa_linux.go | 3 +- collector/mountstats_linux.go | 3 +- collector/nfs_linux.go | 13 +++--- collector/ntp.go | 4 +- collector/runit.go | 1 + collector/stat_linux.go | 3 +- collector/supervisord.go | 1 + collector/systemd_linux.go | 3 +- collector/tcpstat_linux.go | 69 +++++++++++++++++------------- collector/tcpstat_linux_test.go | 4 +- collector/textfile.go | 6 +-- collector/time.go | 4 +- collector/vmstat_linux.go | 3 +- collector/wifi_linux.go | 1 + collector/zfs.go | 20 ++++----- collector/zfs_linux.go | 4 +- node_exporter.go | 2 +- 37 files changed, 136 insertions(+), 129 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index 1d57db35..accfb8cb 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Exporter is a prometheus exporter using multiple Factories to collect and export system metrics. +// Package collector includes all individual collectors to gather and export system metrics. package collector import ( @@ -19,15 +19,17 @@ import ( "github.com/prometheus/common/log" ) +// Namespace defines the common namespace to be used by all metrics. const Namespace = "node" +// Factories contains the list of all available collectors. var Factories = make(map[string]func() (Collector, error)) func warnDeprecated(collector string) { log.Warnf("The %s collector is deprecated and will be removed in the future!", collector) } -// Interface a collector has to implement. +// Collector is the interface a collector has to implement. type Collector interface { // Get new metrics and expose them via prometheus registry. Update(ch chan<- prometheus.Metric) (err error) diff --git a/collector/conntrack_linux.go b/collector/conntrack_linux.go index 3f435718..6632b4bf 100644 --- a/collector/conntrack_linux.go +++ b/collector/conntrack_linux.go @@ -28,8 +28,7 @@ func init() { Factories["conntrack"] = NewConntrackCollector } -// Takes a prometheus registry and returns a new Collector exposing -// conntrack stats +// NewConntrackCollector returns a new Collector exposing conntrack stats. func NewConntrackCollector() (Collector, error) { return &conntrackCollector{ current: prometheus.NewDesc( diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index 8523e23f..5da7aa6a 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -47,8 +47,7 @@ func init() { Factories["diskstats"] = NewDiskstatsCollector } -// Takes a prometheus registry and returns a new Collector exposing -// disk device stats. +// NewDiskstatsCollector returns a new Collector exposing disk device stats. func NewDiskstatsCollector() (Collector, error) { var diskLabelNames = []string{"device"} diff --git a/collector/edac_linux.go b/collector/edac_linux.go index 8b6f6ab8..0d98ca27 100644 --- a/collector/edac_linux.go +++ b/collector/edac_linux.go @@ -52,8 +52,7 @@ func init() { Factories["edac"] = NewEdacCollector } -// Takes a prometheus registry and returns a new Collector exposing -// edac stats. +// NewEdacCollector returns a new Collector exposing edac stats. func NewEdacCollector() (Collector, error) { return &edacCollector{ ceCount: prometheus.NewDesc( diff --git a/collector/entropy_linux.go b/collector/entropy_linux.go index 0ee6db60..549fdcab 100644 --- a/collector/entropy_linux.go +++ b/collector/entropy_linux.go @@ -22,18 +22,17 @@ import ( ) type entropyCollector struct { - entropy_avail *prometheus.Desc + entropyAvail *prometheus.Desc } func init() { Factories["entropy"] = NewEntropyCollector } -// Takes a prometheus registry and returns a new Collector exposing -// entropy stats +// NewEntropyCollector returns a new Collector exposing entropy stats. func NewEntropyCollector() (Collector, error) { return &entropyCollector{ - entropy_avail: prometheus.NewDesc( + entropyAvail: prometheus.NewDesc( prometheus.BuildFQName(Namespace, "", "entropy_available_bits"), "Bits of available entropy.", nil, nil, @@ -47,7 +46,7 @@ func (c *entropyCollector) Update(ch chan<- prometheus.Metric) (err error) { return fmt.Errorf("couldn't get entropy_avail: %s", err) } ch <- prometheus.MustNewConstMetric( - c.entropy_avail, prometheus.GaugeValue, float64(value)) + c.entropyAvail, prometheus.GaugeValue, float64(value)) return nil } diff --git a/collector/filesystem_common.go b/collector/filesystem_common.go index cc83bb35..859db433 100644 --- a/collector/filesystem_common.go +++ b/collector/filesystem_common.go @@ -64,8 +64,7 @@ func init() { Factories["filesystem"] = NewFilesystemCollector } -// Takes a prometheus registry and returns a new Collector exposing -// Filesystems stats. +// NewFilesystemCollector returns a new Collector exposing filesystems stats. func NewFilesystemCollector() (Collector, error) { subsystem := "filesystem" mountPointPattern := regexp.MustCompile(*ignoredMountPoints) diff --git a/collector/filesystem_freebsd.go b/collector/filesystem_freebsd.go index a18945a5..0afdff40 100644 --- a/collector/filesystem_freebsd.go +++ b/collector/filesystem_freebsd.go @@ -26,8 +26,8 @@ import ( const ( defIgnoredMountPoints = "^/(dev)($|/)" defIgnoredFSTypes = "^devfs$" - MNT_RDONLY = 0x1 - MNT_NOWAIT = 0x2 + readOnly = 0x1 // MNT_RDONLY + noWait = 0x2 // MNT_NOWAIT ) func gostring(b []int8) string { @@ -43,7 +43,7 @@ func gostring(b []int8) string { func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) { buf := make([]unix.Statfs_t, 16) for { - n, err := unix.Getfsstat(buf, MNT_NOWAIT) + n, err := unix.Getfsstat(buf, noWait) if err != nil { return nil, err } @@ -69,7 +69,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) { } var ro float64 - if (fs.Flags & MNT_RDONLY) != 0 { + if (fs.Flags & readOnly) != 0 { ro = 1 } diff --git a/collector/filesystem_linux.go b/collector/filesystem_linux.go index 05435e38..bf679158 100644 --- a/collector/filesystem_linux.go +++ b/collector/filesystem_linux.go @@ -27,10 +27,10 @@ import ( const ( defIgnoredMountPoints = "^/(sys|proc|dev)($|/)" defIgnoredFSTypes = "^(sys|proc|auto)fs$" - ST_RDONLY = 0x1 + readOnly = 0x1 // ST_RDONLY ) -// Expose filesystem fullness. +// GetStats returns filesystem stats. func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) { mps, err := mountPointDetails() if err != nil { @@ -57,7 +57,7 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) { } var ro float64 - if buf.Flags&ST_RDONLY != 0 { + if (buf.Flags & readOnly) != 0 { ro = 1 } diff --git a/collector/ganglia/format.go b/collector/ganglia/format.go index 92c7b790..89ec0440 100644 --- a/collector/ganglia/format.go +++ b/collector/ganglia/format.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Types for unmarshalling gmond's XML output. +// Package ganglia provides types for unmarshalling gmond's XML output. // // Not used elements in gmond's XML output are commented. // In case you want to use them, please change the names so that one @@ -20,15 +20,18 @@ package ganglia import "encoding/xml" +// ExtraElement describes EXTRA_ELEMENT elements. type ExtraElement struct { Name string `xml:"NAME,attr"` Val string `xml:"VAL,attr"` } +// ExtraData describes EXTRA_DATA elements. type ExtraData struct { ExtraElements []ExtraElement `xml:"EXTRA_ELEMENT"` } +// Metric describes METRIC elements. type Metric struct { Name string `xml:"NAME,attr"` Value float64 `xml:"VAL,attr"` @@ -42,6 +45,7 @@ type Metric struct { ExtraData ExtraData `xml:"EXTRA_DATA"` } +// Host describes HOST elements. type Host struct { Name string `xml:"NAME,attr"` /* @@ -57,6 +61,7 @@ type Host struct { Metrics []Metric `xml:"METRIC"` } +// Cluster describes CLUSTER elements. type Cluster struct { Name string `xml:"NAME,attr"` /* @@ -68,6 +73,7 @@ type Cluster struct { Hosts []Host `xml:"HOST"` } +// Ganglia describes the top-level XML structure. type Ganglia struct { XMLNAME xml.Name `xml:"GANGLIA_XML"` Clusters []Cluster `xml:"CLUSTER"` diff --git a/collector/gmond.go b/collector/gmond.go index 09840cd4..a7451946 100644 --- a/collector/gmond.go +++ b/collector/gmond.go @@ -46,7 +46,7 @@ func init() { var illegalCharsRE = regexp.MustCompile(`[^a-zA-Z0-9_]`) -// Takes a prometheus registry and returns a new Collector scraping ganglia. +// NewGmondCollector returns a new Collector scraping ganglia. func NewGmondCollector() (Collector, error) { warnDeprecated("gmond") c := gmondCollector{ diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 07f4d3e9..ec0b0936 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -51,8 +51,8 @@ func init() { type hwMonCollector struct{} -// Takes a prometheus registry and returns a new Collector exposing -// /sys/class/hwmon stats (similar to lm-sensors). +// NewHwMonCollector returns a new Collector exposing /sys/class/hwmon stats +// (similar to lm-sensors). func NewHwMonCollector() (Collector, error) { return &hwMonCollector{}, nil } @@ -78,7 +78,7 @@ func addValueFile(data map[string]map[string]string, sensor string, prop string, data[sensor][prop] = value } -// Split a sensor name into _ +// explodeSensorFilename splits a sensor name into _. func explodeSensorFilename(filename string) (ok bool, sensorType string, sensorNum int, sensorProperty string) { matches := hwmonFilenameFormat.FindStringSubmatch(filename) if len(matches) == 0 { @@ -164,7 +164,7 @@ func (c *hwMonCollector) updateHwmon(ch chan<- prometheus.Metric, dir string) (e ) } - // format all sensors + // Format all sensors. for sensor, sensorData := range data { _, sensorType, _, _ := explodeSensorFilename(sensor) diff --git a/collector/infiniband_linux.go b/collector/infiniband_linux.go index 34ce4dab..d5141053 100644 --- a/collector/infiniband_linux.go +++ b/collector/infiniband_linux.go @@ -45,6 +45,7 @@ func init() { Factories["infiniband"] = NewInfiniBandCollector } +// NewInfiniBandCollector returns a new Collector exposing InfiniBand stats. func NewInfiniBandCollector() (Collector, error) { var i infinibandCollector diff --git a/collector/infiniband_linux_test.go b/collector/infiniband_linux_test.go index 991102d6..68370c05 100644 --- a/collector/infiniband_linux_test.go +++ b/collector/infiniband_linux_test.go @@ -24,7 +24,7 @@ func TestInfiniBandDevices(t *testing.T) { } if l := len(devices); l != 1 { - t.Fatal("Retrieved an unexpected number of InfiniBand devices: %d", l) + t.Fatalf("Retrieved an unexpected number of InfiniBand devices: %d", l) } } @@ -35,6 +35,6 @@ func TestInfiniBandPorts(t *testing.T) { } if l := len(ports); l != 2 { - t.Fatal("Retrieved an unexpected number of InfiniBand ports: %d", l) + t.Fatalf("Retrieved an unexpected number of InfiniBand ports: %d", l) } } diff --git a/collector/interrupts_common.go b/collector/interrupts_common.go index 01b5b3d5..2c8babbd 100644 --- a/collector/interrupts_common.go +++ b/collector/interrupts_common.go @@ -26,8 +26,7 @@ func init() { Factories["interrupts"] = NewInterruptsCollector } -// Takes a prometheus registry and returns a new Collector exposing -// interrupts stats +// NewInterruptsCollector returns a new Collector exposing interrupts stats. func NewInterruptsCollector() (Collector, error) { return &interruptsCollector{ desc: typedDesc{prometheus.NewDesc( diff --git a/collector/ksmd_linux.go b/collector/ksmd_linux.go index 198024e9..30327f6d 100644 --- a/collector/ksmd_linux.go +++ b/collector/ksmd_linux.go @@ -46,8 +46,7 @@ func getCanonicalMetricName(filename string) string { } } -// Takes a prometheus registry and returns a new Collector exposing -// kernel/system statistics. +// NewKsmdCollector returns a new Collector exposing kernel/system statistics. func NewKsmdCollector() (Collector, error) { subsystem := "ksmd" descs := make(map[string]*prometheus.Desc) @@ -60,7 +59,7 @@ func NewKsmdCollector() (Collector, error) { return &ksmdCollector{descs}, nil } -// Expose kernel and system statistics. +// Update implements Collector and exposes kernel and system statistics. func (c *ksmdCollector) Update(ch chan<- prometheus.Metric) (err error) { for _, n := range ksmdFiles { val, err := readUintFromFile(sysFilePath(path.Join("kernel/mm/ksm", n))) diff --git a/collector/loadavg.go b/collector/loadavg.go index 94c6f3bc..e461fd54 100644 --- a/collector/loadavg.go +++ b/collector/loadavg.go @@ -31,7 +31,7 @@ func init() { Factories["loadavg"] = NewLoadavgCollector } -// Take a prometheus registry and return a new Collector exposing load average. +// NewLoadavgCollector returns a new Collector exposing load average stats. func NewLoadavgCollector() (Collector, error) { return &loadavgCollector{ metric: []typedDesc{ diff --git a/collector/logind_linux.go b/collector/logind_linux.go index e7172f1b..cdb87394 100644 --- a/collector/logind_linux.go +++ b/collector/logind_linux.go @@ -65,15 +65,15 @@ type logindSession struct { // Struct elements must be public for the reflection magic of godbus to work. type logindSessionEntry struct { - SessionId string - UserId uint32 + SessionID string + UserID uint32 UserName string - SeatId string + SeatID string SessionObjectPath dbus.ObjectPath } type logindSeatEntry struct { - SeatId string + SeatID string SeatObjectPath dbus.ObjectPath } @@ -81,8 +81,7 @@ func init() { Factories["logind"] = NewLogindCollector } -// Takes a prometheus registry and returns a new Collector exposing -// logind statistics. +// NewLogindCollector returns a new Collector exposing logind statistics. func NewLogindCollector() (Collector, error) { return &logindCollector{}, nil } @@ -197,7 +196,7 @@ func (c *logindDbus) listSeats() ([]string, error) { ret := make([]string, len(seats)+1) for i := range seats { - ret[i] = seats[i].SeatId + ret[i] = seats[i].SeatID } // Always add the empty seat, which is used for remote sessions like SSH ret[len(seats)] = "" @@ -260,7 +259,7 @@ func (c *logindDbus) getSession(session logindSessionEntry) *logindSession { } return &logindSession{ - seat: session.SeatId, + seat: session.SeatID, remote: remote.String(), sessionType: knownStringOrOther(sessionTypeStr, attrTypeValues), class: knownStringOrOther(classStr, attrClassValues), diff --git a/collector/logind_linux_test.go b/collector/logind_linux_test.go index 7c941583..349d5387 100644 --- a/collector/logind_linux_test.go +++ b/collector/logind_linux_test.go @@ -31,17 +31,17 @@ func (c *testLogindInterface) listSeats() ([]string, error) { func (c *testLogindInterface) listSessions() ([]logindSessionEntry, error) { return []logindSessionEntry{ { - SessionId: "1", - UserId: 0, + SessionID: "1", + UserID: 0, UserName: "", - SeatId: "", + SeatID: "", SessionObjectPath: dbus.ObjectPath("/org/freedesktop/login1/session/1"), }, { - SessionId: "2", - UserId: 0, + SessionID: "2", + UserID: 0, UserName: "", - SeatId: "seat0", + SeatID: "seat0", SessionObjectPath: dbus.ObjectPath("/org/freedesktop/login1/session/2"), }, }, nil @@ -50,13 +50,13 @@ func (c *testLogindInterface) listSessions() ([]logindSessionEntry, error) { func (c *testLogindInterface) getSession(session logindSessionEntry) *logindSession { sessions := map[dbus.ObjectPath]*logindSession{ dbus.ObjectPath("/org/freedesktop/login1/session/1"): { - seat: session.SeatId, + seat: session.SeatID, remote: "true", sessionType: knownStringOrOther("tty", attrTypeValues), class: knownStringOrOther("user", attrClassValues), }, dbus.ObjectPath("/org/freedesktop/login1/session/2"): { - seat: session.SeatId, + seat: session.SeatID, remote: "false", sessionType: knownStringOrOther("x11", attrTypeValues), class: knownStringOrOther("greeter", attrClassValues), diff --git a/collector/mdadm_linux.go b/collector/mdadm_linux.go index f820d2b3..281056f5 100644 --- a/collector/mdadm_linux.go +++ b/collector/mdadm_linux.go @@ -56,10 +56,8 @@ func evalStatusline(statusline string) (active, total, size int64, err error) { // +1 to make it more obvious that the whole string containing the info is also returned as matches[0]. if len(matches) < 3+1 { return 0, 0, 0, fmt.Errorf("too few matches found in statusline: %s", statusline) - } else { - if len(matches) > 3+1 { - return 0, 0, 0, fmt.Errorf("too many matches found in statusline: %s", statusline) - } + } else if len(matches) > 3+1 { + return 0, 0, 0, fmt.Errorf("too many matches found in statusline: %s", statusline) } size, err = strconv.ParseInt(matches[1], 10, 64) @@ -109,7 +107,7 @@ func evalUnknownPersonalitylineRE(statusline string) (size int64, err error) { return size, nil } -// Gets the size that has already been synced out of the sync-line. +// evalBuildline gets the size that has already been synced out of the sync-line. func evalBuildline(buildline string) (int64, error) { matches := buildlineRE.FindStringSubmatch(buildline) @@ -131,7 +129,7 @@ func evalBuildline(buildline string) (int64, error) { return syncedSize, nil } -// Parses an mdstat-file and returns a struct with the relevant infos. +// parseMdstat parses an mdstat-file and returns a struct with the relevant infos. func parseMdstat(mdStatusFilePath string) ([]mdStatus, error) { content, err := ioutil.ReadFile(mdStatusFilePath) if err != nil { @@ -234,7 +232,7 @@ func parseMdstat(mdStatusFilePath string) ([]mdStatus, error) { return mdStates, nil } -// Just returns the pointer to an empty struct as we only use throwaway-metrics. +// NewMdadmCollector returns a new Collector exposing raid statistics. func NewMdadmCollector() (Collector, error) { return &mdadmCollector{}, nil } diff --git a/collector/megacli.go b/collector/megacli.go index ac2b7937..f5c7dc83 100644 --- a/collector/megacli.go +++ b/collector/megacli.go @@ -47,8 +47,8 @@ func init() { Factories["megacli"] = NewMegaCliCollector } -// Takes a prometheus registry and returns a new Collector exposing -// RAID status through megacli. +// NewMegaCliCollector returns a new Collector exposing RAID status through +// megacli. func NewMegaCliCollector() (Collector, error) { warnDeprecated("megacli") return &megaCliCollector{ diff --git a/collector/meminfo_numa_linux.go b/collector/meminfo_numa_linux.go index f6310c65..960b3d40 100644 --- a/collector/meminfo_numa_linux.go +++ b/collector/meminfo_numa_linux.go @@ -50,8 +50,7 @@ func init() { Factories["meminfo_numa"] = NewMeminfoNumaCollector } -// Takes a prometheus registry and returns a new Collector exposing -// memory stats. +// NewMeminfoNumaCollector returns a new Collector exposing memory stats. func NewMeminfoNumaCollector() (Collector, error) { return &meminfoNumaCollector{ metricDescs: map[string]*prometheus.Desc{}, diff --git a/collector/mountstats_linux.go b/collector/mountstats_linux.go index 9ee1a254..6119124f 100644 --- a/collector/mountstats_linux.go +++ b/collector/mountstats_linux.go @@ -93,6 +93,7 @@ func init() { Factories["mountstats"] = NewMountStatsCollector } +// NewMountStatsCollector returns a new Collector exposing NFS statistics. func NewMountStatsCollector() (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { @@ -105,7 +106,7 @@ func NewMountStatsCollector() (Collector, error) { } const ( - // For the time being, only NFS statistics are available via this mechanism + // For the time being, only NFS statistics are available via this mechanism. subsystem = "mountstats_nfs" ) diff --git a/collector/nfs_linux.go b/collector/nfs_linux.go index 70083d32..827c0cc7 100644 --- a/collector/nfs_linux.go +++ b/collector/nfs_linux.go @@ -78,19 +78,19 @@ var ( nil, ) - nfsRpcOperationsDesc = prometheus.NewDesc( + nfsRPCOperationsDesc = prometheus.NewDesc( prometheus.BuildFQName(Namespace, "nfs", "rpc_operations"), "Number of RPCs performed.", nil, nil, ) - nfsRpcRetransmissionsDesc = prometheus.NewDesc( + nfsRPCRetransmissionsDesc = prometheus.NewDesc( prometheus.BuildFQName(Namespace, "nfs", "rpc_retransmissions"), "Number of RPC transmissions performed.", nil, nil, ) - nfsRpcAuthenticationRefreshesDesc = prometheus.NewDesc( + nfsRPCAuthenticationRefreshesDesc = prometheus.NewDesc( prometheus.BuildFQName(Namespace, "nfs", "rpc_authentication_refreshes"), "Number of RPC authentication refreshes performed.", nil, @@ -111,6 +111,7 @@ func init() { Factories["nfs"] = NewNfsCollector } +// NewNfsCollector returns a new Collector exposing NFS statistics. func NewNfsCollector() (Collector, error) { return &nfsCollector{}, nil } @@ -145,17 +146,17 @@ func (c *nfsCollector) Update(ch chan<- prometheus.Metric) (err error) { } else if fields := rpcLineRE.FindStringSubmatch(line); fields != nil { value, _ := strconv.ParseFloat(fields[1], 64) ch <- prometheus.MustNewConstMetric( - nfsRpcOperationsDesc, + nfsRPCOperationsDesc, prometheus.CounterValue, value) value, _ = strconv.ParseFloat(fields[2], 64) ch <- prometheus.MustNewConstMetric( - nfsRpcRetransmissionsDesc, + nfsRPCRetransmissionsDesc, prometheus.CounterValue, value) value, _ = strconv.ParseFloat(fields[3], 64) ch <- prometheus.MustNewConstMetric( - nfsRpcAuthenticationRefreshesDesc, + nfsRPCAuthenticationRefreshesDesc, prometheus.CounterValue, value) } else if fields := procLineRE.FindStringSubmatch(line); fields != nil { version := fields[1] diff --git a/collector/ntp.go b/collector/ntp.go index a93c1628..d5391095 100644 --- a/collector/ntp.go +++ b/collector/ntp.go @@ -37,8 +37,8 @@ func init() { Factories["ntp"] = NewNtpCollector } -// Takes a prometheus registry and returns a new Collector exposing -// the offset between ntp and the current system time. +// NewNtpCollector returns a new Collector exposing the offset between ntp and +// the current system time. func NewNtpCollector() (Collector, error) { warnDeprecated("ntp") if *ntpServer == "" { diff --git a/collector/runit.go b/collector/runit.go index 2422f804..eb7d558f 100644 --- a/collector/runit.go +++ b/collector/runit.go @@ -36,6 +36,7 @@ func init() { Factories["runit"] = NewRunitCollector } +// NewRunitCollector returns a new Collector exposing runit statistics. func NewRunitCollector() (Collector, error) { var ( subsystem = "service" diff --git a/collector/stat_linux.go b/collector/stat_linux.go index 0a486814..77929354 100644 --- a/collector/stat_linux.go +++ b/collector/stat_linux.go @@ -42,8 +42,7 @@ func init() { Factories["stat"] = NewStatCollector } -// Takes a prometheus registry and returns a new Collector exposing -// kernel/system statistics. +// NewStatCollector returns a new Collector exposing kernel/system statistics. func NewStatCollector() (Collector, error) { return &statCollector{ cpu: prometheus.NewDesc( diff --git a/collector/supervisord.go b/collector/supervisord.go index a8ee5bf1..848ad767 100644 --- a/collector/supervisord.go +++ b/collector/supervisord.go @@ -39,6 +39,7 @@ func init() { Factories["supervisord"] = NewSupervisordCollector } +// NewSupervisordCollector returns a new Collector exposing supervisord statistics. func NewSupervisordCollector() (Collector, error) { client, err := xmlrpc.NewClient(*supervisordURL, nil) if err != nil { diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index 93b25d49..0b789a45 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go @@ -51,8 +51,7 @@ func init() { Factories["systemd"] = NewSystemdCollector } -// Takes a prometheus registry and returns a new Collector exposing -// systemd statistics. +// NewSystemdCollector returns a new Collector exposing systemd statistics. func NewSystemdCollector() (Collector, error) { const subsystem = "systemd" diff --git a/collector/tcpstat_linux.go b/collector/tcpstat_linux.go index 956bc72d..78cb5e09 100644 --- a/collector/tcpstat_linux.go +++ b/collector/tcpstat_linux.go @@ -26,20 +26,31 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -type TCPConnectionState int +type tcpConnectionState int const ( - TCP_ESTABLISHED TCPConnectionState = iota + 1 - TCP_SYN_SENT - TCP_SYN_RECV - TCP_FIN_WAIT1 - TCP_FIN_WAIT2 - TCP_TIME_WAIT - TCP_CLOSE - TCP_CLOSE_WAIT - TCP_LAST_ACK - TCP_LISTEN - TCP_CLOSING + // TCP_ESTABLISHED + tcpEstablished tcpConnectionState = iota + 1 + // TCP_SYN_SENT + tcpSynSent + // TCP_SYN_RECV + tcpSynRecv + // TCP_FIN_WAIT1 + tcpFinWait1 + // TCP_FIN_WAIT2 + tcpFinWait2 + // TCP_TIME_WAIT + tcpTimeWait + // TCP_CLOSE + tcpClose + // TCP_CLOSE_WAIT + tcpCloseWait + // TCP_LAST_ACK + tcpLastAck + // TCP_LISTEN + tcpListen + // TCP_CLOSING + tcpClosing ) type tcpStatCollector struct { @@ -84,10 +95,10 @@ func (c *tcpStatCollector) Update(ch chan<- prometheus.Metric) (err error) { for st, value := range tcpStats { ch <- c.desc.mustNewConstMetric(value, st.String()) } - return err + return nil } -func getTCPStats(statsFile string) (map[TCPConnectionState]float64, error) { +func getTCPStats(statsFile string) (map[tcpConnectionState]float64, error) { file, err := os.Open(statsFile) if err != nil { return nil, err @@ -97,9 +108,9 @@ func getTCPStats(statsFile string) (map[TCPConnectionState]float64, error) { return parseTCPStats(file) } -func parseTCPStats(r io.Reader) (map[TCPConnectionState]float64, error) { +func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) { var ( - tcpStats = map[TCPConnectionState]float64{} + tcpStats = map[tcpConnectionState]float64{} scanner = bufio.NewScanner(r) ) @@ -116,35 +127,35 @@ func parseTCPStats(r io.Reader) (map[TCPConnectionState]float64, error) { return nil, err } - tcpStats[TCPConnectionState(st)]++ + tcpStats[tcpConnectionState(st)]++ } return tcpStats, nil } -func (st TCPConnectionState) String() string { +func (st tcpConnectionState) String() string { switch st { - case TCP_ESTABLISHED: + case tcpEstablished: return "established" - case TCP_SYN_SENT: + case tcpSynSent: return "syn_sent" - case TCP_SYN_RECV: + case tcpSynRecv: return "syn_recv" - case TCP_FIN_WAIT1: + case tcpFinWait1: return "fin_wait1" - case TCP_FIN_WAIT2: + case tcpFinWait2: return "fin_wait2" - case TCP_TIME_WAIT: + case tcpTimeWait: return "time_wait" - case TCP_CLOSE: + case tcpClose: return "close" - case TCP_CLOSE_WAIT: + case tcpCloseWait: return "close_wait" - case TCP_LAST_ACK: + case tcpLastAck: return "last_ack" - case TCP_LISTEN: + case tcpListen: return "listen" - case TCP_CLOSING: + case tcpClosing: return "closing" default: return "unknown" diff --git a/collector/tcpstat_linux_test.go b/collector/tcpstat_linux_test.go index da0bc1c6..806ca223 100644 --- a/collector/tcpstat_linux_test.go +++ b/collector/tcpstat_linux_test.go @@ -30,11 +30,11 @@ func TestTCPStat(t *testing.T) { t.Fatal(err) } - if want, got := 1, int(tcpStats[TCP_ESTABLISHED]); want != got { + if want, got := 1, int(tcpStats[tcpEstablished]); want != got { t.Errorf("want tcpstat number of established state %d, got %d", want, got) } - if want, got := 1, int(tcpStats[TCP_LISTEN]); want != got { + if want, got := 1, int(tcpStats[tcpListen]); want != got { t.Errorf("want tcpstat number of listen state %d, got %d", want, got) } } diff --git a/collector/textfile.go b/collector/textfile.go index 6d525ae2..a35332fe 100644 --- a/collector/textfile.go +++ b/collector/textfile.go @@ -44,8 +44,8 @@ func init() { Factories["textfile"] = NewTextFileCollector } -// Takes a registers a -// SetMetricFamilyInjectionHook. +// NewTextFileCollector returns a new Collector exposing metrics read from files +// in the given textfile directory. func NewTextFileCollector() (Collector, error) { c := &textFileCollector{ path: *textFileDirectory, @@ -65,7 +65,7 @@ func NewTextFileCollector() (Collector, error) { return c, nil } -// textFile collector works via SetMetricFamilyInjectionHook in parseTextFiles. +// Update implements the Collector interface. func (c *textFileCollector) Update(ch chan<- prometheus.Metric) (err error) { return nil } diff --git a/collector/time.go b/collector/time.go index 5854f7a5..e6f44d75 100644 --- a/collector/time.go +++ b/collector/time.go @@ -30,8 +30,8 @@ func init() { Factories["time"] = NewTimeCollector } -// Takes a prometheus registry and returns a new Collector exposing -// the current system time in seconds since epoch. +// NewTimeCollector returns a new Collector exposing the current system time in +// seconds since epoch. func NewTimeCollector() (Collector, error) { return &timeCollector{ desc: prometheus.NewDesc( diff --git a/collector/vmstat_linux.go b/collector/vmstat_linux.go index 030f5dad..beb41e77 100644 --- a/collector/vmstat_linux.go +++ b/collector/vmstat_linux.go @@ -35,8 +35,7 @@ func init() { Factories["vmstat"] = NewvmStatCollector } -// Takes a prometheus registry and returns a new Collector exposing -// vmstat stats. +// NewvmStatCollector returns a new Collector exposing vmstat stats. func NewvmStatCollector() (Collector, error) { return &vmStatCollector{}, nil } diff --git a/collector/wifi_linux.go b/collector/wifi_linux.go index 959d5f46..eeaeac80 100644 --- a/collector/wifi_linux.go +++ b/collector/wifi_linux.go @@ -56,6 +56,7 @@ type wifiStater interface { StationInfo(ifi *wifi.Interface) (*wifi.StationInfo, error) } +// NewWifiCollector returns a new Collector exposing Wifi statistics. func NewWifiCollector() (Collector, error) { const ( subsystem = "wifi" diff --git a/collector/zfs.go b/collector/zfs.go index 4d4c2370..bbe6cd86 100644 --- a/collector/zfs.go +++ b/collector/zfs.go @@ -24,20 +24,16 @@ import ( "github.com/prometheus/common/log" ) -var zfsNotAvailableError = errors.New("ZFS / ZFS statistics are not available") +var errZFSNotAvailable = errors.New("ZFS / ZFS statistics are not available") type zfsSysctl string -// Metrics - type zfsMetric struct { subsystem string // The Prometheus subsystem name. name string // The Prometheus name of the metric. sysctl zfsSysctl // The sysctl of the ZFS metric. } -// Collector - func init() { Factories["zfs"] = NewZFSCollector } @@ -49,6 +45,7 @@ type zfsCollector struct { linuxPathMap map[string]string } +// NewZFSCollector returns a new Collector exposing ZFS statistics. func NewZFSCollector() (Collector, error) { var z zfsCollector z.linuxProcpathBase = "spl/kstat/zfs" @@ -65,14 +62,13 @@ func NewZFSCollector() (Collector, error) { return &z, nil } -func (c *zfsCollector) Update(ch chan<- prometheus.Metric) (err error) { +func (c *zfsCollector) Update(ch chan<- prometheus.Metric) error { for subsystem := range c.linuxPathMap { - err = c.updateZfsStats(subsystem, ch) - switch { - case err == zfsNotAvailableError: - log.Debug(err) - return nil - case err != nil: + if err := c.updateZfsStats(subsystem, ch); err != nil { + if err == errZFSNotAvailable { + log.Debug(err) + return nil + } return err } } diff --git a/collector/zfs_linux.go b/collector/zfs_linux.go index 2d228373..75190f0c 100644 --- a/collector/zfs_linux.go +++ b/collector/zfs_linux.go @@ -30,7 +30,7 @@ func (c *zfsCollector) openProcFile(path string) (file *os.File, err error) { file, err = os.Open(procFilePath(path)) if err != nil { log.Debugf("Cannot open %q for reading. Is the kernel module loaded?", procFilePath(path)) - err = zfsNotAvailableError + err = errZFSNotAvailable } return } @@ -61,7 +61,7 @@ func (c *zfsCollector) updatePoolStats(ch chan<- prometheus.Metric) (err error) file, err := os.Open(zpoolPath) if err != nil { log.Debugf("Cannot open %q for reading. Is the kernel module loaded?", zpoolPath) - return zfsNotAvailableError + return errZFSNotAvailable } err = c.parsePoolProcfsFile(file, zpoolPath, func(poolName string, s zfsSysctl, v int) { diff --git a/node_exporter.go b/node_exporter.go index 3e87bc43..ec69c053 100644 --- a/node_exporter.go +++ b/node_exporter.go @@ -72,7 +72,7 @@ func (n NodeCollector) Collect(ch chan<- prometheus.Metric) { } func filterAvailableCollectors(collectors string) string { - availableCollectors := make([]string, 0) + var availableCollectors []string for _, c := range strings.Split(collectors, ",") { _, ok := collector.Factories[c] if ok {