github.com/dim13/unifi@v0.0.0-20230308161331-9b04946f5e93/cmd/list-health/main.go (about)

     1  // Copyright (c) 2014 Dimitri Sokolyuk. All rights reserved.
     2  // Use of this source code is governed by ISC-style license
     3  // that can be found in the LICENSE file.
     4  
     5  // Example command list-health
     6  // list controller health information
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"fmt"
    12  	"log"
    13  	"os"
    14  	"strconv"
    15  	"strings"
    16  	"text/tabwriter"
    17  	"time"
    18  
    19  	"github.com/dim13/unifi"
    20  )
    21  
    22  var (
    23  	host    = flag.String("host", "unifi", "Controller hostname")
    24  	user    = flag.String("user", "admin", "Controller username")
    25  	pass    = flag.String("pass", "unifi", "Controller password")
    26  	version = flag.Int("version", 5, "Controller base version")
    27  	port    = flag.String("port", "8443", "Controller port")
    28  	siteid  = flag.String("siteid", "default", "Sitename or description")
    29  )
    30  
    31  func main() {
    32  	w := new(tabwriter.Writer)
    33  	w.Init(os.Stdout, 0, 8, 3, ' ', 0)
    34  	defer w.Flush()
    35  
    36  	flag.Parse()
    37  	u, err := unifi.Login(*user, *pass, *host, *port, *siteid, *version)
    38  	if err != nil {
    39  		log.Fatal("Login returned error: ", err)
    40  	}
    41  	defer u.Logout()
    42  
    43  	site, err := u.Site(*siteid)
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  
    48  	health, err := u.Health(site)
    49  	if err != nil {
    50  		log.Fatal(err)
    51  	}
    52  
    53  	var p []string
    54  
    55  	// Print LAN health
    56  	fmt.Fprintln(w, "")
    57  	fmt.Fprintln(w, "Subsystem\tStatus\tNumAdopted\tNumSw\tNumDisconnected\tNumPending\tNumUser\tNumGuest\tRxBytesR\tTxBytesR\tLanIP\t")
    58  
    59  	p = []string{
    60  		health.LAN.Subsystem,
    61  		health.LAN.Status,
    62  		strconv.Itoa(health.LAN.NumAdopted),
    63  		strconv.Itoa(health.LAN.NumSw),
    64  		strconv.Itoa(health.LAN.NumDisconnected),
    65  		strconv.Itoa(health.LAN.NumPending),
    66  		strconv.Itoa(health.LAN.NumUser),
    67  		strconv.Itoa(health.LAN.NumGuest),
    68  		strconv.FormatInt(health.LAN.RxBytesR, 10),
    69  		strconv.FormatInt(health.LAN.TxBytesR, 10),
    70  		health.LAN.LanIP,
    71  	}
    72  	fmt.Fprintln(w, strings.Join(p, "\t"))
    73  
    74  	// Print WLAN health
    75  	fmt.Fprintln(w, "")
    76  	fmt.Fprintln(w, "Subsystem\tStatus\tNumAdopted\tNumAp\tNumDisconnected\tNumPending\tNumUser\tNumGuest\tRxBytesR\tTxBytesR")
    77  
    78  	p = []string{
    79  		health.WLAN.Subsystem,
    80  		health.WLAN.Status,
    81  		strconv.Itoa(health.WLAN.NumAdopted),
    82  		strconv.Itoa(health.WLAN.NumAp),
    83  		strconv.Itoa(health.WLAN.NumDisconnected),
    84  		strconv.Itoa(health.WLAN.NumUser),
    85  		strconv.Itoa(health.WLAN.NumGuest),
    86  		strconv.FormatInt(health.WLAN.RxBytesR, 10),
    87  		strconv.FormatInt(health.WLAN.TxBytesR, 10),
    88  		strconv.Itoa(health.WLAN.NumPending),
    89  	}
    90  	fmt.Fprintln(w, strings.Join(p, "\t"))
    91  
    92  	// Print WAN health
    93  	fmt.Fprintln(w, "")
    94  	fmt.Fprintln(w, "Subsystem\tStatus\tNumAdopted\tNumGw\tNumDisconnected\tNumPending\tNumSta\tNumGuest\tRxBytesR\tTxBytesR\tWanIP\tNameServers")
    95  
    96  	p = []string{
    97  		health.WAN.Subsystem,
    98  		health.WAN.Status,
    99  		strconv.Itoa(health.WAN.NumAdopted),
   100  		strconv.Itoa(health.WAN.NumGw),
   101  		strconv.Itoa(health.WAN.NumDisconnected),
   102  		strconv.Itoa(health.WAN.NumPending),
   103  		strconv.Itoa(health.WAN.NumSta),
   104  		strconv.FormatInt(health.WAN.RxBytesR, 10),
   105  		strconv.FormatInt(health.WAN.TxBytesR, 10),
   106  		health.WAN.WanIP,
   107  		strings.Join(health.WAN.Nameservers, " "),
   108  	}
   109  	fmt.Fprintln(w, strings.Join(p, "\t"))
   110  
   111  	// Print WWW health
   112  	fmt.Fprintln(w, "")
   113  	fmt.Fprintln(w, "Subsystem\tStatus\tRxBytesR\tTxBytesR\tDrops\tGwMac\tLatency\tSpeedtestLastrun\tSpeedtestPing\tSpeedtestStatus\tUptime\tXputDown\tXputUp")
   114  
   115  	p = []string{
   116  		health.WWW.Subsystem,
   117  		health.WWW.Status,
   118  		strconv.FormatInt(health.WWW.RxBytesR, 10),
   119  		strconv.FormatInt(health.WWW.TxBytesR, 10),
   120  		strconv.Itoa(health.WWW.Drops),
   121  		health.WWW.GwMac,
   122  		strconv.Itoa(health.WWW.Latency),
   123  		time.Unix(int64(health.WWW.SpeedtestLastrun), 0).String(),
   124  		strconv.Itoa(health.WWW.SpeedtestPing),
   125  		health.WWW.SpeedtestStatus,
   126  		(time.Duration(health.WWW.Uptime) * time.Second).String(),
   127  		strconv.FormatFloat(health.WWW.XputDown, 'f', 6, 64),
   128  		strconv.FormatFloat(health.WWW.XputUp, 'f', 6, 64),
   129  	}
   130  	fmt.Fprintln(w, strings.Join(p, "\t"))
   131  
   132  	// Print VPN health
   133  	fmt.Fprintln(w, "")
   134  	fmt.Fprintln(w, "Subsystem\tStatus")
   135  
   136  	p = []string{
   137  		health.WAN.Subsystem,
   138  		health.WAN.Status,
   139  	}
   140  	fmt.Fprintln(w, strings.Join(p, "\t"))
   141  
   142  }