void op_in(void) { Var left, right, r; right = pop(); left = pop(); r.type = NUM; if (right.type == LIST) { r.v.num = list_ismember(left, right.v.list); push(r); } else if (right.type == STR) { if (left.type != STR) { raise(E_TYPE); } else { r.v.num = str_in(left.v.str->str, right.v.str->str); push(r); } } else { raise(E_TYPE); } var_free(left); var_free(right); }
/** The watcher-delete command */ int cmd_watcher_delete (int argc, const char *argv[]) { if (OPTIONS.tenant[0] == 0) { fprintf (stderr, "%% No tenantid provided\n"); return 1; } if (OPTIONS.meter[0] == 0) { fprintf (stderr, "%% No meter provided\n"); return 1; } var *req = var_alloc(); var *apires; if (OPTIONS.host[0]) { apires = api_call ("DELETE", req, "/%s/host/%s/watcher/%s", OPTIONS.tenant, OPTIONS.host, OPTIONS.meter); } else { apires = api_call ("DELETE", req, "/%s/watcher/%s", OPTIONS.tenant, OPTIONS.meter); } var_free (req); var_free (apires); return 0; }
void op_break(void) { Var newpc; int break_lvl = frame.m->code[frame.pc++]; while (break_lvl--) { do { newpc = pop(); var_free(newpc); } while (newpc.type != PC || newpc.v.num < 0); } if (frame.m->code[newpc.v.num] == FOR) { (void) pop(); /* pop index */ newpc = pop(); var_free(newpc);/* pop list */ frame.pc = frame.m->code[newpc.v.num + 2]; } else if (frame.m->code[newpc.v.num] == FORRNG) { (void) pop(); /* pop upper range */ (void) pop(); /* pop current value */ frame.pc = frame.m->code[newpc.v.num + 2]; } else if (frame.m->code[newpc.v.num] == DOWHILE) { frame.pc = newpc.v.num + 3; } else if (frame.m->code[newpc.v.num] == PUSHPC) { /* WHILE, actually */ while (frame.m->code[newpc.v.num] != WHILE) { newpc.v.num++; } frame.pc = frame.m->code[newpc.v.num + 1]; } }
void op_for(void) { Var idx, list; idx = pop(); list = pop(); if( list.type == MAP) { list.v.list = map_keys( list.v.map ); list.type = LIST; } if( list.type == STR) { list.v.list = string_list( list.v.str ); list.type = LIST; } if (list.type != LIST) { var_free(list); raise(E_FOR); } else if (idx.v.num >= list.v.list->len) { /* loop is complete */ var_free(list); frame.pc = frame.m->code[frame.pc + 1]; /* skip to end */ } else { var_assign_local(frame.stack, frame.m->code[frame.pc], var_dup(list.v.list->el[idx.v.num])); idx.v.num++; push(list); /* push list */ push(idx); /* push new index */ pushpc(frame.pc - 1); /* push address of FOR statement */ frame.pc += 2; /* go to first instruction in loop */ } }
/** The meter-create command */ int cmd_meter_create (int argc, const char *argv[]) { if (OPTIONS.tenant[0] == 0) { fprintf (stderr, "%% No tenantid provided\n"); return 1; } if (OPTIONS.meter[0] == 0) { fprintf (stderr, "%% No meter provided\n"); return 1; } var *req = var_alloc(); var *req_meter = var_get_dict_forkey (req, "meter"); var_set_str_forkey (req_meter, "type", OPTIONS.type); if (OPTIONS.description[0]) { var_set_str_forkey (req_meter, "description", OPTIONS.description); } if (OPTIONS.unit[0]) { var_set_str_forkey (req_meter, "unit", OPTIONS.unit); } var *apires = api_call ("POST",req,"/%s/meter/%s", OPTIONS.tenant, OPTIONS.meter); var_free (req); var_free (apires); return 0; }
int cmd_session_list (int argc, const char *argv[]) { var *v = api_get ("/session"); if (OPTIONS.json) { var_dump (v, stdout); var_free (v); return 0; } printf ("---------------------------------------------" "-----------------------------------\n"); printf ("Session ID Sender" " Last Refresh\n"); var *v_session = var_get_array_forkey (v, "session"); var *crsr = v_session->value.arr.first; while (crsr) { printf ("%08x-%08x %-39s %s\n", (uint32_t) (var_get_int_forkey (crsr, "sessid")), (uint32_t) (var_get_int_forkey (crsr, "addr")), var_get_str_forkey (crsr, "remote"), var_get_str_forkey (crsr, "lastcycle")); crsr = crsr->next; } printf ("---------------------------------------------" "-----------------------------------\n"); var_free (v); return 0; }
/** The watcher-set command */ int cmd_watcher_set (int argc, const char *argv[]) { if (OPTIONS.tenant[0] == 0) { fprintf (stderr, "%% No tenantid provided\n"); return 1; } if (OPTIONS.meter[0] == 0) { fprintf (stderr, "%% No meter provided\n"); return 1; } if (OPTIONS.match[0] == 0) { fprintf (stderr, "%% No match provided\n"); return 1; } if (OPTIONS.value[0] == 0) { fprintf (stderr, "%% No value provided\n"); return 1; } var *mdef = api_get ("/%s/meter", OPTIONS.tenant); if (! mdef) return 1; var *mdef_m = var_get_dict_forkey (mdef, "meter"); var *mde_meter = var_get_dict_forkey (mdef_m, OPTIONS.meter); const char *inftype = var_get_str_forkey (mde_meter, "type"); var *req = var_alloc(); var *req_watcher = var_get_dict_forkey (req, "watcher"); var *reql = var_get_dict_forkey (req_watcher, OPTIONS.level); if (OPTIONS.host[0] == 0) { var_set_str_forkey (reql, "cmp", OPTIONS.match); } if (strcmp (inftype, "integer") == 0) { var_set_int_forkey (reql, "val", strtoull (OPTIONS.value, NULL, 10)); } else if (strcmp (inftype, "frac") == 0) { var_set_double_forkey (reql, "val", atof (OPTIONS.value)); } else { var_set_str_forkey (reql, "val", OPTIONS.value); } var_set_double_forkey (reql, "weight", atof (OPTIONS.weight)); var *apires; if (OPTIONS.host[0]) { apires = api_call ("POST", req, "/%s/host/%s/watcher/%s", OPTIONS.tenant, OPTIONS.host, OPTIONS.meter); } else { apires = api_call ("POST", req, "/%s/watcher/%s", OPTIONS.tenant, OPTIONS.meter); } var_free (mdef); var_free (req); var_free (apires); return 0; }
void op_ne(void) { Var ret, right, left; right = pop(); left = pop(); ret.type = NUM; ret.v.num = !var_eq(left, right); push (ret); var_free(left); var_free(right); }
char * eval_arglist(const char args[], const char **stop_ptr) { size_t len = 0; char *eval_result = NULL; assert(args[0] != '\0'); while(args[0] != '\0') { char *free_this = NULL; const char *tmp_result = NULL; var_t result = var_false(); const ParsingErrors parsing_error = parse(args, &result); if(parsing_error == PE_INVALID_EXPRESSION && is_prev_token_whitespace()) { result = get_parsing_result(); tmp_result = free_this = var_to_string(result); args = get_last_parsed_char(); } else if(parsing_error == PE_NO_ERROR) { tmp_result = free_this = var_to_string(result); args = get_last_position(); } if(tmp_result == NULL) { var_free(result); break; } if(!is_null_or_empty(eval_result)) { eval_result = extend_string(eval_result, " ", &len); } eval_result = extend_string(eval_result, tmp_result, &len); var_free(result); free(free_this); args = skip_whitespace(args); } if(args[0] == '\0') { return eval_result; } else { free(eval_result); *stop_ptr = args; return NULL; } }
ParsingErrors parse(const char input[], var_t *result) { expr_t expr_root; assert(initialized && "Parser must be initialized before use."); last_error = PE_NO_ERROR; last_token.type = BEGIN; last_position = input; get_next(&last_position); expr_root = parse_or_expr(&last_position); last_parsed_char = last_position; if(last_token.type != END) { if(last_parsed_char > input) { last_parsed_char--; } if(last_error == PE_NO_ERROR) { if(last_token.type == DQ && strchr(last_position, '"') == NULL) { /* This is a comment, just ignore it. */ last_position += strlen(last_position); } else if(eval_expr(&expr_root) == 0) { var_free(res_val); res_val = var_clone(expr_root.value); last_error = PE_INVALID_EXPRESSION; } } } if(last_error == PE_NO_ERROR) { if(eval_expr(&expr_root) == 0) { var_free(res_val); res_val = var_clone(expr_root.value); *result = var_clone(expr_root.value); } } if(last_error == PE_INVALID_EXPRESSION) { last_position = skip_whitespace(input); } free_expr(&expr_root); return last_error; }
/** The host-list command */ int cmd_host_list (int argc, const char *argv[]) { if (OPTIONS.tenant[0] == 0) { fprintf (stderr, "%% No tenantid provided\n"); return 1; } const char *unit; var *apires = api_get ("/%s/host", OPTIONS.tenant); if (OPTIONS.json) { var_dump (apires, stdout); var_free (apires); return 0; } var *v_hosts = var_get_array_forkey (apires, "host"); if (var_get_count (v_hosts)) { printf ("UUID Size " "First record Last record\n"); printf ("---------------------------------------------" "-----------------------------------\n"); var *crsr = v_hosts->value.arr.first; while (crsr) { uint64_t usage = var_get_int_forkey (crsr, "usage"); unit = "KB"; usage = usage / 1024; if (usage > 2048) { unit = "MB"; usage = usage / 1024; } char start[24]; char end[24]; strncpy (start, var_get_str_forkey (crsr, "start"), 23); strncpy (end, var_get_str_forkey (crsr, "end"), 23); start[16] = 0; end[16] = 0; start[10] = ' '; end[10] = ' '; printf ("%s %4llu %s %s %s\n", var_get_str_forkey (crsr, "id"), usage, unit, start, end); crsr = crsr->next; } printf ("---------------------------------------------" "-----------------------------------\n"); } var_free (apires); return 0; }
void op_mul(void) { Var right, left; right = pop(); left = pop(); if (left.type != NUM || right.type != NUM) { var_free(left); var_free(right); raise(E_TYPE); } else { left.v.num *= right.v.num; push(left); } }
/** The tenant-delete command */ int cmd_tenant_delete (int argc, const char *argv[]) { /* Avoid using the default tenant in this case */ disregard_default_tenant(); if (OPTIONS.tenant[0] == 0) { fprintf (stderr, "%% No tenantid provided\n"); return 1; } var *req = var_alloc(); var *apires = api_call ("DELETE", req, "/%s", OPTIONS.tenant); var_free (req); var_free (apires); return 0; }
END_TEST START_TEST (var_delete_test) { int res; const char *key = "%{foo}"; (void) var_free(); res = pr_var_delete(NULL); fail_unless(res == -1, "Failed to handle uninitialized Var table"); fail_unless(errno == EPERM, "Failed to set errno to EPERM"); (void) var_init(); res = pr_var_delete(NULL); fail_unless(res == -1, "Failed to handle null key"); fail_unless(errno == EINVAL, "Failed to set errno to EINVAL"); res = pr_var_delete(key); fail_unless(res == -1, "Failed to handle absent key"); fail_unless(errno == ENOENT, "Failed to set errno to ENOENT"); res = pr_var_set(p, key, "test", PR_VAR_TYPE_STR, "bar", NULL, 0); fail_unless(res == 0, "Failed to add var: %s", strerror(errno)); res = pr_var_delete(key); fail_unless(res == 0, "Failed to delete var: %s", strerror(errno)); res = pr_var_delete(key); fail_unless(res == -1, "Failed to handle absent key"); fail_unless(errno == ENOENT, "Failed to set errno to ENOENT"); }
END_TEST START_TEST (var_exists_test) { int res; const char *key; (void) var_free(); res = pr_var_exists(NULL); fail_unless(res == -1, "Failed to handle uninitialized Var table"); fail_unless(errno == EPERM, "Failed to set errno to EPERM"); (void) var_init(); res = pr_var_exists(NULL); fail_unless(res == -1, "Failed to handle null key"); fail_unless(errno == EINVAL, "Failed to set errno to EINVAL"); key = "%{foo}"; res = pr_var_exists(key); fail_unless(res == FALSE, "Failed to handle absent key"); res = pr_var_set(p, key, NULL, PR_VAR_TYPE_STR, "bar", NULL, 0); fail_unless(res == 0, "Failed to add var: %s", strerror(errno)); res = pr_var_exists(key); fail_unless(res == TRUE, "Failed to detect present key"); }
END_TEST START_TEST (var_next_test) { int ok; const char *res, *desc; (void) var_free(); res = pr_var_next(NULL); fail_unless(res == NULL, "Failed to handle uninitialized Var table"); fail_unless(errno == EPERM, "Failed to set errno to EPERM"); (void) var_init(); res = pr_var_next(NULL); fail_unless(res == NULL, "Failed to handle empty table"); ok = pr_var_set(p, "%{foo}", NULL, PR_VAR_TYPE_STR, "bar", NULL, 0); fail_unless(ok == 0, "Failed to add var: %s", strerror(errno)); res = pr_var_next(&desc); fail_unless(res != NULL, "Failed to get next key: %s", strerror(errno)); fail_unless(desc == NULL, "Expected no desc, got '%s'", desc); res = pr_var_next(&desc); fail_unless(res == NULL, "Expected no more keys, got '%s'", res); }
/** The tenant-list command */ int cmd_tenant_list (int argc, const char *argv[]) { var *res = api_get ("/"); if (OPTIONS.json) { var_dump (res, stdout); } else { printf ("UUID Hosts Name\n"); printf ("---------------------------------------------" "-----------------------------------\n"); var *res_tenant = var_get_array_forkey (res, "tenant"); if (var_get_count (res_tenant)) { var *crsr = res_tenant->value.arr.first; while (crsr) { printf ("%s %5llu %s\n", var_get_str_forkey (crsr, "id"), var_get_int_forkey (crsr, "hostcount"), var_get_str_forkey (crsr, "name")); crsr = crsr->next; } } printf ("----------------------------------------------" "----------------------------------\n"); } var_free (res); return 0; }
/** Remove a named member from a dict */ void var_delete_key (var *v, const char *k) { if (v->type != VAR_DICT) return; var *node = var_find_key (v, k); if (! node) return; if (node->prev) { node->prev->next = node->next; } else { v->value.arr.first = node->next; } if (node->next) { node->next->prev = node->prev; } else { v->value.arr.last = node->prev; } if (node->parent) { node->parent->value.arr.count--; node->value.arr.cachepos = -1; node->value.arr.cachenode = NULL; node->parent = NULL; } node->next = node->prev = NULL; node->root = node->parent = NULL; var_free (node); }
void op_div(void) { Var right, left; right = pop(); left = pop(); if (left.type != NUM || right.type != NUM) { var_free(left); var_free(right); raise(E_TYPE); } else if (!right.v.num) { raise(E_DIV); } else { left.v.num /= right.v.num; push(left); } }
void op_pop(void) { Var v; v = pop(); var_free(v); }
/** The meter-delete command */ int cmd_meter_delete (int argc, const char *argv[]) { if (OPTIONS.tenant[0] == 0) { fprintf (stderr, "%% No tenantid provided\n"); return 1; } if (OPTIONS.meter[0] == 0) { fprintf (stderr, "%% No meter provided\n"); return 1; } var *p = var_alloc(); var *apires = api_call ("DELETE", p, "/%s/meter/%s", OPTIONS.tenant, OPTIONS.meter); var_free (p); var_free (apires); return 0; }
static void tear_down(void) { (void) var_free(); if (p) { destroy_pool(p); p = NULL; permanent_pool = NULL; } }
/** The tenant-create command */ int cmd_tenant_create (int argc, const char *argv[]) { uuid tenant; /* Avoid using the default tenant in this case */ disregard_default_tenant(); if (OPTIONS.tenant[0] == 0) { tenant = uuidgen(); OPTIONS.tenant = (const char *) malloc (40); uuid2str (tenant, (char *) OPTIONS.tenant); } else { tenant = mkuuid (OPTIONS.tenant); } var *req = var_alloc(); var *req_tenant = var_get_dict_forkey (req, "tenant"); if (OPTIONS.key[0]) { var_set_str_forkey (req_tenant, "key", OPTIONS.key); } if (OPTIONS.name[0]) { var_set_str_forkey (req_tenant, "name", OPTIONS.name); } var *apires = api_call ("POST", req, "/%s", OPTIONS.tenant); if (apires) { var *r = var_get_dict_forkey (apires, "tenant"); printf ("Tenant created:\n" "---------------------------------------------" "-----------------------------------\n" " Name: %s\n" " UUID: %s\n" " AES Key: %s\n" "---------------------------------------------" "-----------------------------------\n", var_get_str_forkey (r, "name"), OPTIONS.tenant, var_get_str_forkey (r, "key")); } var_free (req); var_free (apires); return 0; }
/** Main loop for the watchthread */ void watchthread_run (thread *self) { tenant *tcrsr; host *hcrsr; time_t t_now = time (NULL); time_t t_next = (t_now+60)-((t_now+60)%60); log_debug ("Watchthread started"); sleep (t_next - t_now); t_next += 60; while (1) { tcrsr = tenant_first (TENANT_LOCK_READ); while (tcrsr) { summaryinfo_start_round (&tcrsr->summ); hcrsr = tcrsr->first; while (hcrsr) { watchthread_handle_host (hcrsr); hcrsr = hcrsr->next; } var *overv = tenant_overview (tcrsr); var *tally = summaryinfo_tally_round (&tcrsr->summ); if (db_open (APP.writedb, tcrsr->uuid, NULL)) { db_set_summary (APP.writedb, tally); db_set_overview (APP.writedb, overv); db_close (APP.writedb); } var_free (tally); var_free (overv); tcrsr = tenant_next (tcrsr, TENANT_LOCK_READ); } t_now = time (NULL); if (t_now < t_next) { log_debug ("Watchthread took %i seconds", 60-(t_next-t_now)); sleep (t_next-t_now); } else { log_error ("Watchthread round cannot keep up"); } t_next += 60; while (t_next < t_now) t_next += 60; } }
/** The watcher-list command */ int cmd_watcher_list (int argc, const char *argv[]) { if (OPTIONS.tenant[0] == 0) { fprintf (stderr, "%% No tenantid provided\n"); return 1; } var *apires; if (OPTIONS.host[0]) { apires = api_get ("/%s/host/%s/watcher", OPTIONS.tenant, OPTIONS.host); } else { apires = api_get ("/%s/watcher", OPTIONS.tenant); } if (OPTIONS.json) { var_dump (apires, stdout); var_free (apires); return 0; } printf ("From Meter Trigger Match " "Value Weight\n" "-------------------------------------------------------" "-------------------------\n"); var *apiwatch = var_get_dict_forkey (apires, "watcher"); if (var_get_count (apiwatch)) { var *crsr = apiwatch->value.arr.first; while (crsr) { print_data (crsr->id, "warning", var_get_dict_forkey (crsr, "warning")); print_data (crsr->id, "alert", var_get_dict_forkey (crsr, "alert")); print_data (crsr->id, "critical", var_get_dict_forkey (crsr, "critical")); crsr = crsr->next; } printf ("---------------------------------------------" "-----------------------------------\n"); } var_free (apires); return 0; }
void op_index(void) { Var ret, i, base; i = pop(); base = pop(); switch (base.type) { case STR: if (i.type != NUM) { raise(E_TYPE); } else if (i.v.num < 1 || i.v.num > base.v.str->len) { raise(E_RANGE); } else { ret.type = STR; ret.v.str = string_new(2); ret.v.str->str[0] = base.v.str->str[i.v.num - 1]; ret.v.str->str[1] = '\0'; ret.v.str->len = 1; push(ret); } break; case LIST: if (i.type != NUM) { raise(E_TYPE); } else if (i.v.num < 1 || i.v.num > base.v.list->len) { raise(E_RANGE); } else { ret = var_dup(base.v.list->el[i.v.num - 1]); push(ret); } break; case MAP: if (map_find(base.v.map, i, &ret)) { raise(E_MAPNF); } else { push(var_dup(ret)); } break; default: raise(E_TYPE); } var_free(base); var_free(i); }
void op_sub(void) { Var right, left; right = pop(); left = pop(); if (left.type != right.type && left.type != LIST) { var_free(left); var_free(right); raise(E_TYPE); } else if (left.type == NUM) { left.v.num -= right.v.num; push(left); } else if (left.type == LIST) { left.v.list = list_setremove(left.v.list, right); var_free(right); push(left); } else { raise(E_ARGTYPE); } }
void op_message_expr(void) { Var args; Var msg, dest; args = pop_args(count_args()); msg = pop(); dest = pop(); if (dest.type != OBJ) { var_free(args); var_free(dest); var_free(msg); raise(E_INVIND); } else if (msg.type != STR) { var_free(args); var_free(msg); raise(E_TYPE); } else { send_message_and_block(frame.this, dest.v.obj, msg.v.str, args.v.list, dest.v.obj); } }
void op_message(void) { Var args; Var dest; String *msg; args = pop_args(count_args()); dest = pop(); if (dest.type != OBJ) { var_free(args); var_free(dest); frame.pc++; raise(E_INVIND); } else { msg = string_dup(sym_get(frame.on, frame.m->code[frame.pc])); frame.pc++; send_message_and_block(frame.this, dest.v.obj, msg, args.v.list, dest.v.obj); } }
void op_not(void) { Var ret, arg; arg = pop(); ret.type = NUM; ret.v.num = !ISTRUE(arg); var_free(arg); push(ret); }