From a9b6db56f5091b7cab65d525725dbc6f6edfbce9 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Thu, 5 Nov 2015 17:13:52 +0100 Subject: [PATCH] teach powerdns to measure number of open file descriptors + dnsdist to report that as a metric --- pdns/dnsdist.hh | 3 ++- pdns/misc.cc | 24 ++++++++++++++++++++++++ pdns/misc.hh | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index c1e743745..a5c598aac 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -45,7 +45,8 @@ struct DNSDistStats {"latency-avg100", &latencyAvg100}, {"latency-avg1000", &latencyAvg1000}, {"latency-avg10000", &latencyAvg10000}, {"latency-avg1000000", &latencyAvg1000000}, {"uptime", uptimeOfProcess}, - {"real-memory-usage", getRealMemoryUsage} + {"real-memory-usage", getRealMemoryUsage}, + {"fd-usage", getOpenFileDescriptors} }; }; diff --git a/pdns/misc.cc b/pdns/misc.cc index ea4b3f6cd..bf5dae296 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -40,6 +40,8 @@ #include #include #include +#include +#include #include #include #include @@ -1114,6 +1116,28 @@ DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum) throw PDNSException("getTSIGAlgoName does not understand given algorithm, please fix!"); } +uint64_t getOpenFileDescriptors(const std::string&) +{ +#ifdef __linux__ + DIR* dirhdl=opendir(("/proc/"+std::to_string(getpid())+"/fd/").c_str()); + if(!dirhdl) + return 0; + + struct dirent *entry; + int ret=0; + while((entry = readdir(dirhdl))) { + uint32_t num = atoi(entry->d_name); + if(std::to_string(num) == entry->d_name) + ret++; + } + closedir(dirhdl); + return ret; + +#else + return 0; +#endif +} + uint64_t getRealMemoryUsage(const std::string&) { #ifdef __linux__ diff --git a/pdns/misc.hh b/pdns/misc.hh index b72016a0e..2141cd143 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -623,6 +623,7 @@ bool setCloseOnExec(int sock); uint64_t udpErrorStats(const std::string& str); uint64_t getRealMemoryUsage(const std::string&); +uint64_t getOpenFileDescriptors(const std::string&); template std::unique_ptr make_unique(Args&&... args) -- 2.40.0