From be9ee46c20bca88402bf380dd04ded0ce3c09c6a Mon Sep 17 00:00:00 2001 From: Dominik Honnef Date: Thu, 29 Dec 2016 17:06:47 +0100 Subject: [PATCH] Add docs and license --- LICENSE | 20 ++++++++++++++++++++ README.md | 4 ++++ nut.go | 13 +++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..dfd0314 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2016 Dominik Honnef + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ca004e7 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +Package nut implements the Network UPS Tools network protocol. + +This package only implements those functions necessary for the +nut_exporter; it is therefore not complete. diff --git a/nut.go b/nut.go index 3f89be1..0376aa9 100644 --- a/nut.go +++ b/nut.go @@ -1,3 +1,7 @@ +// Package nut implements the Network UPS Tools network protocol. +// +// This package only implements those functions necessary for the +// nut_exporter; it is therefore not complete. package nut import ( @@ -12,11 +16,14 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +// A Client wraps a connection to a NUT server. type Client struct { conn net.Conn br *bufio.Reader } +// Dial dials a NUT server using TCP. If the address does not contain +// a port number, it will default to 3493. func Dial(addr string) (*Client, error) { _, _, err := net.SplitHostPort(addr) if err != nil { @@ -30,10 +37,12 @@ func Dial(addr string) (*Client, error) { return NewClient(conn), nil } +// NewClient wraps an existing net.Conn. func NewClient(conn net.Conn) *Client { return &Client{conn, bufio.NewReader(conn)} } +// Close closes the connection. func (c *Client) Close() error { return c.conn.Close() } @@ -71,6 +80,7 @@ func (c *Client) list(typ string) ([]string, error) { return lines, nil } +// UPSs returns a list of all UPSs on the server. func (c *Client) UPSs() ([]string, error) { lines, err := c.list("UPS") if err != nil { @@ -89,6 +99,7 @@ func (c *Client) UPSs() ([]string, error) { return upss, nil } +// Variables returns all variables and their values for a UPS. func (c *Client) Variables(ups string) (map[string]string, error) { lines, err := c.list("VAR " + ups) if err != nil { @@ -204,6 +215,8 @@ var descriptions = map[string]struct { "battery.packs.bad": {"battery_packs_bad", "Number of bad battery packs"}, } +// NewCollector returns a Prometheus collector, collecting statistics +// from all UPSs on the hosts. func NewCollector(hosts []string) prometheus.Collector { const namespace = "nut"