mslurm.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 time
  26. import pytz
  27. import utils
  28. import dump
  29. import slurm
  30. import parse
  31. import logger
  32. import psycopg2
  33. import schedule
  34. from datetime import datetime
  35. log = logger.get_logger(__name__)
  36. def monitor_slurm():
  37. """monitor_slurm Monitor Slurm
  38. Monitor Slurm Metrics
  39. """
  40. connection = utils.init_tsdb_connection()
  41. node_id_mapping = utils.get_node_id_mapping(connection)
  42. os_idrac_hostname_mapping = utils.get_os_idrac_hostname_mapping()
  43. slurm_config = utils.get_config('slurm_rest_api')
  44. #Schedule fetch slurm
  45. schedule.every().minutes.at(":00").do(fetch_slurm,
  46. slurm_config,
  47. connection,
  48. node_id_mapping,
  49. os_idrac_hostname_mapping)
  50. while True:
  51. try:
  52. schedule.run_pending()
  53. time.sleep(1)
  54. except KeyboardInterrupt:
  55. schedule.clear()
  56. break
  57. def fetch_slurm(slurm_config: dict,
  58. connection: str,
  59. node_id_mapping: dict,
  60. os_idrac_hostname_mapping: dict):
  61. """fetch_slurm Fetch Slurm Metrics
  62. Fetch Slurm metrics from the Slurm REST API
  63. Args:
  64. slurm_config (dict): slurm configuration
  65. connection (str): tsdb connection
  66. node_id_mapping (dict): node-ip mapping
  67. os_idrac_hostname_mapping (dict): OS-iDRAC hostname mapping
  68. """
  69. token = slurm.read_slurm_token(slurm_config)
  70. timestamp = datetime.now(pytz.utc).replace(microsecond=0)
  71. # Get nodes data
  72. nodes_url = slurm.get_slurm_url(slurm_config, 'nodes')
  73. nodes_data = slurm.call_slurm_api(slurm_config, token, nodes_url)
  74. # Get jobs data
  75. jobs_url = slurm.get_slurm_url(slurm_config, 'jobs')
  76. jobs_data = slurm.call_slurm_api(slurm_config, token, jobs_url)
  77. # Process slurm data
  78. if nodes_data and jobs_data:
  79. job_metrics = parse.parse_jobs_metrics(jobs_data,
  80. os_idrac_hostname_mapping)
  81. node_metrics = parse.parse_node_metrics(nodes_data,
  82. node_id_mapping,
  83. os_idrac_hostname_mapping)
  84. node_jobs = parse.parse_node_jobs(jobs_data,
  85. node_id_mapping,
  86. os_idrac_hostname_mapping)
  87. # Dump metrics
  88. with psycopg2.connect(connection) as conn:
  89. dump.dump_job_metrics(job_metrics, conn)
  90. dump.dump_node_metrics(timestamp, node_metrics, conn)
  91. dump.dump_node_jobs(timestamp, node_jobs, conn)
  92. if __name__ == '__main__':
  93. monitor_slurm()