This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
72 lines (62 loc) · 1.81 KB
/
storage.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
package stanislav
import (
"encoding/csv"
"fmt"
influxdb2 "github.com/influxdata/influxdb-client-go"
"os"
"time"
)
func (p *Peng) PushToInfluxDb() {
if p.Config.InfluxAuthToken == "" {
return
}
//Get entropy and set data to influx
client := influxdb2.NewClient(p.Config.InfluxUrl+":"+fmt.Sprint(p.Config.InfluxPort), p.Config.InfluxAuthToken)
defer client.Close()
writeApi := client.WriteAPI(p.Config.InfluxOrganization, p.Config.InfluxBucket) //non-blocking
//Send point of system with hostname and values about in and out bits
point := influxdb2.NewPoint(
"system",
map[string]string{
"entropy": "ports",
},
map[string]interface{}{
"in": p.ServerTraffic.EntropyTotal(),
"out": p.ClientTraffic.EntropyTotal(),
},
time.Now())
writeApi.WritePoint(point)
writeApi.Flush() // Force all unwritten data to be sent
if p.Config.Verbose == 3 {
logger.Printf("[%s] file successfully pushed to influxdb\n", time.Now().Local().String())
}
}
func (p *Peng) ExportToCsv() {
if p.Config.SaveFilePath == "" {
return
}
// 1. Open the file
file, err := os.OpenFile(p.Config.SaveFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777)
if err != nil {
logger.Println("error opening csv file", err.Error())
}
defer file.Close()
currTime := time.Now().Local().String()
writer := csv.NewWriter(file)
var csvData = [][]string{
{currTime, fmt.Sprintf("%f", p.ClientTraffic.EntropyTotal()), fmt.Sprintf("%f", p.ServerTraffic.EntropyTotal())},
}
// 3. Write all the records
err = writer.WriteAll(csvData) // returns error
if err != nil {
logger.Println("error on writing csv data ", err.Error())
return
}
err = file.Chown(65534, 65534)
if err != nil {
logger.Println(err.Error())
}
if p.Config.Verbose == 3 {
logger.Printf("[%s] data successfully exported to csv\n", time.Now().Local().String())
}
}