| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package panos |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "math" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/pkg/metrix" |
| 12 | ) |
| 13 | |
| 14 | func observeStateSet(instrument metrix.StateSetInstrument, active string) { |
| 15 | if active == "" { |
| 16 | return |
| 17 | } |
| 18 | instrument.Enable(active) |
| 19 | } |
| 20 | |
| 21 | func observeStateSetVec(vec metrix.SnapshotStateSetVec, active string, labels ...string) { |
| 22 | if active == "" { |
| 23 | return |
| 24 | } |
| 25 | vec.WithLabelValues(labels...).Enable(active) |
| 26 | } |
| 27 | |
| 28 | func boolState(ok bool, trueState, falseState string) string { |
| 29 | if ok { |
| 30 | return trueState |
| 31 | } |
| 32 | return falseState |
| 33 | } |
| 34 | |
| 35 | func parsePANOSAffirmativeField(field, v string) (bool, error) { |
| 36 | raw := strings.TrimSpace(v) |
| 37 | switch strings.ToLower(raw) { |
| 38 | case "yes", "true", "enabled", "enable", "up", "valid": |
| 39 | return true, nil |
| 40 | case "no", "false", "disabled", "disable", "down", "invalid", "off", "absent", "not present", "not-present": |
| 41 | return false, nil |
| 42 | default: |
| 43 | if raw == "" { |
| 44 | return false, fmt.Errorf("%s: missing status", field) |
| 45 | } |
| 46 | return false, fmt.Errorf("%s: invalid status %q", field, raw) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func normalizeUpDownState(state string) string { |
| 51 | switch strings.ToLower(strings.TrimSpace(state)) { |
| 52 | case "": |
| 53 | return "" |
| 54 | case "up": |
| 55 | return "up" |
| 56 | case "down": |
| 57 | return "down" |
| 58 | default: |
| 59 | return "unknown" |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func alarmState(alarm bool) string { |
| 64 | return boolState(alarm, "alarm", "clear") |
| 65 | } |
| 66 | |
| 67 | func parsePANOSAlarmField(field, v string) (bool, error) { |
| 68 | raw := strings.TrimSpace(v) |
| 69 | switch strings.ToLower(raw) { |
| 70 | case "true", "yes", "on", "active", "alarm", "critical": |
| 71 | return true, nil |
| 72 | case "false", "no", "off", "inactive", "ok", "normal", "clear", "none": |
| 73 | return false, nil |
| 74 | default: |
| 75 | if raw == "" { |
| 76 | return false, fmt.Errorf("%s: missing status", field) |
| 77 | } |
| 78 | return false, fmt.Errorf("%s: invalid status %q", field, raw) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func parsePANOSDecimalField(field, v string, scale int64) (int64, error) { |
| 83 | raw := strings.TrimSpace(v) |
| 84 | v = strings.ReplaceAll(raw, ",", "") |
| 85 | if v == "" { |
| 86 | return 0, nil |
| 87 | } |
| 88 | f, err := strconv.ParseFloat(v, 64) |
| 89 | if err != nil || math.IsInf(f, 0) || math.IsNaN(f) { |
| 90 | return 0, fmt.Errorf("%s: invalid decimal %q", field, raw) |
| 91 | } |
| 92 | return int64(math.Round(f * float64(scale))), nil |
| 93 | } |
| 94 | |
| 95 | func parseRequiredPANOSDecimalField(field, v string, scale int64) (int64, error) { |
| 96 | if strings.TrimSpace(v) == "" { |
| 97 | return 0, fmt.Errorf("%s: missing decimal", field) |
| 98 | } |
| 99 | return parsePANOSDecimalField(field, v, scale) |
| 100 | } |
| 101 | |
| 102 | func panosCommandName(cmd string) string { |
| 103 | switch cmd { |
| 104 | case systemInfoCommand: |
| 105 | return "system info query" |
| 106 | case haStateCommand: |
| 107 | return "HA state query" |
| 108 | case environmentCommand: |
| 109 | return "environmentals query" |
| 110 | case licenseInfoCommand: |
| 111 | return "license info query" |
| 112 | case ipsecSACommand: |
| 113 | return "IPsec SA query" |
| 114 | default: |
| 115 | return bgpCommandName(cmd) |
| 116 | } |
| 117 | } |