rt_sink.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package interpreter
  11. import (
  12. "fmt"
  13. "math"
  14. "strings"
  15. "devt.de/krotik/ecal/engine"
  16. "devt.de/krotik/ecal/parser"
  17. "devt.de/krotik/ecal/scope"
  18. "devt.de/krotik/ecal/util"
  19. )
  20. /*
  21. sinkRuntime is the runtime for sink declarations.
  22. */
  23. type sinkRuntime struct {
  24. *baseRuntime
  25. }
  26. /*
  27. sinkRuntimeInst returns a new runtime component instance.
  28. */
  29. func sinkRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  30. return &sinkRuntime{newBaseRuntime(erp, node)}
  31. }
  32. /*
  33. Validate this node and all its child nodes.
  34. */
  35. func (rt *sinkRuntime) Validate() error {
  36. err := rt.baseRuntime.Validate()
  37. if err == nil {
  38. // Check that all children are valid
  39. for _, child := range rt.node.Children[1:] {
  40. switch child.Name {
  41. case parser.NodeKINDMATCH:
  42. case parser.NodeSCOPEMATCH:
  43. case parser.NodeSTATEMATCH:
  44. case parser.NodePRIORITY:
  45. case parser.NodeSUPPRESSES:
  46. case parser.NodeSTATEMENTS:
  47. continue
  48. default:
  49. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  50. fmt.Sprintf("Unknown expression in sink declaration %v", child.Token.Val),
  51. child)
  52. }
  53. if err != nil {
  54. break
  55. }
  56. }
  57. }
  58. return err
  59. }
  60. /*
  61. Eval evaluate this runtime component.
  62. */
  63. func (rt *sinkRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  64. var kindMatch, scopeMatch, suppresses []string
  65. var stateMatch map[string]interface{}
  66. var priority int
  67. var statements *parser.ASTNode
  68. _, err := rt.baseRuntime.Eval(vs, is, tid)
  69. if err == nil {
  70. // Create default scope
  71. scopeMatch = []string{}
  72. // Get the name of the sink
  73. name := rt.node.Children[0].Token.Val
  74. // Create helper function
  75. makeStringList := func(child *parser.ASTNode) ([]string, error) {
  76. var ret []string
  77. val, err := child.Runtime.Eval(vs, is, tid)
  78. if err == nil {
  79. for _, v := range val.([]interface{}) {
  80. ret = append(ret, fmt.Sprint(v))
  81. }
  82. }
  83. return ret, err
  84. }
  85. // Collect values from children
  86. for _, child := range rt.node.Children[1:] {
  87. switch child.Name {
  88. case parser.NodeKINDMATCH:
  89. kindMatch, err = makeStringList(child)
  90. break
  91. case parser.NodeSCOPEMATCH:
  92. scopeMatch, err = makeStringList(child)
  93. break
  94. case parser.NodeSTATEMATCH:
  95. var val interface{}
  96. stateMatch = make(map[string]interface{})
  97. if val, err = child.Runtime.Eval(vs, is, tid); err == nil {
  98. for k, v := range val.(map[interface{}]interface{}) {
  99. stateMatch[fmt.Sprint(k)] = v
  100. }
  101. }
  102. break
  103. case parser.NodePRIORITY:
  104. var val interface{}
  105. if val, err = child.Runtime.Eval(vs, is, tid); err == nil {
  106. priority = int(math.Floor(val.(float64)))
  107. }
  108. break
  109. case parser.NodeSUPPRESSES:
  110. suppresses, err = makeStringList(child)
  111. break
  112. case parser.NodeSTATEMENTS:
  113. statements = child
  114. break
  115. }
  116. if err != nil {
  117. break
  118. }
  119. }
  120. if err == nil && statements != nil {
  121. var desc string
  122. sinkName := fmt.Sprint(name)
  123. if len(rt.node.Meta) > 0 &&
  124. (rt.node.Meta[0].Type() == parser.MetaDataPreComment ||
  125. rt.node.Meta[0].Type() == parser.MetaDataPostComment) {
  126. desc = strings.TrimSpace(rt.node.Meta[0].Value())
  127. }
  128. rule := &engine.Rule{
  129. Name: sinkName, // Name
  130. Desc: desc, // Description
  131. KindMatch: kindMatch, // Kind match
  132. ScopeMatch: scopeMatch, // Match on event cascade scope
  133. StateMatch: stateMatch, // No state match
  134. Priority: priority, // Priority of the rule
  135. SuppressionList: suppresses, // List of suppressed rules by this rule
  136. Action: func(p engine.Processor, m engine.Monitor, e *engine.Event, tid uint64) error { // Action of the rule
  137. // Create a new root variable scope
  138. sinkVS := scope.NewScope(fmt.Sprintf("sink: %v", sinkName))
  139. // Create a new instance state with the monitor - everything called
  140. // by the rule will have access to the current monitor.
  141. sinkIs := map[string]interface{}{
  142. "monitor": m,
  143. }
  144. err = sinkVS.SetValue("event", map[interface{}]interface{}{
  145. "name": e.Name(),
  146. "kind": strings.Join(e.Kind(), engine.RuleKindSeparator),
  147. "state": e.State(),
  148. })
  149. if err == nil {
  150. scope.SetParentOfScope(sinkVS, vs)
  151. if _, err = statements.Runtime.Eval(sinkVS, sinkIs, tid); err != nil {
  152. if sre, ok := err.(*util.RuntimeErrorWithDetail); ok {
  153. sre.Environment = sinkVS
  154. } else {
  155. var data interface{}
  156. rerr := rt.erp.NewRuntimeError(util.ErrSink, err.Error(), rt.node).(*util.RuntimeError)
  157. if e, ok := err.(*util.RuntimeError); ok {
  158. rerr = e
  159. } else if r, ok := err.(*returnValue); ok {
  160. rerr = r.RuntimeError
  161. data = r.returnValue
  162. }
  163. // Provide additional information for unexpected errors
  164. err = &util.RuntimeErrorWithDetail{
  165. RuntimeError: rerr,
  166. Environment: sinkVS,
  167. Data: data,
  168. }
  169. }
  170. }
  171. }
  172. return err
  173. },
  174. }
  175. if err = rt.erp.Processor.AddRule(rule); err != nil {
  176. err = rt.erp.NewRuntimeError(util.ErrInvalidState, err.Error(), rt.node)
  177. }
  178. }
  179. }
  180. return nil, err
  181. }
  182. // Sink child nodes
  183. // ================
  184. /*
  185. sinkDetailRuntime is the runtime for sink detail declarations.
  186. */
  187. type sinkDetailRuntime struct {
  188. *baseRuntime
  189. valType string
  190. }
  191. /*
  192. Eval evaluate this runtime component.
  193. */
  194. func (rt *sinkDetailRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  195. var ret interface{}
  196. _, err := rt.baseRuntime.Eval(vs, is, tid)
  197. if err == nil {
  198. if ret, err = rt.node.Children[0].Runtime.Eval(vs, is, tid); err == nil {
  199. // Check value is of expected type
  200. if rt.valType == "list" {
  201. if _, ok := ret.([]interface{}); !ok {
  202. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  203. fmt.Sprintf("Expected a list as value"),
  204. rt.node)
  205. }
  206. } else if rt.valType == "map" {
  207. if _, ok := ret.(map[interface{}]interface{}); !ok {
  208. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  209. fmt.Sprintf("Expected a map as value"),
  210. rt.node)
  211. }
  212. } else if rt.valType == "int" {
  213. if _, ok := ret.(float64); !ok {
  214. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  215. fmt.Sprintf("Expected a number as value"),
  216. rt.node)
  217. }
  218. }
  219. }
  220. }
  221. return ret, err
  222. }
  223. /*
  224. kindMatchRuntimeInst returns a new runtime component instance.
  225. */
  226. func kindMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  227. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  228. }
  229. /*
  230. scopeMatchRuntimeInst returns a new runtime component instance.
  231. */
  232. func scopeMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  233. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  234. }
  235. /*
  236. stateMatchRuntimeInst returns a new runtime component instance.
  237. */
  238. func stateMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  239. return &sinkDetailRuntime{newBaseRuntime(erp, node), "map"}
  240. }
  241. /*
  242. priorityRuntimeInst returns a new runtime component instance.
  243. */
  244. func priorityRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  245. return &sinkDetailRuntime{newBaseRuntime(erp, node), "int"}
  246. }
  247. /*
  248. suppressesRuntimeInst returns a new runtime component instance.
  249. */
  250. func suppressesRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  251. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  252. }