-
Notifications
You must be signed in to change notification settings - Fork 0
/
iperf-app.go
471 lines (394 loc) · 11.7 KB
/
iperf-app.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/robfig/cron"
"io/ioutil"
"log"
"math"
"net/http"
"os"
"os/exec"
"regexp"
"strconv"
"time"
)
type Config struct {
IperfServerIp string `json:"iperf_server_ip"`
UseDB bool `json:"use_db"`
DBUser string `json:"db_user"`
DBPass string `json:"db_pass"`
DBHost string `json:"db_host"`
DBPort int `json:"db_port"`
DBName string `json:"db_name"`
}
type SpeedTest struct {
ID int `json:"id"`
Ping float64 `json:"ping"`
Download float64 `json:"download"`
Upload float64 `json:"upload"`
ServerID int `json:"server_id"`
ServerHost string `json:"server_host"`
ServerName string `json:"server_name"`
URL string `json:"url"`
Scheduled bool `json:"scheduled"`
Failed bool `json:"failed"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type NetworkData struct {
Upload string `json:"upload"`
Download string `json:"download"`
PingTime string `json:"pingTime"`
TimeStamp string `json:"timeStamp"`
}
func initializeDB(config Config) error {
// Create the connection string
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", config.DBUser, config.DBPass, config.DBHost, config.DBPort, config.DBName)
// Open a connection to the database
db, err := sql.Open("mysql", dataSourceName)
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
defer db.Close()
// Create the table if it doesn't exist
tableName := config.DBName
createTableSQL := fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
id INT AUTO_INCREMENT PRIMARY KEY,
ping FLOAT,
download FLOAT,
upload FLOAT,
timestamp DATETIME
)`, tableName)
_, err = db.Exec(createTableSQL)
if err != nil {
return fmt.Errorf("failed to create table: %w", err)
}
return nil
}
func parseIperfOutput(output string) (float64, float64) {
senderBitrate := 0.0
receiverBitrate := 0.0
// Search for the sender and receiver bitrates in the iperf output
senderRegex := regexp.MustCompile(`(\d+\.?\d*)\s*([GM]?)bits/sec\s*\d+\s+sender`)
receiverRegex := regexp.MustCompile(`(\d+\.?\d*)\s*([GM]?)bits/sec\s+receiver`)
// Find the sender bitrate
senderMatch := senderRegex.FindStringSubmatch(output)
if senderMatch != nil {
senderBitrate = parseFloat(senderMatch[1])
}
// Find the receiver bitrate
receiverMatch := receiverRegex.FindStringSubmatch(output)
if receiverMatch != nil {
receiverBitrate = parseFloat(receiverMatch[1])
}
return senderBitrate, receiverBitrate
}
func saveJSON(upload float64, download float64, pingTime float64) error {
uploadSpeed := fmt.Sprintf("%.3f", upload)
downloadSpeed := fmt.Sprintf("%.3f", download)
pingContent := fmt.Sprintf("%.3f", pingTime)
currentTime := time.Now().Format(time.RFC3339)
data := NetworkData{
Upload: uploadSpeed,
Download: downloadSpeed,
PingTime: pingContent,
TimeStamp: currentTime,
}
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Println("Error marshaling JSON:", err)
return err
}
err = ioutil.WriteFile("netdata.json", jsonData, 0644)
if err != nil {
log.Println("Error writing JSON to file:", err)
return err
}
return nil
}
func parseFloat(s string) float64 {
value, err := strconv.ParseFloat(s, 64)
if err != nil {
log.Println("Error parsing float:", err)
}
return value
}
func runIperfCommand(serverIP string) (float64, float64, error) {
cmd := exec.Command("iperf3", "-c", serverIP)
// Execute the command and wait for it to finish
output, err := cmd.Output()
if err != nil {
log.Println("Error running iperf3 command:", err)
return 0, 0, err
}
sender, receiver := parseIperfOutput(string(output))
return sender, receiver, nil
}
func pingIP(serverIP string) (float64, error) {
cmd := exec.Command("ping", "-c", "5", serverIP)
output, err := cmd.Output()
if err != nil {
log.Println("Error running ping command:", err)
return 0, err
}
outputStr := string(output)
pingRegex := regexp.MustCompile(`time=(\d+\.?\d*)`)
pingMatches := pingRegex.FindAllStringSubmatch(outputStr, -1)
totalTime := 0.0
for _, match := range pingMatches {
pingTime, err := strconv.ParseFloat(match[1], 64)
if err != nil {
log.Println("Error parsing ping time:", err)
continue
}
totalTime += pingTime
}
avgPingTime := totalTime / float64(len(pingMatches))
return avgPingTime, nil
}
func handleSpeedTestLatest(w http.ResponseWriter, r *http.Request) {
// Redirect if invalid
if r.URL.Path != "/api/speedtest/latest" {
http.Redirect(w, r, "/api/speedtest/latest", http.StatusMovedPermanently)
return
}
log.Printf("%s - - [%s] \"%s %s %s\" %d %d \"%s\" \"%s\"\n",
r.RemoteAddr,
time.Now().Format("02/Jan/2006:15:04:05 -0700"),
r.Method,
r.RequestURI,
r.Proto,
http.StatusOK,
261,
r.Referer(),
r.UserAgent(),
)
// Read JSON file
data, err := ioutil.ReadFile("netdata.json")
if err != nil {
log.Println("Error reading file:", err)
http.Error(w, "Failed to read file", http.StatusInternalServerError)
return
}
var netData NetworkData
err = json.Unmarshal(data, &netData)
if err != nil {
log.Println("Error decoding JSON:", err)
http.Error(w, "Failed to decode JSON", http.StatusInternalServerError)
return
}
pingTime, err := strconv.ParseFloat(netData.PingTime, 64)
if err != nil {
log.Println("Error parsing PingTime:", err)
http.Error(w, "Failed to parse PingTime", http.StatusInternalServerError)
return
}
download, err := strconv.ParseFloat(netData.Download, 64)
if err != nil {
log.Println("Error parsing Download:", err)
http.Error(w, "Failed to parse Download", http.StatusInternalServerError)
return
}
upload, err := strconv.ParseFloat(netData.Upload, 64)
if err != nil {
log.Println("Error parsing Upload:", err)
http.Error(w, "Failed to parse Upload", http.StatusInternalServerError)
return
}
var timeStamp time.Time
layouts := []string{"2006-01-02T15:04:05-07:00", "2006-01-02T15:04:05Z"}
for _, layout := range layouts {
timeStamp, err = time.Parse(layout, netData.TimeStamp)
if err == nil {
break
}
}
if err != nil {
log.Println("Error parsing TimeStamp:", err)
http.Error(w, "Failed to parse TimeStamp", http.StatusInternalServerError)
return
}
speedTest := SpeedTest{
ID: 1,
Ping: roundDecimals(pingTime, 3),
Download: roundDecimals(download, 4),
Upload: roundDecimals(upload, 4),
ServerID: 1,
ServerHost: "iperf.lan",
ServerName: "iperf",
URL: "http://iperf.lan/api/speedtest.latest",
Scheduled: true,
Failed: true,
CreatedAt: timeStamp,
UpdatedAt: timeStamp,
}
// Create the API response object
apiResponse := struct {
Message string `json:"message"`
Data SpeedTest `json:"data"`
}{
Message: "ok",
Data: speedTest,
}
// Marshal the JSON response
response, err := json.Marshal(apiResponse)
if err != nil {
log.Println("Error marshaling JSON:", err)
http.Error(w, "Failed to marshal JSON response", http.StatusInternalServerError)
return
}
// Set the Content-Type header to application/json
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
// roundDecimals rounds a float64 value to the specified number of decimal places
func roundDecimals(value float64, decimals int) float64 {
shift := math.Pow(10, float64(decimals))
return math.Round(value*shift) / shift
}
func handleRedirect(w http.ResponseWriter, r *http.Request) {
// Redirect to /api/speedtest/latest
http.Redirect(w, r, "/api/speedtest/latest", http.StatusMovedPermanently)
}
func main() {
// Read the configuration file
configFile, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatal("Failed to read config file:", err)
}
// Parse the configuration values
var config Config
err = json.Unmarshal(configFile, &config)
if err != nil {
log.Fatal("Failed to parse config file:", err)
}
// Override config values with environment variables
iperfServerIPEnv := os.Getenv("IPERF_SERVER_IP")
if iperfServerIPEnv != "" {
config.IperfServerIp = iperfServerIPEnv
}
useDBEnv := os.Getenv("USE_DB")
if useDBEnv != "" {
useDB, err := strconv.ParseBool(useDBEnv)
if err == nil {
config.UseDB = useDB
}
}
dbUserEnv := os.Getenv("MYSQL_USER")
if dbUserEnv != "" {
config.DBUser = dbUserEnv
}
dbPassEnv := os.Getenv("MYSQL_PASSWORD")
if dbPassEnv != "" {
config.DBPass = dbPassEnv
}
dbHostEnv := os.Getenv("DB_HOST")
if dbHostEnv != "" {
config.DBHost = dbHostEnv
}
dbPortEnv := os.Getenv("DB_PORT")
if dbPortEnv != "" {
dbPort, err := strconv.Atoi(dbPortEnv)
if err == nil {
config.DBPort = dbPort
}
}
dbNameEnv := os.Getenv("MYSQL_DATABASE")
if dbNameEnv != "" {
config.DBName = dbNameEnv
}
// Update the config.json file with the modified values
configJSON, err := json.MarshalIndent(config, "", " ")
if err != nil {
log.Fatal("Failed to marshal config JSON:", err)
}
err = ioutil.WriteFile("config.json", configJSON, 0644)
if err != nil {
log.Fatal("Failed to write config file:", err)
}
if config.UseDB == true {
// Initialize the database
err = initializeDB(config)
if err != nil {
log.Fatal("Failed to initialize the database:", err)
}
}
// Create a new cron scheduler
c := cron.New()
// Run every hour on the hour
c.AddFunc("0 0 * * * *", func() {
sender, receiver, err := runIperfCommand(config.IperfServerIp)
if err != nil {
log.Println("Failed to run iperf3 command:", err)
}
pingTime, err := pingIP(config.IperfServerIp)
if err != nil {
log.Println("Failed to ping IP:", err)
}
err = saveJSON(sender, receiver, pingTime)
if err != nil {
log.Println("Failed to save ping time:", err)
}
if config.UseDB == true {
// if true store data in mariadb
err = storeDataInDB(config)
if err != nil {
log.Println("Error storing data in database:", err)
return
}
log.Println("Data stored successfully:", sender, receiver, pingTime)
}
})
// Start the cron scheduler
c.Start()
http.HandleFunc("/api/speedtest/latest", handleSpeedTestLatest)
http.HandleFunc("/", handleRedirect)
fmt.Println("Server listening on http://localhost:8000/api/speedtest/latest")
log.Fatal(http.ListenAndServe(":8000", nil))
// Keep the program running
select {}
}
func storeDataInDB(config Config) error {
// Create the connection string
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", config.DBUser, config.DBPass, config.DBHost, config.DBPort, config.DBName)
// Read JSON file
jsonData, err := ioutil.ReadFile("netdata.json")
if err != nil {
return fmt.Errorf("failed to read JSON file: %w", err)
}
// Parse JSON data into NetworkData struct
var networkData NetworkData
err = json.Unmarshal(jsonData, &networkData)
if err != nil {
return fmt.Errorf("failed to parse JSON data: %w", err)
}
// Open a connection to the database
db, err := sql.Open("mysql", dataSourceName)
if err != nil {
return fmt.Errorf("failed to connect to database: %w", err)
}
defer db.Close()
// Prepare the SQL statement
tableName := config.DBName
stmt, err := db.Prepare("INSERT INTO " + tableName + "(ping, download, upload, timestamp) VALUES (?, ?, ?, ?)")
if err != nil {
return fmt.Errorf("failed to prepare SQL statement: %w", err)
}
defer stmt.Close()
// Format the timestamp in MySQL datetime format
timestamp, err := time.Parse(time.RFC3339, networkData.TimeStamp)
if err != nil {
return fmt.Errorf("failed to parse timestamp: %w", err)
}
formattedTimestamp := timestamp.Format("2006-01-02 15:04:05")
// Execute the SQL statement
_, err = stmt.Exec(networkData.PingTime, networkData.Download, networkData.Upload, formattedTimestamp)
if err != nil {
return fmt.Errorf("failed to execute SQL statement: %w", err)
}
return nil
}