from time import sleep
from libs.CachetControl import CachetControl

mumble_log = '/var/log/mumble-server.log'


def tail(f, n):
    stdin, stdout = os.popen2("tail -n " + str(n) + " " + f + "|grep Authenticated")
    stdin.close()
    lines = stdout.readlines()
    stdout.close()
    return lines

monitoring = True
users = []

Cachet = CachetControl()
while monitoring:
    sleep(5)
    mumble_tail = tail(mumble_log, 3)
    print mumble_tail
    if mumble_tail:
        if users:
            if users[-1] == mumble_tail[-1]:
                print "Match!"
        else:
            users.append(mumble_tail[0])
            print "Point Sent"
            Cachet.mumbleMonitor()
    else:
        users = []
        Stdout = Output.split("\n")
    else:
        Stdout = []
    if error:
        Stderr = error.split("\n")
    else:
        Stderr = []

    return Stdout, Stderr

if __name__ == "__main__":
    monitoring = True
    users = []

    try:
        Cachet = CachetControl()
        Components = Cachet.getComponenets()
        for component in Components:
            if 'website' or "Website" in component['name']:
                component_id = component['id']
    except Exception as e:
        component_id = ""
        print e
        sys.exit(1)

    # Build the ping command based on OS
    if sys.platform == 'win32':
        Command = 'ping -n {0} -w {1} {2}'.format(NoOfPackets, timeout, url)
    if sys.platform == 'darwin':
        Command = 'ping -c {0} -t {1} {2}'.format(NoOfPackets, timeout, url)
    else: