Created
November 17, 2016 04:46
-
-
Save alexzorin/fd3eaa64d0eaeb6968b8ad77cfe13e32 to your computer and use it in GitHub Desktop.
One-shot program to send OnApp 4.2 IS Datastore Diagnostics data to InfluxDB
This file contains hidden or 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
package main | |
import ( | |
"errors" | |
"fmt" | |
"math" | |
"net/http" | |
"os" | |
"strconv" | |
"time" | |
"github.com/PuerkitoBio/goquery" | |
log "github.com/Sirupsen/logrus" | |
"github.com/influxdata/influxdb/client/v2" | |
"github.com/urfave/cli" | |
) | |
func main() { | |
app := cli.NewApp() | |
app.Flags = []cli.Flag{ | |
cli.StringFlag{ | |
Name: "onapp-host", | |
}, | |
cli.StringFlag{ | |
Name: "onapp-user", | |
}, | |
cli.StringFlag{ | |
Name: "onapp-api-key", | |
}, | |
cli.IntFlag{ | |
Name: "onapp-datastore-id", | |
}, | |
cli.StringFlag{ | |
Name: "influx-addr", | |
}, | |
cli.StringFlag{ | |
Name: "influx-user", | |
}, | |
cli.StringFlag{ | |
Name: "influx-pass", | |
}, | |
cli.StringFlag{ | |
Name: "influx-db", | |
}, | |
} | |
app.Action = run | |
if err := app.Run(os.Args); err != nil { | |
log.Fatal(err) | |
} | |
} | |
func run(ctx *cli.Context) error { | |
req, err := http.NewRequest("GET", | |
fmt.Sprintf("https://%s/storage/%d/health_checks/disk", ctx.GlobalString("onapp-host"), | |
ctx.GlobalInt("onapp-datastore-id")), | |
nil) | |
if err != nil { | |
return err | |
} | |
req.SetBasicAuth(ctx.GlobalString("onapp-user"), ctx.GlobalString("onapp-api-key")) | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
return err | |
} | |
if resp == nil { | |
return errors.New("Response from OnApp API was nil") | |
} | |
defer resp.Body.Close() | |
doc, err := goquery.NewDocumentFromReader(resp.Body) | |
if err != nil { | |
return err | |
} | |
degraded := math.Max(0, float64(doc.Find(`tr[data-healthcheck-section='degraded_disks']`).Length()-1)) | |
log.WithField("Degraded count", degraded).Info("Found degraded disks") | |
zombie := math.Max(0, float64(doc.Find(`tr[data-healthcheck-section='zombie_disks']`).Length()-1)) | |
log.WithField("Zombie count", zombie).Info("Found zombie disks") | |
partialMembers := math.Max(0, float64(doc.Find(`tr[data-healthcheck-section='partial_memberlist_disks']`).Length()-1)) | |
log.WithField("Partial count", zombie).Info("Found partial memberlist disks") | |
// Ship it | |
cl, err := client.NewHTTPClient(client.HTTPConfig{ | |
Addr: ctx.GlobalString("influx-addr"), | |
Username: ctx.GlobalString("influx-user"), | |
Password: ctx.GlobalString("influx-pass"), | |
}) | |
if err != nil { | |
return err | |
} | |
bp, err := client.NewBatchPoints(client.BatchPointsConfig{ | |
Database: ctx.GlobalString("influx-db"), | |
Precision: "s", | |
}) | |
if err != nil { | |
return err | |
} | |
tags := map[string]string{ | |
"cloud": ctx.GlobalString("onapp-host"), | |
"datastore": strconv.Itoa(ctx.GlobalInt("onapp-datastore-id")), | |
} | |
fields := map[string]interface{}{ | |
"degraded_disks": degraded, | |
"zombie_disks": zombie, | |
"partial_members_disks": partialMembers, | |
} | |
pt, err := client.NewPoint("is_diagnostics", tags, fields, time.Now()) | |
if err != nil { | |
return err | |
} | |
bp.AddPoint(pt) | |
if err := cl.Write(bp); err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment