eval.h 2.6 KB

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