Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/usr/bin/python3.4
  2. import mysql.connector
  3. import time
  4. import subprocess
  5. import getopt
  6. import sys
  7. import mysql.connector
  8. import time
  9. import re
  10. from subprocess import check_output
  11.  
  12. def main():
  13.  
  14. outfile = "/var/log/blah2.log"
  15. load = 1
  16.  
  17. try:
  18. opts, args = getopt.getopt(sys.argv[1:], "ho:l:v", ["help", "outfile=", "load="])
  19. except getopt.GetoptError as err:
  20. print (str(err))
  21. usage()
  22. sys.exit(2)
  23. # outfile = None
  24. # verbose = False
  25. for o, a in opts:
  26. if o == '-v':
  27. verbose = True
  28. elif o in ("-h", "--help"):
  29. usage()
  30. sys.exit()
  31. elif o in ("-o", "--outfile"):
  32. outfile = a
  33. elif o in ("-l", "--loadlimit"):
  34. loadlimit = a
  35. else:
  36. assert False, "bip boop error with args"
  37.  
  38. cnx = mysql.connector.connect(user='root', password='1234', host='localhost')
  39. cursor = cnx.cursor()
  40.  
  41. result_file = open(outfile, 'w')
  42.  
  43. p = re.compile("load average: ([0-9]*[.][0-9]+)")
  44.  
  45. while True:
  46. cursor.execute("show full processlist")
  47. processlist = cursor.fetchall()
  48. uptime = check_output(["uptime"])
  49. time.sleep(1);
  50. ldresult = p.search(uptime.decode('utf8'))
  51. if(ldresult.group(1) >= loadlimit):
  52. result_file.write(str(processlist))
  53. result_file.write('\n')
  54. result_file.write(str(uptime))
  55. result_file.write('\n')
  56.  
  57. if __name__ == "__main__":
  58. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement