Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
better buffering, basic templating for documentation, refactoring, bo…
Browse files Browse the repository at this point in the history
…otstrap executable
  • Loading branch information
mriehl committed Jun 18, 2013
1 parent c131046 commit eb3596e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 16 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Expand Up @@ -26,6 +26,15 @@ pip-log.txt
.tox
nosetests.xml

# Build artifacts
target

# Virtualenv
venv

# application logs
livestatus.log

# Translations
*.mo

Expand Down
Empty file modified bootstrap.py 100644 → 100755
Empty file.
5 changes: 3 additions & 2 deletions build.py
Expand Up @@ -14,6 +14,7 @@

name = "livestatus-service"
version = "0.0.1"
description = "Exposes MK's livestatus to the outside world over HTTP"
authors = (Author("Marcel Wolf", "marcel.wolf@immobilienscout24.de"),
Author("Maximilien Riehl", "maximilien.riehl@gmail.com"))
url = "https://github.com/mriehl/livestatus-service"
Expand All @@ -29,9 +30,9 @@ def initialize(project):
project.set_property('copy_resources_target', '$dir_dist')
project.get_property('copy_resources_glob').append('setup.cfg')
project.get_property("filter_resources_glob").append("**/livestatus_service/__init__.py")

project.install_file('/data/is24/livestatus_service/', 'livestatus_service/livestatus_service.wsgi')

project.install_file('/data/is24/livestatus_service/', 'livestatus_service/livestatus_service.wsgi')
project.include_file("livestatus_service", "templates/*.html")
project.set_property("coverage_threshold_warn", 85)
project.set_property("coverage_break_build", False)

Expand Down
25 changes: 15 additions & 10 deletions src/main/python/livestatus_service/livestatus.py
@@ -1,6 +1,6 @@
import socket
import time

BUFFER_SIZE = 8192

def configure_livestatus(configuration):
global socket_path
Expand All @@ -9,21 +9,26 @@ def configure_livestatus(configuration):
def perform_query(query):
global socket_path

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(socket_path)
livestatus_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
livestatus_socket.connect(socket_path)

s.send("{0}\n".format(query))
s.shutdown(socket.SHUT_WR)
answer = s.recv(100000000)
livestatus_socket.send("{0}\n".format(query))
livestatus_socket.shutdown(socket.SHUT_WR)
total_data=[]
while True:
data = livestatus_socket.recv(BUFFER_SIZE)
if not data: break
total_data.append(data)
answer = ''.join(total_data)

return answer

def perform_command(command):
global socket_path

s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(socket_path)
livestatus_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
livestatus_socket.connect(socket_path)
timestamp = str(int(time.time()))
s.send("COMMAND [{0}] {1}\n".format(timestamp, command))
s.shutdown(socket.SHUT_WR)
livestatus_socket.send("COMMAND [{0}] {1}\n".format(timestamp, command))
livestatus_socket.shutdown(socket.SHUT_WR)
return "OK"
60 changes: 60 additions & 0 deletions src/main/python/livestatus_service/templates/index.html
@@ -0,0 +1,60 @@
{% extends "template.html" %}

{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap-combined.min.css" rel="stylesheet">
{% endblock %}

{% block content %}
<div class="container">
<div class="row">
<div class="span8 hero-unit">
<h1>Livestatus service</h1>

<p>
The livestatus-service exposes the functionality of a livestatus socket to the outside world over HTTP.
</p>
<ul>
<li>
<h2>Performing queries</h2>
<p>
<code>GET /query?q=<em>QUERY</em></code>
</p>
<p>
Will perform a query using <em>QUERY</em>. An easy example would be
<code>/query?q=GET%20hosts</code>
</p>
<p>
If you need newlines, e.G. to add a filter, use <code>\n</code>.
</p>
</li>
<li>
<h2>Performing commands</h2>
<p>
<code>GET /cmd?q=<em>COMMAND</em></code>
</p>
<p>
Will perform a command using <em>COMMAND</em>. The <code>COMMAND [%s]</code> directive will be inserted for you.
An easy example would be <code>/cmd?q=DISABLE_HOST_SVC_CHECKS;devytc</code>
</p>
</li>
</ul>
</div>
</div>
</div>

{% endblock %}

{% block footer %}
<hr>
<div class="container">
<div class="offset1 span5">
{{ super() }}
</div>
</div>

<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/js/bootstrap.min.js"></script>
{% endblock %}


19 changes: 19 additions & 0 deletions src/main/python/livestatus_service/templates/template.html
@@ -0,0 +1,19 @@
<!doctype html>
<html>
<head>
{% block head %}
<title>{% block title %}{% endblock %} - pypiproxy</title>
{% endblock %}
</head>
<body>

{% block content %}{% endblock %}

<footer>
{% block footer %}
Livestatus service, version {{ version }} <br>
&copy; Copyright 2013 Marcel Wolf, Maximilien Riehl
{% endblock %}
</footer>
</body>
</html>
11 changes: 7 additions & 4 deletions src/main/python/livestatus_service/webapp.py
@@ -1,29 +1,32 @@
import logging

from flask import Flask, request
from flask import Flask, request, render_template
from livestatus import perform_query, perform_command
from livestatus_service import __version__ as livestatus_version

LOGGER = logging.getLogger("livestatus.webapp")

application = Flask(__name__)

def render_application_template(template_name, **template_parameters):
template_parameters["version"] = livestatus_version
return render_template(template_name, **template_parameters)

@application.route("/")
def handle_index():
return ('todo', 200)
return render_application_template("index.html", **locals())


@application.route('/query', methods=['GET'])
def handle_query():
query_command = request.args.get('q')
query_command = query_command.replace('\\n', '\n')
query_result = perform_query(query_command)
LOGGER.debug('Query {0} had result {1}'.format(query_command, query_result))
return ("{0}\n".format(query_result), 200)

@application.route('/cmd', methods=['GET'])
def handle_command():
command = request.args.get('q')
command = command.replace('\\n', '\n')
command_result = perform_command(command)
LOGGER.debug('Command {0} had result {1}'.format(command, command_result))
return ("{0}\n".format(command_result), 200)

0 comments on commit eb3596e

Please sign in to comment.