Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. [17:33:01] [8.8] root@ded457:~/python# cat blahzaantu.py
  2. #!/usr/bin/python3.4
  3. import mysql.connector
  4. import time
  5. import subprocess
  6. import getopt
  7. import sys
  8. import mysql.connector
  9. import time
  10. import re
  11. import os
  12. from subprocess import check_output
  13.  
  14. def main():
  15.  
  16. outfile = "/var/log/blah2.log"
  17. loadlimit = float(0)
  18.  
  19. try:
  20. opts, args = getopt.getopt(sys.argv[1:], "ho:l:v", ["help", "outfile=", "load="])
  21. except getopt.GetoptError as err:
  22. print (str(err))
  23. usage()
  24. sys.exit(2)
  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='mbREGGYjEeiA', host='localhost')
  39. cursor = cnx.cursor()
  40.  
  41. if os.path.exists(outfile):
  42. append_write = 'a' # append if already exists
  43. else:
  44. append_write = 'w' # make a new file if not
  45.  
  46. result_file = open(outfile, append_write)
  47.  
  48. p = re.compile("load average: ([0-9]*[.][0-9]+)")
  49.  
  50. while True:
  51. cursor.execute("show full processlist")
  52. processlist = cursor.fetchall()
  53. uptime = check_output(["uptime"])
  54. time.sleep(1);
  55. #ldresult = p.search(uptime.decode('ascii'))
  56. ldresult = p.search(uptime.decode('ascii'))
  57. if(float(ldresult.group(1)) >= loadlimit):
  58. result_file.write(str(processlist))
  59. result_file.write('\n')
  60. result_file.write(str(uptime))
  61. result_file.write('\n')
  62.  
  63. if __name__ == "__main__":
  64. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement