-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingu.go
170 lines (126 loc) · 3.38 KB
/
pingu.go
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
package main
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
probing "github.com/prometheus-community/pro-bing"
"github.com/spf13/viper"
)
const (
TTL = 128
TIMEOUT = 1000 // milliseconds
INTERVAL = 1000 // milliseconds
COUNT = 1
ENV_PREFIX = "PINGU"
)
var config Config
type Config struct {
Address string
User string
Password string
Privileged bool
}
func (*Config) init() {
viper.SetEnvPrefix(ENV_PREFIX)
viper.BindEnv("address")
viper.SetDefault("address", "0.0.0.0:8080")
viper.BindEnv("user")
viper.SetDefault("user", "pingu")
viper.BindEnv("password")
viper.BindEnv("privileged")
viper.SetDefault("privileged", true)
viper.AutomaticEnv()
viper.Unmarshal(&config)
if config.Password == "" {
panic(ENV_PREFIX + "_PASSWORD is required!")
}
}
type PingAddresses struct {
Addresses []string `json:"addresses" binding:"required,min=1,lte=10,dive,ipv4"`
Count int `json:"count" binding:"omitempty,min=1,lte=10"`
Interval int `json:"interval" binding:"omitempty,min=1,lte=10000"`
Timeout int `json:"timeout" binding:"omitempty,min=1,lte=10000"`
TTL int `json:"ttl" binding:"omitempty,min=1,lte=128"`
}
type PingResult struct {
Address string `json:"address"`
Status bool `json:"status"`
}
type PingResults struct {
Addresses []PingResult `json:"addresses"`
}
func pingAddress(address string, count int, interval int, timeout int, ttl int) (bool, error) {
pinger, err := probing.NewPinger(address)
if err != nil {
return false, err
}
pinger.Count = count
pinger.Interval = time.Millisecond * time.Duration(interval)
pinger.Timeout = time.Millisecond * time.Duration(timeout)
pinger.TTL = ttl
pinger.SetPrivileged(config.Privileged)
if err := pinger.Run(); err != nil {
return false, err
}
stats := pinger.Statistics()
return stats.PacketsRecv > 0, nil
}
func pingAddresses(data PingAddresses) PingResults {
channel := make(chan struct {
string
bool
})
var results PingResults
results.Addresses = []PingResult{}
for _, address := range data.Addresses {
go func(address string, json PingAddresses) {
var err error
var status bool
status, err = pingAddress(address, json.Count, json.Interval, json.Timeout, json.TTL)
if err != nil {
fmt.Println(err)
}
channel <- struct {
string
bool
}{address, status}
}(address, data)
}
for range data.Addresses {
var result PingResult
// Read data from channel
data := <-channel
result.Address = data.string
result.Status = data.bool
results.Addresses = append(results.Addresses, result)
}
close(channel)
return results
}
func main() {
gin.SetMode(gin.ReleaseMode)
// Load configuration from env vars
config.init()
status, err := pingAddress("127.0.0.1", COUNT, INTERVAL, TIMEOUT, TTL)
if !status {
panic("Pingu is unable to ping. " + err.Error())
}
router := gin.Default()
authorized := router.Group("/", gin.BasicAuth(gin.Accounts{config.User: config.Password}))
authorized.POST("/ping", func(c *gin.Context) {
// Set default values before binding from json data
data := PingAddresses{
TTL: TTL,
Timeout: TIMEOUT,
Interval: INTERVAL,
Count: COUNT,
}
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, pingAddresses(data))
})
router.Run(config.Address)
}