Skip to content

Commit

Permalink
Fixed some pep8/pylint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Smith committed Apr 1, 2015
1 parent d70152a commit c37acd1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 83 deletions.
185 changes: 103 additions & 82 deletions applications/endpointtracker/aci-endpoint-tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
of the Endpoints.
"""
import sys
import acitoolkit.acitoolkit as ACI
import acitoolkit.acitoolkit as aci
import warnings
try:
import mysql.connector as mysql
Expand All @@ -28,93 +28,114 @@


def convert_timestamp_to_mysql(timestamp):
(ts, remaining) = timestamp.split('T')
ts = ts + ' '
ts = ts + remaining.split('+')[0].split('.')[0]
return ts
"""
Convert timestamp to correct format for MySQL
# Take login credentials from the command line if provided
# Otherwise, take them from your environment variables file ~/.profile
description = ('Application that logs on to the APIC and tracks'
' all of the Endpoints in a MySQL database.')
creds = ACI.Credentials(qualifier=('apic', 'mysql'),
description=description)
args = creds.get()
:param timestamp: string containing timestamp in APIC format
:return: string containing timestamp in MySQL format
"""
(resp_ts, remaining) = timestamp.split('T')
resp_ts += ' '
resp_ts = resp_ts + remaining.split('+')[0].split('.')[0]
return resp_ts

# Login to APIC
session = ACI.Session(args.url, args.login, args.password)
resp = session.login()
if not resp.ok:
print '%% Could not login to APIC'
sys.exit(0)

# Create the MySQL database
cnx = mysql.connect(user=args.mysqllogin,
password=args.mysqlpassword,
host=args.mysqlip)
c = cnx.cursor()
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
c.execute('CREATE DATABASE IF NOT EXISTS acitoolkit;')
cnx.commit()
c.execute('USE acitoolkit;')
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
c.execute('''CREATE TABLE IF NOT EXISTS endpoints (
mac CHAR(18) NOT NULL,
ip CHAR(16),
tenant CHAR(100) NOT NULL,
app CHAR(100) NOT NULL,
epg CHAR(100) NOT NULL,
interface CHAR(100) NOT NULL,
timestart TIMESTAMP NOT NULL,
timestop TIMESTAMP);''')
cnx.commit()
def main():
"""
Main Endpoint Tracker routine
# Download all of the Endpoints and store in the database
endpoints = ACI.Endpoint.get(session)
for ep in endpoints:
epg = ep.get_parent()
app_profile = epg.get_parent()
tenant = app_profile.get_parent()
data = (ep.mac, ep.ip, tenant.name, app_profile.name, epg.name,
ep.if_name, convert_timestamp_to_mysql(ep.timestamp))
c.execute("""INSERT INTO endpoints (mac, ip, tenant,
app, epg, interface, timestart)
VALUES ('%s', '%s', '%s', '%s',
'%s', '%s', '%s')""" % data)
cnx.commit()
:return: None
"""
# Take login credentials from the command line if provided
# Otherwise, take them from your environment variables file ~/.profile
description = ('Application that logs on to the APIC and tracks'
' all of the Endpoints in a MySQL database.')
creds = aci.Credentials(qualifier=('apic', 'mysql'),
description=description)
args = creds.get()

# Subscribe to live updates and update the database
ACI.Endpoint.subscribe(session)
while True:
if ACI.Endpoint.has_events(session):
ep = ACI.Endpoint.get_event(session)
# Login to APIC
session = aci.Session(args.url, args.login, args.password)
resp = session.login()
if not resp.ok:
print '%% Could not login to APIC'
sys.exit(0)

# Create the MySQL database
cnx = mysql.connect(user=args.mysqllogin,
password=args.mysqlpassword,
host=args.mysqlip)
c = cnx.cursor()
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
c.execute('CREATE DATABASE IF NOT EXISTS acitoolkit;')
cnx.commit()
c.execute('USE acitoolkit;')
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
c.execute('''CREATE TABLE IF NOT EXISTS endpoints (
mac CHAR(18) NOT NULL,
ip CHAR(16),
tenant CHAR(100) NOT NULL,
app CHAR(100) NOT NULL,
epg CHAR(100) NOT NULL,
interface CHAR(100) NOT NULL,
timestart TIMESTAMP NOT NULL,
timestop TIMESTAMP);''')
cnx.commit()

# Download all of the Endpoints and store in the database
endpoints = aci.Endpoint.get(session)
for ep in endpoints:
epg = ep.get_parent()
app_profile = epg.get_parent()
tenant = app_profile.get_parent()
if ep.is_deleted():
ep.if_name = None
data = (convert_timestamp_to_mysql(ep.timestamp),
ep.mac,
tenant.name)
update_cmd = """UPDATE endpoints SET timestop='%s'
WHERE mac='%s' AND tenant='%s' AND
timestop='0000-00-00 00:00:00'""" % data
c.execute(update_cmd)
else:
data = (ep.mac, ep.ip, tenant.name, app_profile.name, epg.name,
ep.if_name, convert_timestamp_to_mysql(ep.timestamp))
insert_data = "'%s', '%s', '%s', '%s', '%s', '%s', '%s'" % data
query_data = ("mac='%s', ip='%s', tenant='%s', "
"app='%s', epg='%s', interface='%s', "
"timestart='%s'" % data).replace(',', ' AND')
select_cmd = "SELECT COUNT(*) FROM endpoints WHERE %s" % query_data
c.execute(select_cmd)
for (count) in c:
if not count[0]:
insert_cmd = """INSERT INTO endpoints (mac, ip, tenant, app,
epg, interface, timestart)
VALUES (%s)""" % insert_data
c.execute(insert_cmd)
data = (ep.mac, ep.ip, tenant.name, app_profile.name, epg.name,
ep.if_name, convert_timestamp_to_mysql(ep.timestamp))
c.execute("""INSERT INTO endpoints (mac, ip, tenant,
app, epg, interface, timestart)
VALUES ('%s', '%s', '%s', '%s',
'%s', '%s', '%s')""" % data)
cnx.commit()

# Subscribe to live updates and update the database
aci.Endpoint.subscribe(session)
while True:
if aci.Endpoint.has_events(session):
ep = aci.Endpoint.get_event(session)
epg = ep.get_parent()
app_profile = epg.get_parent()
tenant = app_profile.get_parent()
if ep.is_deleted():
ep.if_name = None
data = (convert_timestamp_to_mysql(ep.timestamp),
ep.mac,
tenant.name)
update_cmd = """UPDATE endpoints SET timestop='%s'
WHERE mac='%s' AND tenant='%s' AND
timestop='0000-00-00 00:00:00'""" % data
c.execute(update_cmd)
else:
data = (ep.mac, ep.ip, tenant.name, app_profile.name, epg.name,
ep.if_name, convert_timestamp_to_mysql(ep.timestamp))
insert_data = "'%s', '%s', '%s', '%s', '%s', '%s', '%s'" % data
query_data = ("mac='%s', ip='%s', tenant='%s', "
"app='%s', epg='%s', interface='%s', "
"timestart='%s'" % data).replace(',', ' AND')
select_cmd = """SELECT COUNT(*) FROM endpoints
WHERE %s""" % query_data
c.execute(select_cmd)
for count in c:
if not count[0]:
insert_cmd = """INSERT INTO endpoints (mac, ip,
tenant, app, epg, interface,
timestart)
VALUES (%s)""" % insert_data
c.execute(insert_cmd)
cnx.commit()

if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"flask-admin",
"flask-bootstrap",
"flask-wtf",
"flask"],
"flask",
"pymysql"],
description = "This library allows basic Cisco ACI APIC configuration.",
)

0 comments on commit c37acd1

Please sign in to comment.