Guest User

Untitled

a guest
Feb 27th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #!/bin/python
  2.  
  3. import mysql.connector
  4. from time import time
  5. from datetime import datetime
  6. import logging
  7. from socket import gethostname
  8.  
  9. logging.basicConfig(filename='connection.log',format='%(asctime)s %(levelname)s:%(message)s',level=logging.INFO)
  10.  
  11. host = gethostname()
  12. ts = time()
  13.  
  14. try:
  15. loggerdb = mysql.connector.connect(
  16. user='', password='',
  17. host='',
  18. database='')
  19. logging.debug("Connected to database")
  20.  
  21. cursor=loggerdb.cursor()
  22.  
  23. add_ping = ("INSERT INTO uptime ""(host, timestamp)""VALUES (%s, %s)")
  24.  
  25. timestamp = datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
  26.  
  27. cursor.execute(add_ping, (host, timestamp))
  28. loggerdb.commit()
  29. logging.info("Inserted " + host + " " + timestamp + " into database.")
  30.  
  31. except:
  32. logging.error("Unable to connect. Connection Error")
  33.  
  34. loggerdb.close()
Add Comment
Please, Sign In to add comment