-
Notifications
You must be signed in to change notification settings - Fork 50
/
infping.go
159 lines (143 loc) · 4.08 KB
/
infping.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
// infping.go copyright Tor Hveem
// License: MIT
package main
import (
"github.com/influxdb/influxdb/client"
"github.com/pelletier/go-toml"
"fmt"
"log"
"os"
"bufio"
"os/exec"
"net/url"
"strings"
"time"
"strconv"
)
func herr(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func perr(err error) {
if err != nil {
fmt.Println(err)
}
}
func slashSplitter(c rune) bool {
return c == '/'
}
func readPoints(config *toml.TomlTree, con *client.Client) {
args := []string{"-B 1", "-D", "-r0", "-O 0", "-Q 10", "-p 1000", "-l"}
hosts := config.Get("hosts.hosts").([]interface{})
for _, v := range hosts {
host, _ := v.(string)
args = append(args, host)
}
log.Printf("Going to ping the following hosts: %q", hosts)
cmd := exec.Command("/usr/bin/fping", args...)
stdout, err := cmd.StdoutPipe()
herr(err)
stderr, err := cmd.StderrPipe()
herr(err)
cmd.Start()
perr(err)
buff := bufio.NewScanner(stderr)
for buff.Scan() {
text := buff.Text()
fields := strings.Fields(text)
// Ignore timestamp
if len(fields) > 1 {
host := fields[0]
data := fields[4]
dataSplitted := strings.FieldsFunc(data, slashSplitter)
// Remove ,
dataSplitted[2] = strings.TrimRight(dataSplitted[2], "%,")
sent, recv, lossp := dataSplitted[0], dataSplitted[1], dataSplitted[2]
min, max, avg := "", "", ""
// Ping times
if len(fields) > 5 {
times := fields[7]
td := strings.FieldsFunc(times, slashSplitter)
min, avg, max = td[0], td[1], td[2]
}
//log.Printf("Host:%s, loss: %s, min: %s, avg: %s, max: %s", host, lossp, min, avg, max)
writePoints(config, con, host, sent, recv, lossp, min, avg, max)
}
}
std := bufio.NewReader(stdout)
line, err := std.ReadString('\n')
perr(err)
log.Printf("stdout:%s", line)
}
func writePoints(config *toml.TomlTree, con *client.Client, host string, sent string, recv string, lossp string, min string, avg string, max string) {
db := config.Get("influxdb.db").(string)
loss, _ := strconv.Atoi(lossp)
pts := make([]client.Point, 1)
fields := map[string]interface{}{}
if min != "" && avg != "" && max != "" {
min, _ := strconv.ParseFloat(min, 64)
avg, _ := strconv.ParseFloat(avg, 64)
max, _ := strconv.ParseFloat(max, 64)
fields = map[string]interface{}{
"loss": loss,
"min": min,
"avg": avg,
"max": max,
}
} else {
fields = map[string]interface{}{
"loss": loss,
}
}
pts[0] = client.Point{
Measurement: "ping",
Tags: map[string]string{
"host": host,
},
Fields: fields,
Time: time.Now(),
Precision: "",
}
bps := client.BatchPoints{
Points: pts,
Database: db,
RetentionPolicy: "default",
}
_, err := con.Write(bps)
if err != nil {
log.Fatal(err)
}
}
func main() {
config, err := toml.LoadFile("config.toml")
if err != nil {
fmt.Println("Error:", err.Error())
os.Exit(1)
}
host := config.Get("influxdb.host").(string)
port := config.Get("influxdb.port").(string)
//measurement := config.Get("influxdb.measurement").(string)
username := config.Get("influxdb.user").(string)
password := config.Get("influxdb.pass").(string)
u, err := url.Parse(fmt.Sprintf("http://%s:%s", host, port))
if err != nil {
log.Fatal(err)
}
conf := client.Config{
URL: *u,
Username: username,
Password: password,
}
con, err := client.NewClient(conf)
if err != nil {
log.Fatal(err)
}
dur, ver, err := con.Ping()
if err != nil {
log.Fatal(err)
}
log.Printf("Connected to influxdb! %v, %s", dur, ver)
readPoints(config, con)
}