Search:

Search all manuals
Search this manual
Manual
Membase and Python Development Tutorial
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
Membase and Python Development Tutorial
Chapter Sections
Chapters

7. Example Application: Network Monitoring

Membase lends itself well to real-time monitoring and logging applications. With its simple setting and retrieval mechanisms, putting data in and getting data out are extremely simplified. In this example application, as you will see, there is more code to run the monitoring side of things than there is for the database interaction.

A simplified approach to network monitoring can be taken by simply scanning whether a host is responsive and by checking the availability of open network ports on the system. In this example application we use a Python library that gives simplified access to the nmap port scanning toolset. Data retrieve from the nmap module is that stored in the database for later use.

import nmap, json, time

from couchbase.couchbaseclient import VBucketAwareCouchbaseClient as CbClient
cb = CbClient("http://localhost:8091/pools/default","default","",False)

def scan(host='127.0.0.1',range='22-80'):
  '''Setup port scanner'''
  nm = nmap.PortScanner()
  nm.scan(host, range)
  return nm

nm = scan()

for host in nm.all_hosts():
  host_state = {}
  host_state["host"] = host
  host_state["state"] = nm[host].state()
  for proto in nm[host].all_protocols():
    for port in nm[host][proto].keys():
      port_state = nm[host][proto][port]
      port_state["host"] = host
      port_state["protocol"] = proto
      port_state["port"] = port
      timestamp = time.time()
      host_state["timestamp"] = str(timestamp)
      port_state["timestamp"] = str(timestamp)
      docid = str(host) + ":" + str(proto) + ":" + str(port) + ":" + str(port_state["timestamp"])
      cb.set(docid,0,0,json.dumps(port_state))
      cb.set(str(host),0,0,json.dumps(host_state))
      print cb.get(docid)
      print cb.get(str(host))

cb.done()

You can download the script, including more detailed comments, from: nmap-tutorial-complete.py