]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ws.hh
add OpenSSL exception to PowerDNS, Netherlabs, van Dijk and Hubert copyrights
[thirdparty/pdns.git] / pdns / ws.hh
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2009 PowerDNS.COM BV
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation
8
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 #ifndef WS_HH
23 #define WS_HH
24 #include <string>
25 #include <map>
26 #include <time.h>
27 #include <pthread.h>
28 #include <sstream>
29 #include <iomanip>
30 #include <unistd.h>
31
32 #ifdef HAVE_CONFIG_H
33 # include <config.h>
34 #endif // HAVE_CONFIG_H
35
36 #include "misc.hh"
37 #include "namespaces.hh"
38
39 class Ewma
40 {
41 public:
42 Ewma() : d_last(0), d_10(0), d_5(0), d_1(0), d_max(0){dt.set();}
43 void submit(int val)
44 {
45 int rate=val-d_last;
46 double difft=dt.udiff()/1000000.0;
47 dt.set();
48
49 d_10=((600.0-difft)*d_10+(difft*rate))/600.0;
50 d_5=((300.0-difft)*d_5+(difft*rate))/300.0;
51 d_1=((60.0-difft)*d_1+(difft*rate))/60.0;
52 d_max=max(d_1,d_max);
53
54 d_last=val;
55 }
56 double get10()
57 {
58 return d_10;
59 }
60 double get5()
61 {
62 return d_5;
63 }
64 double get1()
65 {
66 return d_1;
67 }
68 double getMax()
69 {
70 return d_max;
71 }
72 private:
73 DTime dt;
74 int d_last;
75 double d_10, d_5, d_1, d_max;
76 };
77
78 class WebServer;
79
80 class StatWebServer
81 {
82 public:
83 StatWebServer();
84 void go();
85 static string makePercentage(const double& val);
86 private:
87 static void *threadHelper(void *);
88 static void *statThreadHelper(void *p);
89 static string indexfunction(const string& method, const string& post, const map<string,string> &varmap, void *ptr, bool *custom);
90 static string cssfunction(const string& method, const string& post, const map<string,string> &varmap, void *ptr, bool *custom);
91 static string jsonstat(const string& method, const string& post, const map<string,string> &varmap, void *ptr, bool *custom);
92 void printvars(ostringstream &ret);
93 void printargs(ostringstream &ret);
94 void launch();
95 void statThread();
96 pthread_t d_tid;
97
98 time_t d_start;
99 double d_min10, d_min5, d_min1;
100 Ewma d_queries, d_cachehits, d_cachemisses;
101 Ewma d_qcachehits, d_qcachemisses;
102 WebServer *d_ws;
103 };
104
105 #endif