Improve database statistics endpoint
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
parent
49dc3bfda0
commit
a292c8f67f
|
@ -14,6 +14,14 @@
|
|||
#include "routes.h"
|
||||
// struct fifologData
|
||||
#include "../fifo.h"
|
||||
// get_FTL_db_filesize()
|
||||
#include "files.h"
|
||||
// get_sqlite3_version()
|
||||
#include "database/common.h"
|
||||
// get_number_of_queries_in_DB()
|
||||
#include "database/query-table.h"
|
||||
// getgrgid()
|
||||
#include <grp.h>
|
||||
|
||||
int api_ftl_client(struct mg_connection *conn)
|
||||
{
|
||||
|
@ -110,3 +118,77 @@ int api_ftl_dnsmasq_log(struct mg_connection *conn)
|
|||
// Send data
|
||||
JSON_SEND_OBJECT(json);
|
||||
}
|
||||
|
||||
int api_ftl_database(struct mg_connection *conn)
|
||||
{
|
||||
// Verify requesting client is allowed to see this ressource
|
||||
if(check_client_auth(conn) < 0)
|
||||
{
|
||||
send_json_unauthorized(conn);
|
||||
}
|
||||
|
||||
cJSON *json = JSON_NEW_OBJ();
|
||||
|
||||
// Add database stat details
|
||||
struct stat st;
|
||||
get_database_stat(&st);
|
||||
JSON_OBJ_ADD_NUMBER(json, "size", st.st_size); // Total size, in bytes
|
||||
|
||||
// File type
|
||||
char octal[5]; const char *human;
|
||||
cJSON *type = JSON_NEW_OBJ();
|
||||
snprintf(octal, sizeof(octal), "%04o", (st.st_mode & S_IFMT) >> 9);
|
||||
JSON_OBJ_COPY_STR(type, "octal", octal);
|
||||
if((st.st_mode & S_IFMT) == S_IFREG)
|
||||
human = "Regular file";
|
||||
else if((st.st_mode & S_IFMT) == S_IFLNK)
|
||||
human = "Symbolic link";
|
||||
else
|
||||
human = "Unknown";
|
||||
JSON_OBJ_REF_STR(type, "human", human);
|
||||
JSON_OBJ_ADD_ITEM(json, "type", type);
|
||||
|
||||
// File mode
|
||||
cJSON *mode = JSON_NEW_OBJ();
|
||||
snprintf(octal, sizeof(octal), "%03o", st.st_mode & 0x1FF);
|
||||
JSON_OBJ_COPY_STR(mode, "octal", octal);
|
||||
char permissions[10];
|
||||
get_permission_string(permissions, &st);
|
||||
JSON_OBJ_REF_STR(mode, "human", permissions);
|
||||
JSON_OBJ_ADD_ITEM(json, "mode", mode);
|
||||
|
||||
JSON_OBJ_ADD_NUMBER(json, "atime", st.st_atime); // Time of last access
|
||||
JSON_OBJ_ADD_NUMBER(json, "mtime", st.st_mtime); // Time of last modification
|
||||
JSON_OBJ_ADD_NUMBER(json, "ctime", st.st_ctime); // Time of last status change (owner or mode change, etc.)
|
||||
|
||||
// Get owner details
|
||||
cJSON *user = JSON_NEW_OBJ();
|
||||
JSON_OBJ_ADD_NUMBER(user, "uid", st.st_uid); // UID
|
||||
const struct passwd *pw = getpwuid(st.st_uid);
|
||||
if(pw != NULL)
|
||||
{
|
||||
JSON_OBJ_COPY_STR(user, "name", pw->pw_name); // User name
|
||||
JSON_OBJ_COPY_STR(user, "info", pw->pw_gecos); // User information
|
||||
}
|
||||
cJSON *group = JSON_NEW_OBJ();
|
||||
JSON_OBJ_ADD_NUMBER(group, "gid", st.st_gid); // GID
|
||||
const struct group *gr = getgrgid(st.st_uid);
|
||||
if(gr != NULL)
|
||||
{
|
||||
JSON_OBJ_COPY_STR(group, "name", gr->gr_name); // Group name
|
||||
}
|
||||
cJSON *owner = JSON_NEW_OBJ();
|
||||
JSON_OBJ_ADD_ITEM(owner, "user", user);
|
||||
JSON_OBJ_ADD_ITEM(owner, "group", group);
|
||||
JSON_OBJ_ADD_ITEM(json, "owner", owner);
|
||||
|
||||
// Add number of queries in database
|
||||
const int queries_in_database = get_number_of_queries_in_DB();
|
||||
JSON_OBJ_ADD_NUMBER(json, "queries", queries_in_database);
|
||||
|
||||
// Add SQLite library version
|
||||
JSON_OBJ_REF_STR(json, "sqlite_version", get_sqlite3_version());
|
||||
|
||||
// Send reply to user
|
||||
JSON_SEND_OBJECT(json);
|
||||
}
|
||||
|
|
|
@ -60,6 +60,10 @@ int api_handler(struct mg_connection *conn, void *ignored)
|
|||
{
|
||||
ret = api_ftl_dnsmasq_log(conn);
|
||||
}
|
||||
else if(startsWith("/api/ftl/database", request->local_uri))
|
||||
{
|
||||
ret = api_ftl_database(conn);
|
||||
}
|
||||
/******************************** api/network ****************************/
|
||||
else if(startsWith("/api/network", request->local_uri))
|
||||
{
|
||||
|
@ -161,10 +165,6 @@ int api_handler(struct mg_connection *conn, void *ignored)
|
|||
{
|
||||
ret = api_settings_web(conn);
|
||||
}
|
||||
else if(startsWith("/api/settings/ftldb", request->local_uri))
|
||||
{
|
||||
ret = api_settings_ftldb(conn);
|
||||
}
|
||||
/******************************** not found ******************************/
|
||||
else
|
||||
{
|
||||
|
|
|
@ -38,6 +38,7 @@ int api_stats_database_upstreams(struct mg_connection *conn);
|
|||
// FTL methods
|
||||
int api_ftl_client(struct mg_connection *conn);
|
||||
int api_ftl_dnsmasq_log(struct mg_connection *conn);
|
||||
int api_ftl_database(struct mg_connection *conn);
|
||||
|
||||
// Network methods
|
||||
int api_network(struct mg_connection *conn);
|
||||
|
@ -59,6 +60,5 @@ int api_auth_salt(struct mg_connection *conn);
|
|||
|
||||
// Settings methods
|
||||
int api_settings_web(struct mg_connection *conn);
|
||||
int api_settings_ftldb(struct mg_connection *conn);
|
||||
|
||||
#endif // ROUTES_H
|
||||
|
|
|
@ -12,12 +12,6 @@
|
|||
#include "../webserver/http-common.h"
|
||||
#include "../webserver/json_macros.h"
|
||||
#include "routes.h"
|
||||
// get_FTL_db_filesize()
|
||||
#include "files.h"
|
||||
// get_sqlite3_version()
|
||||
#include "database/common.h"
|
||||
// get_number_of_queries_in_DB()
|
||||
#include "database/query-table.h"
|
||||
|
||||
int api_settings_web(struct mg_connection *conn)
|
||||
{
|
||||
|
@ -25,21 +19,4 @@ int api_settings_web(struct mg_connection *conn)
|
|||
JSON_OBJ_REF_STR(json, "layout", "boxed");
|
||||
JSON_OBJ_REF_STR(json, "language", "en");
|
||||
JSON_SEND_OBJECT(json);
|
||||
}
|
||||
|
||||
int api_settings_ftldb(struct mg_connection *conn)
|
||||
{
|
||||
// Verify requesting client is allowed to see this ressource
|
||||
if(check_client_auth(conn) < 0)
|
||||
{
|
||||
send_json_unauthorized(conn);
|
||||
}
|
||||
|
||||
cJSON *json = JSON_NEW_OBJ();
|
||||
const int db_filesize = get_FTL_db_filesize();
|
||||
JSON_OBJ_ADD_NUMBER(json, "filesize", db_filesize);
|
||||
const int queries_in_database = get_number_of_queries_in_DB();
|
||||
JSON_OBJ_ADD_NUMBER(json, "queries", queries_in_database);
|
||||
JSON_OBJ_REF_STR(json, "sqlite_version", get_sqlite3_version());
|
||||
JSON_SEND_OBJECT(json);
|
||||
}
|
43
src/files.c
43
src/files.c
|
@ -59,15 +59,33 @@ bool file_exists(const char *filename)
|
|||
return stat(filename, &st) == 0;
|
||||
}
|
||||
|
||||
bool get_database_stat(struct stat *st)
|
||||
{
|
||||
return stat(FTLfiles.FTL_db, st) != 0;
|
||||
}
|
||||
|
||||
unsigned long long get_FTL_db_filesize(void)
|
||||
{
|
||||
struct stat st;
|
||||
if(stat(FTLfiles.FTL_db, &st) != 0)
|
||||
{
|
||||
// stat() failed (maybe the DB file does not exist?)
|
||||
return 0;
|
||||
}
|
||||
return st.st_size;
|
||||
if(get_database_stat(&st))
|
||||
return st.st_size;
|
||||
return 0llu;
|
||||
}
|
||||
|
||||
void get_permission_string(char permissions[10], struct stat *st)
|
||||
{
|
||||
// Get human-readable format of permissions as known from ls
|
||||
snprintf(permissions, 10u,
|
||||
"%s%s%s%s%s%s%s%s%s",
|
||||
st->st_mode & S_IRUSR ? "r":"-",
|
||||
st->st_mode & S_IWUSR ? "w":"-",
|
||||
st->st_mode & S_IXUSR ? "x":"-",
|
||||
st->st_mode & S_IRGRP ? "r":"-",
|
||||
st->st_mode & S_IWGRP ? "w":"-",
|
||||
st->st_mode & S_IXGRP ? "x":"-",
|
||||
st->st_mode & S_IROTH ? "r":"-",
|
||||
st->st_mode & S_IWOTH ? "w":"-",
|
||||
st->st_mode & S_IXOTH ? "x":"-");
|
||||
}
|
||||
|
||||
void ls_dir(const char* path)
|
||||
|
@ -121,18 +139,7 @@ void ls_dir(const char* path)
|
|||
snprintf(group, sizeof(group), "%d", st.st_gid);
|
||||
|
||||
char permissions[10];
|
||||
// Get human-readable format of permissions as known from ls
|
||||
snprintf(permissions, sizeof(permissions),
|
||||
"%s%s%s%s%s%s%s%s%s",
|
||||
st.st_mode & S_IRUSR ? "r":"-",
|
||||
st.st_mode & S_IWUSR ? "w":"-",
|
||||
st.st_mode & S_IXUSR ? "x":"-",
|
||||
st.st_mode & S_IRGRP ? "r":"-",
|
||||
st.st_mode & S_IWGRP ? "w":"-",
|
||||
st.st_mode & S_IXGRP ? "x":"-",
|
||||
st.st_mode & S_IROTH ? "r":"-",
|
||||
st.st_mode & S_IWOTH ? "w":"-",
|
||||
st.st_mode & S_IXOTH ? "x":"-");
|
||||
get_permission_string(permissions, &st);
|
||||
|
||||
char prefix[2] = " ";
|
||||
double formated = 0.0;
|
||||
|
|
|
@ -12,7 +12,9 @@
|
|||
|
||||
bool chmod_file(const char *filename, const mode_t mode);
|
||||
bool file_exists(const char *filename);
|
||||
bool get_database_stat(struct stat *st);
|
||||
unsigned long long get_FTL_db_filesize(void);
|
||||
void get_permission_string(char permissions[10], struct stat *st);
|
||||
void ls_dir(const char* path);
|
||||
|
||||
#endif //FILE_H
|
||||
|
|
Loading…
Reference in New Issue