static void get_gw_data(char *username, char *device, JsonNode *last) { JsonNode *array; static char *types[] = { "batt", "ext", "status", NULL }; char **t, *js; static UT_string *ts = NULL, *u = NULL, *d = NULL; if (last == NULL || last->tag != JSON_OBJECT) return; for (t = types; t && *t; t++) { utstring_renew(u); utstring_renew(d); utstring_printf(u, "%s", username); utstring_printf(d, "%s", device); lowercase(UB(u)); lowercase(UB(d)); utstring_renew(ts); utstring_printf(ts, "%s/last/%s/%s/%s.json", STORAGEDIR, UB(u), UB(d), *t); /* Read file into JSON array and append to `last' object */ if ((js = slurp_file(UB(ts), TRUE)) != NULL) { if ((array = json_decode(js)) != NULL) { json_append_member(last, *t, array); } free(js); } } }
JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi, int reverse) { JsonNode *json = json_mkobject(); UT_string *path = NULL; char *bp; utstring_renew(path); for (bp = user; bp && *bp; bp++) { if (isupper(*bp)) *bp = tolower(*bp); } for (bp = device; bp && *bp; bp++) { if (isupper(*bp)) *bp = tolower(*bp); } if (!user && !device) { utstring_printf(path, "%s/rec", STORAGEDIR); ls(UB(path), json); } else if (!device) { utstring_printf(path, "%s/rec/%s", STORAGEDIR, user); ls(UB(path), json); } else { utstring_printf(path, "%s/rec/%s/%s", STORAGEDIR, user, device); lsscan(UB(path), s_lo, s_hi, json, reverse); } return (json); }
int rates_cmd(void *cp, cp_arg_t *arg, void *data) { utstring_renew(CF.s); unsigned i,hits; char *when; for(i=0; i<CF.frames_1h->num_buckets; i++) { hits = *(unsigned*)(bkt(CF.frames_1h,i)->data); when = asctime(localtime(&(bkt(CF.frames_1h,i)->start))); utstring_printf(CF.s, " %u frames: %s", hits, when); } cp_add_reply(cp,utstring_body(CF.s),utstring_len(CF.s)); }
void monitorhook(struct udata *userdata, time_t now, char *topic) { // struct udata *ud = (struct udata *)userdata; /* TODO: add monitor hook to a "monitor" key in LMDB ? */ char mpath[BUFSIZ]; static UT_string *us = NULL; utstring_renew(us); utstring_printf(us, "%ld %s\n", now, topic); snprintf(mpath, sizeof(mpath), "%s/monitor", STORAGEDIR); safewrite(mpath, UB(us)); }
void olog(int level, char *fmt, ...) { va_list ap; static UT_string *u = NULL; va_start(ap, fmt); utstring_renew(u); utstring_printf_va(u, fmt, ap); // fprintf(stderr, "+++++ [%s]\n", UB(u)); syslog(level, "%s", UB(u)); va_end(ap); }
FILE *pathn(char *mode, char *prefix, UT_string *user, UT_string *device, char *suffix) { static UT_string *path = NULL; time_t now; utstring_renew(path); ut_lower(user); if (device) { ut_lower(device); utstring_printf(path, "%s/%s/%s/%s", STORAGEDIR, prefix, UB(user), UB(device)); } else { utstring_printf(path, "%s/%s/%s", STORAGEDIR, prefix, UB(user)); } ut_clean(path); if (mkpath(UB(path)) < 0) { olog(LOG_ERR, "Cannot create directory at %s: %m", UB(path)); return (NULL); } #if 0 if (device) { utstring_printf(path, "/%s-%s.%s", UB(user), UB(device), suffix); } else { utstring_printf(path, "/%s.%s", UB(user), suffix); } #endif if (strcmp(prefix, "rec") == 0) { time(&now); utstring_printf(path, "/%s.%s", yyyymm(now), suffix); } else { utstring_printf(path, "/%s.%s", UB(user), suffix); } ut_clean(path); return (fopen(UB(path), mode)); }
char *bindump(char *buf, long buflen) { static UT_string *out = NULL; int i, ch; utstring_renew(out); for (i = 0; i < buflen; i++) { ch = buf[i]; if (isprint(ch)) { utstring_printf(out, "%c", ch); } else { utstring_printf(out, " %02X ", ch & 0xFF); } } return (UB(out)); }
void debug(struct udata *ud, char *fmt, ...) { va_list ap; static UT_string *u = NULL; if (ud->debug == FALSE) return; va_start(ap, fmt); utstring_renew(u); utstring_printf_va(u, fmt, ap); fprintf(stderr, "+++++ [%s]\n", UB(u)); va_end(ap); }
static void lsscan(char *pathpat, time_t s_lo, time_t s_hi, JsonNode *obj, int reverse) { struct dirent **namelist; int i, n; JsonNode *jarr = json_mkarray(); static UT_string *path = NULL; if (obj == NULL || obj->tag != JSON_OBJECT) return; utstring_renew(path); /* Set global t_ values */ t_lo = s_lo; t_hi = s_hi; if ((n = scandir(pathpat, &namelist, filter_filename, cmp)) < 0) { json_append_member(obj, "error", json_mkstring("Cannot lsscan requested directory")); return; } if (reverse) { for (i = n - 1; i >= 0; i--) { utstring_clear(path); utstring_printf(path, "%s/%s", pathpat, namelist[i]->d_name); json_append_element(jarr, json_mkstring(UB(path))); free(namelist[i]); } } else { for (i = 0; i < n; i++) { utstring_clear(path); utstring_printf(path, "%s/%s", pathpat, namelist[i]->d_name); json_append_element(jarr, json_mkstring(UB(path))); free(namelist[i]); } } free(namelist); json_append_member(obj, "results", jarr); }
int stats_cmd(void *cp, cp_arg_t *arg, void *data) { utstring_renew(CF.s); utstring_printf(CF.s, "frames_read: %u\n", CF.frames_read); utstring_printf(CF.s, "frames_processed: %u\n", CF.frames_processed); cp_add_reply(cp,utstring_body(CF.s),utstring_len(CF.s)); }