Example #1
0
def disableVhost():
    try:
        hostname = sys.argv[1]
    except:
        print "Hostname is required."
        return

    ploud_config.initializeConfig()
    ploud = ploud_config.PLOUD_POOL.getconn()
    c1 = ploud.cursor()

    c1.execute("SELECT id FROM vhost WHERE host = %s", (hostname,))
    try:
        id = c1.fetchone()[0]
    except:
        print "Can't find host name: %s"%hostname
        return

    c1.execute("UPDATE sites SET disabled = TRUE WHERE id = %s", (id,))
    c1.execute("SELECT host FROM vhost WHERE id = %s", (id,))
    hosts = [row[0] for row in c1.fetchall()]
    disableVhosts(hosts)

    c1.close()
    ploud.commit()
    ploud.close()
    print "Host is disabled."
Example #2
0
def rebalance():
    ploud_config.initializeConfig()
    APACHE = ptah.get_settings('apache')
    file = APACHE['lbfile']
    processes = APACHE['processes']

    conn = ploud_config.PLOUD_POOL.getconn()
    c1 = conn.cursor()

    c1.execute("SELECT vhost.host,sites.bwin,sites.bwout,sites.site_name FROM vhost, sites "
               "WHERE vhost.id = sites.id and sites.disabled = %s ORDER by sites.id",(False,))

    db = bsddb.hashopen(file, 'w')

    data = [(bwin+bwout, host, name) for host, bwin, bwout, name in c1.fetchall()]
    data.sort()

    i = 1
    for size, host, name in data:
        db[host] = str(i)
        db[name] = str(i)
        i = i + 1
        if i > processes:
            i = 1

    print 'Rebalancing is done.'

    db.close()

    c1.close()
    conn.close()
Example #3
0
def clearBandwidth():
    ploud_config.initializeConfig()

    conn = ploud_config.PLOUD_POOL.getconn()
    cursor = conn.cursor()

    cursor.execute("UPDATE sites SET bwin=0, bwout=0")
    cursor.close()
    conn.commit()
    ploud_config.PLOUD_POOL.putconn(conn)
Example #4
0
def initVirtualHosts():
    ploud_config.initializeConfig()
    ploud = ploud_config.PLOUD_POOL.getconn()
    c1 = ploud.cursor()

    PLOUD = ptah.get_settings('ploud')

    #c1.execute("DELETE FROM vhost")
    c1.execute("SELECT id, site_name FROM sites")
    for row in c1.fetchall():
        host = row[1].endswith('.%s'%PLOUD['domain']) \
            and row[1] or '%s.%s'%(row[1], PLOUD['domain'])
        stmt = "INSERT INTO vhost(id, host) VALUES(%d, '%s')"%(row[0], host)
        c1.execute(stmt)

    c1.close()
    ploud.commit()
    ploud.close()
Example #5
0
def rebuildVirtualHosts():
    ploud_config.initializeConfig()
    ploud = ploud_config.PLOUD_POOL.getconn()
    c1 = ploud.cursor()

    c1.execute("SELECT host FROM vhost")

    hosts = [row[0] for row in c1.fetchall()]

    try:
        os.unlink(file)
    except:
        pass
    addVirtualHosts(hosts, 'plone41')

    print '%d added.'%len(hosts)

    c1.close()
    ploud.close()
Example #6
0
def main():
    user = sys.argv[1]
    polid = int(sys.argv[2])

    ploud_config.initializeConfig()

    conn = ploud_config.PLOUD_POOL.getconn()
    cursor = conn.cursor()

    cursor.execute("SELECT id FROM users WHERE email=%s", (user,))
    uid = cursor.fetchone()[0]

    cursor.execute("UPDATE users SET type=%s WHERE id=%s",(polid, uid))

    POLICIES[polid].apply(uid, cursor)

    cursor.close()
    conn.commit()
    ploud_config.PLOUD_POOL.putconn(conn)
Example #7
0
def rebuildPolicy():
    ploud_config.initializeConfig()

    conn = ploud_config.PLOUD_POOL.getconn()
    c1 = conn.cursor()

    c1.execute("SELECT vhost.host,users.type FROM sites,vhost,users "
               "WHERE users.id = sites.user_id and sites.id = vhost.id")

    hosts = {}
    for host, polid in c1.fetchall():
        data = hosts.setdefault(polid, set())
        data.add(host)

    for polid, data in hosts.items():
        changePolicy(data, polid)
        print 'Policy %s: %d added.'%(polid, len(data))

    c1.close()
    conn.close()
Example #8
0
def rebuildVhosts():
    ploud_config.initializeConfig()
    ploud = ploud_config.PLOUD_POOL.getconn()
    c1 = ploud.cursor()

    c1.execute("SELECT host FROM sites, vhost WHERE disabled = TRUE and sites.id = vhost.id")

    hosts = [row[0] for row in c1.fetchall()]

    try:
        os.unlink(file)
    except:
        pass
        
    disableVhosts(hosts)

    for host in hosts:
        print 'Disabled: %s'%host
    print '%d disabled.'%len(hosts)

    c1.close()
    ploud.close()
Example #9
0
def main():
    ploud_config.initializeConfig()
    APACHE = ptah.get_settings('apache')
    accesspos = APACHE['accesspos']
    pos = 0
    vpos = 0
    try:
        line = open(accesspos, 'rb').read()
        line = line.split(' ')
        pos = int(line[0])
        vpos = int(line[1])
    except:
        pass

    # process ploud-access file
    ploudlog = APACHE['ploudlog']
    logfile = open(ploudlog, 'rb')
    logfile.seek(0, 2)
    size = logfile.tell()

    if pos > size:
        pos = 0
        rotated = True
        logfile = open('%s.1'%ploudlog, 'rb')
    else:
        logfile.seek(pos)
        pos = size
        rotated = False

    data = {}
    accessed = {}
    for line in logfile.xreadlines():
        host = line.split(':', 1)[0]
        ac, bwin, bwout = line.split(':::')[-1].split(' ')

        rec = data.get(host)
        if rec is None:
            rec = [0, 0]
            data[host] = rec

        rec[0] = rec[0] + int(bwin)
        rec[1] = rec[1] + int(bwout)

        if ac == 'False':
            accessed[host] = 1

    logfile.close()

    # process varnish log
    varnishlog = APACHE['varnishlog']
    logfile = open(varnishlog, 'rb')
    logfile.seek(0, 2)
    size = logfile.tell()

    if vpos > size:
        vpos = 0
        logfile = open('%s.1'%varnishlog, 'rb')
    else:
        logfile.seek(vpos)
        vpos = size

    for line in logfile.xreadlines():
        try:
            info, other = line.split('HTTP/1.1"', 1)
        except:
            try:
                info, other = line.split('HTTP/1.0"', 1)
            except:
                pass

        try:
            code, size = other.strip().split(' ', 2)[:2]
            size = int(size)
        except:
            continue

        url = info.split(']', 1)[-1].strip().split(' ', 1)[-1]
        host = urllib.splithost(urllib.splittype(url)[1])[0]

        rec = data.get(host)
        if rec is None:
            rec = [0, 0]
            data[host] = rec

        rec[1] = rec[1] + size

    # save pos
    posfile = open(accesspos, 'wb')
    posfile.write('%s %s'%(pos, vpos))
    posfile.close()

    # save data to db
    ploud = ploud_config.PLOUD_POOL.getconn()
    cursor = ploud.cursor()

    for host, rec in data.items():
        cursor.execute("SELECT id FROM vhost WHERE host = %s", (host,))
        try:
            host_id = cursor.fetchone()[0]
        except:
            continue

        cursor.execute("SELECT bwin, bwout FROM sites WHERE id = %s", (host_id,))
        bwin, bwout = cursor.fetchone()

        cursor.execute("UPDATE sites SET bwin=%d, bwout=%d WHERE id=%d"%(
                bwin+rec[0], bwout+rec[1], host_id))
        print host, 'in: %0.2fK, out: %0.2fK'%(float(bwin+rec[0])/1024, float(bwout+rec[1])/1024)

    now = datetime.now()
    for host in accessed.keys():
        cursor.execute("UPDATE sites SET last_accessed=%s WHERE id in ("
                       "SELECT id FROM vhost WHERE host = %s)", (now, host))

    cursor.close()
    ploud.commit()
    ploud.close()