Use case: HTTP POST to remote server

In this section, we will explain how to send KPIs to a remote HTTP server using POST requests.

For the purpose of this use case, let’s launch a python HTTP server that is able to process POST request on a linux host with IP address a.b.c.d.

  1. Create a file named dummy-server.py with this content:

    #!/usr/bin/env python
    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    
    class HTTPPostHandler(BaseHTTPRequestHandler):
        def do_POST(self):
            content_length = int(self.headers['Content-Length'])
            post_data = self.rfile.read(content_length)
            print(post_data)
    
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            self.wfile.write("<html><body><h1>POST!</h1></body></html>")
    
    def run():
        httpd = HTTPServer(('', 8000), HTTPPostHandler)
        print('Starting httpd...')
        httpd.serve_forever()
    
    if __name__ == "__main__":
        run()
    
  2. Start the server:

    # python dummy-server.py
    
  3. On a freshly booted system, start monitoring:

    # monitoring.sh start
    Starting Monitoring...
    Monitoring successfully started
    
  4. Check that the monitoring has been properly started

    # monitoring.sh status
    Monitoring status...
    [ ok ] Process sysrepod running
    [ ok ] Process netopeer2-server running
    [ ok ] Process kpid running
    
  5. Do an HTTP post of the fast path cpu usage in json format every 10 seconds to http://a.b.c.d:8000/write. Change a.b.c.d to match the remote host’s address:

# while true; do
    kpi-get fp-cpu-usage -o http-json:host=a.b.c.d:port=8000:path=write
    sleep 10
  done

On the web server, you should see POST requests every 10 seconds or so:

{"sixwind-router:monitoring": {"fp:fp-cpu-usage": [{"cpu": "cpu1", "busy": 0}, {"cpu": "cpu2", "busy": 0}, {"cpu": "cpu3", "busy": 0}]}}
127.0.0.1 - - [13/Dec/2017 16:04:37] "POST /write HTTP/1.1" 200 -
{"sixwind-router:monitoring": {"fp:fp-cpu-usage": [{"cpu": "cpu1", "busy": 0}, {"cpu": "cpu2", "busy": 0}, {"cpu": "cpu3", "busy": 0}]}}
127.0.0.1 - - [13/Dec/2017 16:04:48] "POST /write HTTP/1.1" 200 -
{"sixwind-router:monitoring": {"fp:fp-cpu-usage": [{"cpu": "cpu1", "busy": 0}, {"cpu": "cpu2", "busy": 0}, {"cpu": "cpu3", "busy": 0}]}}
127.0.0.1 - - [13/Dec/2017 16:04:59] "POST /write HTTP/1.1" 200 -
{"sixwind-router:monitoring": {"fp:fp-cpu-usage": [{"cpu": "cpu1", "busy": 0}, {"cpu": "cpu2", "busy": 0}, {"cpu": "cpu3", "busy": 0}]}}
127.0.0.1 - - [13/Dec/2017 16:05:09] "POST /write HTTP/1.1" 200 -