Commit 46ea15e0 authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Convert all output statements to proper string.Formatter statements

- Sub name changes
- Add code to check for "stale" servers
- Build configs for modified data
- Update the last_contacted field after each cycle
parent a26150fb
......@@ -82,7 +82,7 @@ def main(argv):
while True:
loopstarttime = datetime.now()
logger.info('Loop starts at ' + str(loopstarttime))
logger.info('Loop starts at {0}'.format(str(loopstarttime)))
# MAIN LOOP
......@@ -96,7 +96,8 @@ def main(argv):
passwd = cfg['dbpass'],
db = cfg['dbname'])
except MySQLdb.Error, e:
logger.exception("MySQL Error [%d]: %s" % (e.args[0], e.args[1]))
logger.exception("MySQL Error [{0}]: {1}".format(e.args[0],
e.args[1]))
sys.exit()
# Get the list of servers to access
......@@ -105,26 +106,25 @@ def main(argv):
# Loop through the list
if (len(serverlist) > 0):
for server in serverlist:
logger.debug('Processing server ' + str(server['id']) +
' (' + server['ip'] + ')')
logger.debug('Processing server {0} ({1})'
.format(str(server['id']), server['ip']))
# Build configs (based on what criteria? always?
# Only when changed?)
configlist = build_config(dbhandle, server['id'])
# Build configs
configlist = build_configs(dbhandle, server['id'])
# SCP configs over (if there are configs)
if (len(configlist) > 0):
push_config(server, server['ssh_key'])
push_configs(server, server['ssh_key'])
# Get results from server
get_results(server, server['ssh_key'])
get_results(dbhandle, str(server['id']), server['ssh_key'])
# Process results, update database, store results
logger.info('Sleeping until next loop iteration')
# Sleep until the beginning of the next minute
sleeptime = 60 - datetime.utcnow().second
logger.info('Sleeping until next loop iteration - {0} seconds'
.format(str(sleeptime)))
time.sleep(sleeptime)
def loadconfig():
......@@ -138,7 +138,7 @@ def loadconfig():
config.read(configfile)
except:
e = sys.exc_info()[0]
logger.exception('Unable to load config file = ' + e)
logger.exception('Unable to load config file = {0}'.format(e))
global cfg
......@@ -154,16 +154,19 @@ def loadconfig():
def get_servers(dbhandle):
logger.info('Loading server list')
cur = dbhandle.cursor()
sql = '''SELECT DISTINCT c.id, c.server_ip, c.ssh_key, c.gpg_key FROM cloud
AS c, timers AS ti, target AS t, spawn AS s WHERE (ti.cloud_id =
c.id AND ti.spawn_id = s.id AND ti.target_id = t.id) AND
(c.last_contacted <= c.last_modified OR c.last_contacted <=
ti.last_modified OR c.last_contacted <= t.last_modified OR
c.last_contacted <= s.last_modified)'''
c.last_contacted <= s.last_modified OR c.last_contacted <=
DATE_SUB(NOW(), INTERVAL c.contact_frequency MINUTE))'''
cur.execute(sql)
try:
cur = dbhandle.cursor()
cur.execute(sql)
except MySQLdb.Error, e:
logger.exception("MySQL Error [{0}]: {1}".format(e.args[0], e.args[1]))
serverlist = []
......@@ -176,23 +179,29 @@ def get_servers(dbhandle):
return serverlist
def build_config(dbhandle, server_id):
def build_configs(dbhandle, server_id):
logger.info('Building server configuration')
cur = dbhandle.cursor()
sql = '''SELECT ti.id, c.server_ip, s.options, s.override, t.address,
ti.hour, ti.minute, ti.day, ti.month FROM timers AS ti, cloud AS c,
spawn AS s, target AS t WHERE c.id = ti.cloud_id AND s.id =
ti.spawn_id AND t.id = ti.target_id AND ti.cloud_id = %s AND
(c.last_contacted <= c.last_modified OR c.last_contacted <=
ti.last_modified OR c.last_contacted <= t.last_modified OR
c.last_contacted <= s.last_modified)'''
cur.execute('''SELECT ti.id, c.server_ip, s.options, s.override, t.address,
ti.hour, ti.minute, ti.day, ti.month FROM timers AS
ti, cloud AS c, spawn AS s, target AS t WHERE c.id =
ti.cloud_id AND s.id = ti.spawn_id AND t.id =
ti.target_id AND ti.cloud_id = ''' + str(server_id))
try:
cur = dbhandle.cursor()
cur.execute(sql, [str(server_id)])
except MySQLdb.Error, e:
logger.exception("MySQL Error [{0}]: {1}".format(e.args[0], e.args[1]))
configlist = []
rows = cur.fetchall()
if (len(rows) > 0):
for row in rows:
logger.debug('Database Retrieval : ' + str(row))
logger.debug('Database Retrieval : {0}'.format(str(row)))
configlist.append({ 'id' : row[0], 'ip' : str(IPAddress(row[1])),
'options' : row[2], 'override' : row[3],
'address' : row[4], 'hour' : row[5],
......@@ -201,12 +210,23 @@ def build_config(dbhandle, server_id):
return configlist
def push_config(server, key):
def push_configs(server, key):
logger.info('Pushing server configuration')
def get_results(server, key):
def get_results(dbhandle, server_id, key):
logger.info('Retrieving results from server')
logger.info('Updating last_contacted for server {0}'.format(server_id))
sql = '''UPDATE cloud SET last_contacted = NOW() WHERE id = %s'''
try:
cur = dbhandle.cursor()
cur.execute(sql, [server_id])
dbhandle.commit()
except MySQLdb.Error, e:
logger.exception("MySQL Error [{0}]: {1}".format(e.args[0], e.args[1]))
###
# Usage
###
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment