tsdb.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """
  2. MIT License
  3. Copyright (c) 2022 Texas Tech University
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. """
  21. This file is part of MonSter.
  22. Author:
  23. Jie Li, jie.li@ttu.edu
  24. """
  25. import sql
  26. import utils
  27. import logger
  28. import schema
  29. import psycopg2
  30. log = logger.get_logger(__name__)
  31. def init_tsdb():
  32. """init_tsdb Initialize TimeScaleDB
  33. Initialize TimeScaleDB; The database specified in the configuration file
  34. should be created before run this function.
  35. """
  36. connection = utils.init_tsdb_connection()
  37. utils.print_status('Getting', 'nodes' , 'metadata')
  38. nodes_metadata = utils.get_clusternodes()
  39. slurm_table_schemas = schema.build_slurm_table_schemas()
  40. with psycopg2.connect(connection) as conn:
  41. cur = conn.cursor()
  42. # Create node metadata table
  43. utils.print_status('Creating', 'TimeScaleDB' , 'tables')
  44. metadata_sql = sql.generate_metadata_table_sql(nodes_metadata, 'nodes')
  45. cur.execute(metadata_sql)
  46. sql.write_nodes_metadata(conn, nodes_metadata)
  47. # Create schema for slurm
  48. slurm_sqls = sql.generate_metric_table_sqls(slurm_table_schemas, 'slurm')
  49. cur.execute(slurm_sqls['schema_sql'])
  50. # Create slurm tables
  51. all_sqls = slurm_sqls['tables_sql']
  52. for s in all_sqls:
  53. table_name = s.split(' ')[5]
  54. cur.execute(s)
  55. # Create hypertable
  56. create_hypertable_sql = "SELECT create_hypertable(" + "'" \
  57. + table_name + "', 'timestamp', if_not_exists => TRUE)"
  58. print(create_hypertable_sql)
  59. cur.execute(create_hypertable_sql)
  60. # Create table for jobs info
  61. slurm_job_sql = sql.generate_slurm_job_table_sql('slurm')
  62. cur.execute(slurm_job_sql['schema_sql'])
  63. for s in slurm_job_sql['tables_sql']:
  64. table_name = s.split(' ')[5]
  65. cur.execute(s)
  66. conn.commit()
  67. cur.close()
  68. utils.print_status('Finish', 'tables' , 'initialization!')
  69. if __name__ == '__main__':
  70. init_tsdb()