StatsManager.cs 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using DnsServerCore.ApplicationCommon;
  16. using System;
  17. using System.Collections.Concurrent;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Net;
  22. using System.Net.Sockets;
  23. using System.Text;
  24. using System.Threading;
  25. using TechnitiumLibrary.IO;
  26. using TechnitiumLibrary.Net;
  27. using TechnitiumLibrary.Net.Dns;
  28. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  29. namespace DnsServerCore.Dns
  30. {
  31. public enum TopStatsType
  32. {
  33. Unknown = 0,
  34. TopClients = 1,
  35. TopDomains = 2,
  36. TopBlockedDomains = 3
  37. }
  38. public sealed class StatsManager : IDisposable
  39. {
  40. #region variables
  41. const int DAILY_STATS_FILE_TOP_LIMIT = 1000;
  42. readonly DnsServer _dnsServer;
  43. readonly string _statsFolder;
  44. readonly StatCounter[] _lastHourStatCounters = new StatCounter[60];
  45. readonly StatCounter[] _lastHourStatCountersCopy = new StatCounter[60];
  46. readonly ConcurrentDictionary<DateTime, HourlyStats> _hourlyStatsCache = new ConcurrentDictionary<DateTime, HourlyStats>();
  47. readonly ConcurrentDictionary<DateTime, StatCounter> _dailyStatsCache = new ConcurrentDictionary<DateTime, StatCounter>();
  48. readonly Timer _maintenanceTimer;
  49. const int MAINTENANCE_TIMER_INITIAL_INTERVAL = 10000;
  50. const int MAINTENANCE_TIMER_PERIODIC_INTERVAL = 10000;
  51. readonly BlockingCollection<StatsQueueItem> _queue = new BlockingCollection<StatsQueueItem>();
  52. readonly Thread _consumerThread;
  53. readonly Timer _statsCleanupTimer;
  54. int _maxStatFileDays = 0;
  55. const int STATS_CLEANUP_TIMER_INITIAL_INTERVAL = 60 * 1000;
  56. const int STATS_CLEANUP_TIMER_PERIODIC_INTERVAL = 60 * 60 * 1000;
  57. #endregion
  58. #region constructor
  59. public StatsManager(DnsServer dnsServer)
  60. {
  61. _dnsServer = dnsServer;
  62. _statsFolder = Path.Combine(dnsServer.ConfigFolder, "stats");
  63. if (!Directory.Exists(_statsFolder))
  64. Directory.CreateDirectory(_statsFolder);
  65. //load stats
  66. LoadLastHourStats();
  67. //do first maintenance
  68. DoMaintenance();
  69. //start periodic maintenance timer
  70. _maintenanceTimer = new Timer(delegate (object state)
  71. {
  72. try
  73. {
  74. DoMaintenance();
  75. }
  76. catch (Exception ex)
  77. {
  78. LogManager log = dnsServer.LogManager;
  79. if (log is not null)
  80. log.Write(ex);
  81. }
  82. }, null, MAINTENANCE_TIMER_INITIAL_INTERVAL, MAINTENANCE_TIMER_PERIODIC_INTERVAL);
  83. //stats consumer thread
  84. _consumerThread = new Thread(delegate ()
  85. {
  86. try
  87. {
  88. foreach (StatsQueueItem item in _queue.GetConsumingEnumerable())
  89. {
  90. StatCounter statCounter = _lastHourStatCounters[item._timestamp.Minute];
  91. if (statCounter is not null)
  92. {
  93. DnsQuestionRecord query;
  94. if (item._request.Question.Count > 0)
  95. query = item._request.Question[0];
  96. else
  97. query = null;
  98. DnsServerResponseType responseType;
  99. if (item._response.Tag is null)
  100. responseType = DnsServerResponseType.Recursive;
  101. else
  102. responseType = (DnsServerResponseType)item._response.Tag;
  103. statCounter.Update(query, item._response.RCODE, responseType, item._remoteEP.Address);
  104. }
  105. foreach (IDnsQueryLogger logger in _dnsServer.DnsApplicationManager.DnsQueryLoggers)
  106. {
  107. try
  108. {
  109. _ = logger.InsertLogAsync(item._timestamp, item._request, item._remoteEP, item._protocol, item._response);
  110. }
  111. catch (Exception ex)
  112. {
  113. LogManager log = dnsServer.LogManager;
  114. if (log is not null)
  115. log.Write(ex);
  116. }
  117. }
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. LogManager log = dnsServer.LogManager;
  123. if (log is not null)
  124. log.Write(ex);
  125. }
  126. });
  127. _consumerThread.Name = "Stats";
  128. _consumerThread.IsBackground = true;
  129. _consumerThread.Start();
  130. _statsCleanupTimer = new Timer(delegate (object state)
  131. {
  132. try
  133. {
  134. if (_maxStatFileDays < 1)
  135. return;
  136. DateTime cutoffDate = DateTime.UtcNow.AddDays(_maxStatFileDays * -1).Date;
  137. LogManager log = dnsServer.LogManager;
  138. //delete hourly logs
  139. {
  140. string[] hourlyStatsFiles = Directory.GetFiles(Path.Combine(_dnsServer.ConfigFolder, "stats"), "*.stat");
  141. foreach (string hourlyStatsFile in hourlyStatsFiles)
  142. {
  143. string hourlyStatsFileName = Path.GetFileNameWithoutExtension(hourlyStatsFile);
  144. if (!DateTime.TryParseExact(hourlyStatsFileName, "yyyyMMddHH", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out DateTime hourlyStatsFileDate))
  145. continue;
  146. if (hourlyStatsFileDate < cutoffDate)
  147. {
  148. try
  149. {
  150. File.Delete(hourlyStatsFile);
  151. if (log != null)
  152. log.Write("StatsManager cleanup deleted the hourly stats file: " + hourlyStatsFile);
  153. }
  154. catch (Exception ex)
  155. {
  156. if (log != null)
  157. log.Write(ex);
  158. }
  159. }
  160. }
  161. }
  162. //delete daily logs
  163. {
  164. string[] dailyStatsFiles = Directory.GetFiles(Path.Combine(_dnsServer.ConfigFolder, "stats"), "*.dstat");
  165. foreach (string dailyStatsFile in dailyStatsFiles)
  166. {
  167. string dailyStatsFileName = Path.GetFileNameWithoutExtension(dailyStatsFile);
  168. if (!DateTime.TryParseExact(dailyStatsFileName, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out DateTime dailyStatsFileDate))
  169. continue;
  170. if (dailyStatsFileDate < cutoffDate)
  171. {
  172. try
  173. {
  174. File.Delete(dailyStatsFile);
  175. if (log != null)
  176. log.Write("StatsManager cleanup deleted the daily stats file: " + dailyStatsFile);
  177. }
  178. catch (Exception ex)
  179. {
  180. if (log != null)
  181. log.Write(ex);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. LogManager log = dnsServer.LogManager;
  190. if (log is not null)
  191. log.Write(ex);
  192. }
  193. });
  194. _statsCleanupTimer.Change(STATS_CLEANUP_TIMER_INITIAL_INTERVAL, STATS_CLEANUP_TIMER_PERIODIC_INTERVAL);
  195. }
  196. #endregion
  197. #region IDisposable
  198. bool _disposed;
  199. readonly object _disposeLock = new object();
  200. private void Dispose(bool disposing)
  201. {
  202. lock (_disposeLock)
  203. {
  204. if (_disposed)
  205. return;
  206. if (disposing)
  207. {
  208. if (_maintenanceTimer != null)
  209. _maintenanceTimer.Dispose();
  210. //do last maintenance
  211. DoMaintenance();
  212. }
  213. _disposed = true;
  214. }
  215. }
  216. public void Dispose()
  217. {
  218. Dispose(true);
  219. }
  220. #endregion
  221. #region private
  222. private void LoadLastHourStats()
  223. {
  224. DateTime currentDateTime = DateTime.UtcNow;
  225. DateTime lastHourDateTime = currentDateTime.AddMinutes(-60);
  226. HourlyStats lastHourlyStats = null;
  227. DateTime lastHourlyStatsDateTime = new DateTime();
  228. for (int i = 0; i < 60; i++)
  229. {
  230. DateTime lastDateTime = lastHourDateTime.AddMinutes(i);
  231. if ((lastHourlyStats == null) || (lastDateTime.Hour != lastHourlyStatsDateTime.Hour))
  232. {
  233. lastHourlyStats = LoadHourlyStats(lastDateTime);
  234. lastHourlyStatsDateTime = lastDateTime;
  235. }
  236. _lastHourStatCounters[lastDateTime.Minute] = lastHourlyStats.MinuteStats[lastDateTime.Minute];
  237. _lastHourStatCountersCopy[lastDateTime.Minute] = _lastHourStatCounters[lastDateTime.Minute];
  238. }
  239. }
  240. private void DoMaintenance()
  241. {
  242. //load new stats counter 5 min ahead of current time
  243. DateTime currentDateTime = DateTime.UtcNow;
  244. for (int i = 0; i < 5; i++)
  245. {
  246. int minute = currentDateTime.AddMinutes(i).Minute;
  247. StatCounter statCounter = _lastHourStatCounters[minute];
  248. if ((statCounter == null) || statCounter.IsLocked)
  249. _lastHourStatCounters[minute] = new StatCounter();
  250. }
  251. //save data upto last 5 mins
  252. DateTime last5MinDateTime = currentDateTime.AddMinutes(-5);
  253. for (int i = 0; i < 5; i++)
  254. {
  255. DateTime lastDateTime = last5MinDateTime.AddMinutes(i);
  256. StatCounter lastStatCounter = _lastHourStatCounters[lastDateTime.Minute];
  257. if ((lastStatCounter != null) && !lastStatCounter.IsLocked)
  258. {
  259. //load hourly stats data
  260. HourlyStats hourlyStats = LoadHourlyStats(lastDateTime);
  261. //update hourly stats file
  262. lastStatCounter.Lock();
  263. hourlyStats.UpdateStat(lastDateTime, lastStatCounter);
  264. //save hourly stats
  265. SaveHourlyStats(lastDateTime, hourlyStats);
  266. //keep copy for api
  267. _lastHourStatCountersCopy[lastDateTime.Minute] = lastStatCounter;
  268. }
  269. }
  270. //load previous day stats to auto create daily stats file
  271. LoadDailyStats(currentDateTime.AddDays(-1));
  272. //remove old data from hourly stats cache
  273. {
  274. DateTime threshold = DateTime.UtcNow.AddHours(-24);
  275. threshold = new DateTime(threshold.Year, threshold.Month, threshold.Day, threshold.Hour, 0, 0, DateTimeKind.Utc);
  276. List<DateTime> _keysToRemove = new List<DateTime>();
  277. foreach (KeyValuePair<DateTime, HourlyStats> item in _hourlyStatsCache)
  278. {
  279. if (item.Key < threshold)
  280. _keysToRemove.Add(item.Key);
  281. }
  282. foreach (DateTime key in _keysToRemove)
  283. _hourlyStatsCache.TryRemove(key, out _);
  284. }
  285. //unload minute stats data from hourly stats cache for data older than last hour
  286. {
  287. DateTime lastHourThreshold = DateTime.UtcNow.AddHours(-1);
  288. lastHourThreshold = new DateTime(lastHourThreshold.Year, lastHourThreshold.Month, lastHourThreshold.Day, lastHourThreshold.Hour, 0, 0, DateTimeKind.Utc);
  289. foreach (KeyValuePair<DateTime, HourlyStats> item in _hourlyStatsCache)
  290. {
  291. if (item.Key < lastHourThreshold)
  292. item.Value.UnloadMinuteStats();
  293. }
  294. }
  295. //remove old data from daily stats cache
  296. {
  297. DateTime threshold = DateTime.UtcNow.AddMonths(-12);
  298. threshold = new DateTime(threshold.Year, threshold.Month, 1, 0, 0, 0, DateTimeKind.Utc);
  299. List<DateTime> _keysToRemove = new List<DateTime>();
  300. foreach (KeyValuePair<DateTime, StatCounter> item in _dailyStatsCache)
  301. {
  302. if (item.Key < threshold)
  303. _keysToRemove.Add(item.Key);
  304. }
  305. foreach (DateTime key in _keysToRemove)
  306. _dailyStatsCache.TryRemove(key, out _);
  307. }
  308. }
  309. private HourlyStats LoadHourlyStats(DateTime dateTime)
  310. {
  311. DateTime hourlyDateTime = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, 0, 0, 0, DateTimeKind.Utc);
  312. if (!_hourlyStatsCache.TryGetValue(hourlyDateTime, out HourlyStats hourlyStats))
  313. {
  314. string hourlyStatsFile = Path.Combine(_statsFolder, dateTime.ToString("yyyyMMddHH") + ".stat");
  315. if (File.Exists(hourlyStatsFile))
  316. {
  317. try
  318. {
  319. using (FileStream fS = new FileStream(hourlyStatsFile, FileMode.Open, FileAccess.Read))
  320. {
  321. hourlyStats = new HourlyStats(new BinaryReader(fS));
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. LogManager log = _dnsServer.LogManager;
  327. if (log != null)
  328. log.Write(ex);
  329. hourlyStats = new HourlyStats();
  330. }
  331. }
  332. else
  333. {
  334. hourlyStats = new HourlyStats();
  335. }
  336. if (!_hourlyStatsCache.TryAdd(hourlyDateTime, hourlyStats))
  337. {
  338. if (!_hourlyStatsCache.TryGetValue(hourlyDateTime, out hourlyStats))
  339. throw new DnsServerException("Unable to load hourly stats.");
  340. }
  341. }
  342. return hourlyStats;
  343. }
  344. private StatCounter LoadDailyStats(DateTime dateTime)
  345. {
  346. DateTime dailyDateTime = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 0, 0, 0, DateTimeKind.Utc);
  347. if (!_dailyStatsCache.TryGetValue(dailyDateTime, out StatCounter dailyStats))
  348. {
  349. string dailyStatsFile = Path.Combine(_statsFolder, dateTime.ToString("yyyyMMdd") + ".dstat");
  350. if (File.Exists(dailyStatsFile))
  351. {
  352. try
  353. {
  354. using (FileStream fS = new FileStream(dailyStatsFile, FileMode.Open, FileAccess.Read))
  355. {
  356. dailyStats = new StatCounter(new BinaryReader(fS));
  357. }
  358. //check if existing file could be truncated to avoid loading unnecessary data in memory
  359. if (dailyStats.Truncate(DAILY_STATS_FILE_TOP_LIMIT))
  360. SaveDailyStats(dailyDateTime, dailyStats); //save truncated file
  361. }
  362. catch (Exception ex)
  363. {
  364. LogManager log = _dnsServer.LogManager;
  365. if (log != null)
  366. log.Write(ex);
  367. }
  368. }
  369. if (dailyStats == null)
  370. {
  371. dailyStats = new StatCounter();
  372. dailyStats.Lock();
  373. for (int hour = 0; hour < 24; hour++) //hours
  374. {
  375. HourlyStats hourlyStats = LoadHourlyStats(dailyDateTime.AddHours(hour));
  376. dailyStats.Merge(hourlyStats.HourStat);
  377. }
  378. if (dailyStats.TotalQueries > 0)
  379. {
  380. _ = dailyStats.Truncate(DAILY_STATS_FILE_TOP_LIMIT);
  381. SaveDailyStats(dailyDateTime, dailyStats);
  382. }
  383. }
  384. if (!_dailyStatsCache.TryAdd(dailyDateTime, dailyStats))
  385. {
  386. if (!_dailyStatsCache.TryGetValue(dailyDateTime, out dailyStats))
  387. throw new DnsServerException("Unable to load daily stats.");
  388. }
  389. }
  390. return dailyStats;
  391. }
  392. private void SaveHourlyStats(DateTime dateTime, HourlyStats hourlyStats)
  393. {
  394. string hourlyStatsFile = Path.Combine(_statsFolder, dateTime.ToString("yyyyMMddHH") + ".stat");
  395. try
  396. {
  397. using (FileStream fS = new FileStream(hourlyStatsFile, FileMode.Create, FileAccess.Write))
  398. {
  399. hourlyStats.WriteTo(new BinaryWriter(fS));
  400. }
  401. }
  402. catch (Exception ex)
  403. {
  404. LogManager log = _dnsServer.LogManager;
  405. if (log != null)
  406. log.Write(ex);
  407. }
  408. }
  409. private void SaveDailyStats(DateTime dateTime, StatCounter dailyStats)
  410. {
  411. string dailyStatsFile = Path.Combine(_statsFolder, dateTime.ToString("yyyyMMdd") + ".dstat");
  412. try
  413. {
  414. using (FileStream fS = new FileStream(dailyStatsFile, FileMode.Create, FileAccess.Write))
  415. {
  416. dailyStats.WriteTo(new BinaryWriter(fS));
  417. }
  418. }
  419. catch (Exception ex)
  420. {
  421. LogManager log = _dnsServer.LogManager;
  422. if (log != null)
  423. log.Write(ex);
  424. }
  425. }
  426. private void Flush()
  427. {
  428. //clear in memory stats
  429. for (int i = 0; i < _lastHourStatCountersCopy.Length; i++)
  430. _lastHourStatCountersCopy[i] = null;
  431. _hourlyStatsCache.Clear();
  432. _dailyStatsCache.Clear();
  433. }
  434. #endregion
  435. #region public
  436. public void ReloadStats()
  437. {
  438. Flush();
  439. LoadLastHourStats();
  440. }
  441. public void DeleteAllStats()
  442. {
  443. foreach (string hourlyStatsFile in Directory.GetFiles(Path.Combine(_dnsServer.ConfigFolder, "stats"), "*.stat", SearchOption.TopDirectoryOnly))
  444. {
  445. File.Delete(hourlyStatsFile);
  446. }
  447. foreach (string dailyStatsFile in Directory.GetFiles(Path.Combine(_dnsServer.ConfigFolder, "stats"), "*.dstat", SearchOption.TopDirectoryOnly))
  448. {
  449. File.Delete(dailyStatsFile);
  450. }
  451. Flush();
  452. }
  453. public void QueueUpdate(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
  454. {
  455. _queue.Add(new StatsQueueItem(request, remoteEP, protocol, response));
  456. }
  457. public Dictionary<string, List<KeyValuePair<string, long>>> GetLastHourMinuteWiseStats(bool utcFormat)
  458. {
  459. StatCounter totalStatCounter = new StatCounter();
  460. totalStatCounter.Lock();
  461. List<KeyValuePair<string, long>> totalQueriesPerInterval = new List<KeyValuePair<string, long>>(60);
  462. List<KeyValuePair<string, long>> totalNoErrorPerInterval = new List<KeyValuePair<string, long>>(60);
  463. List<KeyValuePair<string, long>> totalServerFailurePerInterval = new List<KeyValuePair<string, long>>(60);
  464. List<KeyValuePair<string, long>> totalNxDomainPerInterval = new List<KeyValuePair<string, long>>(60);
  465. List<KeyValuePair<string, long>> totalRefusedPerInterval = new List<KeyValuePair<string, long>>(60);
  466. List<KeyValuePair<string, long>> totalAuthHitPerInterval = new List<KeyValuePair<string, long>>(60);
  467. List<KeyValuePair<string, long>> totalRecursionsPerInterval = new List<KeyValuePair<string, long>>(60);
  468. List<KeyValuePair<string, long>> totalCacheHitPerInterval = new List<KeyValuePair<string, long>>(60);
  469. List<KeyValuePair<string, long>> totalBlockedPerInterval = new List<KeyValuePair<string, long>>(60);
  470. List<KeyValuePair<string, long>> totalClientsPerInterval = new List<KeyValuePair<string, long>>(60);
  471. DateTime lastHourDateTime = DateTime.UtcNow.AddMinutes(-60);
  472. lastHourDateTime = new DateTime(lastHourDateTime.Year, lastHourDateTime.Month, lastHourDateTime.Day, lastHourDateTime.Hour, lastHourDateTime.Minute, 0, DateTimeKind.Utc);
  473. for (int minute = 0; minute < 60; minute++)
  474. {
  475. DateTime lastDateTime = lastHourDateTime.AddMinutes(minute);
  476. string label;
  477. if (utcFormat)
  478. label = lastDateTime.AddMinutes(1).ToString("O");
  479. else
  480. label = lastDateTime.AddMinutes(1).ToLocalTime().ToString("HH:mm");
  481. StatCounter statCounter = _lastHourStatCountersCopy[lastDateTime.Minute];
  482. if ((statCounter != null) && statCounter.IsLocked)
  483. {
  484. totalStatCounter.Merge(statCounter);
  485. totalQueriesPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalQueries));
  486. totalNoErrorPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalNoError));
  487. totalServerFailurePerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalServerFailure));
  488. totalNxDomainPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalNxDomain));
  489. totalRefusedPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalRefused));
  490. totalAuthHitPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalAuthoritative));
  491. totalRecursionsPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalRecursive));
  492. totalCacheHitPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalCached));
  493. totalBlockedPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalBlocked));
  494. totalClientsPerInterval.Add(new KeyValuePair<string, long>(label, statCounter.TotalClients));
  495. }
  496. else
  497. {
  498. totalQueriesPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  499. totalNoErrorPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  500. totalServerFailurePerInterval.Add(new KeyValuePair<string, long>(label, 0));
  501. totalNxDomainPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  502. totalRefusedPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  503. totalAuthHitPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  504. totalRecursionsPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  505. totalCacheHitPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  506. totalBlockedPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  507. totalClientsPerInterval.Add(new KeyValuePair<string, long>(label, 0));
  508. }
  509. }
  510. Dictionary<string, List<KeyValuePair<string, long>>> data = new Dictionary<string, List<KeyValuePair<string, long>>>();
  511. {
  512. List<KeyValuePair<string, long>> stats = new List<KeyValuePair<string, long>>(10);
  513. stats.Add(new KeyValuePair<string, long>("totalQueries", totalStatCounter.TotalQueries));
  514. stats.Add(new KeyValuePair<string, long>("totalNoError", totalStatCounter.TotalNoError));
  515. stats.Add(new KeyValuePair<string, long>("totalServerFailure", totalStatCounter.TotalServerFailure));
  516. stats.Add(new KeyValuePair<string, long>("totalNxDomain", totalStatCounter.TotalNxDomain));
  517. stats.Add(new KeyValuePair<string, long>("totalRefused", totalStatCounter.TotalRefused));
  518. stats.Add(new KeyValuePair<string, long>("totalAuthoritative", totalStatCounter.TotalAuthoritative));
  519. stats.Add(new KeyValuePair<string, long>("totalRecursive", totalStatCounter.TotalRecursive));
  520. stats.Add(new KeyValuePair<string, long>("totalCached", totalStatCounter.TotalCached));
  521. stats.Add(new KeyValuePair<string, long>("totalBlocked", totalStatCounter.TotalBlocked));
  522. stats.Add(new KeyValuePair<string, long>("totalClients", totalStatCounter.TotalClients));
  523. data.Add("stats", stats);
  524. }
  525. data.Add("totalQueriesPerInterval", totalQueriesPerInterval);
  526. data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval);
  527. data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval);
  528. data.Add("totalNxDomainPerInterval", totalNxDomainPerInterval);
  529. data.Add("totalRefusedPerInterval", totalRefusedPerInterval);
  530. data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval);
  531. data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval);
  532. data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval);
  533. data.Add("totalBlockedPerInterval", totalBlockedPerInterval);
  534. data.Add("totalClientsPerInterval", totalClientsPerInterval);
  535. data.Add("topDomains", totalStatCounter.GetTopDomains(10));
  536. data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10));
  537. data.Add("topClients", totalStatCounter.GetTopClients(10));
  538. data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(10));
  539. return data;
  540. }
  541. public Dictionary<string, List<KeyValuePair<string, long>>> GetLastDayHourWiseStats(bool utcFormat)
  542. {
  543. return GetHourWiseStats(DateTime.UtcNow.AddHours(-24), 24, utcFormat);
  544. }
  545. public Dictionary<string, List<KeyValuePair<string, long>>> GetLastWeekDayWiseStats(bool utcFormat)
  546. {
  547. return GetDayWiseStats(DateTime.UtcNow.AddDays(-7).Date, 7, utcFormat);
  548. }
  549. public Dictionary<string, List<KeyValuePair<string, long>>> GetLastMonthDayWiseStats(bool utcFormat)
  550. {
  551. return GetDayWiseStats(DateTime.UtcNow.AddDays(-31).Date, 31, utcFormat);
  552. }
  553. public Dictionary<string, List<KeyValuePair<string, long>>> GetLastYearMonthWiseStats(bool utcFormat)
  554. {
  555. StatCounter totalStatCounter = new StatCounter();
  556. totalStatCounter.Lock();
  557. List<KeyValuePair<string, long>> totalQueriesPerInterval = new List<KeyValuePair<string, long>>();
  558. List<KeyValuePair<string, long>> totalNoErrorPerInterval = new List<KeyValuePair<string, long>>();
  559. List<KeyValuePair<string, long>> totalServerFailurePerInterval = new List<KeyValuePair<string, long>>();
  560. List<KeyValuePair<string, long>> totalNxDomainPerInterval = new List<KeyValuePair<string, long>>();
  561. List<KeyValuePair<string, long>> totalRefusedPerInterval = new List<KeyValuePair<string, long>>();
  562. List<KeyValuePair<string, long>> totalAuthHitPerInterval = new List<KeyValuePair<string, long>>();
  563. List<KeyValuePair<string, long>> totalRecursionsPerInterval = new List<KeyValuePair<string, long>>();
  564. List<KeyValuePair<string, long>> totalCacheHitPerInterval = new List<KeyValuePair<string, long>>();
  565. List<KeyValuePair<string, long>> totalBlockedPerInterval = new List<KeyValuePair<string, long>>();
  566. List<KeyValuePair<string, long>> totalClientsPerInterval = new List<KeyValuePair<string, long>>();
  567. DateTime lastYearDateTime = DateTime.UtcNow.AddMonths(-12);
  568. lastYearDateTime = new DateTime(lastYearDateTime.Year, lastYearDateTime.Month, 1, 0, 0, 0, DateTimeKind.Utc);
  569. for (int month = 0; month < 12; month++) //months
  570. {
  571. StatCounter monthlyStatCounter = new StatCounter();
  572. monthlyStatCounter.Lock();
  573. DateTime lastMonthDateTime = lastYearDateTime.AddMonths(month);
  574. string label;
  575. if (utcFormat)
  576. label = lastMonthDateTime.ToString("O");
  577. else
  578. label = lastMonthDateTime.ToLocalTime().ToString("MM/yyyy");
  579. int days = DateTime.DaysInMonth(lastMonthDateTime.Year, lastMonthDateTime.Month);
  580. for (int day = 0; day < days; day++) //days
  581. {
  582. StatCounter dailyStatCounter = LoadDailyStats(lastMonthDateTime.AddDays(day));
  583. monthlyStatCounter.Merge(dailyStatCounter, true);
  584. }
  585. totalStatCounter.Merge(monthlyStatCounter, true);
  586. totalQueriesPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalQueries));
  587. totalNoErrorPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalNoError));
  588. totalServerFailurePerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalServerFailure));
  589. totalNxDomainPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalNxDomain));
  590. totalRefusedPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalRefused));
  591. totalAuthHitPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalAuthoritative));
  592. totalRecursionsPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalRecursive));
  593. totalCacheHitPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalCached));
  594. totalBlockedPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalBlocked));
  595. totalClientsPerInterval.Add(new KeyValuePair<string, long>(label, monthlyStatCounter.TotalClients));
  596. }
  597. Dictionary<string, List<KeyValuePair<string, long>>> data = new Dictionary<string, List<KeyValuePair<string, long>>>();
  598. {
  599. List<KeyValuePair<string, long>> stats = new List<KeyValuePair<string, long>>(6);
  600. stats.Add(new KeyValuePair<string, long>("totalQueries", totalStatCounter.TotalQueries));
  601. stats.Add(new KeyValuePair<string, long>("totalNoError", totalStatCounter.TotalNoError));
  602. stats.Add(new KeyValuePair<string, long>("totalServerFailure", totalStatCounter.TotalServerFailure));
  603. stats.Add(new KeyValuePair<string, long>("totalNxDomain", totalStatCounter.TotalNxDomain));
  604. stats.Add(new KeyValuePair<string, long>("totalRefused", totalStatCounter.TotalRefused));
  605. stats.Add(new KeyValuePair<string, long>("totalAuthoritative", totalStatCounter.TotalAuthoritative));
  606. stats.Add(new KeyValuePair<string, long>("totalRecursive", totalStatCounter.TotalRecursive));
  607. stats.Add(new KeyValuePair<string, long>("totalCached", totalStatCounter.TotalCached));
  608. stats.Add(new KeyValuePair<string, long>("totalBlocked", totalStatCounter.TotalBlocked));
  609. stats.Add(new KeyValuePair<string, long>("totalClients", totalStatCounter.TotalClients));
  610. data.Add("stats", stats);
  611. }
  612. data.Add("totalQueriesPerInterval", totalQueriesPerInterval);
  613. data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval);
  614. data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval);
  615. data.Add("totalNxDomainPerInterval", totalNxDomainPerInterval);
  616. data.Add("totalRefusedPerInterval", totalRefusedPerInterval);
  617. data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval);
  618. data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval);
  619. data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval);
  620. data.Add("totalBlockedPerInterval", totalBlockedPerInterval);
  621. data.Add("totalClientsPerInterval", totalClientsPerInterval);
  622. data.Add("topDomains", totalStatCounter.GetTopDomains(10));
  623. data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10));
  624. data.Add("topClients", totalStatCounter.GetTopClients(10));
  625. data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(10));
  626. return data;
  627. }
  628. public Dictionary<string, List<KeyValuePair<string, long>>> GetHourWiseStats(DateTime startDate, DateTime endDate, bool utcFormat)
  629. {
  630. int hours = Convert.ToInt32((endDate - startDate).TotalHours) + 1;
  631. if (hours < 24)
  632. hours = 24;
  633. return GetHourWiseStats(startDate, hours, utcFormat);
  634. }
  635. public Dictionary<string, List<KeyValuePair<string, long>>> GetHourWiseStats(DateTime startDate, int hours, bool utcFormat)
  636. {
  637. StatCounter totalStatCounter = new StatCounter();
  638. totalStatCounter.Lock();
  639. List<KeyValuePair<string, long>> totalQueriesPerInterval = new List<KeyValuePair<string, long>>();
  640. List<KeyValuePair<string, long>> totalNoErrorPerInterval = new List<KeyValuePair<string, long>>();
  641. List<KeyValuePair<string, long>> totalServerFailurePerInterval = new List<KeyValuePair<string, long>>();
  642. List<KeyValuePair<string, long>> totalNxDomainPerInterval = new List<KeyValuePair<string, long>>();
  643. List<KeyValuePair<string, long>> totalRefusedPerInterval = new List<KeyValuePair<string, long>>();
  644. List<KeyValuePair<string, long>> totalAuthHitPerInterval = new List<KeyValuePair<string, long>>();
  645. List<KeyValuePair<string, long>> totalRecursionsPerInterval = new List<KeyValuePair<string, long>>();
  646. List<KeyValuePair<string, long>> totalCacheHitPerInterval = new List<KeyValuePair<string, long>>();
  647. List<KeyValuePair<string, long>> totalBlockedPerInterval = new List<KeyValuePair<string, long>>();
  648. List<KeyValuePair<string, long>> totalClientsPerInterval = new List<KeyValuePair<string, long>>();
  649. for (int hour = 0; hour < hours; hour++)
  650. {
  651. DateTime lastDateTime = startDate.AddHours(hour);
  652. string label;
  653. if (utcFormat)
  654. label = lastDateTime.AddHours(1).ToString("O");
  655. else
  656. label = lastDateTime.AddHours(1).ToLocalTime().ToString("MM/dd HH") + ":00";
  657. HourlyStats hourlyStats = LoadHourlyStats(lastDateTime);
  658. StatCounter hourlyStatCounter = hourlyStats.HourStat;
  659. totalStatCounter.Merge(hourlyStatCounter);
  660. totalQueriesPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalQueries));
  661. totalNoErrorPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalNoError));
  662. totalServerFailurePerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalServerFailure));
  663. totalNxDomainPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalNxDomain));
  664. totalRefusedPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalRefused));
  665. totalAuthHitPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalAuthoritative));
  666. totalRecursionsPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalRecursive));
  667. totalCacheHitPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalCached));
  668. totalBlockedPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalBlocked));
  669. totalClientsPerInterval.Add(new KeyValuePair<string, long>(label, hourlyStatCounter.TotalClients));
  670. }
  671. Dictionary<string, List<KeyValuePair<string, long>>> data = new Dictionary<string, List<KeyValuePair<string, long>>>();
  672. {
  673. List<KeyValuePair<string, long>> stats = new List<KeyValuePair<string, long>>(6);
  674. stats.Add(new KeyValuePair<string, long>("totalQueries", totalStatCounter.TotalQueries));
  675. stats.Add(new KeyValuePair<string, long>("totalNoError", totalStatCounter.TotalNoError));
  676. stats.Add(new KeyValuePair<string, long>("totalServerFailure", totalStatCounter.TotalServerFailure));
  677. stats.Add(new KeyValuePair<string, long>("totalNxDomain", totalStatCounter.TotalNxDomain));
  678. stats.Add(new KeyValuePair<string, long>("totalRefused", totalStatCounter.TotalRefused));
  679. stats.Add(new KeyValuePair<string, long>("totalAuthoritative", totalStatCounter.TotalAuthoritative));
  680. stats.Add(new KeyValuePair<string, long>("totalRecursive", totalStatCounter.TotalRecursive));
  681. stats.Add(new KeyValuePair<string, long>("totalCached", totalStatCounter.TotalCached));
  682. stats.Add(new KeyValuePair<string, long>("totalBlocked", totalStatCounter.TotalBlocked));
  683. stats.Add(new KeyValuePair<string, long>("totalClients", totalStatCounter.TotalClients));
  684. data.Add("stats", stats);
  685. }
  686. data.Add("totalQueriesPerInterval", totalQueriesPerInterval);
  687. data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval);
  688. data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval);
  689. data.Add("totalNxDomainPerInterval", totalNxDomainPerInterval);
  690. data.Add("totalRefusedPerInterval", totalRefusedPerInterval);
  691. data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval);
  692. data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval);
  693. data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval);
  694. data.Add("totalBlockedPerInterval", totalBlockedPerInterval);
  695. data.Add("totalClientsPerInterval", totalClientsPerInterval);
  696. data.Add("topDomains", totalStatCounter.GetTopDomains(10));
  697. data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10));
  698. data.Add("topClients", totalStatCounter.GetTopClients(10));
  699. data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(10));
  700. return data;
  701. }
  702. public Dictionary<string, List<KeyValuePair<string, long>>> GetDayWiseStats(DateTime startDate, DateTime endDate, bool utcFormat)
  703. {
  704. return GetDayWiseStats(startDate, Convert.ToInt32((endDate - startDate).TotalDays) + 1, utcFormat);
  705. }
  706. public Dictionary<string, List<KeyValuePair<string, long>>> GetDayWiseStats(DateTime startDate, int days, bool utcFormat)
  707. {
  708. StatCounter totalStatCounter = new StatCounter();
  709. totalStatCounter.Lock();
  710. List<KeyValuePair<string, long>> totalQueriesPerInterval = new List<KeyValuePair<string, long>>();
  711. List<KeyValuePair<string, long>> totalNoErrorPerInterval = new List<KeyValuePair<string, long>>();
  712. List<KeyValuePair<string, long>> totalServerFailurePerInterval = new List<KeyValuePair<string, long>>();
  713. List<KeyValuePair<string, long>> totalNxDomainPerInterval = new List<KeyValuePair<string, long>>();
  714. List<KeyValuePair<string, long>> totalRefusedPerInterval = new List<KeyValuePair<string, long>>();
  715. List<KeyValuePair<string, long>> totalAuthHitPerInterval = new List<KeyValuePair<string, long>>();
  716. List<KeyValuePair<string, long>> totalRecursionsPerInterval = new List<KeyValuePair<string, long>>();
  717. List<KeyValuePair<string, long>> totalCacheHitPerInterval = new List<KeyValuePair<string, long>>();
  718. List<KeyValuePair<string, long>> totalBlockedPerInterval = new List<KeyValuePair<string, long>>();
  719. List<KeyValuePair<string, long>> totalClientsPerInterval = new List<KeyValuePair<string, long>>();
  720. for (int day = 0; day < days; day++) //days
  721. {
  722. DateTime lastDayDateTime = startDate.AddDays(day);
  723. string label;
  724. if (utcFormat)
  725. label = lastDayDateTime.ToString("O");
  726. else
  727. label = lastDayDateTime.ToLocalTime().ToString("MM/dd");
  728. StatCounter dailyStatCounter = LoadDailyStats(lastDayDateTime);
  729. totalStatCounter.Merge(dailyStatCounter, true);
  730. totalQueriesPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalQueries));
  731. totalNoErrorPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalNoError));
  732. totalServerFailurePerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalServerFailure));
  733. totalNxDomainPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalNxDomain));
  734. totalRefusedPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalRefused));
  735. totalAuthHitPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalAuthoritative));
  736. totalRecursionsPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalRecursive));
  737. totalCacheHitPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalCached));
  738. totalBlockedPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalBlocked));
  739. totalClientsPerInterval.Add(new KeyValuePair<string, long>(label, dailyStatCounter.TotalClients));
  740. }
  741. Dictionary<string, List<KeyValuePair<string, long>>> data = new Dictionary<string, List<KeyValuePair<string, long>>>();
  742. {
  743. List<KeyValuePair<string, long>> stats = new List<KeyValuePair<string, long>>(6);
  744. stats.Add(new KeyValuePair<string, long>("totalQueries", totalStatCounter.TotalQueries));
  745. stats.Add(new KeyValuePair<string, long>("totalNoError", totalStatCounter.TotalNoError));
  746. stats.Add(new KeyValuePair<string, long>("totalServerFailure", totalStatCounter.TotalServerFailure));
  747. stats.Add(new KeyValuePair<string, long>("totalNxDomain", totalStatCounter.TotalNxDomain));
  748. stats.Add(new KeyValuePair<string, long>("totalRefused", totalStatCounter.TotalRefused));
  749. stats.Add(new KeyValuePair<string, long>("totalAuthoritative", totalStatCounter.TotalAuthoritative));
  750. stats.Add(new KeyValuePair<string, long>("totalRecursive", totalStatCounter.TotalRecursive));
  751. stats.Add(new KeyValuePair<string, long>("totalCached", totalStatCounter.TotalCached));
  752. stats.Add(new KeyValuePair<string, long>("totalBlocked", totalStatCounter.TotalBlocked));
  753. stats.Add(new KeyValuePair<string, long>("totalClients", totalStatCounter.TotalClients));
  754. data.Add("stats", stats);
  755. }
  756. data.Add("totalQueriesPerInterval", totalQueriesPerInterval);
  757. data.Add("totalNoErrorPerInterval", totalNoErrorPerInterval);
  758. data.Add("totalServerFailurePerInterval", totalServerFailurePerInterval);
  759. data.Add("totalNxDomainPerInterval", totalNxDomainPerInterval);
  760. data.Add("totalRefusedPerInterval", totalRefusedPerInterval);
  761. data.Add("totalAuthHitPerInterval", totalAuthHitPerInterval);
  762. data.Add("totalRecursionsPerInterval", totalRecursionsPerInterval);
  763. data.Add("totalCacheHitPerInterval", totalCacheHitPerInterval);
  764. data.Add("totalBlockedPerInterval", totalBlockedPerInterval);
  765. data.Add("totalClientsPerInterval", totalClientsPerInterval);
  766. data.Add("topDomains", totalStatCounter.GetTopDomains(10));
  767. data.Add("topBlockedDomains", totalStatCounter.GetTopBlockedDomains(10));
  768. data.Add("topClients", totalStatCounter.GetTopClients(10));
  769. data.Add("queryTypes", totalStatCounter.GetTopQueryTypes(10));
  770. return data;
  771. }
  772. public List<KeyValuePair<string, long>> GetLastHourTopStats(TopStatsType type, int limit)
  773. {
  774. StatCounter totalStatCounter = new StatCounter();
  775. totalStatCounter.Lock();
  776. DateTime lastHourDateTime = DateTime.UtcNow.AddMinutes(-60);
  777. lastHourDateTime = new DateTime(lastHourDateTime.Year, lastHourDateTime.Month, lastHourDateTime.Day, lastHourDateTime.Hour, lastHourDateTime.Minute, 0, DateTimeKind.Utc);
  778. for (int minute = 0; minute < 60; minute++)
  779. {
  780. DateTime lastDateTime = lastHourDateTime.AddMinutes(minute);
  781. StatCounter statCounter = _lastHourStatCountersCopy[lastDateTime.Minute];
  782. if ((statCounter != null) && statCounter.IsLocked)
  783. totalStatCounter.Merge(statCounter);
  784. }
  785. switch (type)
  786. {
  787. case TopStatsType.TopDomains:
  788. return totalStatCounter.GetTopDomains(limit);
  789. case TopStatsType.TopBlockedDomains:
  790. return totalStatCounter.GetTopBlockedDomains(limit);
  791. case TopStatsType.TopClients:
  792. return totalStatCounter.GetTopClients(limit);
  793. default:
  794. throw new NotSupportedException();
  795. }
  796. }
  797. public List<KeyValuePair<string, long>> GetLastDayTopStats(TopStatsType type, int limit)
  798. {
  799. return GetHourWiseTopStats(DateTime.UtcNow.AddHours(-24), 24, type, limit);
  800. }
  801. public List<KeyValuePair<string, long>> GetLastWeekTopStats(TopStatsType type, int limit)
  802. {
  803. return GetDayWiseTopStats(DateTime.UtcNow.AddDays(-7).Date, 7, type, limit);
  804. }
  805. public List<KeyValuePair<string, long>> GetLastMonthTopStats(TopStatsType type, int limit)
  806. {
  807. return GetDayWiseTopStats(DateTime.UtcNow.AddDays(-31).Date, 31, type, limit);
  808. }
  809. public List<KeyValuePair<string, long>> GetLastYearTopStats(TopStatsType type, int limit)
  810. {
  811. StatCounter totalStatCounter = new StatCounter();
  812. totalStatCounter.Lock();
  813. DateTime lastYearDateTime = DateTime.UtcNow.AddMonths(-12);
  814. lastYearDateTime = new DateTime(lastYearDateTime.Year, lastYearDateTime.Month, 1, 0, 0, 0, DateTimeKind.Utc);
  815. for (int month = 0; month < 12; month++) //months
  816. {
  817. StatCounter monthlyStatCounter = new StatCounter();
  818. monthlyStatCounter.Lock();
  819. DateTime lastMonthDateTime = lastYearDateTime.AddMonths(month);
  820. int days = DateTime.DaysInMonth(lastMonthDateTime.Year, lastMonthDateTime.Month);
  821. for (int day = 0; day < days; day++) //days
  822. {
  823. StatCounter dailyStatCounter = LoadDailyStats(lastMonthDateTime.AddDays(day));
  824. monthlyStatCounter.Merge(dailyStatCounter, true);
  825. }
  826. totalStatCounter.Merge(monthlyStatCounter, true);
  827. }
  828. switch (type)
  829. {
  830. case TopStatsType.TopDomains:
  831. return totalStatCounter.GetTopDomains(limit);
  832. case TopStatsType.TopBlockedDomains:
  833. return totalStatCounter.GetTopBlockedDomains(limit);
  834. case TopStatsType.TopClients:
  835. return totalStatCounter.GetTopClients(limit);
  836. default:
  837. throw new NotSupportedException();
  838. }
  839. }
  840. public List<KeyValuePair<string, long>> GetHourWiseTopStats(DateTime startDate, DateTime endDate, TopStatsType type, int limit)
  841. {
  842. int hours = Convert.ToInt32((endDate - startDate).TotalHours) + 1;
  843. if (hours < 24)
  844. hours = 24;
  845. return GetHourWiseTopStats(startDate, hours, type, limit);
  846. }
  847. public List<KeyValuePair<string, long>> GetHourWiseTopStats(DateTime startDate, int hours, TopStatsType type, int limit)
  848. {
  849. StatCounter totalStatCounter = new StatCounter();
  850. totalStatCounter.Lock();
  851. for (int hour = 0; hour < hours; hour++)
  852. {
  853. DateTime lastDateTime = startDate.AddHours(hour);
  854. HourlyStats hourlyStats = LoadHourlyStats(lastDateTime);
  855. StatCounter hourlyStatCounter = hourlyStats.HourStat;
  856. totalStatCounter.Merge(hourlyStatCounter);
  857. }
  858. switch (type)
  859. {
  860. case TopStatsType.TopDomains:
  861. return totalStatCounter.GetTopDomains(limit);
  862. case TopStatsType.TopBlockedDomains:
  863. return totalStatCounter.GetTopBlockedDomains(limit);
  864. case TopStatsType.TopClients:
  865. return totalStatCounter.GetTopClients(limit);
  866. default:
  867. throw new NotSupportedException();
  868. }
  869. }
  870. public List<KeyValuePair<string, long>> GetDayWiseTopStats(DateTime startDate, DateTime endDate, TopStatsType type, int limit)
  871. {
  872. return GetDayWiseTopStats(startDate, Convert.ToInt32((endDate - startDate).TotalDays) + 1, type, limit);
  873. }
  874. public List<KeyValuePair<string, long>> GetDayWiseTopStats(DateTime startDate, int days, TopStatsType type, int limit)
  875. {
  876. StatCounter totalStatCounter = new StatCounter();
  877. totalStatCounter.Lock();
  878. for (int day = 0; day < days; day++) //days
  879. {
  880. DateTime lastDayDateTime = startDate.AddDays(day);
  881. StatCounter dailyStatCounter = LoadDailyStats(lastDayDateTime);
  882. totalStatCounter.Merge(dailyStatCounter, true);
  883. }
  884. switch (type)
  885. {
  886. case TopStatsType.TopDomains:
  887. return totalStatCounter.GetTopDomains(limit);
  888. case TopStatsType.TopBlockedDomains:
  889. return totalStatCounter.GetTopBlockedDomains(limit);
  890. case TopStatsType.TopClients:
  891. return totalStatCounter.GetTopClients(limit);
  892. default:
  893. throw new NotSupportedException();
  894. }
  895. }
  896. public List<KeyValuePair<DnsQuestionRecord, long>> GetLastHourEligibleQueries(int minimumHitsPerHour)
  897. {
  898. StatCounter totalStatCounter = new StatCounter();
  899. totalStatCounter.Lock();
  900. DateTime lastHourDateTime = DateTime.UtcNow.AddMinutes(-60);
  901. lastHourDateTime = new DateTime(lastHourDateTime.Year, lastHourDateTime.Month, lastHourDateTime.Day, lastHourDateTime.Hour, lastHourDateTime.Minute, 0, DateTimeKind.Utc);
  902. for (int minute = 0; minute < 60; minute++)
  903. {
  904. DateTime lastDateTime = lastHourDateTime.AddMinutes(minute);
  905. StatCounter statCounter = _lastHourStatCountersCopy[lastDateTime.Minute];
  906. if ((statCounter != null) && statCounter.IsLocked)
  907. totalStatCounter.Merge(statCounter);
  908. }
  909. return totalStatCounter.GetEligibleQueries(minimumHitsPerHour);
  910. }
  911. public void GetLatestClientSubnetStats(int minutes, int ipv4PrefixLength, int ipv6PrefixLength, out IReadOnlyDictionary<IPAddress, long> clientSubnetStats, out IReadOnlyDictionary<IPAddress, long> errorClientSubnetStats)
  912. {
  913. StatCounter totalStatCounter = new StatCounter();
  914. totalStatCounter.Lock();
  915. DateTime lastHourDateTime = DateTime.UtcNow.AddMinutes(1 - minutes);
  916. lastHourDateTime = new DateTime(lastHourDateTime.Year, lastHourDateTime.Month, lastHourDateTime.Day, lastHourDateTime.Hour, lastHourDateTime.Minute, 0, DateTimeKind.Utc);
  917. for (int minute = 0; minute < minutes; minute++)
  918. {
  919. DateTime lastDateTime = lastHourDateTime.AddMinutes(minute);
  920. StatCounter statCounter = _lastHourStatCounters[lastDateTime.Minute];
  921. if (statCounter is not null)
  922. totalStatCounter.Merge(statCounter, false, true);
  923. }
  924. clientSubnetStats = totalStatCounter.GetClientSubnetStats(ipv4PrefixLength, ipv6PrefixLength);
  925. errorClientSubnetStats = totalStatCounter.GetErrorClientSubnetStats(ipv4PrefixLength, ipv6PrefixLength);
  926. }
  927. #endregion
  928. #region properties
  929. public int MaxStatFileDays
  930. {
  931. get { return _maxStatFileDays; }
  932. set { _maxStatFileDays = value; }
  933. }
  934. #endregion
  935. class HourlyStats
  936. {
  937. #region variables
  938. readonly StatCounter _hourStat; //calculated value
  939. StatCounter[] _minuteStats = new StatCounter[60];
  940. #endregion
  941. #region constructor
  942. public HourlyStats()
  943. {
  944. _hourStat = new StatCounter();
  945. _hourStat.Lock();
  946. }
  947. public HourlyStats(BinaryReader bR)
  948. {
  949. if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "HS") //format
  950. throw new InvalidDataException("HourlyStats format is invalid.");
  951. byte version = bR.ReadByte();
  952. switch (version)
  953. {
  954. case 1:
  955. _hourStat = new StatCounter();
  956. _hourStat.Lock();
  957. for (int i = 0; i < _minuteStats.Length; i++)
  958. {
  959. _minuteStats[i] = new StatCounter(bR);
  960. _hourStat.Merge(_minuteStats[i]);
  961. }
  962. break;
  963. default:
  964. throw new InvalidDataException("HourlyStats version not supported.");
  965. }
  966. }
  967. #endregion
  968. #region public
  969. public void UpdateStat(DateTime dateTime, StatCounter minuteStat)
  970. {
  971. if (!minuteStat.IsLocked)
  972. throw new DnsServerException("StatCounter must be locked.");
  973. _hourStat.Merge(minuteStat);
  974. _minuteStats[dateTime.Minute] = minuteStat;
  975. }
  976. public void UnloadMinuteStats()
  977. {
  978. _minuteStats = null;
  979. }
  980. public void WriteTo(BinaryWriter bW)
  981. {
  982. bW.Write(Encoding.ASCII.GetBytes("HS")); //format
  983. bW.Write((byte)1); //version
  984. for (int i = 0; i < _minuteStats.Length; i++)
  985. {
  986. if (_minuteStats[i] == null)
  987. {
  988. _minuteStats[i] = new StatCounter();
  989. _minuteStats[i].Lock();
  990. }
  991. _minuteStats[i].WriteTo(bW);
  992. }
  993. }
  994. #endregion
  995. #region properties
  996. public StatCounter HourStat
  997. { get { return _hourStat; } }
  998. public StatCounter[] MinuteStats
  999. {
  1000. get { return _minuteStats; }
  1001. }
  1002. #endregion
  1003. }
  1004. class StatCounter
  1005. {
  1006. #region variables
  1007. volatile bool _locked;
  1008. long _totalQueries;
  1009. long _totalNoError;
  1010. long _totalServerFailure;
  1011. long _totalNxDomain;
  1012. long _totalRefused;
  1013. long _totalAuthoritative;
  1014. long _totalRecursive;
  1015. long _totalCached;
  1016. long _totalBlocked;
  1017. long _totalClients;
  1018. readonly ConcurrentDictionary<string, Counter> _queryDomains;
  1019. readonly ConcurrentDictionary<string, Counter> _queryBlockedDomains;
  1020. readonly ConcurrentDictionary<DnsResourceRecordType, Counter> _queryTypes;
  1021. readonly ConcurrentDictionary<IPAddress, Counter> _clientIpAddresses; //includes all queries
  1022. readonly ConcurrentDictionary<IPAddress, Counter> _errorIpAddresses; //includes REFUSED, FORMERR and SERVFAIL
  1023. readonly ConcurrentDictionary<DnsQuestionRecord, Counter> _queries;
  1024. bool _truncationFoundDuringMerge;
  1025. long _totalClientsDailyStatsSummation;
  1026. #endregion
  1027. #region constructor
  1028. public StatCounter()
  1029. {
  1030. _queryDomains = new ConcurrentDictionary<string, Counter>();
  1031. _queryBlockedDomains = new ConcurrentDictionary<string, Counter>();
  1032. _queryTypes = new ConcurrentDictionary<DnsResourceRecordType, Counter>();
  1033. _clientIpAddresses = new ConcurrentDictionary<IPAddress, Counter>();
  1034. _errorIpAddresses = new ConcurrentDictionary<IPAddress, Counter>();
  1035. _queries = new ConcurrentDictionary<DnsQuestionRecord, Counter>();
  1036. }
  1037. public StatCounter(BinaryReader bR)
  1038. {
  1039. if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "SC") //format
  1040. throw new InvalidDataException("StatCounter format is invalid.");
  1041. byte version = bR.ReadByte();
  1042. switch (version)
  1043. {
  1044. case 1:
  1045. case 2:
  1046. case 3:
  1047. case 4:
  1048. case 5:
  1049. case 6:
  1050. _totalQueries = bR.ReadInt32();
  1051. _totalNoError = bR.ReadInt32();
  1052. _totalServerFailure = bR.ReadInt32();
  1053. _totalNxDomain = bR.ReadInt32();
  1054. _totalRefused = bR.ReadInt32();
  1055. if (version >= 3)
  1056. {
  1057. _totalAuthoritative = bR.ReadInt32();
  1058. _totalRecursive = bR.ReadInt32();
  1059. _totalCached = bR.ReadInt32();
  1060. _totalBlocked = bR.ReadInt32();
  1061. }
  1062. else
  1063. {
  1064. _totalBlocked = bR.ReadInt32();
  1065. if (version >= 2)
  1066. _totalCached = bR.ReadInt32();
  1067. }
  1068. if (version >= 6)
  1069. _totalClients = bR.ReadInt32();
  1070. {
  1071. int count = bR.ReadInt32();
  1072. _queryDomains = new ConcurrentDictionary<string, Counter>(1, count);
  1073. for (int i = 0; i < count; i++)
  1074. _queryDomains.TryAdd(bR.ReadShortString(), new Counter(bR.ReadInt32()));
  1075. }
  1076. {
  1077. int count = bR.ReadInt32();
  1078. _queryBlockedDomains = new ConcurrentDictionary<string, Counter>(1, count);
  1079. for (int i = 0; i < count; i++)
  1080. _queryBlockedDomains.TryAdd(bR.ReadShortString(), new Counter(bR.ReadInt32()));
  1081. }
  1082. {
  1083. int count = bR.ReadInt32();
  1084. _queryTypes = new ConcurrentDictionary<DnsResourceRecordType, Counter>(1, count);
  1085. for (int i = 0; i < count; i++)
  1086. _queryTypes.TryAdd((DnsResourceRecordType)bR.ReadUInt16(), new Counter(bR.ReadInt32()));
  1087. }
  1088. {
  1089. int count = bR.ReadInt32();
  1090. _clientIpAddresses = new ConcurrentDictionary<IPAddress, Counter>(1, count);
  1091. for (int i = 0; i < count; i++)
  1092. _clientIpAddresses.TryAdd(IPAddressExtensions.ReadFrom(bR), new Counter(bR.ReadInt32()));
  1093. if (version < 6)
  1094. _totalClients = count;
  1095. }
  1096. if (version >= 4)
  1097. {
  1098. int count = bR.ReadInt32();
  1099. _queries = new ConcurrentDictionary<DnsQuestionRecord, Counter>(1, count);
  1100. for (int i = 0; i < count; i++)
  1101. _queries.TryAdd(new DnsQuestionRecord(bR.BaseStream), new Counter(bR.ReadInt32()));
  1102. }
  1103. else
  1104. {
  1105. _queries = new ConcurrentDictionary<DnsQuestionRecord, Counter>(1, 0);
  1106. }
  1107. if (version >= 5)
  1108. {
  1109. int count = bR.ReadInt32();
  1110. _errorIpAddresses = new ConcurrentDictionary<IPAddress, Counter>(1, count);
  1111. for (int i = 0; i < count; i++)
  1112. _errorIpAddresses.TryAdd(IPAddressExtensions.ReadFrom(bR), new Counter(bR.ReadInt32()));
  1113. }
  1114. else
  1115. {
  1116. _errorIpAddresses = new ConcurrentDictionary<IPAddress, Counter>(1, 0);
  1117. }
  1118. break;
  1119. case 7:
  1120. _totalQueries = bR.ReadInt64();
  1121. _totalNoError = bR.ReadInt64();
  1122. _totalServerFailure = bR.ReadInt64();
  1123. _totalNxDomain = bR.ReadInt64();
  1124. _totalRefused = bR.ReadInt64();
  1125. _totalAuthoritative = bR.ReadInt64();
  1126. _totalRecursive = bR.ReadInt64();
  1127. _totalCached = bR.ReadInt64();
  1128. _totalBlocked = bR.ReadInt64();
  1129. _totalClients = bR.ReadInt64();
  1130. {
  1131. int count = bR.ReadInt32();
  1132. _queryDomains = new ConcurrentDictionary<string, Counter>(1, count);
  1133. for (int i = 0; i < count; i++)
  1134. _queryDomains.TryAdd(bR.ReadShortString(), new Counter(bR.ReadInt64()));
  1135. }
  1136. {
  1137. int count = bR.ReadInt32();
  1138. _queryBlockedDomains = new ConcurrentDictionary<string, Counter>(1, count);
  1139. for (int i = 0; i < count; i++)
  1140. _queryBlockedDomains.TryAdd(bR.ReadShortString(), new Counter(bR.ReadInt64()));
  1141. }
  1142. {
  1143. int count = bR.ReadInt32();
  1144. _queryTypes = new ConcurrentDictionary<DnsResourceRecordType, Counter>(1, count);
  1145. for (int i = 0; i < count; i++)
  1146. _queryTypes.TryAdd((DnsResourceRecordType)bR.ReadUInt16(), new Counter(bR.ReadInt64()));
  1147. }
  1148. {
  1149. int count = bR.ReadInt32();
  1150. _clientIpAddresses = new ConcurrentDictionary<IPAddress, Counter>(1, count);
  1151. for (int i = 0; i < count; i++)
  1152. _clientIpAddresses.TryAdd(IPAddressExtensions.ReadFrom(bR), new Counter(bR.ReadInt64()));
  1153. }
  1154. {
  1155. int count = bR.ReadInt32();
  1156. _queries = new ConcurrentDictionary<DnsQuestionRecord, Counter>(1, count);
  1157. for (int i = 0; i < count; i++)
  1158. _queries.TryAdd(new DnsQuestionRecord(bR.BaseStream), new Counter(bR.ReadInt64()));
  1159. }
  1160. {
  1161. int count = bR.ReadInt32();
  1162. _errorIpAddresses = new ConcurrentDictionary<IPAddress, Counter>(1, count);
  1163. for (int i = 0; i < count; i++)
  1164. _errorIpAddresses.TryAdd(IPAddressExtensions.ReadFrom(bR), new Counter(bR.ReadInt64()));
  1165. }
  1166. break;
  1167. default:
  1168. throw new InvalidDataException("StatCounter version not supported.");
  1169. }
  1170. _locked = true;
  1171. }
  1172. #endregion
  1173. #region private
  1174. private static List<KeyValuePair<string, long>> GetTopList(List<KeyValuePair<string, long>> list, int limit)
  1175. {
  1176. list.Sort(delegate (KeyValuePair<string, long> item1, KeyValuePair<string, long> item2)
  1177. {
  1178. return item2.Value.CompareTo(item1.Value);
  1179. });
  1180. if (list.Count > limit)
  1181. list.RemoveRange(limit, list.Count - limit);
  1182. return list;
  1183. }
  1184. private static Counter GetNewCounter<T>(T key)
  1185. {
  1186. return new Counter();
  1187. }
  1188. #endregion
  1189. #region public
  1190. public void Lock()
  1191. {
  1192. _locked = true;
  1193. }
  1194. public void Update(DnsQuestionRecord query, DnsResponseCode responseCode, DnsServerResponseType responseType, IPAddress clientIpAddress)
  1195. {
  1196. if (_locked)
  1197. return;
  1198. if (clientIpAddress.IsIPv4MappedToIPv6)
  1199. clientIpAddress = clientIpAddress.MapToIPv4();
  1200. Interlocked.Increment(ref _totalQueries);
  1201. switch (responseCode)
  1202. {
  1203. case DnsResponseCode.NoError:
  1204. if (query is not null)
  1205. {
  1206. switch (responseType)
  1207. {
  1208. case DnsServerResponseType.Blocked:
  1209. case DnsServerResponseType.UpstreamBlocked:
  1210. case DnsServerResponseType.CacheBlocked:
  1211. //skip blocked domains
  1212. break;
  1213. default:
  1214. _queryDomains.GetOrAdd(query.Name.ToLower(), GetNewCounter).Increment();
  1215. _queries.GetOrAdd(query, GetNewCounter).Increment();
  1216. break;
  1217. }
  1218. }
  1219. Interlocked.Increment(ref _totalNoError);
  1220. break;
  1221. case DnsResponseCode.ServerFailure:
  1222. _errorIpAddresses.GetOrAdd(clientIpAddress, GetNewCounter).Increment();
  1223. Interlocked.Increment(ref _totalServerFailure);
  1224. break;
  1225. case DnsResponseCode.NxDomain:
  1226. Interlocked.Increment(ref _totalNxDomain);
  1227. break;
  1228. case DnsResponseCode.Refused:
  1229. _errorIpAddresses.GetOrAdd(clientIpAddress, GetNewCounter).Increment();
  1230. Interlocked.Increment(ref _totalRefused);
  1231. break;
  1232. case DnsResponseCode.FormatError:
  1233. _errorIpAddresses.GetOrAdd(clientIpAddress, GetNewCounter).Increment();
  1234. break;
  1235. }
  1236. switch (responseType)
  1237. {
  1238. case DnsServerResponseType.Authoritative:
  1239. Interlocked.Increment(ref _totalAuthoritative);
  1240. break;
  1241. case DnsServerResponseType.Recursive:
  1242. Interlocked.Increment(ref _totalRecursive);
  1243. break;
  1244. case DnsServerResponseType.Cached:
  1245. Interlocked.Increment(ref _totalCached);
  1246. break;
  1247. case DnsServerResponseType.Blocked:
  1248. if (query is not null)
  1249. _queryBlockedDomains.GetOrAdd(query.Name.ToLower(), GetNewCounter).Increment();
  1250. Interlocked.Increment(ref _totalBlocked);
  1251. break;
  1252. case DnsServerResponseType.UpstreamBlocked:
  1253. Interlocked.Increment(ref _totalRecursive);
  1254. if (query is not null)
  1255. _queryBlockedDomains.GetOrAdd(query.Name.ToLower(), GetNewCounter).Increment();
  1256. Interlocked.Increment(ref _totalBlocked);
  1257. break;
  1258. case DnsServerResponseType.CacheBlocked:
  1259. Interlocked.Increment(ref _totalCached);
  1260. if (query is not null)
  1261. _queryBlockedDomains.GetOrAdd(query.Name.ToLower(), GetNewCounter).Increment();
  1262. Interlocked.Increment(ref _totalBlocked);
  1263. break;
  1264. }
  1265. if (query is not null)
  1266. _queryTypes.GetOrAdd(query.Type, GetNewCounter).Increment();
  1267. _clientIpAddresses.GetOrAdd(clientIpAddress, GetNewCounter).Increment();
  1268. _totalClients = _clientIpAddresses.Count;
  1269. }
  1270. public void Merge(StatCounter statCounter, bool isDailyStatCounter = false, bool skipLock = false)
  1271. {
  1272. if (!skipLock && (!_locked || !statCounter._locked))
  1273. throw new DnsServerException("StatCounter must be locked.");
  1274. _totalQueries += statCounter._totalQueries;
  1275. _totalNoError += statCounter._totalNoError;
  1276. _totalServerFailure += statCounter._totalServerFailure;
  1277. _totalNxDomain += statCounter._totalNxDomain;
  1278. _totalRefused += statCounter._totalRefused;
  1279. _totalAuthoritative += statCounter._totalAuthoritative;
  1280. _totalRecursive += statCounter._totalRecursive;
  1281. _totalCached += statCounter._totalCached;
  1282. _totalBlocked += statCounter._totalBlocked;
  1283. foreach (KeyValuePair<string, Counter> queryDomain in statCounter._queryDomains)
  1284. _queryDomains.GetOrAdd(queryDomain.Key, GetNewCounter).Merge(queryDomain.Value);
  1285. foreach (KeyValuePair<string, Counter> queryBlockedDomain in statCounter._queryBlockedDomains)
  1286. _queryBlockedDomains.GetOrAdd(queryBlockedDomain.Key, GetNewCounter).Merge(queryBlockedDomain.Value);
  1287. foreach (KeyValuePair<DnsResourceRecordType, Counter> queryType in statCounter._queryTypes)
  1288. _queryTypes.GetOrAdd(queryType.Key, GetNewCounter).Merge(queryType.Value);
  1289. foreach (KeyValuePair<IPAddress, Counter> clientIpAddress in statCounter._clientIpAddresses)
  1290. _clientIpAddresses.GetOrAdd(clientIpAddress.Key, GetNewCounter).Merge(clientIpAddress.Value);
  1291. foreach (KeyValuePair<IPAddress, Counter> refusedIpAddress in statCounter._errorIpAddresses)
  1292. _errorIpAddresses.GetOrAdd(refusedIpAddress.Key, GetNewCounter).Merge(refusedIpAddress.Value);
  1293. foreach (KeyValuePair<DnsQuestionRecord, Counter> query in statCounter._queries)
  1294. _queries.GetOrAdd(query.Key, GetNewCounter).Merge(query.Value);
  1295. _totalClients = _clientIpAddresses.Count;
  1296. _totalClientsDailyStatsSummation += statCounter._totalClients;
  1297. if (isDailyStatCounter && (statCounter._totalClients > statCounter._clientIpAddresses.Count))
  1298. _truncationFoundDuringMerge = true;
  1299. }
  1300. public bool Truncate(int limit)
  1301. {
  1302. bool truncated = false;
  1303. if (_queryDomains.Count > limit)
  1304. {
  1305. List<KeyValuePair<string, Counter>> topDomains = new List<KeyValuePair<string, Counter>>(_queryDomains);
  1306. _queryDomains.Clear();
  1307. topDomains.Sort(delegate (KeyValuePair<string, Counter> item1, KeyValuePair<string, Counter> item2)
  1308. {
  1309. return item2.Value.Count.CompareTo(item1.Value.Count);
  1310. });
  1311. if (topDomains.Count > limit)
  1312. topDomains.RemoveRange(limit, topDomains.Count - limit);
  1313. foreach (KeyValuePair<string, Counter> item in topDomains)
  1314. _queryDomains[item.Key] = item.Value;
  1315. truncated = true;
  1316. }
  1317. if (_queryBlockedDomains.Count > limit)
  1318. {
  1319. List<KeyValuePair<string, Counter>> topBlockedDomains = new List<KeyValuePair<string, Counter>>(_queryBlockedDomains);
  1320. _queryBlockedDomains.Clear();
  1321. topBlockedDomains.Sort(delegate (KeyValuePair<string, Counter> item1, KeyValuePair<string, Counter> item2)
  1322. {
  1323. return item2.Value.Count.CompareTo(item1.Value.Count);
  1324. });
  1325. if (topBlockedDomains.Count > limit)
  1326. topBlockedDomains.RemoveRange(limit, topBlockedDomains.Count - limit);
  1327. foreach (KeyValuePair<string, Counter> item in topBlockedDomains)
  1328. _queryBlockedDomains[item.Key] = item.Value;
  1329. truncated = true;
  1330. }
  1331. if (_queryTypes.Count > limit)
  1332. {
  1333. List<KeyValuePair<DnsResourceRecordType, Counter>> queryTypes = new List<KeyValuePair<DnsResourceRecordType, Counter>>(_queryTypes);
  1334. _queryTypes.Clear();
  1335. queryTypes.Sort(delegate (KeyValuePair<DnsResourceRecordType, Counter> item1, KeyValuePair<DnsResourceRecordType, Counter> item2)
  1336. {
  1337. return item2.Value.Count.CompareTo(item1.Value.Count);
  1338. });
  1339. if (queryTypes.Count > limit)
  1340. {
  1341. long othersCount = 0;
  1342. for (int i = limit; i < queryTypes.Count; i++)
  1343. othersCount += queryTypes[i].Value.Count;
  1344. queryTypes.RemoveRange(limit - 1, queryTypes.Count - (limit - 1));
  1345. queryTypes.Add(new KeyValuePair<DnsResourceRecordType, Counter>(DnsResourceRecordType.Unknown, new Counter(othersCount)));
  1346. }
  1347. foreach (KeyValuePair<DnsResourceRecordType, Counter> item in queryTypes)
  1348. _queryTypes[item.Key] = item.Value;
  1349. truncated = true;
  1350. }
  1351. if (_clientIpAddresses.Count > limit)
  1352. {
  1353. List<KeyValuePair<IPAddress, Counter>> topClients = new List<KeyValuePair<IPAddress, Counter>>(_clientIpAddresses);
  1354. _clientIpAddresses.Clear();
  1355. topClients.Sort(delegate (KeyValuePair<IPAddress, Counter> item1, KeyValuePair<IPAddress, Counter> item2)
  1356. {
  1357. return item2.Value.Count.CompareTo(item1.Value.Count);
  1358. });
  1359. if (topClients.Count > limit)
  1360. topClients.RemoveRange(limit, topClients.Count - limit);
  1361. foreach (KeyValuePair<IPAddress, Counter> item in topClients)
  1362. _clientIpAddresses[item.Key] = item.Value;
  1363. truncated = true;
  1364. }
  1365. if (_errorIpAddresses.Count > limit)
  1366. {
  1367. List<KeyValuePair<IPAddress, Counter>> topErrorClients = new List<KeyValuePair<IPAddress, Counter>>(_errorIpAddresses);
  1368. _errorIpAddresses.Clear();
  1369. topErrorClients.Sort(delegate (KeyValuePair<IPAddress, Counter> item1, KeyValuePair<IPAddress, Counter> item2)
  1370. {
  1371. return item2.Value.Count.CompareTo(item1.Value.Count);
  1372. });
  1373. if (topErrorClients.Count > limit)
  1374. topErrorClients.RemoveRange(limit, topErrorClients.Count - limit);
  1375. foreach (KeyValuePair<IPAddress, Counter> item in topErrorClients)
  1376. _errorIpAddresses[item.Key] = item.Value;
  1377. truncated = true;
  1378. }
  1379. if (_queries.Count > limit)
  1380. {
  1381. //only last hour queries data is required for cache auto prefetching
  1382. _queries.Clear();
  1383. truncated = true;
  1384. }
  1385. return truncated;
  1386. }
  1387. public void WriteTo(BinaryWriter bW)
  1388. {
  1389. if (!_locked)
  1390. throw new DnsServerException("StatCounter must be locked.");
  1391. bW.Write(Encoding.ASCII.GetBytes("SC")); //format
  1392. bW.Write((byte)7); //version
  1393. bW.Write(_totalQueries);
  1394. bW.Write(_totalNoError);
  1395. bW.Write(_totalServerFailure);
  1396. bW.Write(_totalNxDomain);
  1397. bW.Write(_totalRefused);
  1398. bW.Write(_totalAuthoritative);
  1399. bW.Write(_totalRecursive);
  1400. bW.Write(_totalCached);
  1401. bW.Write(_totalBlocked);
  1402. bW.Write(_totalClients);
  1403. {
  1404. bW.Write(_queryDomains.Count);
  1405. foreach (KeyValuePair<string, Counter> queryDomain in _queryDomains)
  1406. {
  1407. bW.WriteShortString(queryDomain.Key);
  1408. bW.Write(queryDomain.Value.Count);
  1409. }
  1410. }
  1411. {
  1412. bW.Write(_queryBlockedDomains.Count);
  1413. foreach (KeyValuePair<string, Counter> queryBlockedDomain in _queryBlockedDomains)
  1414. {
  1415. bW.WriteShortString(queryBlockedDomain.Key);
  1416. bW.Write(queryBlockedDomain.Value.Count);
  1417. }
  1418. }
  1419. {
  1420. bW.Write(_queryTypes.Count);
  1421. foreach (KeyValuePair<DnsResourceRecordType, Counter> queryType in _queryTypes)
  1422. {
  1423. bW.Write((ushort)queryType.Key);
  1424. bW.Write(queryType.Value.Count);
  1425. }
  1426. }
  1427. {
  1428. bW.Write(_clientIpAddresses.Count);
  1429. foreach (KeyValuePair<IPAddress, Counter> clientIpAddress in _clientIpAddresses)
  1430. {
  1431. clientIpAddress.Key.WriteTo(bW);
  1432. bW.Write(clientIpAddress.Value.Count);
  1433. }
  1434. }
  1435. {
  1436. bW.Write(_queries.Count);
  1437. foreach (KeyValuePair<DnsQuestionRecord, Counter> query in _queries)
  1438. {
  1439. query.Key.WriteTo(bW.BaseStream, null);
  1440. bW.Write(query.Value.Count);
  1441. }
  1442. }
  1443. {
  1444. bW.Write(_errorIpAddresses.Count);
  1445. foreach (KeyValuePair<IPAddress, Counter> refusedIpAddress in _errorIpAddresses)
  1446. {
  1447. refusedIpAddress.Key.WriteTo(bW);
  1448. bW.Write(refusedIpAddress.Value.Count);
  1449. }
  1450. }
  1451. }
  1452. public List<KeyValuePair<string, long>> GetTopDomains(int limit)
  1453. {
  1454. List<KeyValuePair<string, long>> topDomains = new List<KeyValuePair<string, long>>(_queryDomains.Count);
  1455. foreach (KeyValuePair<string, Counter> item in _queryDomains)
  1456. topDomains.Add(new KeyValuePair<string, long>(item.Key, item.Value.Count));
  1457. return GetTopList(topDomains, limit);
  1458. }
  1459. public List<KeyValuePair<string, long>> GetTopBlockedDomains(int limit)
  1460. {
  1461. List<KeyValuePair<string, long>> topBlockedDomains = new List<KeyValuePair<string, long>>(_queryBlockedDomains.Count);
  1462. foreach (KeyValuePair<string, Counter> item in _queryBlockedDomains)
  1463. topBlockedDomains.Add(new KeyValuePair<string, long>(item.Key, item.Value.Count));
  1464. return GetTopList(topBlockedDomains, limit);
  1465. }
  1466. public List<KeyValuePair<string, long>> GetTopClients(int limit)
  1467. {
  1468. List<KeyValuePair<string, long>> topClients = new List<KeyValuePair<string, long>>(_clientIpAddresses.Count);
  1469. foreach (KeyValuePair<IPAddress, Counter> item in _clientIpAddresses)
  1470. topClients.Add(new KeyValuePair<string, long>(item.Key.ToString(), item.Value.Count));
  1471. return GetTopList(topClients, limit);
  1472. }
  1473. public List<KeyValuePair<string, long>> GetTopQueryTypes(int limit)
  1474. {
  1475. List<KeyValuePair<string, long>> queryTypes = new List<KeyValuePair<string, long>>(_queryTypes.Count);
  1476. foreach (KeyValuePair<DnsResourceRecordType, Counter> item in _queryTypes)
  1477. queryTypes.Add(new KeyValuePair<string, long>(item.Key.ToString(), item.Value.Count));
  1478. queryTypes.Sort(delegate (KeyValuePair<string, long> item1, KeyValuePair<string, long> item2)
  1479. {
  1480. return item2.Value.CompareTo(item1.Value);
  1481. });
  1482. if (queryTypes.Count > limit)
  1483. {
  1484. long othersCount = 0;
  1485. for (int i = limit; i < queryTypes.Count; i++)
  1486. othersCount += queryTypes[i].Value;
  1487. queryTypes.RemoveRange((limit - 1), queryTypes.Count - (limit - 1));
  1488. queryTypes.Add(new KeyValuePair<string, long>("Others", othersCount));
  1489. }
  1490. return queryTypes;
  1491. }
  1492. public List<KeyValuePair<DnsQuestionRecord, long>> GetEligibleQueries(int minimumHits)
  1493. {
  1494. List<KeyValuePair<DnsQuestionRecord, long>> eligibleQueries = new List<KeyValuePair<DnsQuestionRecord, long>>(Convert.ToInt32(_queries.Count * 0.1));
  1495. foreach (KeyValuePair<DnsQuestionRecord, Counter> item in _queries)
  1496. {
  1497. if (item.Value.Count >= minimumHits)
  1498. eligibleQueries.Add(new KeyValuePair<DnsQuestionRecord, long>(item.Key, item.Value.Count));
  1499. }
  1500. return eligibleQueries;
  1501. }
  1502. public IReadOnlyDictionary<IPAddress, long> GetClientSubnetStats(int ipv4PrefixLength, int ipv6PrefixLength)
  1503. {
  1504. Dictionary<IPAddress, long> clientSubnetStats = new Dictionary<IPAddress, long>(_clientIpAddresses.Count);
  1505. foreach (KeyValuePair<IPAddress, Counter> item in _clientIpAddresses)
  1506. {
  1507. IPAddress clientSubnet;
  1508. switch (item.Key.AddressFamily)
  1509. {
  1510. case AddressFamily.InterNetwork:
  1511. clientSubnet = item.Key.GetNetworkAddress(ipv4PrefixLength);
  1512. break;
  1513. case AddressFamily.InterNetworkV6:
  1514. clientSubnet = item.Key.GetNetworkAddress(ipv6PrefixLength);
  1515. break;
  1516. default:
  1517. throw new NotSupportedException("AddressFamily not supported.");
  1518. }
  1519. if (clientSubnetStats.TryGetValue(clientSubnet, out long existingValue))
  1520. clientSubnetStats[clientSubnet] = existingValue + item.Value.Count;
  1521. else
  1522. clientSubnetStats.Add(clientSubnet, item.Value.Count);
  1523. }
  1524. return clientSubnetStats;
  1525. }
  1526. public IReadOnlyDictionary<IPAddress, long> GetErrorClientSubnetStats(int ipv4PrefixLength, int ipv6PrefixLength)
  1527. {
  1528. Dictionary<IPAddress, long> errorClientSubnetStats = new Dictionary<IPAddress, long>(_errorIpAddresses.Count);
  1529. foreach (KeyValuePair<IPAddress, Counter> item in _errorIpAddresses)
  1530. {
  1531. IPAddress clientSubnet;
  1532. switch (item.Key.AddressFamily)
  1533. {
  1534. case AddressFamily.InterNetwork:
  1535. clientSubnet = item.Key.GetNetworkAddress(ipv4PrefixLength);
  1536. break;
  1537. case AddressFamily.InterNetworkV6:
  1538. clientSubnet = item.Key.GetNetworkAddress(ipv6PrefixLength);
  1539. break;
  1540. default:
  1541. throw new NotSupportedException("AddressFamily not supported.");
  1542. }
  1543. if (errorClientSubnetStats.TryGetValue(clientSubnet, out long existingValue))
  1544. errorClientSubnetStats[clientSubnet] = existingValue + item.Value.Count;
  1545. else
  1546. errorClientSubnetStats.Add(clientSubnet, item.Value.Count);
  1547. }
  1548. return errorClientSubnetStats;
  1549. }
  1550. #endregion
  1551. #region properties
  1552. public bool IsLocked
  1553. { get { return _locked; } }
  1554. public long TotalQueries
  1555. { get { return _totalQueries; } }
  1556. public long TotalNoError
  1557. { get { return _totalNoError; } }
  1558. public long TotalServerFailure
  1559. { get { return _totalServerFailure; } }
  1560. public long TotalNxDomain
  1561. { get { return _totalNxDomain; } }
  1562. public long TotalRefused
  1563. { get { return _totalRefused; } }
  1564. public long TotalAuthoritative
  1565. { get { return _totalAuthoritative; } }
  1566. public long TotalRecursive
  1567. { get { return _totalRecursive; } }
  1568. public long TotalCached
  1569. { get { return _totalCached; } }
  1570. public long TotalBlocked
  1571. { get { return _totalBlocked; } }
  1572. public long TotalClients
  1573. {
  1574. get
  1575. {
  1576. if (_truncationFoundDuringMerge)
  1577. return _totalClientsDailyStatsSummation;
  1578. return _totalClients;
  1579. }
  1580. }
  1581. #endregion
  1582. class Counter
  1583. {
  1584. #region variables
  1585. long _count;
  1586. #endregion
  1587. #region constructor
  1588. public Counter()
  1589. { }
  1590. public Counter(long count)
  1591. {
  1592. _count = count;
  1593. }
  1594. #endregion
  1595. #region public
  1596. public void Increment()
  1597. {
  1598. Interlocked.Increment(ref _count);
  1599. }
  1600. public void Merge(Counter counter)
  1601. {
  1602. _count += counter._count;
  1603. }
  1604. #endregion
  1605. #region properties
  1606. public long Count
  1607. { get { return _count; } }
  1608. #endregion
  1609. }
  1610. }
  1611. class StatsQueueItem
  1612. {
  1613. #region variables
  1614. public readonly DateTime _timestamp;
  1615. public readonly DnsDatagram _request;
  1616. public readonly IPEndPoint _remoteEP;
  1617. public readonly DnsTransportProtocol _protocol;
  1618. public readonly DnsDatagram _response;
  1619. #endregion
  1620. #region constructor
  1621. public StatsQueueItem(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
  1622. {
  1623. _timestamp = DateTime.UtcNow;
  1624. _request = request;
  1625. _remoteEP = remoteEP;
  1626. _protocol = protocol;
  1627. _response = response;
  1628. }
  1629. #endregion
  1630. }
  1631. }
  1632. }