Skip to content

Instantly share code, notes, and snippets.

@gunnaraasen
Last active April 12, 2018 23:44
Show Gist options
  • Select an option

  • Save gunnaraasen/8bfb89097facdaada493a0d276710e89 to your computer and use it in GitHub Desktop.

Select an option

Save gunnaraasen/8bfb89097facdaada493a0d276710e89 to your computer and use it in GitHub Desktop.
Marshal a TICKscript into JSON
// This small script will marshal a tickscript into JSON.
// The script can be run using the following commands (Go must be installed):
//
// go get -u github.com/influxdata/chronograf
// go run jsontickscript.go
package main
import (
"fmt"
"os"
"github.com/influxdata/chronograf/kapacitor"
)
var script = `
var db = 'telegraf'
var rp = 'autogen'
var measurement = 'cpu'
var groupBy = ['host', 'cluster_id']
var whereFilter = lambda: ("cpu" == 'cpu_total') AND ("host" == 'acc-0eabc309-eu-west-1-data-3' OR "host" == 'prod')
var period = 10m
var every = 30s
var name = 'name'
var idVar = name + ':{{.Group}}'
var message = 'message'
var idTag = 'alertID'
var levelTag = 'level'
var messageField = 'message'
var durationField = 'duration'
var outputDB = 'chronograf'
var outputRP = 'autogen'
var outputMeasurement = 'alerts'
var triggerType = 'threshold'
var crit = 90
var data = stream
|from()
.database(db)
.retentionPolicy(rp)
.measurement(measurement)
.groupBy(groupBy)
.where(whereFilter)
|window()
.period(period)
.every(every)
.align()
|mean('usage_user')
.as('value')
var trigger = data
|alert()
.crit(lambda: "value" > crit)
.stateChangesOnly()
.message(message)
.id(idVar)
.idTag(idTag)
.levelTag(levelTag)
.messageField(messageField)
.durationField(durationField)
.slack()
.victorOps()
.email()
trigger
|influxDBOut()
.create()
.database(outputDB)
.retentionPolicy(outputRP)
.measurement(outputMeasurement)
.tag('alertName', name)
.tag('triggerType', triggerType)
trigger
|httpOut('output')
`
func main() {
b, err := kapacitor.MarshalTICK(script)
if err != nil {
fmt.Printf("could not marshal TICKscript: %s", err)
os.Exit(1)
}
fmt.Println("JSON marshalled TICKscript:")
fmt.Println(string(b[:]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment