From 72d422938f0adeed94db3675764908d8b6b128da Mon Sep 17 00:00:00 2001 From: "PAEPCKE, Michael" Date: Fri, 20 Dec 2024 08:51:04 +0000 Subject: [PATCH] add: individual asset tag (location, inventory, ...) tags for each opn appliance --- README.md | 3 ++- actionOPN.go | 10 +++++----- api.go | 2 +- example-env-config-unifi.sh | 5 +++-- httpd-handler.go | 6 +++--- setup.go | 2 +- srv.go | 26 ++++++++++++++++++++++---- status.go | 10 +++++++--- 8 files changed, 44 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index cda3e20..0718162 100644 --- a/README.md +++ b/README.md @@ -106,10 +106,11 @@ see opnborg-prometheus-grafana.nix # Required - OPN_APIKEY - OPNsense Backup User APIKEY [string, base64 encoded] - OPN_APISECRET - OPNsense Backup User APISECRET [string, base64 encoded] -- OPN_TARGETS - list of OPNSense Target Server to Backup [string, hostnames, comma separated] +- OPN_TARGETS - list of OPNSense Target Server to Backup [string, hostnames, comma separated] [optional: add asset-tag, via # as seprator for each host] - OPN_TARGETS_* - alternative: custom groups for OPNSense Target server [example: OPN_TARGETS_INTRANET="opn-int-01.lan:8443,..."] - OPN_TARGETS_IMGURL_* - alternative: custom image url for customs groups within WebUI [example: OPN_TARGETS_IMGURL_INTRANET="https://paepcke.de/img/intra.png"] + # Optional - OPN_PATH - specify a target path (absolut or releative) to store backups [string: defaults to '.'] - OPN_TLSKEYPIN - OPNsense TLS MitM proof Certificate Keypin [string] diff --git a/actionOPN.go b/actionOPN.go index 0246936..4ab0f31 100644 --- a/actionOPN.go +++ b/actionOPN.go @@ -7,7 +7,7 @@ import ( ) // actionOPN, perform individual server backup -func actionOPN(server string, config *OPNCall, id int, wg *sync.WaitGroup) { +func actionOPN(server, tag string, config *OPNCall, id int, wg *sync.WaitGroup) { // setup defer wg.Done() @@ -51,7 +51,7 @@ func actionOPN(server string, config *OPNCall, id int, wg *sync.WaitGroup) { serverXML, err := fetchXML(server, config) if err != nil { displayChan <- []byte("[BACKUP][ERROR][FAIL:UNABLE-TO-FETCH-XML] " + server + err.Error()) - setOPNStatus(config, server, id, ts, notice, degraded, false) + setOPNStatus(config, server, tag, id, ts, notice, degraded, false) return } @@ -62,7 +62,7 @@ func actionOPN(server string, config *OPNCall, id int, wg *sync.WaitGroup) { if config.Debug { displayChan <- []byte("[BACKUP][SERVER][NO-CHANGE] " + server) } - setOPNStatus(config, server, id, ts, notice, degraded, true) + setOPNStatus(config, server, tag, id, ts, notice, degraded, true) return } @@ -74,9 +74,9 @@ func actionOPN(server string, config *OPNCall, id int, wg *sync.WaitGroup) { // check xml file into storage if err = checkIntoStore(config, server, "xml", serverXML, ts, sum); err != nil { displayChan <- []byte("[BACKUP][ERROR][FAIL:XML-STORE-CHECKIN] " + err.Error()) - setOPNStatus(config, server, id, ts, notice, degraded, false) + setOPNStatus(config, server, tag, id, ts, notice, degraded, false) return } displayChan <- []byte("[BACKUP][OK][SUCCESS:XML-STORE-CHECKIN-OF-MODIFIED-XML]") - setOPNStatus(config, server, id, ts, notice, degraded, true) + setOPNStatus(config, server, tag, id, ts, notice, degraded, true) } diff --git a/api.go b/api.go index b90a65e..06cb408 100644 --- a/api.go +++ b/api.go @@ -6,7 +6,7 @@ import ( ) // global exported consts -const SemVer = "v0.1.50" +const SemVer = "v0.1.51" // global var var ( diff --git a/example-env-config-unifi.sh b/example-env-config-unifi.sh index bb285c1..7e36c9f 100644 --- a/example-env-config-unifi.sh +++ b/example-env-config-unifi.sh @@ -1,6 +1,7 @@ #!/bin/sh -export OPN_TARGETS_STANDBY='opn00.lan:8443' -export OPN_TARGETS_INTRANET='opn01.lan:8443,opn02.lan:8443' +# export OPN_TARGETS='opn00.lan:8443,opn01.lan:8443#RACK-PROD01,opn02.lan:8443#RACK-PROD02' +export OPN_TARGETS_STANDBY='opn00.lan:8443#RACK-LAB' +export OPN_TARGETS_INTRANET='opn01.lan:8443#RACK-PROD01,opn02.lan:8443#RACK-PROD02' export OPN_TARGETS_EXTERNAL='opn03.lan:8443,opn04.lan:8443' export OPN_TARGETS_IMGURL_STANDBY='https://paepcke.de/res/hot.png' export OPN_TARGETS_IMGURL_INTRANET='https://paepcke.de/res/int.png' diff --git a/httpd-handler.go b/httpd-handler.go index afad11b..2bfa0ec 100644 --- a/httpd-handler.go +++ b/httpd-handler.go @@ -122,7 +122,8 @@ func getHive() string { s.WriteString(" ") if grp.OPN { for _, line := range hive { - if strings.Contains(line, srv) { + target := strings.Split(srv, "#") + if strings.Contains(line, target[0]) { s.WriteString(line) break } @@ -187,8 +188,7 @@ func getNavi() string { s.WriteString(_nwin) s.WriteString("> ") } - if unifiWebUI != nil { - // if unifiWebUI != nil && !unifiEnable.Load() { + if unifiWebUI != nil && !unifiEnable.Load() { s.WriteString(" Member: " + server + " Version: n/a Last Seen: n/a
" - hive = append(hive, status) + s := strings.Split(server, "#") + switch len(s) { + case 1: + if len(s[0]) > 0 { + status := _na + " Member: " + s[0] + " Version: n/a Last Seen: n/a
" + hive = append(hive, status) + } + case 2: + if len(s[0]) > 0 && len(s[1]) > 0 { + status := _na + " Member: " + s[0] + " Version: n/a Last Seen: n/a AssetTag: " + s[1] + "
" + hive = append(hive, status) + } + } } } displayChan <- []byte("[SERVICE][OPN-BACKUP-AND-MONITORING]" + state) @@ -96,8 +107,15 @@ func srv(config *OPNCall) error { displayChan <- []byte("[STARTING][BACKUP]") } for id, server := range servers { - wg.Add(1) - go actionOPN(server, config, id, &wg) + s := strings.Split(server, "#") + switch len(s) { + case 1: + wg.Add(1) + go actionOPN(s[0], "", config, id, &wg) + case 2: + wg.Add(1) + go actionOPN(s[0], s[1], config, id, &wg) + } } // wait till all worker done diff --git a/status.go b/status.go index e14667d..488ed70 100644 --- a/status.go +++ b/status.go @@ -17,7 +17,7 @@ const ( ) // setOPNStatus sets the hive member server status -func setOPNStatus(config *OPNCall, server string, id int, ts time.Time, notice string, degraded, ok bool) { +func setOPNStatus(config *OPNCall, server, tag string, id int, ts time.Time, notice string, degraded, ok bool) { year, month, _ := ts.Date() archive := filepath.Join(_archive, strconv.Itoa(year), padMonth(strconv.Itoa(int(month)))) if ok { @@ -35,8 +35,12 @@ func setOPNStatus(config *OPNCall, server string, id int, ts time.Time, notice s linkVS := "
" linkCurrent := "" linkArchive := "" - links := linkCurrent + " " + linkArchive - status := state + _b + linkUI + _b + linkVS + " " + links + "
" + links := " " + linkCurrent + " " + linkArchive + tags := "" + if tag != "" { + tags = " " + } + status := state + _b + linkUI + _b + linkVS + " " + tags + links + "
" hiveMutex.Lock() hive[id] = status hiveMutex.Unlock()