Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Knot projects
Knot DNS
Commits
c9419784
Commit
c9419784
authored
Nov 07, 2016
by
Filip Siroky
Committed by
Daniel Salzman
Dec 29, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
python: add statistics over HTTP/HTTPS script
parent
41a32249
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
0 deletions
+73
-0
python/stats_http.py
python/stats_http.py
+73
-0
No files found.
python/stats_http.py
0 → 100755
View file @
c9419784
#!/usr/bin/env python3
"""Simple program for exposing statistics from Knot DNS over HTTP/HTTPS."""
import
http.server
import
libknot.control
import
json
import
ssl
import
time
# Configuration.
#libknot.control.load_lib("../src/.libs/libknot.so")
ctl_socket
=
"/tmp/knot.sock"
ctl_timeout
=
2
ctl_flags
=
""
# set "F" for all supported counters.
http_host
=
"127.0.0.1"
http_port
=
8080
ssl_enable
=
False
ssl_keyfile
=
"./mykey.key"
ssl_certfile
=
"./mycert.crt"
class
StatsServer
(
http
.
server
.
BaseHTTPRequestHandler
):
def
do_GET
(
self
):
self
.
send_response
(
200
)
self
.
send_header
(
"Content-type"
,
"text/html"
)
self
.
end_headers
()
# Connect to Knot server.
ctl
=
libknot
.
control
.
KnotCtl
()
ctl
.
connect
(
ctl_socket
)
ctl
.
set_timeout
(
ctl_timeout
)
# Get global metrics.
global_stats
=
dict
()
try
:
ctl
.
send_block
(
cmd
=
"stats"
,
flags
=
ctl_flags
)
global_stats
=
ctl
.
receive_stats
()
except
:
pass
# Get zone metrics.
zone_stats
=
dict
()
try
:
ctl
.
send_block
(
cmd
=
"zone-stats"
,
flags
=
ctl_flags
)
zone_stats
=
ctl
.
receive_stats
()
except
:
pass
# Disconnect from the server.
ctl
.
send
(
libknot
.
control
.
KnotCtlType
.
END
)
ctl
.
close
()
# Publish the stats.
stats
=
{
**
global_stats
,
**
zone_stats
}
self
.
wfile
.
write
(
bytes
(
json
.
dumps
(
stats
,
indent
=
4
,
sort_keys
=
True
),
"utf-8"
))
httpd
=
http
.
server
.
HTTPServer
((
http_host
,
http_port
),
StatsServer
)
if
ssl_enable
:
httpd
.
socket
=
ssl
.
wrap_socket
(
httpd
.
socket
,
keyfile
=
ssl_keyfile
,
certfile
=
ssl_certfile
,
server_side
=
True
)
print
(
"%s: HTTP%s Server Start - %s:%s"
%
(
time
.
asctime
(),
"S"
if
ssl_enable
else
""
,
http_host
,
http_port
))
try
:
httpd
.
serve_forever
()
except
KeyboardInterrupt
:
pass
httpd
.
server_close
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment