| 1 |
// Copyright 2023 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package internal |
| 15 |
|
| 16 |
import ( |
| 17 |
"flag" |
| 18 |
"fmt" |
| 19 |
"net" |
| 20 |
"os" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"gopkg.in/yaml.v2" |
| 24 |
) |
| 25 |
|
| 26 |
const WhatsupPort = "99" |
| 27 |
|
| 28 |
var ( |
| 29 |
WhatsAppFlags = flag.NewFlagSet("whatsapp", flag.ExitOnError) |
| 30 |
|
| 31 |
prometheusAddr = WhatsAppFlags.String("prometheus-address", "", "The address of Prometheus to query.") |
| 32 |
traceEndpoint = WhatsAppFlags.String("trace-endpoint", "", "Optional GRPC OTLP endpoint for tracing backend. Set it to 'stdout' to print traces to the output instead.") |
| 33 |
traceSamplingRatio = WhatsAppFlags.Float64("trace-sampling-ratio", 1.0, "Sampling ratio. Currently 1.0 is the best value to use with exemplars.") |
| 34 |
configFile = WhatsAppFlags.String("config-file", "./whatsup.yaml", "YAML configuration with same options as flags here. Flags override the configuration items.") |
| 35 |
) |
| 36 |
|
| 37 |
type Config struct { |
| 38 |
PrometheusAddr string `yaml:"PrometheusAddress,omitempty"` |
| 39 |
TraceEndpoint string `yaml:"TraceEndpoint,omitempty"` |
| 40 |
TraceSamplingRatio float64 `yaml:"TraceSamplingRatio,omitempty"` |
| 41 |
} |
| 42 |
|
| 43 |
func isWSL() bool { |
| 44 |
b, err := os.ReadFile("/proc/version") |
| 45 |
if err != nil { |
| 46 |
return false |
| 47 |
} |
| 48 |
|
| 49 |
if strings.Contains(string(b), "WSL") { |
| 50 |
return true |
| 51 |
} |
| 52 |
|
| 53 |
return false |
| 54 |
} |
| 55 |
|
| 56 |
func getInterfaceIpv4Addr(interfaceName string) (addr string, err error) { |
| 57 |
var ( |
| 58 |
ief *net.Interface |
| 59 |
addrs []net.Addr |
| 60 |
ipv4Addr net.IP |
| 61 |
) |
| 62 |
|
| 63 |
if ief, err = net.InterfaceByName(interfaceName); err != nil { |
| 64 |
return "", err |
| 65 |
} |
| 66 |
|
| 67 |
if addrs, err = ief.Addrs(); err != nil { |
| 68 |
return "", err |
| 69 |
} |
| 70 |
|
| 71 |
for _, addr := range addrs { |
| 72 |
if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil { |
| 73 |
break |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if ipv4Addr == nil { |
| 78 |
return "", fmt.Errorf("interface %s does not have an ipv4 address", interfaceName) |
| 79 |
} |
| 80 |
|
| 81 |
return ipv4Addr.String(), nil |
| 82 |
} |
| 83 |
|
| 84 |
func whatsupAddr(defAddress string) string { |
| 85 |
if a := os.Getenv("HOSTADDR"); a != "" { |
| 86 |
return a + ":" + WhatsupPort |
| 87 |
} |
| 88 |
|
| 89 |
// use eth0 IP address if running WSL, return default if check fails |
| 90 |
if isWSL() { |
| 91 |
a, err := getInterfaceIpv4Addr("eth0") |
| 92 |
if err != nil { |
| 93 |
return defAddress |
| 94 |
} |
| 95 |
|
| 96 |
return a + ":" + WhatsupPort |
| 97 |
} |
| 98 |
|
| 99 |
return defAddress |
| 100 |
} |
| 101 |
|
| 102 |
func ParseOptions(args []string) (Config, error) { |
| 103 |
c := Config{} |
| 104 |
|
| 105 |
if err := WhatsAppFlags.Parse(args); err != nil { |
| 106 |
return c, err |
| 107 |
} |
| 108 |
|
| 109 |
if *configFile != "" { |
| 110 |
b, err := os.ReadFile(*configFile) |
| 111 |
if err != nil { |
| 112 |
return c, err |
| 113 |
} |
| 114 |
if err := yaml.Unmarshal(b, &c); err != nil { |
| 115 |
return c, err |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
if *prometheusAddr != "" { |
| 120 |
c.PrometheusAddr = *prometheusAddr |
| 121 |
} |
| 122 |
if *traceEndpoint != "" { |
| 123 |
c.TraceEndpoint = *traceEndpoint |
| 124 |
} |
| 125 |
if *traceSamplingRatio > 0 { |
| 126 |
c.TraceSamplingRatio = *traceSamplingRatio |
| 127 |
} |
| 128 |
return c, nil |
| 129 |
} |