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
    * kpid.service - KPI Daemon
       Loaded: loaded (/lib/systemd/system/kpid.service; disabled; vendor preset: enabled)
       Active: active (running) since Tue 2020-06-16 09:31:53 UTC; 3s ago
     Main PID: 3526 (kpid)
        Tasks: 1 (limit: 1088)
       CGroup: /system.slice/kpid.service
               `-3526 /usr/bin/python3 /usr/bin/kpid
    
    * netopeer2-server.service - NETCONF Server
       Loaded: loaded (/lib/systemd/system/netopeer2-server.service; enabled; vendor preset: enabled)
       Active: active (running) since Mon 2020-06-16 09:31:52 UTC; 4s ago
     Main PID: 1067 (netopeer2-serve)
        Tasks: 7 (limit: 1088)
       CGroup: /system.slice/netopeer2-server.service
               `-1067 /usr/bin/netopeer2-server -U -g netconf -m 660
    
  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 -