add icmp check
This commit is contained in:
1 parent
157836f1a0
commit
4cc9ce7cd5
4 files changed
+79
-10
No files matched your search
@@ -1,5 +1,14 @@
|
||||
# Prometheus Latency Exporter
|
||||
|
||||
## Features
|
||||
|
||||
Available checks:
|
||||
|
||||
- TCP
|
||||
- ICMP
|
||||
- Redis
|
||||
- MySQL
|
||||
|
||||
## Build
|
||||
|
||||
Requirements:
|
||||
@@ -20,28 +29,38 @@ make
|
||||
latency-exporter --help
|
||||
Usage of latency-exporter:
|
||||
-a string
|
||||
address to use (default "0.0.0.0")
|
||||
address to use (default "0.0.0.0")
|
||||
-c string
|
||||
configuration file to use (default "config.yml")
|
||||
configuration file to use (default "config.yml")
|
||||
-i int
|
||||
time interval in seconds between two checks (default 30)
|
||||
time interval in seconds between two checks (default 30)
|
||||
-p string
|
||||
port to run on (default "9062")
|
||||
port to run on (default "9062")
|
||||
-d
|
||||
enable debug logs
|
||||
```
|
||||
|
||||
## Configuration Example
|
||||
|
||||
```yml
|
||||
services:
|
||||
# TCP Check
|
||||
- name: pytest
|
||||
host: 127.0.0.1
|
||||
port: 8000
|
||||
|
||||
# Host Ping Check
|
||||
- name: server-test
|
||||
host: 10.9.0.2
|
||||
type: host
|
||||
|
||||
# Redis Check
|
||||
- name: redis-test
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
type: redis
|
||||
|
||||
# MySQL Check
|
||||
- name: mysql-test
|
||||
host: 127.0.0.1
|
||||
port: 3306
|
||||
@@ -49,3 +68,18 @@ services:
|
||||
username: prometheus
|
||||
password: changeme
|
||||
```
|
||||
|
||||
To perform host ping checks with an unprivileged user on Linux, you will need to execute the following command first:
|
||||
|
||||
```bash
|
||||
sudo sysctl -w net.ipv4.ping_group_range="0 2147483647"
|
||||
```
|
||||
|
||||
Or just run it as root.
|
||||
For more info: [Doc](https://www.kernel.org/doc/html/latest/networking/ip-sysctl.html)
|
||||
|
||||
## Acknowledgments
|
||||
|
||||
- [prometheus/client_golang](https://github.com/prometheus/client_golang)
|
||||
- [prometheus-community/pro-bing](https://github.com/prometheus-community/pro-bing)
|
||||
- [go-sql-driver/mysql](https://github.com/go-sql-driver/mysql)
|
||||
@@ -12,11 +12,15 @@ require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.1 // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
|
||||
github.com/prometheus-community/pro-bing v0.3.0 // indirect
|
||||
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
|
||||
github.com/prometheus/common v0.44.0 // indirect
|
||||
github.com/prometheus/procfs v0.11.1 // indirect
|
||||
golang.org/x/net v0.11.0 // indirect
|
||||
golang.org/x/sync v0.3.0 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
)
|
||||
@@ -14,12 +14,16 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
|
||||
github.com/prometheus-community/pro-bing v0.3.0 h1:SFT6gHqXwbItEDJhTkzPWVqU6CLEtqEfNAPp47RUON4=
|
||||
github.com/prometheus-community/pro-bing v0.3.0/go.mod h1:p9dLb9zdmv+eLxWfCT6jESWuDrS+YzpPkQBgysQF8a0=
|
||||
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
|
||||
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
|
||||
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 h1:v7DLqVdK4VrYkVD5diGdl4sxJurKJEMnODWRJlxV9oM=
|
||||
@@ -30,7 +34,11 @@ github.com/prometheus/procfs v0.11.1 h1:xRC8Iq1yyca5ypa9n1EZnWZkt7dwcoRPQwX/5gwa
|
||||
github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/y0/p/ScXhY=
|
||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
|
||||
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
|
||||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/prometheus-community/pro-bing"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
@@ -71,7 +72,10 @@ func main() {
|
||||
}
|
||||
|
||||
func checkLatency() {
|
||||
var config Config
|
||||
var (
|
||||
config Config
|
||||
duration time.Duration
|
||||
)
|
||||
source, err := ioutil.ReadFile(*conf)
|
||||
if err != nil {
|
||||
log.Fatalf("ERROR: Failed to read configuration file, error: %v", err)
|
||||
@@ -84,16 +88,33 @@ func checkLatency() {
|
||||
|
||||
for _, service := range config.Services {
|
||||
if service.Type == "" {
|
||||
service.Type = "tcp"
|
||||
if service.Port == "" {
|
||||
service.Type = "host"
|
||||
} else {
|
||||
service.Type = "tcp"
|
||||
}
|
||||
}
|
||||
if *debug == true {
|
||||
log.Printf("DEBUG: Dialing %s: host: %s, port: %s, type: %s",
|
||||
log.Printf("DEBUG: Dialing [%s]: host: [%s], port: [%s], type: [%s]",
|
||||
service.Name, service.Host, service.Port, service.Type)
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
if strings.ToLower(service.Type) == "host" {
|
||||
pinger, err := probing.NewPinger(service.Host)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Failed to check service: %s, error: %s", service.Name, err)
|
||||
continue
|
||||
}
|
||||
pinger.Count = 3
|
||||
err = pinger.Run()
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Failed to check service: %s, error: %s", service.Name, err)
|
||||
continue
|
||||
}
|
||||
duration = pinger.Statistics().AvgRtt
|
||||
|
||||
if strings.ToLower(service.Type) == "mysql" {
|
||||
} else if strings.ToLower(service.Type) == "mysql" {
|
||||
start := time.Now()
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/", service.Username, service.Password, service.Host, service.Port)
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
@@ -106,8 +127,10 @@ func checkLatency() {
|
||||
continue
|
||||
}
|
||||
defer db.Close()
|
||||
duration = time.Since(start)
|
||||
|
||||
} else {
|
||||
start := time.Now()
|
||||
conn, err := net.Dial("tcp", net.JoinHostPort(service.Host, service.Port))
|
||||
if err != nil {
|
||||
log.Printf("ERROR: Failed to check service: %s, error: %s", service.Name, err)
|
||||
@@ -128,9 +151,9 @@ func checkLatency() {
|
||||
}
|
||||
}
|
||||
conn.Close()
|
||||
duration = time.Since(start)
|
||||
}
|
||||
|
||||
duration := time.Since(start)
|
||||
latency.With(prometheus.Labels{"service": service.Name}).Set(duration.Seconds())
|
||||
if *debug == true {
|
||||
log.Printf("DEBUG: Latency %s: %v seconds", service.Name, duration.Seconds())
|
||||
|
||||
Reference in new issue
Block a user