recorder/misc.c

96 lines
2.6 KiB
C
Raw Normal View History

2015-09-01 08:19:52 -07:00
/*
* Copyright (C) 2015 Jan-Piet Mens <jpmens@gmail.com> and OwnTracks
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* JAN-PIET MENS OR OWNTRACKS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
2015-08-14 23:14:34 -07:00
#include <stdio.h>
#include <stdlib.h>
#include "utstring.h"
#include "ctype.h"
2015-08-15 01:46:46 -07:00
#include "udata.h"
2015-08-14 23:14:34 -07:00
#include "misc.h"
2015-09-01 08:15:00 -07:00
#include "util.h"
2015-08-14 23:14:34 -07:00
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);
}
}
2015-09-21 07:07:31 -07:00
return (UB(out));
2015-08-14 23:14:34 -07:00
}
2015-08-15 01:42:55 -07:00
/*
* At each received message, the recorder invokes this function with the
* current epoch time and the topic being handled. Use this to update
* a monitoring hoook. If we have Redis, use that exclusively.
2015-08-15 01:42:55 -07:00
*/
2015-08-15 01:46:46 -07:00
void monitorhook(struct udata *userdata, time_t now, char *topic)
2015-08-15 01:42:55 -07:00
{
2015-09-01 04:31:49 -07:00
// struct udata *ud = (struct udata *)userdata;
2015-08-15 01:46:46 -07:00
2015-09-01 04:31:49 -07:00
/* TODO: add monitor hook to a "monitor" key in LMDB ? */
2015-09-01 04:31:49 -07:00
char mpath[BUFSIZ];
static UT_string *us = NULL;
2015-09-01 04:31:49 -07:00
utstring_renew(us);
utstring_printf(us, "%ld %s\n", now, topic);
snprintf(mpath, sizeof(mpath), "%s/monitor", STORAGEDIR);
2015-09-21 07:07:31 -07:00
safewrite(mpath, UB(us));
2015-08-15 01:42:55 -07:00
}
/*
* Return a pointer to a static area containing the last monitor entry or NULL.
*/
char *monitor_get()
{
char mpath[BUFSIZ];
static char monitorline[BUFSIZ], *ret = NULL;
FILE *fp;
snprintf(mpath, sizeof(mpath), "%s/monitor", STORAGEDIR);
if ((fp = fopen(mpath, "r")) != NULL) {
if (fgets(monitorline, sizeof(monitorline), fp) != NULL) {
char *bp = strchr(monitorline, '\n');
if (bp)
*bp = 0;
ret = monitorline;
}
fclose(fp);
}
return (ret);
}