From fad9a4cdedafe0ace72eca74c43fbdff4aaf3286 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Mon, 11 Jul 2022 18:41:57 -0700 Subject: [PATCH 1/3] allow bind prometheus to a different interface than the ssh one. --- README.md | 6 ++++-- main.go | 16 ++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4267145..1fd22fe 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Usage of ./endlessh-go -geoip_supplier string Supplier to obtain Geohash of IPs. Possible values are "off", "ip-api", "freegeoip", "max-mind-db" (default "off") -host string - Listening address (default "0.0.0.0") + SSH listening address (default "0.0.0.0") -interval_ms int Message millisecond delay (default 1000) -line_length int @@ -68,9 +68,11 @@ Usage of ./endlessh-go -max_mind_db string Path to the MaxMind DB file. -port string - Listening port (default "2222") + SSH listening port (default "2222") -prometheus_entry string Entry point for prometheus (default "metrics") + -prometheus_host string + The address for prometheus (default "0.0.0.0") -prometheus_port string The port for prometheus (default "2112") -stderrthreshold value diff --git a/main.go b/main.go index 49d513e..5b1dc90 100644 --- a/main.go +++ b/main.go @@ -44,7 +44,7 @@ var ( clientSeconds *prometheus.CounterVec ) -func initPrometheus(connHost, prometheusPort, prometheusEntry string) { +func initPrometheus(prometheusHost, prometheusPort, prometheusEntry string) { totalClients = prometheus.NewCounterFunc( prometheus.CounterOpts{ Name: "endlessh_client_open_count_total", @@ -99,8 +99,8 @@ func initPrometheus(connHost, prometheusPort, prometheusEntry string) { prometheus.MustRegister(clientSeconds) http.Handle("/"+prometheusEntry, promhttp.Handler()) go func() { - glog.Infof("Starting Prometheus on %v:%v, entry point is /%v", connHost, prometheusPort, prometheusEntry) - http.ListenAndServe(connHost+":"+prometheusPort, nil) + glog.Infof("Starting Prometheus on %v:%v, entry point is /%v", prometheusHost, prometheusPort, prometheusEntry) + http.ListenAndServe(prometheusHost+":"+prometheusPort, nil) }() } @@ -109,9 +109,10 @@ func main() { bannerMaxLength := flag.Int64("line_length", 32, "Maximum banner line length") maxClients := flag.Int64("max_clients", 4096, "Maximum number of clients") connType := flag.String("conn_type", "tcp", "Connection type. Possible values are tcp, tcp4, tcp6") - connHost := flag.String("host", "0.0.0.0", "Listening address") - connPort := flag.String("port", "2222", "Listening port") + connHost := flag.String("host", "0.0.0.0", "SSH listening address") + connPort := flag.String("port", "2222", "SSH listening port") prometheusEnabled := flag.Bool("enable_prometheus", false, "Enable prometheus") + prometheusHost := flag.String("prometheus_host", "0.0.0.0", "The address for prometheus") prometheusPort := flag.String("prometheus_port", "2112", "The port for prometheus") prometheusEntry := flag.String("prometheus_entry", "metrics", "Entry point for prometheus") geoipSupplier := flag.String("geoip_supplier", "off", "Supplier to obtain Geohash of IPs. Possible values are \"off\", \"ip-api\", \"freegeoip\", \"max-mind-db\"") @@ -123,8 +124,11 @@ func main() { } flag.Parse() + if *connType == "tcp6" && *prometheusHost == "0.0.0.0" { + *prometheusHost = "[::]" + } if *prometheusEnabled { - initPrometheus(*connHost, *prometheusPort, *prometheusEntry) + initPrometheus(*prometheusHost, *prometheusPort, *prometheusEntry) } rand.Seed(time.Now().UnixNano()) From 09833807a8bc8ba87f06f96e6c5ccc3513a4302e Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Sun, 17 Jul 2022 21:07:47 -0700 Subject: [PATCH 2/3] Add GitHub and Grafana links to the dashboard --- dashboard/endlessh.json | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/dashboard/endlessh.json b/dashboard/endlessh.json index e271a3c..0943ced 100755 --- a/dashboard/endlessh.json +++ b/dashboard/endlessh.json @@ -73,14 +73,39 @@ } ] }, - "description": "Dashboard for endlessh (add option All to job and host)", + "description": "Dashboard for endlessh", "editable": true, "fiscalYearStartMonth": 0, "gnetId": 15156, "graphTooltip": 0, "id": null, "iteration": 1645482521172, - "links": [], + "links": [ + { + "asDropdown": false, + "icon": "external link", + "includeVars": false, + "keepTime": false, + "tags": [], + "targetBlank": true, + "title": "GitHub", + "tooltip": "GitHub", + "type": "link", + "url": "https://github.com/shizunge/endlessh-go" + }, + { + "asDropdown": false, + "icon": "external link", + "includeVars": false, + "keepTime": false, + "tags": [], + "targetBlank": true, + "title": "Grafana", + "tooltip": "Grafana Dashboard", + "type": "link", + "url": "https://grafana.com/grafana/dashboards/15156" + } + ], "liveNow": false, "panels": [ { @@ -1461,6 +1486,6 @@ "timezone": "", "title": "Endlessh", "uid": "ATIxYkO7k", - "version": 7, + "version": 8, "weekStart": "" } From 5f597b596b1b5310b09fb4b09ead3cf541356ed5 Mon Sep 17 00:00:00 2001 From: Shizun Ge Date: Sun, 17 Jul 2022 21:13:36 -0700 Subject: [PATCH 3/3] move the example to docker-simple folder --- examples/README.md | 22 ++----------------- examples/docker-simple/README.md | 22 +++++++++++++++++++ .../{ => docker-simple}/docker-compose.yml | 0 .../grafana-datasource.yml | 0 examples/{ => docker-simple}/prometheus.yml | 0 5 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 examples/docker-simple/README.md rename examples/{ => docker-simple}/docker-compose.yml (100%) rename examples/{ => docker-simple}/grafana-datasource.yml (100%) rename examples/{ => docker-simple}/prometheus.yml (100%) diff --git a/examples/README.md b/examples/README.md index 0850dd3..58ab558 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,24 +1,6 @@ # Examples -## docker compose +## docker-simple -This is an example how to setup endlessh-go, Prometheus, and Grafana using [docker compose](https://docs.docker.com/compose/). The reference of the compose file can be found [here](https://docs.docker.com/compose/compose-file/). +An example how to setup endlessh-go, Prometheus, and Grafana using [docker compose](https://docs.docker.com/compose/). -*prometheus.yml* is used as a [Prometheus configuration](https://prometheus.io/docs/prometheus/latest/configuration/configuration/). - -*grafana-datasource.yml* is used to provision a data source for Grafana to ease the setup, though Grafana data source can also be setup manually. - -To start the stack, in the *examples* folder, run: - -``` -sudo docker-compose up -d -``` - -This example exposes the following ports. Except the SSH port, you should not expose other ports to public without protections (not included in this example) in production. - -* **2222**: The SSH port. You may test endlessh-go by running `ssh -p 2222 localhost`. Your SSH client should hang. View the log of endlessh-go by running `sudo docker logs endlessh`. -* **2112**: The Prometheus metrics exported by endlessh-go. Go to [http://localhost:2112/metrics](http://localhost:2112/metrics) in your web browser to view the metrics. -* **9090**: Prometheus web interface. Go to [http://localhost:9090](http://localhost:9090) in your web browser for Prometheus. You can check whether the target of endlessh-go is up (Click Status, then Targets). -* **3000**: Grafana. Go to [http://localhost:3000](http://localhost:3000) in your web browser for Grafana. Use username *examples* and password *examples* to login. - -In this example, we do not provision a dashboard for Grafana. You need to manually load the endlessh-go dashboard, by either importing it from the Grafana.com using ID [15156](https://grafana.com/grafana/dashboards/15156), or pasting the dashboard JSON text to the text area. See the [Grafana documentation](https://grafana.com/docs/grafana/latest/dashboards/export-import/) about import. Then select *Prometheus* as the data source. diff --git a/examples/docker-simple/README.md b/examples/docker-simple/README.md new file mode 100644 index 0000000..18d9d9c --- /dev/null +++ b/examples/docker-simple/README.md @@ -0,0 +1,22 @@ +## docker compose + +This is an example how to setup endlessh-go, Prometheus, and Grafana using [docker compose](https://docs.docker.com/compose/). The reference of the compose file can be found [here](https://docs.docker.com/compose/compose-file/). + +*prometheus.yml* is used as a [Prometheus configuration](https://prometheus.io/docs/prometheus/latest/configuration/configuration/). + +*grafana-datasource.yml* is used to provision a data source for Grafana to ease the setup, though Grafana data source can also be setup manually. + +To start the stack, in the *examples* folder, run: + +``` +sudo docker-compose up -d +``` + +This example exposes the following ports. Except the SSH port, you should not expose other ports to public without protections (not included in this example) in production. + +* **2222**: The SSH port. You may test endlessh-go by running `ssh -p 2222 localhost`. Your SSH client should hang. View the log of endlessh-go by running `sudo docker logs endlessh`. +* **2112**: The Prometheus metrics exported by endlessh-go. Go to [http://localhost:2112/metrics](http://localhost:2112/metrics) in your web browser to view the metrics. +* **9090**: Prometheus web interface. Go to [http://localhost:9090](http://localhost:9090) in your web browser for Prometheus. You can check whether the target of endlessh-go is up (Click Status, then Targets). +* **3000**: Grafana. Go to [http://localhost:3000](http://localhost:3000) in your web browser for Grafana. Use username *examples* and password *examples* to login. + +In this example, we do not provision a dashboard for Grafana. You need to manually load the endlessh-go dashboard, by either importing it from the Grafana.com using ID [15156](https://grafana.com/grafana/dashboards/15156), or pasting the dashboard JSON text to the text area. See the [Grafana documentation](https://grafana.com/docs/grafana/latest/dashboards/export-import/) about import. Then select *Prometheus* as the data source. diff --git a/examples/docker-compose.yml b/examples/docker-simple/docker-compose.yml similarity index 100% rename from examples/docker-compose.yml rename to examples/docker-simple/docker-compose.yml diff --git a/examples/grafana-datasource.yml b/examples/docker-simple/grafana-datasource.yml similarity index 100% rename from examples/grafana-datasource.yml rename to examples/docker-simple/grafana-datasource.yml diff --git a/examples/prometheus.yml b/examples/docker-simple/prometheus.yml similarity index 100% rename from examples/prometheus.yml rename to examples/docker-simple/prometheus.yml