config support for init and transform scripts

This commit is contained in:
sqshq 2019-04-05 22:11:52 -04:00
parent 061d77c76a
commit 800c9b2e3e
5 changed files with 72 additions and 49 deletions

View File

@ -12,11 +12,11 @@ runcharts:
scale: 3 scale: 3
items: items:
- label: GOOGLE - label: GOOGLE
value: curl -o /dev/null -s -w '%{time_total}' https://www.google.com sample: curl -o /dev/null -s -w '%{time_total}' https://www.google.com
- label: YAHOO - label: YAHOO
value: curl -o /dev/null -s -w '%{time_total}' https://search.yahoo.com sample: curl -o /dev/null -s -w '%{time_total}' https://search.yahoo.com
- label: BING - label: BING
value: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com sample: curl -o /dev/null -s -w '%{time_total}' https://www.bing.com
- title: MONGO COLLECTIONS COUNT - title: MONGO COLLECTIONS COUNT
position: [[53, 0], [27, 8]] position: [[53, 0], [27, 8]]
legend: legend:
@ -25,9 +25,11 @@ runcharts:
scale: 0 scale: 0
items: items:
- label: ACTIVE - label: ACTIVE
value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()" init: mongo --quiet --host=localhost blog
sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
transform: $sample | grep cpu
- label: INACTIVE - label: INACTIVE
value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
barcharts: barcharts:
- title: EVENTS BY STATUS - title: EVENTS BY STATUS
refresh-rate-ms: 1000 refresh-rate-ms: 1000
@ -35,34 +37,40 @@ barcharts:
scale: 0 scale: 0
items: items:
- label: NEW - label: NEW
value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()" sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()"
- label: TRIGGERED - label: TRIGGERED
value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
- label: IN_PROCESS - label: IN_PROCESS
value: echo 0 sample: echo 0
- label: FAILED - label: FAILED
value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()" sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'ACTIVE'}).itcount()"
- label: FINISHED - label: FINISHED
value: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()" sample: mongo --quiet --host=localhost blog --eval "db.getCollection('posts').find({status:'INACTIVE'}).itcount()"
gauges: gauges:
- title: YEAR PROGRESS - title: YEAR PROGRESS
position: [[53, 8], [27, 2]] position: [[53, 8], [27, 2]]
values: cur:
cur: date +%j sample: date +%j
max: echo 365 max:
min: echo 0 sample: echo 365
min:
sample: echo 0
- title: DAY PROGRESS - title: DAY PROGRESS
position: [[53, 10], [27, 2]] position: [[53, 10], [27, 2]]
values: cur:
cur: date +%H sample: date +%H
max: echo 24 max:
min: echo 0 sample: echo 24
min:
sample: echo 0
- title: HOUR PROGRESS - title: HOUR PROGRESS
position: [[53, 12], [27, 2]] position: [[53, 12], [27, 2]]
values: cur:
cur: date +%M sample: date +%M
max: echo 60 max:
min: echo 0 sample: echo 60
min:
sample: echo 0
- title: MINUTE PROGRESS - title: MINUTE PROGRESS
position: [[53, 14], [27, 2]] position: [[53, 14], [27, 2]]
triggers: triggers:
@ -71,24 +79,26 @@ gauges:
actions: actions:
sound: true sound: true
script: say -v samantha `date +%I:%M%p` script: say -v samantha `date +%I:%M%p`
values: cur:
cur: date +%S sample: date +%S
max: echo 60 max:
min: echo 0 sample: echo 60
min:
sample: echo 0
asciiboxes: asciiboxes:
- title: LOCAL TIME - title: LOCAL TIME
position: [[53, 17], [27, 5]] position: [[53, 17], [27, 5]]
value: date +%r sample: date +%r
- title: UTC TIME - title: UTC TIME
position: [[53, 22], [27, 7]] position: [[53, 22], [27, 7]]
value: env TZ=UTC date +%r sample: env TZ=UTC date +%r
font: 3d font: 3d
sparklines: sparklines:
- title: CPU usage - title: CPU usage
position: [[27, 22], [25, 7]] position: [[27, 22], [25, 7]]
scale: 0 scale: 0
value: ps -A -o %cpu | awk '{s+=$1} END {print s}' sample: ps -A -o %cpu | awk '{s+=$1} END {print s}'
- title: Memory pages free - title: Memory pages free
position: [[27, 17], [25, 5]] position: [[27, 17], [25, 5]]
scale: 0 scale: 0
value: memory_pressure | grep 'Pages free' | awk '{print $3}' sample: memory_pressure | grep 'Pages free' | awk '{print $3}'

View File

@ -49,8 +49,9 @@ type GaugeConfig struct {
ComponentConfig `yaml:",inline"` ComponentConfig `yaml:",inline"`
Scale *int `yaml:"scale,omitempty"` Scale *int `yaml:"scale,omitempty"`
Color *ui.Color `yaml:"color,omitempty"` Color *ui.Color `yaml:"color,omitempty"`
Values map[string]string `yaml:"values"` Cur Item `yaml:"cur"`
Items []Item `yaml:",omitempty"` Max Item `yaml:"max"`
Min Item `yaml:"min"`
} }
type SparkLineConfig struct { type SparkLineConfig struct {
@ -86,8 +87,10 @@ type LegendConfig struct {
type Item struct { type Item struct {
Label *string `yaml:"label,omitempty"` Label *string `yaml:"label,omitempty"`
Script string `yaml:"value"`
Color *ui.Color `yaml:"color,omitempty"` Color *ui.Color `yaml:"color,omitempty"`
InitScript *string `yaml:"init,omitempty"`
SampleScript *string `yaml:"sample"`
TransformScript *string `yaml:"transform,omitempty"`
} }
type Location struct { type Location struct {

View File

@ -89,12 +89,15 @@ func (c *Config) setDefaultValues() {
p := defaultScale p := defaultScale
g.Scale = &p g.Scale = &p
} }
var items []Item
for label, script := range g.Values { cur := "cur"
l := label max := "max"
items = append(items, Item{Label: &l, Script: script}) min := "min"
}
g.Items = items g.Cur.Label = &cur
g.Max.Label = &max
g.Min.Label = &min
c.Gauges[i] = g c.Gauges[i] = g
} }

View File

@ -10,7 +10,9 @@ import (
type Item struct { type Item struct {
Label string Label string
Script string SampleScript string
InitScript *string
TransformScript *string
Color *ui.Color Color *ui.Color
} }
@ -19,7 +21,12 @@ func NewItems(cfgs []config.Item) []Item {
items := make([]Item, 0) items := make([]Item, 0)
for _, i := range cfgs { for _, i := range cfgs {
item := Item{Label: *i.Label, Script: i.Script, Color: i.Color} item := Item{
Label: *i.Label,
SampleScript: *i.SampleScript,
InitScript: i.InitScript,
TransformScript: i.TransformScript,
Color: i.Color}
items = append(items, item) items = append(items, item)
} }
@ -28,7 +35,7 @@ func NewItems(cfgs []config.Item) []Item {
func (i *Item) nextValue(variables []string) (value string, err error) { func (i *Item) nextValue(variables []string) (value string, err error) {
cmd := exec.Command("sh", "-c", i.Script) cmd := exec.Command("sh", "-c", i.SampleScript)
cmd.Env = os.Environ() cmd.Env = os.Environ()
for _, variable := range variables { for _, variable := range variables {

View File

@ -68,7 +68,7 @@ func main() {
for _, c := range cfg.Gauges { for _, c := range cfg.Gauges {
cpt := gauge.NewGauge(c, palette) cpt := gauge.NewGauge(c, palette)
starter.start(cpt, cpt.Consumer, c.ComponentConfig, c.Items, c.Triggers) starter.start(cpt, cpt.Consumer, c.ComponentConfig, []config.Item{c.Cur, c.Min, c.Max}, c.Triggers)
} }
handler := event.NewHandler(lout, opt) handler := event.NewHandler(lout, opt)