Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import mysql.connector
  2. from mysql.connector import Error
  3. import psutil
  4. import datetime
  5. import time
  6. # Faire un init qui ouvre self connect
  7.  
  8. class BrianDuCon:
  9. def __init__(self) :
  10. connection=self.createConnection()
  11. # self.createTable(connection)
  12. self.insertVariblesIntoTable(connection);
  13.  
  14.  
  15.  
  16. def createConnection(self):
  17. try:
  18. connection = mysql.connector.connect(host='vlesdi.hevs.ch',
  19. database='SIn37',
  20. user='SIn37',
  21. password='7d9f8be6d7572cefbc658e6b41e9f302')
  22. return connection
  23.  
  24. except Error as e:
  25. print("Error dans createConnection", e)
  26. if (connection.is_connected()):
  27. connection.close()
  28.  
  29. # Tables
  30. # Structure de la table `Lab2_ComputerMonitoring`
  31.  
  32. def createTable(self,connection):
  33. try :
  34. cursor = connection.cursor()
  35. mySql_create_query = """CREATE TABLE `Lab2_ComputerMonitoring` (
  36. `Quantity_CPU` double NOT NULL,
  37. `RAM_usage` double NOT NULL,
  38. `Bytes_recv` double NOT NULL,
  39. `Bytes_sent` double NOT NULL,
  40. `Date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  41. PRIMARY KEY (Date)
  42. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;"""
  43.  
  44. cursor.execute(mySql_create_query)
  45. connection.commit()
  46.  
  47. except Error as e:
  48. print("erreur dans create table",e)
  49. if (connection.is_connected()):
  50. connection.close()
  51.  
  52.  
  53. def insertVariblesIntoTable(self,connect):
  54. while True :
  55. # Données
  56. try :
  57. CPU_use=float(psutil.cpu_percent())
  58. RamUse=float((psutil.virtual_memory().active*100)/(psutil.virtual_memory().total))
  59. Bytes_rcv=float(psutil.net_io_counters().bytes_recv)
  60. Bytes_sent=float(psutil.net_io_counters().bytes_sent)
  61. Date="CURRENT_TIMESTAMP"
  62.  
  63. cursor = connect.cursor()
  64. requete = "INSERT INTO Lab2_ComputerMonitoring (Quantity_CPU, RAM_usage, Bytes_recv, Bytes_sent, Date) VALUES (%s, %s, %s, %s, %s)" % (CPU_use, RamUse, Bytes_rcv, Bytes_sent, Date)
  65. # requete = "INSERT INTO `Lab2_ComputerMonitoring` (`Quantity_CPU`, `RAM_usage`, `Bytes_recv`, `Bytes_sent`, `Date`) VALUES ('12', '12', '12', '12', CURRENT_TIMESTAMP);"
  66. print(requete)
  67. cursor.execute(requete)
  68. connect.commit()
  69. print("Record inserted successfully into Laptop table")
  70.  
  71. except mysql.connector.Error as error:
  72. print("Failed to insert into MySQL table {}".format(error))
  73. if (connect.is_connected()):
  74. connect.close()
  75. time.sleep(2)
  76.  
  77. if (connect.is_connected()):
  78. cursor.close()
  79. connect.close()
  80. print("MySQL connection is closed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement