if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8 go use NucelarMonitor if object_id(N'dbo.tables_create', N'P') is not null drop procedure dbo.tables_create go create procedure dbo.tables_create as begin set nocount on -- Level 0. if object_id(N'dbo.Machines', N'U') is null create table dbo.Machines( id integer not null identity(1, 1), [key] varchar(32) not null, [description] varchar(512), date_in datetime not null constraint machines_df_date_in default getdate(), date_out datetime, constraint machines_pk primary key clustered (id), constraint machines_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), constraint machines_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%') ) if object_id(N'dbo.Types', N'U') is null create table dbo.Types( id integer not null identity(1, 1), [name] varchar(32) not null, [description] varchar(512), date_in datetime not null constraint types_df_date_in default getdate(), date_out datetime, constraint types_pk primary key clustered (id), constraint types_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), constraint types_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') ) if object_id(N'dbo.Domains', N'U') is null create table dbo.Domains( id integer not null identity(1, 1), domain varchar(64) not null, [description] varchar(512), date_in datetime not null constraint domains_df_date_in default getdate(), date_out datetime, constraint domains_pk primary key clustered (id), constraint domains_uk_domain unique nonclustered (domain asc) with (fillfactor = 90), constraint domains_ck_domain check (len(domain) > 0 and domain not like '%[^a-zA-Z0-9_-.]%') ) if object_id(N'dbo.Hostnames', N'U') is null create table dbo.Hostnames( id integer not null identity(1, 1), [name] varchar(32) not null, [description] varchar(512), date_in datetime not null constraint hostnames_df_date_in default getdate(), date_out datetime, constraint hostnames_pk primary key clustered (id), constraint hostnames_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), constraint hostnames_ck_name check (len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%') ) if object_id(N'dbo.Disks', N'U') is null create table dbo.Disks( id integer not null identity(1, 1), [name] varchar(32) not null, [size] bigint not null, mountpoint varchar(128) not null, [description] varchar(512), date_in datetime not null constraint disks_df_date_in default getdate(), date_out datetime, constraint disks_pk primary key clustered (id), -- constraint disks_uk_name unique nonclustered ([name] asc, mountpoint asc) with (fillfactor = 90), constraint disks_ck_name check (( len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%' ) or [name] like '[A-Z]:'), constraint disks_ck_size check (size >= 0 and size < power(cast(2 as bigint), 53)), constraint disks_ck_mountpoint check ( mountpoint like '/%' or mountpoint like '[a-zA-Z]:\%' or mountpoint like '\\%' ) ) if object_id(N'dbo.Interfaces', N'U') is null create table dbo.Interfaces( id integer not null identity(1, 1), [name] varchar(32) not null, [description] varchar(512), date_in datetime not null constraint interfaces_df_date_in default getdate(), date_out datetime, constraint interfaces_pk primary key clustered (id), -- constraint interfaces_uk_machine_name unique nonclustered ([name] asc) with (fillfactor = 90), constraint interfaces_ck_name check (len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%') ) if object_id(N'dbo.CandlesTimes', N'U') is null create table dbo.CandlesTimes( id integer not null identity(1, 1), [from] datetime not null, [to] datetime not null, iterations integer not null, date_in datetime not null constraint candles_times_df_date_in default getdate(), date_out datetime, constraint candles_times_pk primary key clustered (id), constraint candles_times_uk_from_to unique nonclustered ([from] asc, [to] asc) with (fillfactor = 90), constraint candles_times_ck_from_to check ([from] < [to]), constraint candles_times_ck_iterations check (iterations > 0) ) if object_id(N'dbo.BigData', N'U') is null create table dbo.BigData( id integer not null identity(1, 1), [hash] binary(64) not null, [value] varchar(max) not null, date_in datetime not null constraint big_data_df_date_in default getdate(), date_out datetime, constraint big_data_pk primary key clustered (id), constraint big_data_uk_hash unique nonclustered ([hash] asc) with (fillfactor = 90) ) if object_id(N'dbo.Messages', N'U') is null create table dbo.Messages( id integer not null identity(1, 1), [key] varchar(128) not null, date_in datetime not null constraint messages_df_date_in default getdate(), date_out datetime, constraint messages_pk primary key clustered (id), constraint messages_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), constraint messages_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%') ) if object_id(N'dbo.Databases', N'U') is null create table dbo.Databases( id integer not null identity(1, 1), [name] varchar(64) not null, date_in datetime not null constraint databases_df_date_in default getdate(), date_out datetime, constraint databases_pk primary key clustered (id), constraint databases_uk_name unique nonclustered ([name] asc) with (fillfactor = 90), constraint databases_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') ) -- Level 1. if object_id(N'dbo.MachineCPU', N'U') is null create table dbo.MachineCPU( id integer not null identity(1, 1), machine integer not null, candle_time integer not null, [in] float not null, [out] float not null, minimum float not null, maximum float not null, average float not null, date_in datetime not null constraint machine_cpu_df_date_in default getdate(), date_out datetime, constraint machine_cpu_pk primary key clustered (id), constraint machine_cpu_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, constraint machine_cpu_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) on update no action on delete no action, constraint machine_cpu_ck_cpu_in check ( [in] between 0 and 100 and [in] between minimum and maximum ), constraint machine_cpu_ck_cpu_out check ( [out] between 0 and 100 and [out] between minimum and maximum ), constraint machine_cpu_ck_cpu_minimum check ( minimum between 0 and 100 and minimum <= maximum ), constraint machine_cpu_ck_cpu_maximum check ( maximum between 0 and 100 and maximum >= minimum ), constraint machine_cpu_ck_cpu_average check ( average between 0 and 100 and average between minimum and maximum ) ) if object_id(N'dbo.MachineRAM', N'U') is null create table dbo.MachineRAM( id integer not null identity(1, 1), machine integer not null, candle_time integer not null, total bigint not null, [in] bigint not null, [out] bigint not null, minimum bigint not null, maximum bigint not null, average bigint not null, date_in datetime not null constraint machine_ram_df_date_in default getdate(), date_out datetime, constraint machine_ram_pk primary key clustered (id), constraint machine_ram_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, constraint machine_ram_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) on update no action on delete no action, constraint machine_ram_ck_memory_total check ( total between 0 and power(cast(2 as bigint), 53) - 1 ), constraint machine_ram_ck_memory_in check ( [in] between 0 and power(cast(2 as bigint), 53) - 1 and [in] between minimum and maximum and [in] <= total ), constraint machine_ram_ck_memory_out check ( [out] between 0 and power(cast(2 as bigint), 53) - 1 and [out] between minimum and maximum and [out] <= total ), constraint machine_ram_ck_memory_minimum check ( minimum between 0 and power(cast(2 as bigint), 53) - 1 and minimum <= total and minimum <= maximum ), constraint machine_ram_ck_memory_maximum check ( maximum between 0 and power(cast(2 as bigint), 53) - 1 and maximum between minimum and total ), constraint machine_ram_ck_memory_average check ( average between 0 and power(cast(2 as bigint), 53) - 1 and average between minimum and maximum and average <= total ) ) if object_id(N'dbo.MachineDisks', N'U') is null create table dbo.MachineDisks( id integer not null identity(1, 1), machine integer not null, [disk] integer not null, belongs bit not null constraint machine_disks_df_belongs default 1, mounted bit not null constraint machine_disks_df_mounted default 1, date_in datetime not null constraint machine_disks_df_date_in default getdate(), date_out datetime, constraint machine_disks_pk primary key clustered (id), constraint machine_disks_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, constraint machine_disks_fk_disk foreign key ([disk]) references dbo.Disks(id) on update no action on delete no action ) if object_id(N'dbo.MachineInterfaces', N'U') is null create table dbo.MachineInterfaces( id integer not null identity(1, 1), machine integer not null, [interface] integer not null, belongs bit not null constraint machine_interfaces_df_belongs default 1, mounted bit not null constraint machine_interfaces_df_mounted default 1, is_ipv6 bit not null constraint machine_interfaces_data_df_is_ipv6 default 0, [address] varchar(45) not null, mask tinyint not null, date_in datetime not null constraint machine_interfaces_df_date_in default getdate(), date_out datetime, constraint machine_interfaces_pk primary key clustered (id), constraint machine_interfaces_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, constraint machine_interfaces_fk_interface foreign key ([interface]) references dbo.Interfaces(id) on update no action on delete no action ) if object_id(N'dbo.Procedures', N'U') is null create table dbo.Procedures( id integer not null identity(1, 1), [database] integer not null, [name] varchar(64) not null, date_in datetime not null constraint procedures_df_date_in default getdate(), date_out datetime, constraint procedures_pk primary key clustered (id), constraint procedures_fk_database foreign key ([database]) references dbo.Databases(id) on update no action on delete no action, constraint procedures_uk_name unique nonclustered ([database] asc, [name] asc) with (fillfactor = 90), constraint procedures_ck_name check ([name] not like '%[^a-zA-Z0-9_]%' and [name] like '[a-zA-Z_]%') ) -- Level 2. if object_id(N'dbo.MachineDisksSpace', N'U') is null create table dbo.MachineDisksSpace( id integer not null identity(1, 1), machine_disk integer not null, candle_time integer not null, free bigint not null, date_in datetime not null constraint machine_disks_space_df_date_in default getdate(), date_out datetime, constraint machine_disks_space_pk primary key clustered (id), constraint machine_disks_space_fk_machine_disk foreign key (machine_disk) references dbo.MachineDisks(id) on update no action on delete no action, constraint machine_disks_space_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) on update no action on delete no action, constraint machine_disks_space_ck_free check ( free between 0 and power(cast(2 as bigint), 53) - 1 -- and -- free <= isnull(( -- select top 1 disks.[size] -- from dbo.Disks disks -- join dbo.MachineDisks machine_disks on machine_disks.[disk] = disks.id -- where -- disks.date_out is null and -- machine_disks.date_out is null and -- machine_disks.belongs = 1 and -- machine_disks.id = machine_disk -- ), 0) ) ) if object_id(N'dbo.MachineInterfacesTraffic', N'U') is null create table dbo.MachineInterfacesTraffic( id integer not null identity(1, 1), machine_interface integer not null, candle_time integer not null, [type] integer not null, bytes bigint not null, packets integer not null, errors integer not null, date_in datetime not null constraint machine_interfaces_traffic_df_date_in default getdate(), date_out datetime, constraint machine_interfaces_traffic_pk primary key clustered (id), constraint machine_interfaces_traffic_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) on update no action on delete no action, constraint machine_interfaces_traffic_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) on update no action on delete no action, constraint machine_interfaces_traffic_fk_type foreign key ([type]) references dbo.Types(id) on update no action on delete no action, constraint machine_interfaces_traffic_ck_bytes check ( bytes between 0 and power(cast(2 as bigint), 53) - 1 ), constraint machine_interfaces_traffic_ck_packets check ( packets between 0 and power(2, 30) - 1 ), constraint machine_interfaces_traffic_ck_errors check ( errors between 0 and power(2, 30) - 1 ) ) -- if object_id(N'dbo.MachineInterfacesData', N'U') is null create table dbo.MachineInterfacesData( -- id integer not null identity(1, 1), -- machine_interface integer not null, -- is_ipv6 bit not null constraint machine_interfaces_data_df_is_ipv6 default 0, -- [address] varchar(45) not null, -- mask tinyint not null, -- date_in datetime not null constraint machine_interfaces_data_df_date_in default getdate(), -- date_out datetime, -- constraint machine_interfaces_data_pk primary key clustered (id), -- constraint machine_interfaces_data_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) -- on update no action -- on delete no action, -- constraint machine_interfaces_data_ck_is_ipv6 check ( -- (is_ipv6 = 0 and [address] like '[0-9]%.%.%.%') or -- (is_ipv6 = 1 and [address] like '%:%') -- ), -- constraint machine_interfaces_data_ck_address check ( -- ([address] like '[0-9]%.%.%.%' and mask between 0 and 32) or -- ([address] like '%:%' and mask between 0 and 128) -- ), -- constraint machine_interfaces_data_ck_mask check ( -- (mask between 0 and (case when is_ipv6 = 1 then 128 else 32 end)) -- ) -- ) if object_id(N'dbo.Exceptions', N'U') is null create table dbo.Exceptions( id integer not null identity(1, 1), [procedure] integer not null, [message] integer not null, parameters integer, exception integer not null, [status] varchar(16), code integer, date_in datetime not null constraint exceptions_df_date_in default getdate(), date_out datetime, constraint exceptions_pk primary key clustered (id), constraint exceptions_fk_procedure foreign key ([procedure]) references dbo.Procedures(id) on update no action on delete no action, constraint exceptions_fk_message foreign key ([message]) references dbo.Messages(id) on update no action on delete no action, constraint exceptions_fk_parameters foreign key (parameters) references dbo.BigData(id) on update no action on delete no action, constraint exceptions_fk_exception foreign key (exception) references dbo.BigData(id) on update no action on delete no action ) -- Level Plains 0. if object_id(N'dbo.MachinePlain', N'U') is null create table dbo.MachinePlain( id integer not null identity(1, 1), [key] varchar(32) not null, machine integer not null, candle_time integer not null, machine_ram integer not null, machine_cpu integer not null, ram_total bigint not null, ram_in bigint not null, ram_out bigint not null, ram_minimum bigint not null, ram_maximum bigint not null, ram_average bigint not null, cpu_in float not null, cpu_out float not null, cpu_minimum float not null, cpu_maximum float not null, cpu_average float not null, date_in datetime not null constraint machine_plain_df_date_in default getdate(), date_out datetime, constraint machine_plain_pk primary key clustered (id), constraint machine_plain_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, constraint machine_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) on update no action on delete no action, constraint machine_plain_fk_machine_ram foreign key (machine_ram) references dbo.MachineRAM(id) on update no action on delete no action, constraint machine_plain_fk_machine_cpu foreign key (machine_cpu) references dbo.MachineCPU(id) on update no action on delete no action, constraint machine_plain_uk_key unique nonclustered ([key] asc) with (fillfactor = 90), constraint machine_plain_ck_key check ([key] not like '%[^a-zA-Z0-9_]%' and [key] like '[a-zA-Z_]%'), constraint machine_plain_ck_memory_total check ( ram_total between 0 and power(cast(2 as bigint), 53) - 1 ), constraint machine_plain_ck_memory_in check ( ram_in between 0 and power(cast(2 as bigint), 53) - 1 and ram_in between ram_minimum and ram_maximum and ram_in <= ram_total ), constraint machine_plain_ck_memory_out check ( ram_out between 0 and power(cast(2 as bigint), 53) - 1 and ram_out between ram_minimum and ram_maximum and ram_out <= ram_total ), constraint machine_plain_ck_memory_minimum check ( ram_minimum between 0 and power(cast(2 as bigint), 53) - 1 and ram_minimum <= ram_total and ram_minimum <= ram_maximum ), constraint machine_plain_ck_memory_maximum check ( ram_maximum between 0 and power(cast(2 as bigint), 53) - 1 and ram_maximum between ram_minimum and ram_total ), constraint machine_plain_ck_memory_average check ( ram_average between 0 and power(cast(2 as bigint), 53) - 1 and ram_average between ram_minimum and ram_maximum and ram_average <= ram_total ), constraint machine_plain_ck_cpu_in check ( cpu_in between 0 and 100 and cpu_in between cpu_minimum and cpu_maximum ), constraint machine_plain_ck_cpu_out check ( cpu_out between 0 and 100 and cpu_out between cpu_minimum and cpu_maximum ), constraint machine_plain_ck_cpu_minimum check ( cpu_minimum between 0 and 100 and cpu_minimum <= cpu_maximum ), constraint machine_plain_ck_cpu_maximum check ( cpu_maximum between 0 and 100 and cpu_maximum >= cpu_minimum ), constraint machine_plain_ck_cpu_average check ( cpu_average between 0 and 100 and cpu_average between cpu_minimum and cpu_maximum ) ) -- Level Plains 1. if object_id(N'dbo.MachineInterfacesPlain', N'U') is null create table dbo.MachineInterfacesPlain( id integer not null identity(1, 1), plain integer not null, machine integer not null, interface integer not null, machine_interface integer not null, mounted bit not null constraint machine_interfaces_plain_df_mounted default 1, is_ipv6 bit not null constraint machine_interfaces_plain_df_is_ipv6 default 0, [address] varchar(45) not null, mask tinyint not null, belongs bit not null constraint machine_interfaces_plain_df_belongs default 1, date_in datetime not null constraint machine_interfaces_plain_df_date_in default getdate(), date_out datetime, constraint machine_interfaces_plain_pk primary key clustered (id), constraint machine_interfaces_plain_fk_plain foreign key (plain) references dbo.MachinePlain(id) on update no action on delete no action, constraint machine_interfaces_plain_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, constraint machine_interfaces_plain_fk_interface foreign key (interface) references dbo.Interfaces(id) on update no action on delete no action, constraint machine_interfaces_plain_fk_machine_interface foreign key (machine_interface) references dbo.MachineInterfaces(id) on update no action on delete no action, constraint machine_interfaces_plain_uk_machine_interface unique nonclustered (machine asc, [interface] asc) with (fillfactor = 90), -- constraint machine_interfaces_plain_uk_machine_name unique nonclustered ([name] asc) with (fillfactor = 90) ) if object_id(N'dbo.MachineDisksPlain', N'U') is null create table dbo.MachineDisksPlain( id integer not null identity(1, 1), plain integer not null, machine integer not null, [disk] integer not null, machine_disk integer not null, candle_time integer not null, [name] varchar(32) not null, [size] bigint not null, mountpoint varchar(128) not null, free bigint not null, belongs bit not null constraint machine_disks_plain_df_belongs default 1, date_in datetime not null constraint machine_disks_plain_df_date_in default getdate(), date_out datetime, constraint machine_disks_plain_pk primary key clustered (id), constraint machine_disks_plain_fk_plain foreign key (plain) references dbo.MachinePlain(id) on update no action on delete no action, constraint machine_disks_plain_fk_machine foreign key (machine) references dbo.Machines(id) on update no action on delete no action, constraint machine_disks_plain_fk_disk foreign key ([disk]) references dbo.Disks(id) on update no action on delete no action, constraint machine_disks_plain_uk_machine_disk unique nonclustered (machine asc, [disk] asc) with (fillfactor = 90), -- constraint machine_disks_plain_uk_name unique nonclustered ([name] asc, mountpoint asc) with (fillfactor = 90), constraint machine_disks_plain_ck_name check (( len([name]) > 0 and [name] not like '%[^a-zA-Z0-9_-]%' ) or [name] like '[A-Z]:'), constraint machine_disks_plain_ck_size check (size >= 0 and size < power(cast(2 as bigint), 53)), constraint machine_disks_plain_ck_mountpoint check ( mountpoint like '/%' or mountpoint like '[a-zA-Z]:\%' or mountpoint like '\\%' ), constraint machine_disks_plain_ck_free check ( free between 0 and power(cast(2 as bigint), 53) - 1 and free <= [size] ) ) -- Plain 2. if object_id(N'dbo.MachineInterfacesTrafficPlain', N'U') is null create table dbo.MachineInterfacesTrafficPlain( id integer not null identity(1, 1), plain integer not null, candle_time integer not null, [type] integer not null, bytes bigint not null, packets integer not null, errors integer not null, date_in datetime not null constraint machine_interfaces_traffic_plain_df_date_in default getdate(), date_out datetime, constraint machine_interfaces_traffic_plain_pk primary key clustered (id), constraint machine_interfaces_traffic_plain_fk_plain foreign key (plain) references dbo.MachineInterfacesPlain(id) on update no action on delete no action, constraint machine_interfaces_traffic_plain_fk_candle_time foreign key (candle_time) references dbo.CandlesTimes(id) on update no action on delete no action, constraint machine_interfaces_traffic_plain_fk_type foreign key ([type]) references dbo.Types(id) on update no action on delete no action, constraint machine_interfaces_traffic_plain_ck_bytes check (bytes between 0 and power(cast(2 as bigint), 53) - 1), constraint machine_interfaces_traffic_plain_ck_packets check (packets between 0 and power(2, 30) - 1), constraint machine_interfaces_traffic_plain_ck_errors check (errors between 0 and power(2, 30) - 1) ) end go execute dbo.tables_create