flexmetric 0.5.1__py3-none-any.whl → 0.5.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,11 +2,55 @@ import clickhouse_connect
2
2
  import sqlite3
3
3
  from flexmetric.logging_module.logger import get_logger
4
4
  import os
5
+ import psycopg2
5
6
 
6
- logger = get_logger(__name__)
7
7
 
8
+ logger = get_logger(__name__)
8
9
  logger.info("database logs")
9
10
 
11
+ def create_postgres_client(db_conf: dict):
12
+ host = db_conf.get('host', 'localhost')
13
+ port = db_conf.get('port', 5432)
14
+ database = db_conf.get('database')
15
+ username = db_conf.get('username', 'postgres')
16
+ password = db_conf.get('password', '')
17
+
18
+ sslmode = db_conf.get('sslmode', 'prefer')
19
+ sslcert = db_conf.get('client_cert')
20
+ sslkey = db_conf.get('client_key')
21
+ sslrootcert = db_conf.get('ca_cert')
22
+
23
+ if not database:
24
+ raise ValueError("Missing 'database' name in PostgreSQL configuration")
25
+
26
+ conn_params = {
27
+ 'host': host,
28
+ 'port': port,
29
+ 'dbname': database,
30
+ 'user': username,
31
+ 'password': password,
32
+ 'sslmode': sslmode
33
+ }
34
+
35
+ if sslcert and sslkey and sslrootcert:
36
+ if not (os.path.isfile(sslcert) and os.path.isfile(sslkey) and os.path.isfile(sslrootcert)):
37
+ raise FileNotFoundError("One or more SSL certificate files not found.")
38
+ conn_params.update({
39
+ 'sslcert': sslcert,
40
+ 'sslkey': sslkey,
41
+ 'sslrootcert': sslrootcert,
42
+ 'sslmode': 'verify-full'
43
+ })
44
+
45
+ try:
46
+ conn = psycopg2.connect(**conn_params)
47
+ logger.info(f"PostgreSQL connection to '{database}' established successfully (SSL: {'Yes' if 'sslcert' in conn_params else 'No'})")
48
+ return conn
49
+
50
+ except Exception as e:
51
+ logger.error(f"Failed to create PostgreSQL client: {e}")
52
+ raise
53
+
10
54
  def create_clickhouse_client(db_conf):
11
55
  id = db_conf.get('id')
12
56
  host = db_conf.get('host', 'localhost')
@@ -1,7 +1,7 @@
1
1
  import yaml
2
2
  import re
3
- from flexmetric.metric_process.database_connections import create_clickhouse_client,create_sqlite_client
4
- from flexmetric.metric_process.queries_execution import execute_clickhouse_command,execute_sqlite_query
3
+ from flexmetric.metric_process.database_connections import create_clickhouse_client,create_sqlite_client,create_postgres_client
4
+ from flexmetric.metric_process.queries_execution import execute_clickhouse_command,execute_sqlite_query,execute_postgres_query
5
5
  from flexmetric.logging_module.logger import get_logger
6
6
  logger = get_logger(__name__)
7
7
 
@@ -59,7 +59,10 @@ def create_clients_from_config(config_file: str):
59
59
  client = create_sqlite_client(db_conf)
60
60
  clients[db_id] = client
61
61
  logger.info(clients)
62
-
62
+ elif db_type == 'postgres':
63
+ client = create_postgres_client(db_conf)
64
+ clients[db_id] = client
65
+ logger.info(clients)
63
66
  else:
64
67
  logger.info(f"Unsupported database type: {db_type}")
65
68
 
@@ -123,10 +126,13 @@ def execute_commands(clients: dict, commands_file: str):
123
126
  response ,column_names = execute_sqlite_query(client,query)
124
127
  result = process_and_get_value(cmd,response,column_names)
125
128
  results.append(result)
129
+ elif db_type == 'postgres':
130
+ response ,column_names = execute_postgres_query(client,query)
131
+ result = process_and_get_value(cmd,response,column_names)
132
+ results.append(result)
126
133
  else:
127
134
  logger.info(f"Unknown type '{db_type}' in command '{cmd_id}'")
128
135
  continue
129
-
130
136
  except Exception as e:
131
137
  logger.error(f"Command '{cmd_id}' failed: {e}")
132
138
  return results
@@ -21,4 +21,20 @@ def execute_sqlite_query(conn, query):
21
21
  return rows, column_names
22
22
  except Exception as ex:
23
23
  logger.error(f"Exception during SQLite query: {ex}")
24
+ return [], []
25
+
26
+ def execute_postgres_query(conn, query):
27
+ try:
28
+ cursor = conn.cursor()
29
+ cursor.execute(query)
30
+
31
+ rows = cursor.fetchall()
32
+ column_names = [desc[0] for desc in cursor.description]
33
+
34
+ logger.info(f"Query executed successfully. Rows fetched: {len(rows)}")
35
+
36
+ return rows, column_names
37
+
38
+ except Exception as ex:
39
+ logger.error(f"Exception during Postgres query: {ex}")
24
40
  return [], []
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flexmetric
3
- Version: 0.5.1
3
+ Version: 0.5.3
4
4
  Summary: A secure flexible Prometheus exporter for commands, databases, functions.
5
5
  Home-page: https://github.com/nikhillingadhal1999/flexmetric
6
6
  Author: Nikhil Lingadhal
@@ -21,6 +21,7 @@ Requires-Dist: wheel
21
21
  Requires-Dist: twine
22
22
  Requires-Dist: flask
23
23
  Requires-Dist: clickhouse-connect
24
+ Requires-Dist: psycopg2-binary
24
25
  Dynamic: author
25
26
  Dynamic: classifier
26
27
  Dynamic: description
@@ -32,25 +33,38 @@ Dynamic: requires-dist
32
33
  Dynamic: requires-python
33
34
  Dynamic: summary
34
35
 
35
- # FlexMetric
36
+ FlexMetric is a lightweight, pluggable, and extensible Prometheus exporter that helps you collect, expose, and visualize custom metrics with minimal setup and maximum flexibility.
36
37
 
37
- FlexMetric is a lightweight, flexible, and extensible Prometheus exporter that allows you to securely expose system metrics, database query results, Python function outputs, and externally submitted metrics—via an optional Flask API with HTTPS support—as Prometheus-compatible metrics, all with minimal setup and maximum customization.
38
+ With FlexMetric, you can:
38
39
 
39
- ---
40
+ Run system commands and expose their output as metrics
41
+
42
+ Execute SQL queries against databases like SQLite, PostgreSQL, and ClickHouse
43
+
44
+ Call custom Python functions and export their results
40
45
 
41
- ## Features
46
+ Receive externally submitted metrics via a secure Flask-based API with optional HTTPS (TLS)
42
47
 
43
- - Run shell commands and expose the results as Prometheus metrics.
44
- ➔ **Harmful commands (e.g., file deletion, system shutdown) are blocked for safety.**
45
- - Execute SQL queries (e.g., SQLite) and monitor database statistics.
46
- ➔ **Potentially dangerous queries (e.g., `DROP`, `DELETE`, `TRUNCATE`) are not allowed.**
47
- - Automatically discover and expose Python function outputs as metrics.
48
- - Expose an optional **Flask API** (`/update_metric`) to receive external metrics dynamically.
49
- - Modular and easy to extend—add your own custom integrations.
50
- - Built-in Prometheus HTTP server (`/metrics`) with configurable port.
51
- - **Supports HTTPS** to securely expose both metrics and API endpoints.
52
- - **Input sanitization** is performed to ensure only safe commands and queries are executed.
48
+ All metrics are exposed in Prometheus-compatible format, making them ready for visualization in Grafana or any Prometheus-based monitoring stack.
49
+ ---
53
50
 
51
+ Paste your rich text content here. You can paste directly from Word or other rich text sources.
52
+
53
+ * Run **shell commands** and expose the results as **Prometheus metrics**.
54
+ ➔ _Harmful commands (e.g., file deletion, system shutdown) are blocked for safety._
55
+
56
+ * Execute **SQL queries** and monitor database statistics:
57
+
58
+ * **SQLite** (lightweight, file-based databases)
59
+ * **PostgreSQL** (robust, production-grade relational databases)
60
+ * **ClickHouse** (high-performance, analytical databases)
61
+ * Potentially dangerous queries (e.g., `DROP`, `DELETE`, `TRUNCATE`) are blocked by default.
62
+ * Automatically discover and expose **Python function outputs** as metrics.
63
+ * Expose an optional **Flask API** (`/update_metric`) to receive and update external metrics **dynamically**.
64
+ * Modular and **easy to extend**—add your own **custom integrations** with minimal effort.
65
+ * Built-in **Prometheus-compatible HTTP server** (`/metrics`) with configurable port.
66
+ * Supports **HTTPS** to securely expose both the metrics endpoint and API.
67
+ * Input **sanitization and validation** ensure only safe commands and queries are executed.
54
68
 
55
69
  ---
56
70
 
@@ -189,52 +203,99 @@ Filesystem Size Used Avail Use% Mounted on
189
203
  | `timeout_seconds` | Maximum time (in seconds) to wait for the command to complete. If it exceeds this time, the command is aborted. |
190
204
 
191
205
  ## Database mode
206
+ ## 🔗 Supported Database Connections
207
+
208
+ FlexMetric supports fetching and exposing metrics from multiple databases using a simple YAML-based configuration. This allows you to monitor data directly from your existing data sources without writing custom code.
209
+
210
+ ### ✅ Currently Supported Databases:
211
+
212
+ | Database | Type Name | Description |
213
+ |--------------|---------------|--------------------------------------------------|
214
+ | **SQLite** | `sqlite` | Lightweight, file-based relational database |
215
+ | **PostgreSQL** | `postgres` | Robust, production-grade relational database |
216
+ | **ClickHouse** | `clickhouse` | High-performance, analytical columnar database |
217
+
218
+ ---
219
+
220
+ ### 📄 Example `database.yaml` Configuration:
192
221
  file - database.yaml
193
222
  ```yaml
194
223
  databases:
195
- - id: "active_user_count"
224
+ - id: "local_sqlite"
225
+ type: "sqlite"
226
+ db_connection: "/path/to/example.db"
227
+
228
+ - id: "analytics_pg"
229
+ type: "postgres"
230
+ host: "localhost"
231
+ port: 5432
232
+ database: "metricsdb"
233
+ username: "postgres"
234
+ password: "postgres_password"
235
+ sslmode: "disable"
236
+ client_cert: "/path/to/cert.pem"
237
+ client_key: "/path/to/key.pem"
238
+ ca_cert: "/path/to/ca.pem"
239
+
240
+ - id: "clickhouse_cluster"
196
241
  type: "clickhouse"
197
242
  host: "localhost"
198
- port: 8123
243
+ port: 8443
199
244
  username: "default"
200
- password: ""
201
- client_cert: ""
202
- client_key: ""
203
- ca_cert: ""
204
-
205
- - id: "userdb"
206
- type: "sqlite"
207
- db_connection: "/path/to/my.db"
245
+ password: "clickhouse_password"
246
+ secure: true
247
+ client_cert: "/path/to/cert.pem"
248
+ client_key: "/path/to/key.pem"
249
+ ca_cert: "/path/to/ca.pem"
208
250
  ```
209
- file - queries.yaml
251
+
252
+ ## Supported Query Configuration
253
+
254
+ FlexMetric allows you to define custom queries in a simple YAML format. Each query is linked to a database using the `database_id` and can expose the results as Prometheus metrics with flexible labeling and value extraction.
255
+
256
+ ### Example `queries.yaml` Configuration:
257
+
210
258
  ```yaml
211
259
  commands:
212
- - id: "active_user_count"
213
- type: "clickhouse"
214
- database_id: "active_user_count"
260
+ - id: "active_user_count_pg"
261
+ type: "postgres"
262
+ database_id: "analytics_pg"
215
263
  query: |
216
264
  SELECT
217
265
  country AS country_name,
218
- COUNT() AS active_user_count
266
+ COUNT(*) AS active_user_count
219
267
  FROM users
220
- WHERE is_active = 1
221
- GROUP BY country
268
+ WHERE is_active = true
269
+ GROUP BY country;
222
270
  main_label: "active_user_count"
223
271
  labels: ["country_name"]
224
272
  value_column: "active_user_count"
225
273
 
226
- - id: "list_all_users_sqlite"
274
+ - id: "total_user_count_sqlite"
227
275
  type: "sqlite"
228
- database_id: "userdb"
276
+ database_id: "local_sqlite"
277
+ query: |
278
+ SELECT
279
+ 'all' AS user_group,
280
+ COUNT(*) AS total_user_count
281
+ FROM users;
282
+ main_label: "total_user_count"
283
+ labels: ["user_group"]
284
+ value_column: "total_user_count"
285
+
286
+ - id: "clickhouse_user_summary"
287
+ type: "clickhouse"
288
+ database_id: "clickhouse_cluster"
229
289
  query: |
230
290
  SELECT
231
- id,
232
- name
291
+ region AS region_name,
292
+ COUNT() AS user_count
233
293
  FROM users
234
- main_label: "user_list"
235
- labels: ["id", "name"]
236
- value_column: "id"
237
-
294
+ WHERE status = 'active'
295
+ GROUP BY region;
296
+ main_label: "clickhouse_user_count"
297
+ labels: ["region_name"]
298
+ value_column: "user_count"
238
299
  ```
239
300
  ## Functions mode
240
301
 
@@ -6,15 +6,15 @@ flexmetric/file_recognition/exec_file.py,sha256=9wBbnqPConxtLhqWTgigEr8VQG-fa9K_
6
6
  flexmetric/logging_module/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
7
  flexmetric/logging_module/logger.py,sha256=hXj9m2Q_KxJVI5YRHRoK6PXV5tO6NmvuVjq2Ipx_1tE,447
8
8
  flexmetric/metric_process/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- flexmetric/metric_process/database_connections.py,sha256=3yYQmFTBz9h_DKWlRxpKi-RFmaFDjz95oZh0Flvfpd8,1591
10
- flexmetric/metric_process/database_processing.py,sha256=_7C1DyC9jtgiTUti4_YYFpQlE0BlJwbaEx3OPpDCGSs,4633
9
+ flexmetric/metric_process/database_connections.py,sha256=Pa_Lcs8Im6pZxmET2pQRR3-DCyu8goEvup5dpYZH1Xk,3050
10
+ flexmetric/metric_process/database_processing.py,sha256=Sbbk-ktdUSx7zFpjaJdC_8L6r61d82t4VKGOr7oAlB0,5066
11
11
  flexmetric/metric_process/expiring_queue.py,sha256=1oC0MjloitPiRo7yDgVarz81ETEQavKI_W-GFUvp5_Y,1920
12
12
  flexmetric/metric_process/process_commands.py,sha256=clGMQhLNcuJUO1gElpAS9Dyk0KU5w41DIguczjo7ceA,4089
13
13
  flexmetric/metric_process/prometheus_agent.py,sha256=pDPqiPtHLv1cfF6dPRNg2LtAhVF1UKf8MNZ_S9xuRQY,7712
14
- flexmetric/metric_process/queries_execution.py,sha256=NdnQ9Flbc8_6VOSY3aHxSmhcCsZf2zIxKTnc0hCp1oU,790
14
+ flexmetric/metric_process/queries_execution.py,sha256=ed8GnWbBdvCb7WfQOmeJdpR0DarF_EHbXBPc6tKaEyg,1229
15
15
  flexmetric/metric_process/views.py,sha256=BY695dCpTkJRc1KLC9RNpFTieFdHeHvyqyefmHhMauE,3297
16
- flexmetric-0.5.1.dist-info/METADATA,sha256=pTpT0fjs-5GcnO8-Mq1t3UDz5DvCpgw7QaM5S7gZWyY,11276
17
- flexmetric-0.5.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
18
- flexmetric-0.5.1.dist-info/entry_points.txt,sha256=urVePn5EWr3JqNvkYP7OsB_h2_Bqvv-Wq1MJRBexm8A,79
19
- flexmetric-0.5.1.dist-info/top_level.txt,sha256=zBlrNwKhXUNhgu9RRZnXxYwYnmE-eocRe6wKSmQROA4,11
20
- flexmetric-0.5.1.dist-info/RECORD,,
16
+ flexmetric-0.5.3.dist-info/METADATA,sha256=CVkAz_6fG6cJgoKN7e_GB-RxTBQJM1ehGz_s5p23Q8I,13820
17
+ flexmetric-0.5.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
18
+ flexmetric-0.5.3.dist-info/entry_points.txt,sha256=urVePn5EWr3JqNvkYP7OsB_h2_Bqvv-Wq1MJRBexm8A,79
19
+ flexmetric-0.5.3.dist-info/top_level.txt,sha256=zBlrNwKhXUNhgu9RRZnXxYwYnmE-eocRe6wKSmQROA4,11
20
+ flexmetric-0.5.3.dist-info/RECORD,,