Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Thor77 committed Feb 6, 2019
0 parents commit 4172062
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
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
```
7 changes: 7 additions & 0 deletions go.mod
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
)
283 changes: 283 additions & 0 deletions go.sum

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions main.go
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)
}
}
22 changes: 22 additions & 0 deletions ovpnstats2influxdb/influxdb.go
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
}
15 changes: 15 additions & 0 deletions ovpnstats2influxdb/metrics.go
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}}
}
29 changes: 29 additions & 0 deletions ovpnstats2influxdb/telegraf.go
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
}

0 comments on commit 4172062

Please sign in to comment.