70 |
71 |
80 |
81 |
87 |
88 |
89 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # KubePing
2 |
3 | It is a Kubernetes solution designed to monitor the availability of external endpoints from each node of the Kubernetes cluster over TCP, HTTP, and ICMP. It exports Prometheus metrics and has a user-friendly web interface that helps you save time instead of making telnet/curl on each node.
4 |
5 |
6 |
7 |
8 |
9 | ## Use case
10 | In Kubernetes environments, ensuring the accessibility of external endpoints is crucial. Whether it's databases, APIs, or third-party services, connectivity issues can lead to degraded application performance or outages. Traditionally, engineers troubleshoot these issues by manually running telnet, curl, or ping commands on individual nodes. However, this process is time-consuming and inefficient, especially in large-scale clusters.
11 |
12 | __KubePing__ helps to solve these issues🎉
13 |
14 | Imagine a situation where your pods were evicted because of a node failure. The pods were then relocated to new nodes that had recently been added to the cluster. Unfortunately, this caused errors and resulted in service unavailability due to a lack of access to essential external endpoints. It was later discovered that the security department had failed to apply the appropriate access rules to the new cluster nodes.
15 |
16 | This is just one scenario that highlights how KubePing can help you identify potential issues before they escalate into major problems.
17 |
18 | ## How It Works
19 | The solution runs a lightweight DaemonSet in Kubernetes, ensuring that each node has a running instance. These instances probe external endpoints over:
20 |
21 | __TCP__ – Checking port availability (e.g., database:5432, api:443)\
22 | __HTTP__ – Ensuring services respond with the expected status codes\
23 | __ICMP (Ping)__ – Verifying network reachability
24 |
25 | The results are aggregated and exposed as Prometheus metrics:
26 | ```
27 | probe_result{address="api.example.com:8080", instance="worker-node-1", job="kubeping", module="tcp", target="target1"}=1
28 | probe_result{address="api.example.com:8080", instance="worker-node-2", job="kubeping", module="tcp", target="target1"}=0
29 | probe_result{address="api.example.com:8080", instance="worker-node-3", job="kubeping", module="tcp", target="target1"}=1
30 |
31 | probe_result{address="https://example.com", instance="worker-node-1", job="kubeping", module="http", target="target2"}=0
32 | probe_result{address="https://example.com", instance="worker-node-2", job="kubeping", module="http", target="target2"}=1
33 | probe_result{address="https://example.com", instance="worker-node-3", job="kubeping", module="http", target="target2"}=1
34 |
35 | probe_result{address="192.168.0.1", instance="worker-node-1", job="kubeping", module="icmp", target="target3"}=1
36 | probe_result{address="192.168.0.1", instance="worker-node-2", job="kubeping", module="icmp", target="target3"}=0
37 | probe_result{address="192.168.0.1", instance="worker-node-3", job="kubeping", module="icmp", target="target3"}=1
38 | ```
39 |
40 | And instead of SSH-ing into nodes, you can simply visit the web UI, where you can perform an ad-hoc connectivity test:
41 |
42 |
43 |
44 |
45 | Here is how KubePing can be integrated into your workflow:
46 |
47 |
48 |
49 |
50 | ## Installation
51 | ### Helm
52 | Clone repository
53 | ```
54 | git clone https://github.com/teymurgahramanov/kubeping.git && cd kubeping
55 | ```
56 | Install Helm chart
57 | ```
58 | helm upgrade --install kubeping ./helm
59 | ```
60 | Test Web UI
61 | ```
62 | kubectl port-forward svc/kubeping-web 8000:8000
63 | ```
64 | To configure the exporter with static targets, refer to [values.yaml](./helm/values.yaml). Here is an example:
65 | ```yaml
66 | exporter:
67 | config:
68 | exporter:
69 | defaultProbeInterval: 31
70 | defaultProbeTimeout: 13
71 | targets:
72 | target1:
73 | address: api.example.com:8080
74 | module: tcp
75 | timeout: 15
76 | target2:
77 | address: https://example.com
78 | module: http
79 | interval: 60
80 | target3:
81 | address: 192.168.0.1
82 | module: icmp
83 | ```
84 |
85 | ### Prometheus
86 | Example job configuration:
87 | ```yaml
88 | - job_name: kubeping
89 | kubernetes_sd_configs:
90 | - role: endpoints
91 | relabel_configs:
92 | - source_labels: [__meta_kubernetes_endpoints_name]
93 | regex: kubeping-exporter
94 | action: keep
95 | - source_labels: [__meta_kubernetes_endpoint_node_name]
96 | action: replace
97 | target_label: instance
98 | ```
99 |
--------------------------------------------------------------------------------
/web/static/styles.css:
--------------------------------------------------------------------------------
1 | :root {
2 | --primary-color: #4f46e5;
3 | --primary-hover: #4338ca;
4 | --success-color: #22c55e;
5 | --error-color: #ef4444;
6 | --background-color: #f8fafc;
7 | --card-background: #ffffff;
8 | --text-color: #1e293b;
9 | --text-secondary: #64748b;
10 | --border-color: #e2e8f0;
11 | --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
12 | --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
13 | }
14 |
15 | * {
16 | margin: 0;
17 | padding: 0;
18 | box-sizing: border-box;
19 | }
20 |
21 | body {
22 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
23 | background-color: var(--background-color);
24 | color: var(--text-color);
25 | line-height: 1.5;
26 | min-height: 100vh;
27 | display: flex;
28 | flex-direction: column;
29 | }
30 |
31 | .container {
32 | max-width: 1200px;
33 | margin: 0 auto;
34 | padding: 2rem;
35 | flex: 1;
36 | }
37 |
38 | .header {
39 | text-align: center;
40 | margin-bottom: 2rem;
41 | }
42 |
43 | .header h1 {
44 | font-size: 2.5rem;
45 | color: var(--primary-color);
46 | display: flex;
47 | align-items: center;
48 | justify-content: center;
49 | gap: 0.75rem;
50 | }
51 |
52 | .card {
53 | background: var(--card-background);
54 | border-radius: 12px;
55 | padding: 2rem;
56 | box-shadow: var(--shadow-md);
57 | margin-bottom: 2rem;
58 | transition: transform 0.2s ease-in-out;
59 | }
60 |
61 | .card:hover {
62 | transform: translateY(-2px);
63 | }
64 |
65 | .form-row {
66 | display: grid;
67 | grid-template-columns: 1fr 1fr;
68 | gap: 1.5rem;
69 | margin-bottom: 1.5rem;
70 | }
71 |
72 | @media (max-width: 640px) {
73 | .form-row {
74 | grid-template-columns: 1fr;
75 | }
76 | }
77 |
78 | .form-group {
79 | display: flex;
80 | flex-direction: column;
81 | gap: 0.5rem;
82 | }
83 |
84 | label {
85 | font-weight: 500;
86 | color: var(--text-color);
87 | display: flex;
88 | align-items: center;
89 | gap: 0.5rem;
90 | }
91 |
92 | input {
93 | padding: 0.75rem 1rem;
94 | border: 1px solid var(--border-color);
95 | border-radius: 8px;
96 | font-size: 1rem;
97 | transition: all 0.2s ease;
98 | }
99 |
100 | input:focus {
101 | outline: none;
102 | border-color: var(--primary-color);
103 | box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
104 | }
105 |
106 | .form-submit {
107 | text-align: center;
108 | }
109 |
110 | button {
111 | background-color: var(--primary-color);
112 | color: white;
113 | border: none;
114 | padding: 0.75rem 2rem;
115 | border-radius: 8px;
116 | font-size: 1rem;
117 | font-weight: 500;
118 | cursor: pointer;
119 | transition: all 0.2s ease;
120 | display: inline-flex;
121 | align-items: center;
122 | gap: 0.5rem;
123 | }
124 |
125 | button:hover {
126 | background-color: var(--primary-hover);
127 | transform: translateY(-1px);
128 | }
129 |
130 | button .btn-loading {
131 | display: none;
132 | }
133 |
134 | button.loading .btn-content {
135 | display: none;
136 | }
137 |
138 | button.loading .btn-loading {
139 | display: inline-flex;
140 | align-items: center;
141 | gap: 0.5rem;
142 | }
143 |
144 | .results-container {
145 | margin-top: 3rem;
146 | }
147 |
148 | .results-container h2 {
149 | color: var(--text-color);
150 | margin-bottom: 1.5rem;
151 | display: flex;
152 | align-items: center;
153 | gap: 0.75rem;
154 | }
155 |
156 | .results-grid {
157 | display: grid;
158 | grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
159 | gap: 1rem;
160 | }
161 |
162 | .result-card {
163 | background: var(--card-background);
164 | border-radius: 10px;
165 | padding: 1.5rem;
166 | box-shadow: var(--shadow-sm);
167 | transition: transform 0.2s ease;
168 | }
169 |
170 | .result-card:hover {
171 | transform: translateY(-2px);
172 | }
173 |
174 | .result-card.success {
175 | border-left: 4px solid var(--success-color);
176 | }
177 |
178 | .result-card.error {
179 | border-left: 4px solid var(--error-color);
180 | }
181 |
182 | .result-header {
183 | display: flex;
184 | align-items: center;
185 | gap: 0.75rem;
186 | margin-bottom: 1rem;
187 | color: var(--text-secondary);
188 | }
189 |
190 | .result-content {
191 | font-size: 1rem;
192 | word-break: break-word;
193 | }
194 |
195 | .success-icon {
196 | font-size: 1.5rem;
197 | }
198 |
199 | .footer {
200 | text-align: center;
201 | padding: 2rem;
202 | background-color: var(--card-background);
203 | border-top: 1px solid var(--border-color);
204 | }
205 |
206 | .footer p {
207 | display: flex;
208 | align-items: center;
209 | justify-content: center;
210 | gap: 1rem;
211 | color: var(--text-secondary);
212 | }
213 |
214 | .footer a {
215 | color: var(--primary-color);
216 | text-decoration: none;
217 | display: flex;
218 | align-items: center;
219 | gap: 0.5rem;
220 | transition: color 0.2s ease;
221 | }
222 |
223 | .footer a:hover {
224 | color: var(--primary-hover);
225 | }
226 |
227 | .separator {
228 | color: var(--border-color);
229 | }
--------------------------------------------------------------------------------
/exporter/go.sum:
--------------------------------------------------------------------------------
1 | github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
2 | github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
3 | github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
4 | github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
5 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
6 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
7 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8 | github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
9 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
10 | github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
11 | github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
12 | github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
13 | github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
14 | github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
15 | github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
16 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
17 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
18 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
19 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
20 | github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
21 | github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
22 | github.com/prometheus-community/pro-bing v0.3.0 h1:SFT6gHqXwbItEDJhTkzPWVqU6CLEtqEfNAPp47RUON4=
23 | github.com/prometheus-community/pro-bing v0.3.0/go.mod h1:p9dLb9zdmv+eLxWfCT6jESWuDrS+YzpPkQBgysQF8a0=
24 | github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk=
25 | github.com/prometheus/client_golang v1.18.0/go.mod h1:T+GXkCk5wSJyOqMIzVgvvjFDlkOQntgjkJWKrN5txjA=
26 | github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
27 | github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
28 | github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
29 | github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
30 | github.com/prometheus/common v0.46.0 h1:doXzt5ybi1HBKpsZOL0sSkaNHJJqkyfEWZGGqqScV0Y=
31 | github.com/prometheus/common v0.46.0/go.mod h1:Tp0qkxpb9Jsg54QMe+EAmqXkSV7Evdy1BTn+g2pa/hQ=
32 | github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
33 | github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
34 | github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
35 | github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
36 | golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
37 | golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
38 | golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
39 | golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
40 | golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
41 | golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
42 | golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
43 | golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
44 | golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
45 | golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
46 | golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
47 | golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
48 | golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
49 | golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
50 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
51 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
52 | google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
53 | google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
54 | google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
55 | google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
56 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
57 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
58 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
59 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
60 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
61 |
--------------------------------------------------------------------------------
/exporter/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "encoding/json"
5 | "fmt"
6 | "log/slog"
7 | "net/http"
8 | "os"
9 | "sync"
10 | "time"
11 |
12 | "github.com/prometheus/client_golang/prometheus"
13 | "github.com/prometheus/client_golang/prometheus/promhttp"
14 | "github.com/teymurgahramanov/KubePing/exporter/modules"
15 | "gopkg.in/yaml.v3"
16 | )
17 |
18 | type configuration struct {
19 | Targets map[string]targetConfig `yaml:"targets"`
20 | Exporter exporterConfig `yaml:"exporter"`
21 | }
22 |
23 | type targetConfig struct {
24 | Address string `yaml:"address"`
25 | Module string `yaml:"module"`
26 | Interval int `yaml:"interval"`
27 | Timeout int `yaml:"timeout"`
28 | }
29 |
30 | type exporterConfig struct {
31 | ListenPort int `yaml:"listenPort"`
32 | DefaultProbeInterval int `yaml:"defaultProbeInterval"`
33 | DefaultProbeTimeout int `yaml:"defaultProbeTimeout"`
34 | }
35 |
36 | type probeRequest struct {
37 | Module string `json:"module"`
38 | Address string `json:"address"`
39 | Timeout int `json:"timeout"`
40 | }
41 |
42 | type probeResponse struct {
43 | Result bool `json:"result"`
44 | Error string `json:"error,omitempty"`
45 | }
46 |
47 | func main() {
48 | logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
49 | configFile := "config.yaml"
50 | var config configuration
51 |
52 | // **Ensure Targets map is always initialized**
53 | config.Targets = make(map[string]targetConfig)
54 |
55 | // Set default exporter values
56 | config.Exporter.ListenPort = 8000
57 | config.Exporter.DefaultProbeInterval = 30
58 | config.Exporter.DefaultProbeTimeout = 5
59 |
60 | // Try reading the config file
61 | data, err := os.ReadFile(configFile)
62 | if err != nil {
63 | if os.IsNotExist(err) {
64 | logger.Error(fmt.Sprintf("Config file %s not found, proceeding with defaults", configFile))
65 | } else {
66 | logger.Error(fmt.Sprintf("Failed to read config file %s: %v", configFile, err))
67 | }
68 | } else {
69 | err = yaml.Unmarshal(data, &config)
70 | if err != nil {
71 | logger.Error(fmt.Sprintf("Failed to parse config file %s: %v", configFile, err))
72 | }
73 | }
74 |
75 | // Ensure default values are applied if missing in the config file
76 | if config.Exporter.ListenPort == 0 {
77 | config.Exporter.ListenPort = 8000
78 | }
79 | if config.Exporter.DefaultProbeInterval == 0 {
80 | config.Exporter.DefaultProbeInterval = 30
81 | }
82 | if config.Exporter.DefaultProbeTimeout == 0 {
83 | config.Exporter.DefaultProbeTimeout = 5
84 | }
85 |
86 | // **Log message to indicate program continues execution**
87 | logger.Info("Starting server with final configuration", slog.Any("config", config))
88 |
89 | // Prometheus metric setup
90 | var (
91 | probeResult = prometheus.NewGaugeVec(
92 | prometheus.GaugeOpts{
93 | Name: "probe_result",
94 | Help: "Current status of the probe (1 for success, 0 for failure)",
95 | },
96 | []string{"target", "module", "address"},
97 | )
98 | )
99 |
100 | promRegistry := prometheus.NewRegistry()
101 | prometheus.DefaultRegisterer = promRegistry
102 | prometheus.DefaultGatherer = promRegistry
103 | prometheus.MustRegister(probeResult)
104 |
105 | // HTTP handlers
106 | http.Handle("/metrics", promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}))
107 | http.HandleFunc("/probe", func(w http.ResponseWriter, r *http.Request) {
108 | if r.Method != http.MethodPost {
109 | http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
110 | return
111 | }
112 |
113 | var request probeRequest
114 | err := json.NewDecoder(r.Body).Decode(&request)
115 | if err != nil {
116 | http.Error(w, "Bad request", http.StatusBadRequest)
117 | return
118 | }
119 |
120 | timeout := request.Timeout
121 | if timeout == 0 {
122 | timeout = config.Exporter.DefaultProbeTimeout
123 | }
124 |
125 | resultHandler := func(result bool, err error) {
126 | var response probeResponse
127 | response.Result = false
128 | if result {
129 | logger.Info("Probe successful")
130 | response.Result = true
131 | } else {
132 | if err != nil {
133 | logger.Error(fmt.Sprint(err.Error()))
134 | }
135 | response.Error = err.Error()
136 | }
137 | w.Header().Set("Content-Type", "application/json")
138 | json.NewEncoder(w).Encode(response)
139 | }
140 |
141 | switch request.Module {
142 | case "tcp":
143 | result, err := modules.ProbeTCP(request.Address, timeout)
144 | resultHandler(result, err)
145 | case "http":
146 | result, err := modules.ProbeHTTP(request.Address, timeout)
147 | resultHandler(result, err)
148 | case "icmp":
149 | result, err := modules.ProbeICMP(request.Address)
150 | resultHandler(result, err)
151 | default:
152 | logger.Error("Unknown module")
153 | http.Error(w, "Unknown module", http.StatusBadRequest)
154 | return
155 | }
156 | })
157 |
158 | // Start HTTP server
159 | go func() {
160 | err := http.ListenAndServe(fmt.Sprintf(":%d", config.Exporter.ListenPort), nil)
161 | if err != nil {
162 | logger.Error(fmt.Sprintf("Failed to start HTTP server: %v", err))
163 | os.Exit(1)
164 | }
165 | }()
166 |
167 | var wg sync.WaitGroup
168 |
169 | // **If no targets exist, ensure the program does not deadlock**
170 | if len(config.Targets) == 0 {
171 | logger.Info("No targets configured, service running with only HTTP API")
172 | select {} // Keeps the program running if no targets exist
173 | }
174 |
175 | // Start probes if any targets are defined
176 | for key, value := range config.Targets {
177 | wg.Add(1)
178 | go func(target string, module string, address string, interval int, timeout int) {
179 | defer wg.Done()
180 | if interval == 0 {
181 | interval = config.Exporter.DefaultProbeInterval
182 | }
183 | if timeout == 0 {
184 | timeout = config.Exporter.DefaultProbeTimeout
185 | }
186 | targetLogger := logger.With(slog.String("target", target))
187 | resultHandler := func(result bool, err error, interval int) {
188 | if result {
189 | targetLogger.Info("Probe successful")
190 | probeResult.WithLabelValues(target, module, address).Set(1)
191 | } else {
192 | if err != nil {
193 | targetLogger.Error(fmt.Sprint(err.Error()))
194 | }
195 | probeResult.WithLabelValues(target, module, address).Set(0)
196 | }
197 | time.Sleep(time.Duration(interval) * time.Second)
198 | }
199 | switch module {
200 | case "tcp":
201 | for {
202 | result, err := modules.ProbeTCP(address, timeout)
203 | resultHandler(result, err, interval)
204 | }
205 | case "http":
206 | for {
207 | result, err := modules.ProbeHTTP(address, timeout)
208 | resultHandler(result, err, interval)
209 | }
210 | case "icmp":
211 | for {
212 | result, err := modules.ProbeICMP(address)
213 | resultHandler(result, err, interval)
214 | }
215 | default:
216 | targetLogger.Error("Unknown module")
217 | }
218 | }(key, value.Module, value.Address, value.Interval, value.Timeout)
219 | }
220 |
221 | wg.Wait()
222 | }
223 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
--------------------------------------------------------------------------------
/kubeping.drawio:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
--------------------------------------------------------------------------------