-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
194 lines (179 loc) · 6.56 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import psycopg2
from psycopg2 import sql
from psycopg2.extras import RealDictCursor
import logging
from contextlib import contextmanager
import json
logger = logging.getLogger(__name__)
class Database:
def __init__(self, config):
self.config = config
self.conn = None
def connect(self):
if self.conn is None:
try:
self.conn = psycopg2.connect(
host=self.config['host'],
database=self.config['database_name'],
user=self.config['username'],
password=self.config['password'],
port=self.config['port']
)
logger.info("Database connection established")
except (Exception, psycopg2.Error) as error:
logger.error(f"Error while connecting to PostgreSQL: {error}")
raise
@contextmanager
def get_cursor(self):
if self.conn is None:
self.connect()
try:
cursor = self.conn.cursor(cursor_factory=RealDictCursor)
yield cursor
self.conn.commit()
except Exception as e:
self.conn.rollback()
logger.error(f"Database error: {e}")
raise
finally:
cursor.close()
def close(self):
if self.conn:
self.conn.close()
self.conn = None
logger.info("Database connection closed")
def create_database_if_not_exists(config):
conn = None
try:
conn = psycopg2.connect(
host=config['host'],
database='postgres', # Connect to the default database
user=config['username'],
password=config['password'],
port=config['port']
)
conn.autocommit = True
with conn.cursor() as cursor:
cursor.execute(
sql.SQL("SELECT 1 FROM pg_catalog.pg_database WHERE datname = %s"),
[config['database_name']]
)
exists = cursor.fetchone()
if not exists:
cursor.execute(sql.SQL("CREATE DATABASE {}").format(
sql.Identifier(config['database_name'])
))
logger.info(f"Database {config['database_name']} created")
else:
logger.info(f"Database {config['database_name']} already exists")
except (Exception, psycopg2.Error) as error:
logger.error(f"Error while creating database: {error}")
raise
finally:
if conn:
conn.close()
db = None
def init_db(config):
global db
create_database_if_not_exists(config)
db = Database(config)
db.connect()
# Create tables if they don't exist
with db.get_cursor() as cursor:
tables = [
("hosts", '''
CREATE TABLE IF NOT EXISTS hosts (
id SERIAL PRIMARY KEY,
hostname VARCHAR(255) UNIQUE NOT NULL,
tags JSONB
)
'''),
("metrics", '''
CREATE TABLE IF NOT EXISTS metrics (
id SERIAL PRIMARY KEY,
host_id INTEGER REFERENCES hosts(id) ON DELETE CASCADE,
metric_name VARCHAR(255) NOT NULL,
timestamp FLOAT NOT NULL,
value JSONB NOT NULL,
message TEXT
)
'''),
("alerts", '''
CREATE TABLE IF NOT EXISTS alerts (
id SERIAL PRIMARY KEY,
host_id INTEGER REFERENCES hosts(id) ON DELETE CASCADE,
metric_name VARCHAR(255) NOT NULL,
condition VARCHAR(50) NOT NULL,
threshold FLOAT NOT NULL,
duration INTEGER NOT NULL,
enabled BOOLEAN DEFAULT TRUE
)
'''),
("downtimes", '''
CREATE TABLE IF NOT EXISTS downtimes (
id SERIAL PRIMARY KEY,
host_id INTEGER REFERENCES hosts(id) ON DELETE CASCADE,
start_time FLOAT NOT NULL,
end_time FLOAT NOT NULL
)
'''),
("alert_history", '''
CREATE TABLE IF NOT EXISTS alert_history (
id SERIAL PRIMARY KEY,
host_id INTEGER REFERENCES hosts(id) ON DELETE CASCADE,
alert_id INTEGER REFERENCES alerts(id) ON DELETE CASCADE,
timestamp FLOAT NOT NULL,
value JSONB NOT NULL
)
'''),
("client_configs", '''
CREATE TABLE IF NOT EXISTS client_configs (
client_id VARCHAR(255) PRIMARY KEY,
hostname VARCHAR(255) NOT NULL,
config JSONB NOT NULL,
tags JSONB,
last_updated TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
)
'''),
("metric_scripts", '''
CREATE TABLE IF NOT EXISTS metric_scripts (
name VARCHAR(255) PRIMARY KEY,
code TEXT NOT NULL,
tags JSONB
)
'''),
("users", '''
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(60) NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
)
''')
]
for table_name, create_statement in tables:
try:
cursor.execute(create_statement)
logger.info(f"Table '{table_name}' created or already exists.")
except Exception as e:
logger.error(f"Error creating table '{table_name}': {str(e)}")
logger.info("All necessary tables have been processed")
def get_db():
return db
def load_config(config_path='server_config.json'):
try:
with open(config_path, 'r') as config_file:
return json.load(config_file)
except FileNotFoundError:
logger.error(f"Config file not found: {config_path}")
raise
except json.JSONDecodeError:
logger.error(f"Invalid JSON in config file: {config_path}")
raise
def setup_database():
config = load_config()
init_db(config['database'])
logger.info("Database setup completed")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
setup_database()