added sendNotification function
authorft
Mon, 09 Dec 2013 10:12:57 +0100
changeset 80a5ba0ef1058
parent 7 e75bc05c279d
child 9 cc99197f1e08
added sendNotification function
src/OsecFS.py
     1.1 --- a/src/OsecFS.py	Fri Dec 06 13:15:45 2013 +0100
     1.2 +++ b/src/OsecFS.py	Mon Dec 09 10:12:57 2013 +0100
     1.3 @@ -17,6 +17,9 @@
     1.4  import subprocess
     1.5  
     1.6  import urllib3
     1.7 +import urllib
     1.8 +import netifaces
     1.9 +import netaddr
    1.10  
    1.11  
    1.12  MINOPTS = { "Main" : ["Logfile", "Mountpoint", "Rootpath", "LocalScanserverURL", "RemoteScanserverURL", "ReadOnly"]}
    1.13 @@ -212,6 +215,29 @@
    1.14  
    1.15      return whitelisted
    1.16  
    1.17 +def sendNotification (type, message):
    1.18 +    netifaces.ifaddresses("eth0")[2][0]["addr"]
    1.19 +    
    1.20 +    # Get first address in network (0 = network ip -> 192.168.0.0)
    1.21 +    remote_ip = netaddr.IPNetwork("%s/%s" %(netifaces.ifaddresses("eth0")[2][0]["addr"], netifaces.ifaddresses("eth0")[2][0]["netmask"]))[1]
    1.22 +    
    1.23 +    url_options = {"type" : type, "message" : message }
    1.24 +    url = ("http://%s/notification?%s" %(remote_ip, urllib.urlencode(url_options)))
    1.25 +    
    1.26 +    LOG.debug ("Send notification to \"%s\"" %(url, ))
    1.27 +    
    1.28 +    try:
    1.29 +        response = httpPool.request_encode_body('GET', url, retries = 0)
    1.30 +    except:
    1.31 +        LOG.error("Remote host not reachable")
    1.32 +        LOG.error ("Exception: %s" %(sys.exc_info()[0]))
    1.33 +        return
    1.34 +    
    1.35 +    if response.status == STATUS_CODE_OK:
    1.36 +        LOG.info("Notification sent successfully")
    1.37 +    else:
    1.38 +        LOG.error("Server returned errorcode: %s" %(response.status,))
    1.39 +
    1.40  class OsecFS (Fuse):
    1.41  
    1.42      __rootpath = None
    1.43 @@ -368,6 +394,8 @@
    1.44  
    1.45      config = loadConfig ()
    1.46      initLog (config)
    1.47 +    
    1.48 +    #sendNotification("Info", "OsecFS started")
    1.49  
    1.50      scanserverTimestamp = time.time()
    1.51