Merge pull request #92 from shizunge/prometheus

Prometheus roundup the trapped time to the interval
This commit is contained in:
Shizun Ge 2024-01-29 23:08:30 -08:00 committed by GitHub
commit 890b9210c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 21 additions and 15 deletions

View file

@ -41,11 +41,11 @@ jobs:
uses: docker/metadata-action@v5 uses: docker/metadata-action@v5
with: with:
images: | images: |
${{ github.repository }} ${{ github.repository }}-development
ghcr.io/${{ github.repository }} ghcr.io/${{ github.repository }}-development
tags: | tags: |
type=raw,value=dev-{{date 'X'}} type=raw,value=dev-{{date 'X'}}
type=raw,value=development type=raw,value=latest
type=ref,event=branch type=ref,event=branch
type=edge,branch=main type=edge,branch=main
- name: Build and push ${{ github.repository }}:${{ steps.git.outputs.image_tag }} - name: Build and push ${{ github.repository }}:${{ steps.git.outputs.image_tag }}

View file

@ -22,7 +22,7 @@ go build .
Alternatively, you can use the [docker image](https://hub.docker.com/r/shizunge/endlessh-go): Alternatively, you can use the [docker image](https://hub.docker.com/r/shizunge/endlessh-go):
``` ```
sudo docker run -d -p 2222:2222 shizunge/endlessh-go -logtostderr -v=1 docker run -d -p 2222:2222 shizunge/endlessh-go -logtostderr -v=1
``` ```
It listens to port `2222` by default. It listens to port `2222` by default.

View file

@ -5,12 +5,12 @@ This is an example how to setup endlessh-go with the Maxmind GeoIP Database usin
To start the stack, in the _examples_ folder, run: To start the stack, in the _examples_ folder, run:
``` ```
sudo docker-compose up -d docker-compose up -d
``` ```
The GeoIP Database will be saved in a mounted volume in: `./geo-data`. And the endlessh-go container will use this database to do the location lookups. The GeoIP Database will be saved in a mounted volume in: `./geo-data`. And the endlessh-go container will use this database to do the location lookups.
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. 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`. - **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 `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. - **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.

View file

@ -9,12 +9,12 @@ This is an example how to setup endlessh-go, Prometheus, and Grafana using [dock
To start the stack, in the *examples* folder, run: To start the stack, in the *examples* folder, run:
``` ```
sudo docker-compose up -d 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. 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`. * **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 `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. * **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). * **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. * **3000**: Grafana. Go to [http://localhost:3000](http://localhost:3000) in your web browser for Grafana. Use username *examples* and password *examples* to login.

11
main.go
View file

@ -42,23 +42,24 @@ func startSending(maxClients int64, bannerMaxLength int64, records chan<- metric
bytesSent, err := c.Send(bannerMaxLength) bytesSent, err := c.Send(bannerMaxLength)
remoteIpAddr := c.RemoteIpAddr() remoteIpAddr := c.RemoteIpAddr()
localPort := c.LocalPort() localPort := c.LocalPort()
millisecondsSpent := c.MillisecondsSinceLast()
if err != nil { if err != nil {
c.Close() c.Close()
records <- metrics.RecordEntry{ records <- metrics.RecordEntry{
RecordType: metrics.RecordEntryTypeStop, RecordType: metrics.RecordEntryTypeStop,
IpAddr: remoteIpAddr, IpAddr: remoteIpAddr,
LocalPort: localPort, LocalPort: localPort,
MillisecondsSpent: millisecondsSpent,
} }
return return
} }
millisecondsSpent := c.MillisecondsSinceLast()
clients <- c clients <- c
records <- metrics.RecordEntry{ records <- metrics.RecordEntry{
RecordType: metrics.RecordEntryTypeSend, RecordType: metrics.RecordEntryTypeSend,
IpAddr: remoteIpAddr, IpAddr: remoteIpAddr,
LocalPort: localPort, LocalPort: localPort,
BytesSent: bytesSent,
MillisecondsSpent: millisecondsSpent, MillisecondsSpent: millisecondsSpent,
BytesSent: bytesSent,
} }
}() }()
} }

View file

@ -106,8 +106,8 @@ type RecordEntry struct {
RecordType int RecordType int
IpAddr string IpAddr string
LocalPort string LocalPort string
BytesSent int
MillisecondsSpent int64 MillisecondsSpent int64
BytesSent int
} }
func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUnseenSeconds int, geoOption geoip.GeoOption) chan RecordEntry { func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUnseenSeconds int, geoOption geoip.GeoOption) chan RecordEntry {
@ -140,10 +140,15 @@ func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUns
clientSeconds.With(prometheus.Labels{ clientSeconds.With(prometheus.Labels{
"ip": r.IpAddr, "ip": r.IpAddr,
"local_port": r.LocalPort}).Add(secondsSpent) "local_port": r.LocalPort}).Add(secondsSpent)
totalBytes.With(prometheus.Labels{"local_port": r.LocalPort}).Add(float64(r.BytesSent))
totalSeconds.With(prometheus.Labels{"local_port": r.LocalPort}).Add(secondsSpent) totalSeconds.With(prometheus.Labels{"local_port": r.LocalPort}).Add(secondsSpent)
totalBytes.With(prometheus.Labels{"local_port": r.LocalPort}).Add(float64(r.BytesSent))
pq.Update(r.IpAddr, time.Now()) pq.Update(r.IpAddr, time.Now())
case RecordEntryTypeStop: case RecordEntryTypeStop:
secondsSpent := float64(r.MillisecondsSpent) / 1000
clientSeconds.With(prometheus.Labels{
"ip": r.IpAddr,
"local_port": r.LocalPort}).Add(secondsSpent)
totalSeconds.With(prometheus.Labels{"local_port": r.LocalPort}).Add(secondsSpent)
totalClientsClosed.With(prometheus.Labels{"local_port": r.LocalPort}).Inc() totalClientsClosed.With(prometheus.Labels{"local_port": r.LocalPort}).Inc()
pq.Update(r.IpAddr, time.Now()) pq.Update(r.IpAddr, time.Now())
case RecordEntryTypeClean: case RecordEntryTypeClean: