/nutclient

Simple Go package for interacting with a NUT server

Primary LanguageGoMIT LicenseMIT

nutclient

Go Reference MIT License

This package provides a very simple NUT client for Go.

Usage

To use the package in your program, begin by importing it:

import "github.com/nathan-osman/nutclient"

Next, create a Client instance using nutclient.New().

The Config struct passed to New specifies the address of the NUT server and the name of the UPS you are connecting to. It also allows you to specify the callbacks that will be invoked when power events occur:

c := nutclient.New(&nutclient.Config{
    Addr: "localhost:3493",
    Name: "ups",
    ConnectedFn: func() {
        fmt.Println("Connected!")
    },
    DisconnectedFn: func() {
        fmt.Println("Disconnected!")
    },
    PowerLostFn: func() {
        fmt.Println("Power lost!")
    },
    PowerRestoredFn: func() {
        fmt.Println("Power restored!")
    },
})
defer c.Close()

To obtain all status values, use Status(). For example, to manually obtain the UPS battery status, use:

var statusVars map[string]string
statusVars = c.Status()
if statusVars != nil {
    fmt.Printf(
        "Battery: %s\n",
        statusVars["ups.status"],
    )
}