(config)
| 74 | |
| 75 | |
| 76 | def build_db_diag_tool(config) -> Tool: |
| 77 | tool = Tool( |
| 78 | "Database Diagnosis", |
| 79 | "Diagnose the bottlenecks of a database based on relevant metrics", |
| 80 | name_for_model="db_diag", |
| 81 | description_for_model="Plugin for diagnosing the bottlenecks of a database based on relevant metrics", |
| 82 | logo_url="https://commons.wikimedia.org/wiki/File:Postgresql_elephant.svg", |
| 83 | contact_email="hello@contact.com", |
| 84 | legal_info_url="hello@legal.com" |
| 85 | ) |
| 86 | |
| 87 | #URL_CURRENT_WEATHER= "http://api.weatherapi.com/v1/current.json" |
| 88 | #URL_FORECAST_WEATHER = "http://api.weatherapi.com/v1/forecast.json" |
| 89 | |
| 90 | URL_PROMETHEUS = 'http://8.131.229.55:9090/' |
| 91 | prometheus_metrics = {"cpu_usage": "avg(rate(process_cpu_seconds_total{instance=\"172.27.58.65:9187\"}[5m]) * 1000)", |
| 92 | "cpu_metrics": ["node_scrape_collector_duration_seconds{instance=\"172.27.58.65:9100\"}", "node_procs_running{instance=\"172.27.58.65:9100\"}", "node_procs_blocked{instance=\"172.27.58.65:9100\"}", "node_entropy_available_bits{instance=\"172.27.58.65:9100\"}", "node_load1{instance=\"172.27.58.65:9100\"}", "node_load5{instance=\"172.27.58.65:9100\"}", "node_load15{instance=\"172.27.58.65:9100\"}"], |
| 93 | "memory_usage": "node_memory_MemTotal_bytes{instance=~\"172.27.58.65:9100\"} - (node_memory_Cached_bytes{instance=~\"172.27.58.65:9100\"} + node_memory_Buffers_bytes{instance=~\"172.27.58.65:9100\"} + node_memory_MemFree_bytes{instance=~\"172.27.58.65:9100\"})", |
| 94 | "memory_metrics": ["node_memory_Inactive_anon_bytes{instance=\"172.27.58.65:9100\"}", "node_memory_MemFree_bytes{instance=\"172.27.58.65:9100\"}", "node_memory_Dirty_bytes{instance=\"172.27.58.65:9100\"}", "pg_stat_activity_count{datname=~\"(imdbload|postgres|sysbench|template0|template1|tpcc|tpch)\", instance=~\"172.27.58.65:9187\", state=\"active\"} !=0"], |
| 95 | "network_metrics": ["node_sockstat_TCP_tw{instance=\"172.27.58.65:9100\"}", "node_sockstat_TCP_orphan{instance=\"172.27.58.65:9100\"}"]} |
| 96 | # "node_sockstat_TCP_tw{instance=\"172.27.58.65:9100\"}", |
| 97 | |
| 98 | # load knowlege extractor |
| 99 | knowledge_matcher = KnowledgeExtraction("/bmtools/tools/db_diag/root_causes_dbmind.jsonl") |
| 100 | |
| 101 | # load db settings |
| 102 | script_path = os.path.abspath(__file__) |
| 103 | script_dir = os.path.dirname(script_path) |
| 104 | config = get_conf(script_dir + '/my_config.ini', 'postgresql') |
| 105 | dbargs = DBArgs("postgresql", config=config) # todo assign database name |
| 106 | |
| 107 | # send request to database |
| 108 | db = Database(dbargs, timeout=-1) |
| 109 | |
| 110 | server_config = get_conf(script_dir + '/my_config.ini', 'benchserver') |
| 111 | |
| 112 | monitoring_metrics = [] |
| 113 | with open(str(os.getcwd()) + "/bmtools/tools/db_diag/database_monitoring_metrics", 'r') as f: |
| 114 | monitoring_metrics = f.read() |
| 115 | monitoring_metrics = eval(monitoring_metrics) |
| 116 | |
| 117 | @tool.get("/obtain_start_and_end_time_of_anomaly") |
| 118 | def obtain_start_and_end_time_of_anomaly(): |
| 119 | # Create SSH client |
| 120 | ssh = paramiko.SSHClient() |
| 121 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) |
| 122 | start_time = 0 |
| 123 | end_time = 0 |
| 124 | |
| 125 | try: |
| 126 | # Connect to the remote server |
| 127 | ssh.connect(server_config["server_address"], username=server_config["username"], password=server_config["password"]) |
| 128 | |
| 129 | # Create an SFTP client |
| 130 | sftp = ssh.open_sftp() |
| 131 | |
| 132 | # Change to the remote directory |
| 133 | sftp.chdir(server_config["remote_directory"]) |
nothing calls this directly
no test coverage detected