rt_sink.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. // Provide additional information for unexpected errors
  156. err = &util.RuntimeErrorWithDetail{
  157. RuntimeError: err.(*util.RuntimeError),
  158. Environment: sinkVS,
  159. Data: nil,
  160. }
  161. }
  162. }
  163. }
  164. return err
  165. },
  166. }
  167. if err = rt.erp.Processor.AddRule(rule); err != nil {
  168. err = rt.erp.NewRuntimeError(util.ErrInvalidState, err.Error(), rt.node)
  169. }
  170. }
  171. }
  172. return nil, err
  173. }
  174. // Sink child nodes
  175. // ================
  176. /*
  177. sinkDetailRuntime is the runtime for sink detail declarations.
  178. */
  179. type sinkDetailRuntime struct {
  180. *baseRuntime
  181. valType string
  182. }
  183. /*
  184. Eval evaluate this runtime component.
  185. */
  186. func (rt *sinkDetailRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  187. var ret interface{}
  188. _, err := rt.baseRuntime.Eval(vs, is, tid)
  189. if err == nil {
  190. if ret, err = rt.node.Children[0].Runtime.Eval(vs, is, tid); err == nil {
  191. // Check value is of expected type
  192. if rt.valType == "list" {
  193. if _, ok := ret.([]interface{}); !ok {
  194. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  195. fmt.Sprintf("Expected a list as value"),
  196. rt.node)
  197. }
  198. } else if rt.valType == "map" {
  199. if _, ok := ret.(map[interface{}]interface{}); !ok {
  200. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  201. fmt.Sprintf("Expected a map as value"),
  202. rt.node)
  203. }
  204. } else if rt.valType == "int" {
  205. if _, ok := ret.(float64); !ok {
  206. return nil, rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  207. fmt.Sprintf("Expected a number as value"),
  208. rt.node)
  209. }
  210. }
  211. }
  212. }
  213. return ret, err
  214. }
  215. /*
  216. kindMatchRuntimeInst returns a new runtime component instance.
  217. */
  218. func kindMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  219. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  220. }
  221. /*
  222. scopeMatchRuntimeInst returns a new runtime component instance.
  223. */
  224. func scopeMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  225. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  226. }
  227. /*
  228. stateMatchRuntimeInst returns a new runtime component instance.
  229. */
  230. func stateMatchRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  231. return &sinkDetailRuntime{newBaseRuntime(erp, node), "map"}
  232. }
  233. /*
  234. priorityRuntimeInst returns a new runtime component instance.
  235. */
  236. func priorityRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  237. return &sinkDetailRuntime{newBaseRuntime(erp, node), "int"}
  238. }
  239. /*
  240. suppressesRuntimeInst returns a new runtime component instance.
  241. */
  242. func suppressesRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  243. return &sinkDetailRuntime{newBaseRuntime(erp, node), "list"}
  244. }