Add docs and license

This commit is contained in:
Dominik Honnef
2016-12-29 17:06:47 +01:00
parent a92b6b60b0
commit be9ee46c20
3 changed files with 37 additions and 0 deletions
+20
View File
@@ -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.
+4
View File
@@ -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.
+13
View File
@@ -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"