From 17401cde1d2d6343c62459e9283df47b4171ce18 Mon Sep 17 00:00:00 2001 From: Tristan Colgate Date: Mon, 13 Jun 2016 15:04:06 +0100 Subject: [PATCH] Add WaitCondition* and Limit query options WaitConditions are useful for efficient polling of livestatus. --- query.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/query.go b/query.go index a67e400..d52548d 100644 --- a/query.go +++ b/query.go @@ -8,6 +8,7 @@ import ( "net" "strconv" "strings" + "time" ) // Query is a binding query instance. @@ -49,6 +50,60 @@ func (q *Query) Negate() *Query { return q } +// Limit the query to n responses. +func (q *Query) Limit(n int) *Query { + q.headers = append(q.headers, fmt.Sprintf("Limit: %d", n)) + return q +} + +// WaitObject sets the object within the queried table to wait on. For the table +// hosts, hostgroups, servicegroups, contacts and contactgroups this is simply +// the name of the object. For the table services it is the hostname followed +// by a space followed by the service description +func (q *Query) WaitObject(obj string) *Query { + q.headers = append(q.headers, "WaitObject: "+obj) + return q +} + +// WaitCondition sets a new wait condition to apply to the query. +func (q *Query) WaitCondition(rule string) *Query { + q.headers = append(q.headers, "WaitCondition: "+rule) + return q +} + +// WaitConditionAnd combines the n last wait conditions into a new wait +// condition using a `And` operation. +func (q *Query) WaitConditionAnd(n int) *Query { + q.headers = append(q.headers, fmt.Sprintf("WaitConditionAnd: %d", n)) + return q +} + +// WaitConditionOr combines the n last wait condition into a new wait condition +// using a `Or` operation. +func (q *Query) WaitConditionOr(n int) *Query { + q.headers = append(q.headers, fmt.Sprintf("WaitConditionOr: %d", n)) + return q +} + +// WaitConditionNegate negates the most recent wait condition. +func (q *Query) WaitConditionNegate() *Query { + q.headers = append(q.headers, "WaitConditionNegate:") + return q +} + +// WaitTrigger sets the nagios event that will trigger a check of +// the wait condition. +func (q *Query) WaitTrigger(event string) *Query { + q.headers = append(q.headers, "WaitTrigger: "+event) + return q +} + +// WaitTimeout set a timeout for the wait condition. +func (q *Query) WaitTimeout(t time.Duration) *Query { + q.headers = append(q.headers, fmt.Sprintf("WaitTimeout: %d", t/time.Millisecond)) + return q +} + // Exec executes the query. func (q *Query) Exec() (*Response, error) { resp := &Response{}