mirror of
https://github.com/rwinkhart/uni-nut.git
synced 2026-09-05 16:37:15 -04:00
Add docs and license
This commit is contained in:
@@ -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.
|
||||||
@@ -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.
|
||||||
@@ -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
|
package nut
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -12,11 +16,14 @@ import (
|
|||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A Client wraps a connection to a NUT server.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
br *bufio.Reader
|
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) {
|
func Dial(addr string) (*Client, error) {
|
||||||
_, _, err := net.SplitHostPort(addr)
|
_, _, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -30,10 +37,12 @@ func Dial(addr string) (*Client, error) {
|
|||||||
return NewClient(conn), nil
|
return NewClient(conn), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewClient wraps an existing net.Conn.
|
||||||
func NewClient(conn net.Conn) *Client {
|
func NewClient(conn net.Conn) *Client {
|
||||||
return &Client{conn, bufio.NewReader(conn)}
|
return &Client{conn, bufio.NewReader(conn)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes the connection.
|
||||||
func (c *Client) Close() error {
|
func (c *Client) Close() error {
|
||||||
return c.conn.Close()
|
return c.conn.Close()
|
||||||
}
|
}
|
||||||
@@ -71,6 +80,7 @@ func (c *Client) list(typ string) ([]string, error) {
|
|||||||
return lines, nil
|
return lines, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UPSs returns a list of all UPSs on the server.
|
||||||
func (c *Client) UPSs() ([]string, error) {
|
func (c *Client) UPSs() ([]string, error) {
|
||||||
lines, err := c.list("UPS")
|
lines, err := c.list("UPS")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -89,6 +99,7 @@ func (c *Client) UPSs() ([]string, error) {
|
|||||||
return upss, nil
|
return upss, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Variables returns all variables and their values for a UPS.
|
||||||
func (c *Client) Variables(ups string) (map[string]string, error) {
|
func (c *Client) Variables(ups string) (map[string]string, error) {
|
||||||
lines, err := c.list("VAR " + ups)
|
lines, err := c.list("VAR " + ups)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -204,6 +215,8 @@ var descriptions = map[string]struct {
|
|||||||
"battery.packs.bad": {"battery_packs_bad", "Number of bad battery packs"},
|
"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 {
|
func NewCollector(hosts []string) prometheus.Collector {
|
||||||
const namespace = "nut"
|
const namespace = "nut"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user