-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4172062
Showing
7 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
ovpnstats2influxdb | ||
================== | ||
|
||
Read metrics from OpenVPN `openvpn-status.log` and output them as InfluxDB metrics. | ||
|
||
## Installation | ||
* Build it yourself with `go build main.go` and move the binary somewhere into your `$PATH` | ||
|
||
## Usage | ||
Add it as an exec plugin to your `telegraf.conf`: | ||
``` | ||
[[inputs.exec]] | ||
commands = ["/usr/bin/ovpnstats -path /etc/openvpn/server/openvpn-status.log"] | ||
data_format = "influx" | ||
timeout = 1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module github.com/thor77/ovpnstats2influxdb | ||
|
||
require ( | ||
github.com/influxdata/influxdb v1.7.3 | ||
github.com/influxdata/platform v0.0.0-20190117200541-d500d3cf5589 // indirect | ||
github.com/thor77/ovpnstats v0.1.3 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
|
||
"github.com/thor77/ovpnstats2influxdb/ovpnstats2influxdb" | ||
) | ||
|
||
func main() { | ||
// read configuration | ||
pathFlagPointer := flag.String("path", "", "Path to openvpn-status.log") | ||
|
||
flag.Parse() | ||
|
||
if *pathFlagPointer == "" { | ||
log.Fatal("No path provided") | ||
} | ||
err := ovpnstats2influxdb.RunTelegraf(*pathFlagPointer) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ovpnstats2influxdb | ||
|
||
import ( | ||
"time" | ||
|
||
influxdb "github.com/influxdata/influxdb/client/v2" | ||
) | ||
|
||
// createBatchPoints converts metrics into InfluxDB points | ||
func createBatchPoints(measurementName string, metrics []Metric) ([]*influxdb.Point, error) { | ||
var result []*influxdb.Point | ||
measurement := measurementName | ||
timestamp := time.Now() | ||
for _, metric := range metrics { | ||
point, err := influxdb.NewPoint(measurement, metric.Tags, metric.Fields, timestamp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result = append(result, point) | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ovpnstats2influxdb | ||
|
||
import ( | ||
"github.com/thor77/ovpnstats" | ||
) | ||
|
||
// Metric represents metric events with all required information to write it into an InfluxDB | ||
type Metric struct { | ||
Fields map[string]interface{} | ||
Tags map[string]string | ||
} | ||
|
||
func createMetrics(clients []ovpnstats.ClientInfo, routes []ovpnstats.RoutingInfo) []Metric { | ||
return []Metric{Metric{map[string]interface{}{"clients": len(clients), "routes": len(routes)}, nil}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ovpnstats2influxdb | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/thor77/ovpnstats" | ||
) | ||
|
||
// RunTelegraf prints Telegraf-compatible metric-output to stdout | ||
func RunTelegraf(path string) error { | ||
clients, routes, err := ovpnstats.ParseStatusFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
metrics := createMetrics(clients, routes) | ||
|
||
// convert metrics to influxdb line protocol | ||
points, err := createBatchPoints("openvpn", metrics) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// output line protocol lines | ||
for _, point := range points { | ||
fmt.Println(point.String()) | ||
} | ||
return nil | ||
} |