Add /api/dns/cacheinfo

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2019-12-05 13:56:14 +00:00
parent 091e656de8
commit 6871dbdd04
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
6 changed files with 51 additions and 8 deletions

View File

@ -11,6 +11,7 @@
#include "FTL.h"
// counters
#include "shmem.h"
#include "dns.h"
#include "http-common.h"
#include "routes.h"
#include "json_macros.h"
@ -287,3 +288,20 @@ int api_dns_somelist(struct mg_connection *conn, bool exact, bool whitelist)
return 0;
}
}
int api_dns_cacheinfo(struct mg_connection *conn)
{
// Verify requesting client is allowed to access this ressource
if(check_client_auth(conn) < 0)
{
return send_json_unauthorized(conn);
}
cacheinforecord cacheinfo;
getCacheInformation(&cacheinfo);
cJSON *json = JSON_NEW_OBJ();
JSON_OBJ_ADD_NUMBER(json, "cache-size", cacheinfo.cache_size);
JSON_OBJ_ADD_NUMBER(json, "cache-live-freed", cacheinfo.cache_live_freed);
JSON_OBJ_ADD_NUMBER(json, "cache-inserted", cacheinfo.cache_inserted);
JSON_SEND_OBJECT(json);
}

22
src/api/dns.h Normal file
View File

@ -0,0 +1,22 @@
/* Pi-hole: A black hole for Internet advertisements
* (c) 2019 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* API DNS prototypes
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#ifndef API_DNS_H
#define API_DNS_H
typedef struct cacheinforecord {
int cache_size;
int cache_live_freed;
int cache_inserted;
} cacheinforecord;
// defined in src/dnsmasq_interface.c
extern void getCacheInformation(cacheinforecord *cacheinfo);
#endif // API_DNS_H

View File

@ -45,6 +45,10 @@ int api_handler(struct mg_connection *conn, void *ignored)
{
ret = api_dns_somelist(conn, false, false);
}
else if(startsWith("/api/dns/cacheinfo", request->local_uri))
{
ret = api_dns_cacheinfo(conn);
}
/******************************** api/ftl ****************************/
else if(startsWith("/api/ftl/clientIP", request->local_uri))
{

View File

@ -35,6 +35,7 @@ int api_ftl_network(struct mg_connection *conn);
// DNS methods
int api_dns_status(struct mg_connection *conn);
int api_dns_somelist(struct mg_connection *conn, bool exact, bool whitelist);
int api_dns_cacheinfo(struct mg_connection *conn);
// Version method
int api_version(struct mg_connection *conn);

View File

@ -1689,16 +1689,14 @@ void FTL_fork_and_bind_sockets(struct passwd *ent_pw)
}
// int cache_inserted, cache_live_freed are defined in dnsmasq/cache.c
int *getCacheInformation(void)
void getCacheInformation(cacheinforecord *cacheinfo)
{
// Allocate memory
int *cacheinfo = calloc(3,sizeof(int));
// cache-size - interpretation is obvious
cacheinfo[0] = daemon->cachesize;
cacheinfo->cache_size = daemon->cachesize;
// cache-live-freed - interpretation see below
cacheinfo[1] = daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED];
cacheinfo->cache_live_freed = daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED];
// cache-inserted - interpretation see below
cacheinfo[2] = daemon->metrics[METRIC_DNS_CACHE_INSERTED];
cacheinfo->cache_inserted = daemon->metrics[METRIC_DNS_CACHE_INSERTED];
// cache-live-freed and cache-inserted:
// It means the resolver handled <cache-inserted> names lookups that
// needed to be sent to upstream servers and that <cache-live-freed>
@ -1708,7 +1706,6 @@ int *getCacheInformation(void)
// cached. If the cache is full with entries which haven't reached
// the end of their time-to-live, then the entry which hasn't been
// looked up for the longest time is evicted.
return cacheinfo;
}
void _FTL_forwarding_failed(const struct server *server, const char* file, const int line)

View File

@ -11,6 +11,8 @@
#define DNSMASQ_INTERFACE_H
#include <stdbool.h>
// cacheinforecord
#include "api/dns.h"
extern unsigned char* pihole_privacylevel;
enum { TCP, UDP };
@ -46,7 +48,6 @@ void _FTL_get_blocking_metadata(union all_addr **addrp, unsigned int *flags, con
#define FTL_CNAME(domain, cpp, id) _FTL_CNAME(domain, cpp, id, __FILE__, __LINE__)
bool _FTL_CNAME(const char *domain, const struct crec *cpp, const int id, const char* file, const int line);
int *getCacheInformation(void);
void FTL_dnsmasq_reload(void);
void FTL_fork_and_bind_sockets(struct passwd *ent_pw);