eval.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_EVAL_H
  3. #define NETDATA_EVAL_H 1
  4. #include "../libnetdata.h"
  5. #define EVAL_MAX_VARIABLE_NAME_LENGTH 300
  6. typedef enum rrdcalc_status {
  7. RRDCALC_STATUS_REMOVED = -2,
  8. RRDCALC_STATUS_UNDEFINED = -1,
  9. RRDCALC_STATUS_UNINITIALIZED = 0,
  10. RRDCALC_STATUS_CLEAR = 1,
  11. RRDCALC_STATUS_RAISED = 2,
  12. RRDCALC_STATUS_WARNING = 3,
  13. RRDCALC_STATUS_CRITICAL = 4
  14. } RRDCALC_STATUS;
  15. typedef struct eval_variable {
  16. char *name;
  17. uint32_t hash;
  18. struct eval_variable *next;
  19. } EVAL_VARIABLE;
  20. typedef struct eval_expression {
  21. const char *source;
  22. const char *parsed_as;
  23. RRDCALC_STATUS *status;
  24. NETDATA_DOUBLE *myself;
  25. time_t *after;
  26. time_t *before;
  27. NETDATA_DOUBLE result;
  28. int error;
  29. BUFFER *error_msg;
  30. // hidden EVAL_NODE *
  31. void *nodes;
  32. // custom data to be used for looking up variables
  33. struct rrdcalc *rrdcalc;
  34. } EVAL_EXPRESSION;
  35. #define EVAL_VALUE_INVALID 0
  36. #define EVAL_VALUE_NUMBER 1
  37. #define EVAL_VALUE_VARIABLE 2
  38. #define EVAL_VALUE_EXPRESSION 3
  39. // parsing and evaluation
  40. #define EVAL_ERROR_OK 0
  41. // parsing errors
  42. #define EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION 1
  43. #define EVAL_ERROR_UNKNOWN_OPERAND 2
  44. #define EVAL_ERROR_MISSING_OPERAND 3
  45. #define EVAL_ERROR_MISSING_OPERATOR 4
  46. #define EVAL_ERROR_REMAINING_GARBAGE 5
  47. #define EVAL_ERROR_IF_THEN_ELSE_MISSING_ELSE 6
  48. // evaluation errors
  49. #define EVAL_ERROR_INVALID_VALUE 101
  50. #define EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS 102
  51. #define EVAL_ERROR_VALUE_IS_NAN 103
  52. #define EVAL_ERROR_VALUE_IS_INFINITE 104
  53. #define EVAL_ERROR_UNKNOWN_VARIABLE 105
  54. // parse the given string as an expression and return:
  55. // a pointer to an expression if it parsed OK
  56. // NULL in which case the pointer to error has the error code
  57. extern EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error);
  58. // free all resources allocated for an expression
  59. extern void expression_free(EVAL_EXPRESSION *expression);
  60. // convert an error code to a message
  61. extern const char *expression_strerror(int error);
  62. // evaluate an expression and return
  63. // 1 = OK, the result is in: expression->result
  64. // 2 = FAILED, the error message is in: buffer_tostring(expression->error_msg)
  65. extern int expression_evaluate(EVAL_EXPRESSION *expression);
  66. extern int health_variable_lookup(const char *variable, uint32_t hash, struct rrdcalc *rc, NETDATA_DOUBLE *result);
  67. #endif //NETDATA_EVAL_H