#!/usr/bin/python
######################################################################
# Cloud Routes Bridge
# -------------------------------------------------------------------
# Actions Module
######################################################################

import rethinkdb as r
from rethinkdb.errors import RqlRuntimeError, RqlDriverError
import syslog

def action(**kwargs):
    ''' This method is called to action a reaction '''
    jdata = kwargs['jdata']
    rdb = kwargs['rdb']
    r_server = kwargs['r_server']
    if jdata['check']['prev_status'] != jdata['check']['status']:
        chStatus(jdata['cid'], jdata['check']['status'], rdb, r_server)
        if "web" in jdata['check']['status']:
            # Web Checks always get an increased failcount
            incFailcount(jdata['cid'],
                jdata['prev_failcount'], rdb, r_server)
        else:
            # Reset failcount as status is different
            resetFailcount(jdata['cid'], rdb, r_server)
    else:
        # Increase failcount for each returned status
        incFailcount(jdata['cid'],
            jdata['prev_failcount'], rdb, r_server)
    return True


def chStatus(cid, status, rdb, r_server):
    ''' This method will be called to change a users status in the db '''
    success = False
    cacheonly = False

    # Try to set rethinkdb first
    try:
        results = r.table('monitors').get(
            cid).update({'status': status}).run(rdb)
        if results['replaced'] == 1:
            success = True
            cacheonly = False
        else:
            success = False
    except (RqlDriverError, RqlRuntimeError) as e:
        success = False
        cacheonly = True
        line = "chstatus: RethinkDB is inaccessible cannot change status for %s" % cid
        syslog.syslog(syslog.LOG_INFO, line)
        line = "chstatus: RethinkDB Error: %s" % e.message
        syslog.syslog(syslog.LOG_INFO, line)

    # Then set redis cache
    try:
        r_server.hset("monitor:" + cid, 'status', status)
        success = True
    except:
        line = "chstatus: Redis is inaccessible cannot change status for %s" % cid
        syslog.syslog(syslog.LOG_INFO, line)
        success = False

    return success


def resetFailcount(cid, rdb, r_server):
    ''' This method will reset a users failcount '''
    failcount = 0
    success = False
    cacheonly = False

    # Try to set rethinkdb first
    try:
        results = r.table('monitors').get(cid).update(
            {'failcount': failcount}).run(rdb)
        if results['replaced'] == 1:
            success = True
            cacheonly = False
        else:
            success = False
    except (RqlDriverError, RqlRuntimeError) as e:
        success = False
        cacheonly = True
        line = "chstatus: RethinkDB is inaccessible cannot change failcount for %s" % cid
        syslog.syslog(syslog.LOG_INFO, line)
        line = "chstatus: RethinkDB Error: %s" % e.message
        syslog.syslog(syslog.LOG_INFO, line)

    # Then set redis cache
    try:
        r_server.hset("monitor:" + cid, 'failcount', failcount)
        success = True
    except:
        line = "chstatus: Redis is inaccessible cannot change failcount for %s" % cid
        syslog.syslog(syslog.LOG_INFO, line)
        success = False

    return success


def incFailcount(cid, failcount, rdb, r_server):
    ''' This method will increase a users failcount '''
    failcount = int(failcount) + 1
    success = False
    cacheonly = False

    # Try to set rethinkdb first
    try:
        results = r.table('monitors').get(cid).update(
            {'failcount': failcount}).run(rdb)
        if results['replaced'] == 1:
            success = True
            cacheonly = False
        else:
            success = False
    except (RqlDriverError, RqlRuntimeError) as e:
        success = False
        cacheonly = True
        line = "chstatus: RethinkDB is inaccessible cannot change failcount for %s" % cid
        syslog.syslog(syslog.LOG_INFO, line)
        line = "chstatus: RethinkDB Error: %s" % e.message
        syslog.syslog(syslog.LOG_INFO, line)

    # Then set redis cache
    try:
        r_server.hset("monitor:" + cid, 'failcount', failcount)
        success = True
    except:
        line = "chstatus: Redis is inaccessible cannot change failcount for %s" % cid
        syslog.syslog(syslog.LOG_INFO, line)
        success = False

    return success

@@ -349,22 +349,8 @@
 rror
-%0Aimport syslog
 %0A%0Ade
@@ -476,16 +476,46 @@
 %5B'rdb'%5D%0A
+    logger = kwargs%5B'logger'%5D%0A
     r_se
@@ -668,32 +668,40 @@
 %5D, rdb, r_server
+, logger
 )%0A        if %22we
@@ -880,24 +880,32 @@
 db, r_server
+, logger
 )%0A        el
@@ -1011,24 +1011,32 @@
 db, r_server
+, logger
 )%0A    else:%0A
@@ -1174,16 +1174,24 @@
 r_server
+, logger
 )%0A    re
@@ -1233,32 +1233,40 @@
 s, rdb, r_server
+, logger
 ):%0A    ''' This 
@@ -1842,39 +1842,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line
@@ -1927,39 +1927,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line
@@ -2166,39 +2166,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line
@@ -2255,32 +2255,40 @@
 d, rdb, r_server
+, logger
 ):%0A    ''' This 
@@ -2870,39 +2870,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line
@@ -2955,39 +2955,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line
@@ -3203,39 +3203,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line
@@ -3309,16 +3309,24 @@
 r_server
+, logger
 ):%0A    '
@@ -3936,39 +3936,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line
@@ -4021,39 +4021,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line
@@ -4269,39 +4269,20 @@
     
-syslog.syslog(syslog.LOG_INFO, 
+logger.info(
 line

Convert reactions syslog to logger: chstatus